String coercion

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

String coercion

Frank Cohen
Whatever happened to the String CharSequence issue in Jython 2.1? I  
remember a while back that calls to a Java class that required a  
String parameter cause Jython to generate an exception:

TypeError: matcher(): 1st arg can't be coerced to java.lang.CharSequence

I found a thread at

http://aspn.activestate.com/ASPN/Mail/Message/1580322

that discusses the problem of coercing Jython string types into  
CharSequences. Jeff Emanuel in the email suggests patching  
PyString.java in Jython but Finn Bock objected because the proposed  
patch is JDK 1.4 specific.

I bumped into this problem when writing Jython script. My workaround  
is as follows:

...
from java.lang import String
...
rh.addValue( String( "myvalue" ) )

This feels clumsy and I hope I don't have to continue to do this when  
working with Jython scripts.

-Frank







This is also the change that I would expect to work (I haven't tested
it) but your change would tie jython to jdk1.4. That is clearly not
acceptable.

So a patch would have to be more dynamic in nature, perhaps by loading
the CharSequence class within a try .. catch clause and only attempt to
load the class when the jdk version number is above 1.4.

A patch that gets all this right so that it also works with jdk1.1
applets will indeed be welcome.

regards,
finn










Jython-dev
RE: [Jython-dev] Question about Strings and java.util.regex
by Jeff Emanuel other posts by this author
Mar 24 2003 3:12PM messages near this date



  [Jython-dev] Graphical installer hangs on Mac OSX 10.2.4 w/ Java  
1.4.1_01 | Re: [Jython-dev] Question about Strings and java.util.regex

PyString.java has this line (line 658 in the Jython 2.1 source)
in the method __tojava__:

     if (c == String.class || c == Object.class || c ==  
Serializable.class)

If you change it to this, it should work for you:

         if (c == String.class || c == CharSequence.class ||
             c == Object.class || c == Serializable.class)

I would suggest submitting a patch to the jython sourceforge site
www.sf.net/projects/jython.




 >  -----Original Message-----
 >  From: Mike GARCIA [mailto:mgarcia@[...].com]
 >  Sent: Friday, March 21, 2003 3:35 AM
 >  To: '[hidden email]'
 >  Subject: [Jython-dev] Question about Strings and java.util.regex
 >
 >
 >  Jython is awesome!!  I've been using it for over 2 years now
 >  and it still
 >  makes my job really cool.  Got a quick question about using the
 >  java.util.regex package in J2SDK1.4.1.  In this version of
 >  Java Strings
 >  implement a new interface called CharSequence.  This is to provide
 >  additional functionality for the regex package plus some
 >  other things.
 >
 >  Problem is when using the java.util.regex like this:
 >  ================================================
 >
 >  >>> p=Pattern.compile(".*")
 >  >>> simpleStr="foobar"
 >  >>> match=p.matcher(simpleStr)
 >  Traceback (innermost last):
 >    File "<console>", line 1, in ?
 >  TypeError: matcher(): 1st arg can't be coerced to
 >  java.lang.CharSequence
 >  >>>
 >  ================================================
 >
 >  The matcher method takes a CharSequence as an arg.
 >  Apparently the Jython
 >  string is not a CharSequence.  When a do type(simpleStr) it
 >  tells me that it
 >  is an org.python.core.PyString.  Is there any chance of
 >  getting a PyString
 >  to be a CharSequence also?  This would allow native Jython
 >  strings to be
 >  used in jdk 1.4 and up.
 >
 >  Here is the case if I use a new java String:
 >  ================================================
 >  >>> simpleStr=String("foobar")
 >  >>> match=p.matcher(simpleStr)
 >  >>> match.matches()
 >  1
 >  >>>
 >  ================================================
 >
 >  thanks,
 >  Mike Garcia
 >
 >
 >  -------------------------------------------------------
 >  This SF.net email is sponsored by:Crypto Challenge is now open!
 >  Get cracking and register here for some mind boggling fun and
 >  the chance of winning an Apple iPod:
 >  http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
 >  _______________________________________________
 >  Jython-dev mailing list
 >  Jython-dev@[...].net
 >  https://lists.sourceforge.net/lists/listinfo/jython-dev
 >


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Jython-dev mailing list
Jython-dev@[...].net
https://lists.sourceforge.net/lists/listinfo/jython-dev
Thread:


 
  Jeff Emanuel


 
  Finn Bock


arrow_left.gif (162 bytes) Download Attachment
arrow_right.gif (162 bytes) Download Attachment
x.gif (96 bytes) Download Attachment
L.gif (96 bytes) Download Attachment
x.gif (96 bytes) Download Attachment
L.gif (96 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: String coercion

Khalid Zuberi
On 4/9/06, Frank Cohen <[hidden email]> wrote:
> Whatever happened to the String CharSequence issue in Jython 2.1? I
> remember a while back that calls to a Java class that required a
> String parameter cause Jython to generate an exception:

The current 2.2 code uses Class.isAssignableFrom() instead of
explicitly matching against a list of classes as in 2.1. A quick test
with java Pattern.matcher()  now succeeds, so it seems one of the
trusty jython developers has already fixed that.

- kz


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
Jython-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-dev
Reply | Threaded
Open this post in threaded view
|

Re: String coercion

Frank Cohen
Great. Thanks for confirming that. -Frank



On Apr 10, 2006, at 9:22 PM, Khalid Zuberi wrote:

> On 4/9/06, Frank Cohen <[hidden email]> wrote:
>> Whatever happened to the String CharSequence issue in Jython 2.1? I
>> remember a while back that calls to a Java class that required a
>> String parameter cause Jython to generate an exception:
>
> The current 2.2 code uses Class.isAssignableFrom() instead of
> explicitly matching against a list of classes as in 2.1. A quick test
> with java Pattern.matcher()  now succeeds, so it seems one of the
> trusty jython developers has already fixed that.
>
> - kz
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting  
> language
> that extends applications into web and mobile media. Attend the  
> live webcast
> and join the prime developer group breaking into this new coding  
> territory!
> <a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
> _______________________________________________
> Jython-dev mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jython-dev
>



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
Jython-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-dev