This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Type returned from .keys() is not checked
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: bhandley, isandler, rhettinger
Priority: normal Keywords:

Created on 2004-08-06 15:08 by bhandley, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg21969 - (view) Author: ben handley (bhandley) Date: 2004-08-06 15:08
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

msg21970 - (view) Author: Ilya Sandler (isandler) Date: 2004-08-06 18:49
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
msg21971 - (view) Author: ben handley (bhandley) Date: 2004-08-06 23:05
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?
msg21972 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-08-06 23:43
Logged In: YES 
user_id=80475

It's a bug.  Will fix.
msg21973 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-08-07 04:55
Logged In: YES 
user_id=80475

Fixed.
See:
    Objects/object.c 2.220
    Lib/ test/test_builtin.py 1.33 

Thanks for the clear bug report and diagnosis.
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40701
2004-08-06 15:08:08bhandleycreate