Message12198

Author amoebam
Recipients amoebam, k870611, stefan.richthofer
Date 2018-12-06.23:18:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1544138335.54.0.788709270274.issue2716@psf.upfronthosting.co.za>
In-reply-to
Content
no, jython should not try to match arbitrary undocumented implementation details of cpython.  a brief look at some of cpython's behavior should make it clear why.

for example, "hello" may be interned, but what if we include a non-alphanumeric character?

>>> a = "hello!"
>>> b = "hello!"
>>> a is b
False

except it turns out this is a case where a semicolon is not the same thing as a newline in cpython, because if we use a semicolon we get the opposite result:

>>> a = "hello!"; b = "hello!"
>>> a is b
True

now, obviously the length of the string matters too, because non-alphanumeric strings of length 1 do get interned:

>>> a = "!"
>>> b = "!"
>>> a is b
True

is it just simple literals?  no!  you also have to consider constant folding, for which you want to produce just the right amount of optimization; you have to replace x + y or x * y with a simple literal, but of course you mustn't combine the two and replace x + y * z with a literal!

>>> a = "aaaaa"
>>> b = "aa" + "aaa"
>>> c = "a" * 5
>>> d = "a" + "a" * 4
>>> a is b, a is c, a is d
(True, True, False)

and obviously * should only work if the result is <= 20 characters, even though a 21-character string will still be interned if you write it out in full ...

>>> a20 = "aaaaaaaaaaaaaaaaaaaa"
>>> a21 = "aaaaaaaaaaaaaaaaaaaaa"
>>> b20 = "a" * 20
>>> b21 = "a" * 21
>>> c21 = "aaaaaaaaaaaaaaaaaaaaa"
>>> a20 is b20, a21 is b21, a21 is c21
(True, False, True)

... but, of course, these last two examples produce different results in cpython 3 ...

in short: it would be foolish to try to make jython match cpython here.   code that relies on implementation details like these is not correct python, and jython should not try to make it work.

pass your strings through the intern() function instead, if you really need to use "is" instead of "==".
History
Date User Action Args
2018-12-06 23:18:55amoebamsetmessageid: <1544138335.54.0.788709270274.issue2716@psf.upfronthosting.co.za>
2018-12-06 23:18:55amoebamsetrecipients: + amoebam, stefan.richthofer, k870611
2018-12-06 23:18:55amoebamlinkissue2716 messages
2018-12-06 23:18:53amoebamcreate