Issue1471

classification
Title: issue of the key word "is not"
Type: behaviour Severity: critical
Components: Core Versions: 2.5.0
Milestone:
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: asnama, fwierzbicki, thobes
Priority: high Keywords:

Created on 2009-09-17.03:38:31 by asnama, last changed 2009-09-17.09:01:50 by thobes.

Files
File name Uploaded Description Edit Remove
test.py asnama, 2009-09-17.06:44:36
service.py asnama, 2009-09-17.07:01:07
test.py asnama, 2009-09-17.07:12:38 another test script
Messages
msg5171 (view) Author: Lance (asnama) Date: 2009-09-17.03:38:30
When I use the Google Calendar APIs and Tools, I test this function.

# \gdata\calendar\service.py, line 490
  def _SetOrderBy(self, val):
    if val is not 'lastmodified' and val is not 'starttime':
      raise Error, "Order By must be either 'lastmodified' or 'starttime'"
    self['orderby'] = val

I got error no matter what I passed to it.


Jython 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_14
Type "help", "copyright", "credits" or "license" for more information.
>>> def t(v):
...   if v is not 'a' and v is not 'b': return 'e'
...   return 't'
...
>>> t('a')
'e'
>>> t('b')
'e'
>>> t('c')
'e'
>>>
msg5172 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2009-09-17.03:51:37
That's a strange one -- I can't reproduce this in a file, but in
interactive mode it breaks as reported, suggesting (to me) that the
problem is in parsing in single mode...
msg5173 (view) Author: Tobias Ivarsson (thobes) Date: 2009-09-17.06:14:16
This has to do with string interning.
When executed in a file we discover all the occurrences of the same
string and use the same object for them (the same as CPython), but when
running in interactive mode we use new string objects for all strings
that are in different compilation units. CPython does it this way to.
The reason this is not working is because 'is' (and 'is not') is an
identity equals comparison, it returns true if the objects are the same,
and should not be used to compare strings. For string comparison '=='
and '!=' should be used.

The reason the example works in CPython is that the strings are
"identifier looking strings", i.e. strings that are valid as
identifiers, and CPython interns all such strings since it improves
performance there. Jython does not intern strings like that.

If you instead try this code (again in interactive mode) it will behave
the same in CPython as in Jython:

>>> def t(v):
...     if v is not 'this string object': return 'e'
...     return 't'
... 
>>> t('this string object')
'e'
msg5174 (view) Author: Lance (asnama) Date: 2009-09-17.06:44:36
But when I run the test.py, it still error. Please advise how can I run
it under Jython.

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    query.orderby = 'lastmodified'
  File "...\Lib\site-packages\gdata\calendar\service.py", line 492, in
_SetOrderBy
    raise Error, "Order By must be either 'lastmodified' or 'starttime'"
gdata.calendar.service.Error: Order By must be either 'lastmodified' or
'starttime'
msg5175 (view) Author: Lance (asnama) Date: 2009-09-17.07:01:07
I also upload the service.py for your reference.
msg5176 (view) Author: Tobias Ivarsson (thobes) Date: 2009-09-17.07:04:24
From where I'm standing this looks like a bug in the gdata API. CPython
does intern these kinds of strings, but there are no guarantees in
Python as a language that this kind of interning will be done. The 'is'
comparator is an identity equals comparator, nothing else. And interning
of strings that look like identifiers is a performance optimization in
CPython, any code comparing strings with 'is'/'is not' when identity
equality isn't what they are after (this is almost never the case with
strings) is faulty.
msg5177 (view) Author: Lance (asnama) Date: 2009-09-17.07:12:38
The issue maybe cause by the class from dict, please find the update
test script
msg5178 (view) Author: Tobias Ivarsson (thobes) Date: 2009-09-17.07:34:04
The issue that you are experiencing is not related to the subscript
assignment to self. The exception occurs before that. Your first
identification of where the problem occurs is correct, but it's not an
error in Jython but in gdata.calendar.service, the developers at Google
ought to know better than to use identity equals for string comparison,
but I guess we all make mistakes...
The sortorder property has the same issue.
Please report the issue to the gdata API instead:
http://code.google.com/p/gdata-python-client/issues
msg5179 (view) Author: Tobias Ivarsson (thobes) Date: 2009-09-17.08:18:11
It's even wrong to use 'is' for comparing integers in CPython, even
regular integers (as opposed to long integers), try the following in
CPython:
>>> a = 1<<10
>>> b = 1<<10
>>> a is b
False

This would however be True for small integers in both CPython and
Jython, since both implementations have a "small integer cache":
>>> a = 1 << 8
>>> b = 1 << 8
>>> a is b
True

But again, this is not something you should rely on.
msg5180 (view) Author: Lance (asnama) Date: 2009-09-17.08:42:06
ok, thanks for your support. I will report the issue to Google then.
msg5181 (view) Author: Tobias Ivarsson (thobes) Date: 2009-09-17.09:01:49
This is an issue in the gdata API, and have been reported accordingly:
http://code.google.com/p/gdata-python-client/issues/detail?id=288
History
Date User Action Args
2009-09-17 09:01:50thobessetstatus: open -> closed
resolution: accepted -> invalid
messages: + msg5181
2009-09-17 08:42:06asnamasetmessages: + msg5180
2009-09-17 08:18:12thobessetmessages: + msg5179
2009-09-17 07:34:05thobessetmessages: + msg5178
2009-09-17 07:12:39asnamasetfiles: + test.py
messages: + msg5177
2009-09-17 07:04:24thobessetmessages: + msg5176
2009-09-17 07:01:10asnamasetfiles: + service.py
messages: + msg5175
2009-09-17 06:44:37asnamasetfiles: + test.py
messages: + msg5174
2009-09-17 06:14:17thobessetnosy: + thobes
messages: + msg5173
2009-09-17 03:51:38fwierzbickisetpriority: high
assignee: fwierzbicki
resolution: accepted
messages: + msg5172
nosy: + fwierzbicki
2009-09-17 03:38:31asnamacreate