Kaydet (Commit) f00368f9 authored tarafından Jeremy Hylton's avatar Jeremy Hylton

Remove many blanket try/except clauses.

SF bug [ 751276 ] cPickle doesn't raise error, pickle does (recursiondepth)

Most of the calls to PyErr_Clear() were intended to catch & clear an
attribute error and try something different.  Guard all those cases
with a PyErr_ExceptionMatches() and fail if some other error
occurred.  The other error is likely a bug in the user code.

This is basically the C equivalent of changing "except:" to
"except AttributeError:"
üst a1ad5f65
...@@ -833,8 +833,12 @@ whichmodule(PyObject *global, PyObject *global_name) ...@@ -833,8 +833,12 @@ whichmodule(PyObject *global, PyObject *global_name)
*global_name_attr = 0, *name = 0; *global_name_attr = 0, *name = 0;
module = PyObject_GetAttrString(global, "__module__"); module = PyObject_GetAttrString(global, "__module__");
if (module) return module; if (module)
PyErr_Clear(); return module;
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
return NULL;
if (!( modules_dict = PySys_GetObject("modules"))) if (!( modules_dict = PySys_GetObject("modules")))
return NULL; return NULL;
...@@ -846,7 +850,10 @@ whichmodule(PyObject *global, PyObject *global_name) ...@@ -846,7 +850,10 @@ whichmodule(PyObject *global, PyObject *global_name)
global_name_attr = PyObject_GetAttr(module, global_name); global_name_attr = PyObject_GetAttr(module, global_name);
if (!global_name_attr) { if (!global_name_attr) {
PyErr_Clear(); if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
return NULL;
continue; continue;
} }
...@@ -1814,7 +1821,10 @@ save_inst(Picklerobject *self, PyObject *args) ...@@ -1814,7 +1821,10 @@ save_inst(Picklerobject *self, PyObject *args)
} }
} }
else { else {
PyErr_Clear(); if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
goto finally;
} }
if (!self->bin) { if (!self->bin) {
...@@ -1859,10 +1869,16 @@ save_inst(Picklerobject *self, PyObject *args) ...@@ -1859,10 +1869,16 @@ save_inst(Picklerobject *self, PyObject *args)
goto finally; goto finally;
} }
else { else {
PyErr_Clear(); if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
goto finally;
if (!( state = PyObject_GetAttr(args, __dict___str))) { if (!( state = PyObject_GetAttr(args, __dict___str))) {
PyErr_Clear(); if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
goto finally;
res = 0; res = 0;
goto finally; goto finally;
} }
...@@ -2141,7 +2157,10 @@ save_reduce(Picklerobject *self, PyObject *args, PyObject *ob) ...@@ -2141,7 +2157,10 @@ save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
PyObject *temp = PyObject_GetAttr(callable, __name___str); PyObject *temp = PyObject_GetAttr(callable, __name___str);
if (temp == NULL) { if (temp == NULL) {
PyErr_Clear(); if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
return -1;
use_newobj = 0; use_newobj = 0;
} }
else { else {
...@@ -2176,8 +2195,13 @@ save_reduce(Picklerobject *self, PyObject *args, PyObject *ob) ...@@ -2176,8 +2195,13 @@ save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
PyObject *ob_dot_class; PyObject *ob_dot_class;
ob_dot_class = PyObject_GetAttr(ob, __class___str); ob_dot_class = PyObject_GetAttr(ob, __class___str);
if (ob_dot_class == NULL) if (ob_dot_class == NULL) {
PyErr_Clear(); if (PyErr_ExceptionMatches(
PyExc_AttributeError))
PyErr_Clear();
else
return -1;
}
i = ob_dot_class != cls; /* true iff a problem */ i = ob_dot_class != cls; /* true iff a problem */
Py_XDECREF(ob_dot_class); Py_XDECREF(ob_dot_class);
if (i) { if (i) {
...@@ -2447,7 +2471,10 @@ save(Picklerobject *self, PyObject *args, int pers_save) ...@@ -2447,7 +2471,10 @@ save(Picklerobject *self, PyObject *args, int pers_save)
} }
} }
else { else {
PyErr_Clear(); if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
goto finally;
/* Check for a __reduce__ method. */ /* Check for a __reduce__ method. */
__reduce__ = PyObject_GetAttr(args, __reduce___str); __reduce__ = PyObject_GetAttr(args, __reduce___str);
if (__reduce__ != NULL) { if (__reduce__ != NULL) {
...@@ -3564,7 +3591,7 @@ Instance_New(PyObject *cls, PyObject *args) ...@@ -3564,7 +3591,7 @@ Instance_New(PyObject *cls, PyObject *args)
PyObject *__getinitargs__; PyObject *__getinitargs__;
__getinitargs__ = PyObject_GetAttr(cls, __getinitargs__ = PyObject_GetAttr(cls,
__getinitargs___str); __getinitargs___str);
if (!__getinitargs__) { if (!__getinitargs__) {
/* We have a class with no __getinitargs__, /* We have a class with no __getinitargs__,
so bypass usual construction */ so bypass usual construction */
...@@ -4253,6 +4280,8 @@ load_build(Unpicklerobject *self) ...@@ -4253,6 +4280,8 @@ load_build(Unpicklerobject *self)
Py_DECREF(junk); Py_DECREF(junk);
return 0; return 0;
} }
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return -1;
PyErr_Clear(); PyErr_Clear();
/* A default __setstate__. First see whether state embeds a /* A default __setstate__. First see whether state embeds a
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment