mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
Replace BadInternalCall with TypeError. Add a test case. Fix whitespace.
Just van Rossum showed a weird, but clever way for pure python code to trigger the BadInternalCall. The C code had assumed that calling a class constructor would return an instance of that class; however, classes that abuse __new__ can invalidate that assumption.
This commit is contained in:
parent
21d77f5e9c
commit
b02bb5ed0a
2 changed files with 10 additions and 2 deletions
|
@ -558,6 +558,13 @@ if type(dictlike.fromkeys('a')) is not dictlike:
|
||||||
raise TestFailed, 'dictsubclass.fromkeys created wrong type'
|
raise TestFailed, 'dictsubclass.fromkeys created wrong type'
|
||||||
if type(dictlike().fromkeys('a')) is not dictlike:
|
if type(dictlike().fromkeys('a')) is not dictlike:
|
||||||
raise TestFailed, 'dictsubclass.fromkeys created wrong type'
|
raise TestFailed, 'dictsubclass.fromkeys created wrong type'
|
||||||
|
from UserDict import UserDict
|
||||||
|
class mydict(dict):
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
return UserDict(*args, **kwargs)
|
||||||
|
try: mydict.fromkeys('a b c'.split())
|
||||||
|
except TypeError: pass
|
||||||
|
else: raise TestFailed, 'dict.fromkeys() failed to detect non-dict class.'
|
||||||
# dict.copy()
|
# dict.copy()
|
||||||
d = {1:1, 2:2, 3:3}
|
d = {1:1, 2:2, 3:3}
|
||||||
if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
|
if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
|
||||||
|
|
|
@ -973,15 +973,16 @@ dict_fromkeys(PyObject *mp, PyObject *args)
|
||||||
PyObject *cls;
|
PyObject *cls;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "OO|O:fromkeys", &cls, &seq, &value))
|
if (!PyArg_ParseTuple(args, "OO|O:fromkeys", &cls, &seq, &value))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
d = PyObject_CallObject(cls, NULL);
|
d = PyObject_CallObject(cls, NULL);
|
||||||
if (d == NULL)
|
if (d == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!PyDict_Check(d)) {
|
if (!PyDict_Check(d)) {
|
||||||
PyErr_BadInternalCall();
|
|
||||||
Py_DECREF(d);
|
Py_DECREF(d);
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"class constructor must return a subclass of dict");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue