diff Objects/typeobject.c @ 65599:cd80d05f2fd6

Revert r85797 (and r85798): it broke the Windows buildbots because of test_multiprocessing's misbehaviour.
author Antoine Pitrou <solipsis@pitrou.net>
date Fri, 22 Oct 2010 21:41:05 +0000
parents d985ba9811e9
children be8ae6b17539
line wrap: on
line diff
--- a/Objects/typeobject.c	Fri Oct 22 19:45:13 2010 +0000
+++ b/Objects/typeobject.c	Fri Oct 22 21:41:05 2010 +0000
@@ -3010,19 +3010,14 @@
 import_copyreg(void)
 {
     static PyObject *copyreg_str;
-    static PyObject *mod_copyreg = NULL;
 
     if (!copyreg_str) {
         copyreg_str = PyUnicode_InternFromString("copyreg");
         if (copyreg_str == NULL)
             return NULL;
     }
-    if (!mod_copyreg) {
-        mod_copyreg = PyImport_Import(copyreg_str);
-    }
-
-    Py_XINCREF(mod_copyreg);
-    return mod_copyreg;
+
+    return PyImport_Import(copyreg_str);
 }
 
 static PyObject *
@@ -3031,16 +3026,14 @@
     PyObject *clsdict;
     PyObject *copyreg;
     PyObject *slotnames;
-    static PyObject *str_slotnames;
-
-    if (str_slotnames == NULL) {
-        str_slotnames = PyUnicode_InternFromString("__slotnames__");
-        if (str_slotnames == NULL)
-            return NULL;
+
+    if (!PyType_Check(cls)) {
+        Py_INCREF(Py_None);
+        return Py_None;
     }
 
     clsdict = ((PyTypeObject *)cls)->tp_dict;
-    slotnames = PyDict_GetItem(clsdict, str_slotnames);
+    slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
     if (slotnames != NULL && PyList_Check(slotnames)) {
         Py_INCREF(slotnames);
         return slotnames;
@@ -3074,20 +3067,12 @@
     PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
     PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
     Py_ssize_t i, n;
-    static PyObject *str_getnewargs = NULL, *str_getstate = NULL,
-                    *str_newobj = NULL;
-
-    if (str_getnewargs == NULL) {
-        str_getnewargs = PyUnicode_InternFromString("__getnewargs__");
-        str_getstate = PyUnicode_InternFromString("__getstate__");
-        str_newobj = PyUnicode_InternFromString("__newobj__");
-        if (!str_getnewargs || !str_getstate || !str_newobj)
-            return NULL;
-    }
-
-    cls = (PyObject *) Py_TYPE(obj);
-
-    getnewargs = PyObject_GetAttr(obj, str_getnewargs);
+
+    cls = PyObject_GetAttrString(obj, "__class__");
+    if (cls == NULL)
+        return NULL;
+
+    getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
     if (getnewargs != NULL) {
         args = PyObject_CallObject(getnewargs, NULL);
         Py_DECREF(getnewargs);
@@ -3105,7 +3090,7 @@
     if (args == NULL)
         goto end;
 
-    getstate = PyObject_GetAttr(obj, str_getstate);
+    getstate = PyObject_GetAttrString(obj, "__getstate__");
     if (getstate != NULL) {
         state = PyObject_CallObject(getstate, NULL);
         Py_DECREF(getstate);
@@ -3113,18 +3098,17 @@
             goto end;
     }
     else {
-        PyObject **dict;
         PyErr_Clear();
-        dict = _PyObject_GetDictPtr(obj);
-        if (dict && *dict)
-            state = *dict;
-        else
+        state = PyObject_GetAttrString(obj, "__dict__");
+        if (state == NULL) {
+            PyErr_Clear();
             state = Py_None;
-        Py_INCREF(state);
+            Py_INCREF(state);
+        }
         names = slotnames(cls);
         if (names == NULL)
             goto end;
-        if (names != Py_None && PyList_GET_SIZE(names) > 0) {
+        if (names != Py_None) {
             assert(PyList_Check(names));
             slots = PyDict_New();
             if (slots == NULL)
@@ -3183,7 +3167,7 @@
     copyreg = import_copyreg();
     if (copyreg == NULL)
         goto end;
-    newobj = PyObject_GetAttr(copyreg, str_newobj);
+    newobj = PyObject_GetAttrString(copyreg, "__newobj__");
     if (newobj == NULL)
         goto end;
 
@@ -3191,8 +3175,8 @@
     args2 = PyTuple_New(n+1);
     if (args2 == NULL)
         goto end;
-    Py_INCREF(cls);
     PyTuple_SET_ITEM(args2, 0, cls);
+    cls = NULL;
     for (i = 0; i < n; i++) {
         PyObject *v = PyTuple_GET_ITEM(args, i);
         Py_INCREF(v);
@@ -3202,6 +3186,7 @@
     res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
 
   end:
+    Py_XDECREF(cls);
     Py_XDECREF(args);
     Py_XDECREF(args2);
     Py_XDECREF(slots);
@@ -3261,34 +3246,31 @@
 static PyObject *
 object_reduce_ex(PyObject *self, PyObject *args)
 {
-    static PyObject *str_reduce = NULL, *objreduce;
     PyObject *reduce, *res;
     int proto = 0;
 
     if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
         return NULL;
 
-    if (str_reduce == NULL) {
-        str_reduce = PyUnicode_InternFromString("__reduce__");
-        objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
-                                         "__reduce__");
-        if (str_reduce == NULL || objreduce == NULL)
-            return NULL;
-    }
-
-    reduce = PyObject_GetAttr(self, str_reduce);
+    reduce = PyObject_GetAttrString(self, "__reduce__");
     if (reduce == NULL)
         PyErr_Clear();
     else {
-        PyObject *cls, *clsreduce;
+        PyObject *cls, *clsreduce, *objreduce;
         int override;
-
-        cls = (PyObject *) Py_TYPE(self);
-        clsreduce = PyObject_GetAttr(cls, str_reduce);
+        cls = PyObject_GetAttrString(self, "__class__");
+        if (cls == NULL) {
+            Py_DECREF(reduce);
+            return NULL;
+        }
+        clsreduce = PyObject_GetAttrString(cls, "__reduce__");
+        Py_DECREF(cls);
         if (clsreduce == NULL) {
             Py_DECREF(reduce);
             return NULL;
         }
+        objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
+                                         "__reduce__");
         override = (clsreduce != objreduce);
         Py_DECREF(clsreduce);
         if (override) {