You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot concatenate 'str' and 'java.lang.String' #195
changed the title [-]Concatenation of str and String, also str and unicode causes garbage output or 'TypeError: cannot concatenate 'str' and 'java.lang.String' objects' error[/-][+]Cannot concatenate 'str' and 'java.lang.String'[/+]on Jul 3, 2022
Ideally we would get the same behaviour in Jython towards Java String that CPython has towards unicoode. There may be limitations that follow from String sometimes meaning str. (History.)
CPython:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> euroSymb=u"\u20ac"
>>> str(euroSymb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 0: ordinal not in range(128)
>>> "%s" % euroSymb
u'\u20ac'
>>>
Jython (development tip):
Jython 2.7.3a1-DEV (heads/non-byte-str-optimisation:8e2cdf623, Jul 3 2022, 08:45:03)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_321
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.lang import String
>>> euroSymb=String(u"\u20ac")
>>> str(euroSymb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 0: ordinal not in range(128)
>>> "%s" % euroSymb
u'\u20ac'
Hi, surely the point is that these lines don't work? My code was pure example to force the issue for you as a demo.
Anyway, the reason being that my Jython code is an extension that sits within a pure java application. I have no control over the java app. All the data comes from the app and is always UTF8/Unicode. Hence I have to deal with it.. I.e. I receive a java stored String that has utf8/unicode contents...
Surely if "money %s" %(euroSymb) works, then "money" + str(euroSymb) should work? It shouldn't crash or return garbage..? It seems a bit crazy that I have to create my own method called safeStr(x) which returns "%s" %(x) to get round the random garbage or exceptions caused by str() in these scenarios..?
I'm afraid we're dealing with Python 2 here. (You do realise I hope?) str holds bytes. If we give it characters, it cannot be expected to know what bytes to turn them into on its own.
As a concession to 50 years of anglo-centric programming, and the fact that all supported codecs agree for the first 128 characters, it will try US-ASCII. Hence the error message is about the 'ascii' codec.
I think your code should be written using unicode objects. If you're used to Python 3, you'll hardly have had to think about bytes, because the Python 3 str is unicode.
str(euroSymb) should be an error and it is in both implementations. You asked for a str and it can't be done without guesswork. In contrast, "money %s" % euroSymb works because the formatter gets to the %s argument, realises it is unicode and decides to give you a unicoderesult.
If your data arrive as Java String, then turn them into unicode like this: unicode(euroSymb). When you pass a unicode value to a Java call that expects String it should get coerced automatically.
If your data arrive as bytes, they might be in a str or in an array.array, and you need to decode them. You can pass a str to a Java method but it won't go well unless it's US-ASCII. Generally you will have to convert them to Unicode with:
😂 - yes I am very aware I'm using Python 2.7... I come from a background where I was a Basic/Cobol/DBase programmer 30+ years ago, and recently had to learn Java, Python, Jython, and the application's API just to get an extension working.. YUP - Unicode and UTF8 was a complete mystery, but I cobbled something together... I'm much more experienced now. This was an issue that first hit me...I still don't think Jython should cause an exception or show different results depending on how you achieve the same thing... Anyway, I'll have a think about what you said - thanks
So under the covers is "%s" %(txt) actually doing the same as unicode(txt) and also the same as repr(txt)..?
Not really. You ask the object's __format__ method, passing it the %s specification.
The PyString or PyUnicode first produce their string contents for truncations and padding. The whole thing works in chars, but it remembers whether it is supposed to produce a str or unicodeat the end.
// Return a result that has the same type (str or unicode) as the formatSpec argument.
returnf.pad().getPyResult();
The last comment is wrong: result type depends on the bytes flag, which you can see depends on the type of argument as well as the type of format string.
Activity
jeff5 commentedon Jul 3, 2022
Do you have to use Java
Stringfor this?What's wrong with
euroSymb = u"\u20ac"? Or even:with an encoding declaration, of course.
JLabelought to receive ajava.lang.Stringautomatically. If it doesn't, that's a more sensible bug to fix. (I haven't tested any of this.)[-]Concatenation of str and String, also str and unicode causes garbage output or 'TypeError: cannot concatenate 'str' and 'java.lang.String' objects' error[/-][+]Cannot concatenate 'str' and 'java.lang.String'[/+]jeff5 commentedon Jul 3, 2022
Ideally we would get the same behaviour in Jython towards Java String that CPython has towards
unicoode. There may be limitations that follow fromStringsometimes meaningstr. (History.)CPython:
Jython (development tip):
LGTM. That last bit may be thanks to #192.
yogi1967 commentedon Jul 3, 2022
Hi, surely the point is that these lines don't work? My code was pure example to force the issue for you as a demo.
Anyway, the reason being that my Jython code is an extension that sits within a pure java application. I have no control over the java app. All the data comes from the app and is always UTF8/Unicode. Hence I have to deal with it.. I.e. I receive a java stored String that has utf8/unicode contents...
jeff5 commentedon Jul 3, 2022
You may have a point with:
But I'm not sure it needs work when we have a perfectly good type in
unicode.str(euroSymb)shouldn't work.unicode(euroSymb)works fine.If the data arrive as UTF-8, then there are codecs.
yogi1967 commentedon Jul 3, 2022
Surely if "money %s" %(euroSymb) works, then "money" + str(euroSymb) should work? It shouldn't crash or return garbage..? It seems a bit crazy that I have to create my own method called safeStr(x) which returns "%s" %(x) to get round the random garbage or exceptions caused by str() in these scenarios..?
jeff5 commentedon Jul 3, 2022
I'm afraid we're dealing with Python 2 here. (You do realise I hope?)
strholds bytes. If we give it characters, it cannot be expected to know what bytes to turn them into on its own.As a concession to 50 years of anglo-centric programming, and the fact that all supported codecs agree for the first 128 characters, it will try US-ASCII. Hence the error message is about the 'ascii' codec.
I think your code should be written using
unicodeobjects. If you're used to Python 3, you'll hardly have had to think aboutbytes, because the Python 3strisunicode.str(euroSymb)should be an error and it is in both implementations. You asked for astrand it can't be done without guesswork. In contrast,"money %s" % euroSymbworks because the formatter gets to the%sargument, realises it isunicodeand decides to give you aunicoderesult.If your data arrive as Java
String, then turn them intounicodelike this:unicode(euroSymb). When you pass aunicodevalue to a Java call that expectsStringit should get coerced automatically.If your data arrive as
bytes, they might be in astror in anarray.array, and you need to decode them. You can pass astrto a Java method but it won't go well unless it's US-ASCII. Generally you will have to convert them to Unicode with:yogi1967 commentedon Jul 3, 2022
😂 - yes I am very aware I'm using Python 2.7... I come from a background where I was a Basic/Cobol/DBase programmer 30+ years ago, and recently had to learn Java, Python, Jython, and the application's API just to get an extension working.. YUP - Unicode and UTF8 was a complete mystery, but I cobbled something together... I'm much more experienced now. This was an issue that first hit me...I still don't think Jython should cause an exception or show different results depending on how you achieve the same thing... Anyway, I'll have a think about what you said - thanks
yogi1967 commentedon Jul 3, 2022
thanks for the great unicode(txt) tip.. It does seem to work better.. One day I will perhaps understand bytes vs characters.
So under the covers is "%s" %(txt) actually doing the same as unicode(txt) and also the same as repr(txt)..?
jeff5 commentedon Jul 3, 2022
Not really. You ask the object's
__format__method, passing it the%sspecification.The
PyStringorPyUnicodefirst produce their string contents for truncations and padding. The whole thing works inchars, but it remembers whether it is supposed to produce astrorunicodeat the end.jython/src/org/python/core/PyString.java
Lines 4215 to 4223 in 6ee04ab
The last comment is wrong: result type depends on the
bytesflag, which you can see depends on the type of argument as well as the type of format string.