mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Two fixes to find_class:
1. Only DECREF the class's module when the module is retrieved via PyImport_Import. If it is retrieved from the modules dictionary with PyDict_GetItem, it is using a borrowed reference. 2. If the module doesn't define the desired class, raise the same SystemError that pickle.py does instead of returning an AttributeError (which is cryptic at best). Also, fix the PyArg_ParseTuple in cpm_loads (the externally visible loads) function: Use "S" instead of "O" because cStringIO will croak with a "bad arguments to internal function" if passed anything other than a string.
This commit is contained in:
parent
370f3c9eb6
commit
d10552379d
1 changed files with 22 additions and 9 deletions
|
@ -1935,15 +1935,28 @@ static PyObject *
|
|||
find_class(PyObject *py_module_name, PyObject *py_global_name) {
|
||||
PyObject *global = 0, *module;
|
||||
|
||||
UNLESS(module=PySys_GetObject("modules")) return NULL;
|
||||
UNLESS(module=PyDict_GetItem(module, py_module_name)) {
|
||||
PyErr_Clear();
|
||||
UNLESS(module=PyImport_Import(py_module_name)) return NULL;
|
||||
module = PySys_GetObject("modules");
|
||||
if (module == NULL)
|
||||
return NULL;
|
||||
|
||||
module = PyDict_GetItem(module, py_module_name);
|
||||
if (module == NULL) {
|
||||
module = PyImport_Import(py_module_name);
|
||||
if (!module)
|
||||
return NULL;
|
||||
global = PyObject_GetAttr(module, py_global_name);
|
||||
Py_DECREF(module);
|
||||
}
|
||||
else
|
||||
global = PyObject_GetAttr(module, py_global_name);
|
||||
if (global == NULL) {
|
||||
char buf[256 + 37];
|
||||
sprintf(buf, "Failed to import class %.128s from moduile %.128s",
|
||||
PyString_AS_STRING(py_global_name),
|
||||
PyString_AS_STRING(py_module_name));
|
||||
PyErr_SetString(PyExc_SystemError, buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
global=PyObject_GetAttr(module, py_global_name);
|
||||
Py_DECREF(module);
|
||||
|
||||
return global;
|
||||
}
|
||||
|
||||
|
@ -4093,7 +4106,7 @@ cpm_loads(PyObject *self, PyObject *args) {
|
|||
PyObject *ob, *file = 0, *res = NULL;
|
||||
Unpicklerobject *unpickler = 0;
|
||||
|
||||
UNLESS(PyArg_ParseTuple(args, "O", &ob))
|
||||
UNLESS(PyArg_ParseTuple(args, "S", &ob))
|
||||
goto finally;
|
||||
|
||||
UNLESS(file = PycStringIO->NewInput(ob))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue