[ python-Bugs-1004669 ] Type returned from .keys() is not checked
SourceForge.net
noreply at sourceforge.net
Sat Aug 7 01:43:10 CEST 2004
Bugs item #1004669, was opened at 2004-08-06 10:08
Message generated for change (Comment added) made by rhettinger
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1004669&group_id=5470
Category: Python Interpreter Core
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: ben handley (bhandley)
Assigned to: Raymond Hettinger (rhettinger)
Summary: Type returned from .keys() is not checked
Initial Comment:
When passing a mapping object as the locals to eval, it
doesn't check that the return value of .keys() is a
tuple early enough, resulting in a SystemError:
>>> class C:
... def __getitem__(self, item):
... raise KeyError, item
... def keys(self):
... return 'a'
...
>>> c=C()
>>> print eval('dir()', globals(), c)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 0, in ?
SystemError: Objects/listobject.c:2110: bad argument to
internal function
----------------------------------------------------------------------
>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-08-06 18:43
Message:
Logged In: YES
user_id=80475
It's a bug. Will fix.
----------------------------------------------------------------------
Comment By: ben handley (bhandley)
Date: 2004-08-06 18:05
Message:
Logged In: YES
user_id=765626
If it raised a TypeError rather than SystemError I would
think it fine. I didn't think that python code should be
able to cause correct C code to generate `bad argument to
internal function'. To me, that sounds like `some C function
called some other C function badly', rather than just a bad
type from python.
In fact I believe that that is exactly what is happening.
Here are the final two lines from the backtrace:
#0 PyList_Sort (v=0x4029bc20) at Objects/listobject.c:2110
#1 0x0807b659 in PyObject_Dir (arg=0x0) at
Objects/object.c:1705
PyList_Sort seems to assume that it will always be passed a
list, hence it calls PyErr_BadInternalCall() if it's not.
Assuming this is true, either PyObject_Dir or PyMapping_Keys
should do the type checking and raise TypeError if it's not
a list. Doesn't PyErr_BadInternalCall() mean that someone
violated the C API of an internal function, therefore the
bug is in C code rather than python?
----------------------------------------------------------------------
Comment By: Ilya Sandler (isandler)
Date: 2004-08-06 13:49
Message:
Logged In: YES
user_id=971153
Why do you think the existing behaviour is wrong?
All Python type checking is done at run time
E.g.
>>> 12+"123"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand types for +: 'int' and 'str'
How is your example different?
I would suggest to close the bug
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1004669&group_id=5470
More information about the Python-bugs-list
mailing list