Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(164965)

Delta Between Two Patch Sets: Objects/typeobject.c

Issue 23722: During metaclass.__init__, super() of the constructed class does not work
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* Type object implementation */ 1 /* Type object implementation */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include "frameobject.h" 4 #include "frameobject.h"
5 #include "structmember.h" 5 #include "structmember.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 8
9 9
10 /* Support type attribute cache */ 10 /* Support type attribute cache */
(...skipping 845 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 else if (!PyUnicode_Check(mod)) { 856 else if (!PyUnicode_Check(mod)) {
857 Py_DECREF(mod); 857 Py_DECREF(mod);
858 mod = NULL; 858 mod = NULL;
859 } 859 }
860 name = type_qualname(type, NULL); 860 name = type_qualname(type, NULL);
861 if (name == NULL) { 861 if (name == NULL) {
862 Py_XDECREF(mod); 862 Py_XDECREF(mod);
863 return NULL; 863 return NULL;
864 } 864 }
865 865
866 if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) 866 if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins))
867 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); 867 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
868 else 868 else
869 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); 869 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
870 870
871 Py_XDECREF(mod); 871 Py_XDECREF(mod);
872 Py_DECREF(name); 872 Py_DECREF(name);
873 return rtn; 873 return rtn;
874 } 874 }
875 875
876 static PyObject * 876 static PyObject *
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 /* We get here only if the type has GC */ 1129 /* We get here only if the type has GC */
1130 1130
1131 /* UnTrack and re-Track around the trashcan macro, alas */ 1131 /* UnTrack and re-Track around the trashcan macro, alas */
1132 /* See explanation at end of function for full disclosure */ 1132 /* See explanation at end of function for full disclosure */
1133 PyObject_GC_UnTrack(self); 1133 PyObject_GC_UnTrack(self);
1134 ++_PyTrash_delete_nesting; 1134 ++_PyTrash_delete_nesting;
1135 ++ tstate->trash_delete_nesting; 1135 ++ tstate->trash_delete_nesting;
1136 Py_TRASHCAN_SAFE_BEGIN(self); 1136 Py_TRASHCAN_SAFE_BEGIN(self);
1137 --_PyTrash_delete_nesting; 1137 --_PyTrash_delete_nesting;
1138 -- tstate->trash_delete_nesting; 1138 -- tstate->trash_delete_nesting;
1139 /* DO NOT restore GC tracking at this point. weakref callbacks
1140 * (if any, and whether directly here or indirectly in something we
1141 * call) may trigger GC, and if self is tracked at that point, it
1142 * will look like trash to GC and GC will try to delete self again.
1143 */
1144 1139
1145 /* Find the nearest base with a different tp_dealloc */ 1140 /* Find the nearest base with a different tp_dealloc */
1146 base = type; 1141 base = type;
1147 while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { 1142 while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) {
1148 base = base->tp_base; 1143 base = base->tp_base;
1149 assert(base); 1144 assert(base);
1150 } 1145 }
1151 1146
1152 has_finalizer = type->tp_finalize || type->tp_del; 1147 has_finalizer = type->tp_finalize || type->tp_del;
1153 1148
1154 /* Maybe call finalizer; exit early if resurrected */ 1149 if (type->tp_finalize) {
1155 if (has_finalizer)
1156 _PyObject_GC_TRACK(self); 1150 _PyObject_GC_TRACK(self);
1157
1158 if (type->tp_finalize) {
1159 if (PyObject_CallFinalizerFromDealloc(self) < 0) { 1151 if (PyObject_CallFinalizerFromDealloc(self) < 0) {
1160 /* Resurrected */ 1152 /* Resurrected */
1161 goto endlabel; 1153 goto endlabel;
1162 } 1154 }
1163 } 1155 _PyObject_GC_UNTRACK(self);
1164 /* If we added a weaklist, we clear it. Do this *before* calling 1156 }
1165 tp_del, clearing slots, or clearing the instance dict. */ 1157 /*
1158 If we added a weaklist, we clear it. Do this *before* calling tp_del,
1159 clearing slots, or clearing the instance dict.
1160
1161 GC tracking must be off at this point. weakref callbacks (if any, and
1162 whether directly here or indirectly in something we call) may trigger GC,
1163 and if self is tracked at that point, it will look like trash to GC and GC
1164 will try to delete self again.
1165 */
1166 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) 1166 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
1167 PyObject_ClearWeakRefs(self); 1167 PyObject_ClearWeakRefs(self);
1168 1168
1169 if (type->tp_del) { 1169 if (type->tp_del) {
1170 _PyObject_GC_TRACK(self);
1170 type->tp_del(self); 1171 type->tp_del(self);
1171 if (self->ob_refcnt > 0) { 1172 if (self->ob_refcnt > 0) {
1172 /* Resurrected */ 1173 /* Resurrected */
1173 goto endlabel; 1174 goto endlabel;
1174 } 1175 }
1176 _PyObject_GC_UNTRACK(self);
1175 } 1177 }
1176 if (has_finalizer) { 1178 if (has_finalizer) {
1177 _PyObject_GC_UNTRACK(self);
1178 /* New weakrefs could be created during the finalizer call. 1179 /* New weakrefs could be created during the finalizer call.
1179 If this occurs, clear them out without calling their 1180 If this occurs, clear them out without calling their
1180 finalizers since they might rely on part of the object 1181 finalizers since they might rely on part of the object
1181 being finalized that has already been destroyed. */ 1182 being finalized that has already been destroyed. */
1182 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { 1183 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
1183 /* Modeled after GET_WEAKREFS_LISTPTR() */ 1184 /* Modeled after GET_WEAKREFS_LISTPTR() */
1184 PyWeakReference **list = (PyWeakReference **) \ 1185 PyWeakReference **list = (PyWeakReference **) \
1185 PyObject_GET_WEAKREFS_LISTPTR(self); 1186 PyObject_GET_WEAKREFS_LISTPTR(self);
1186 while (*list) 1187 while (*list)
1187 _PyWeakref_ClearRef(*list); 1188 _PyWeakref_ClearRef(*list);
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 } 1619 }
1619 n = PyDict_Size(set); 1620 n = PyDict_Size(set);
1620 1621
1621 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ 1622 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1622 consistent method resolution\norder (MRO) for bases"); 1623 consistent method resolution\norder (MRO) for bases");
1623 i = 0; 1624 i = 0;
1624 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) { 1625 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1625 PyObject *name = class_name(k); 1626 PyObject *name = class_name(k);
1626 char *name_str; 1627 char *name_str;
1627 if (name != NULL) { 1628 if (name != NULL) {
1628 name_str = _PyUnicode_AsString(name); 1629 name_str = PyUnicode_AsUTF8(name);
1629 if (name_str == NULL) 1630 if (name_str == NULL)
1630 name_str = "?"; 1631 name_str = "?";
1631 } else 1632 } else
1632 name_str = "?"; 1633 name_str = "?";
1633 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); 1634 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str);
1634 Py_XDECREF(name); 1635 Py_XDECREF(name);
1635 if (--n && (size_t)(off+1) < sizeof(buf)) { 1636 if (--n && (size_t)(off+1) < sizeof(buf)) {
1636 buf[off++] = ','; 1637 buf[off++] = ',';
1637 buf[off] = '\0'; 1638 buf[off] = '\0';
1638 } 1639 }
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after
2395 base->tp_name); 2396 base->tp_name);
2396 goto error; 2397 goto error;
2397 } 2398 }
2398 2399
2399 /* Check for valid slot names and two special cases */ 2400 /* Check for valid slot names and two special cases */
2400 for (i = 0; i < nslots; i++) { 2401 for (i = 0; i < nslots; i++) {
2401 PyObject *tmp = PyTuple_GET_ITEM(slots, i); 2402 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
2402 if (!valid_identifier(tmp)) 2403 if (!valid_identifier(tmp))
2403 goto error; 2404 goto error;
2404 assert(PyUnicode_Check(tmp)); 2405 assert(PyUnicode_Check(tmp));
2405 if (_PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) { 2406 if (_PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) {
2406 if (!may_add_dict || add_dict) { 2407 if (!may_add_dict || add_dict) {
2407 PyErr_SetString(PyExc_TypeError, 2408 PyErr_SetString(PyExc_TypeError,
2408 "__dict__ slot disallowed: " 2409 "__dict__ slot disallowed: "
2409 "we already got one"); 2410 "we already got one");
2410 goto error; 2411 goto error;
2411 } 2412 }
2412 add_dict++; 2413 add_dict++;
2413 } 2414 }
2414 if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) { 2415 if (_PyUnicode_EqualToASCIIString(tmp, "__weakref__")) {
2415 if (!may_add_weak || add_weak) { 2416 if (!may_add_weak || add_weak) {
2416 PyErr_SetString(PyExc_TypeError, 2417 PyErr_SetString(PyExc_TypeError,
2417 "__weakref__ slot disallowed: " 2418 "__weakref__ slot disallowed: "
2418 "either we already got one, " 2419 "either we already got one, "
2419 "or __itemsize__ != 0"); 2420 "or __itemsize__ != 0");
2420 goto error; 2421 goto error;
2421 } 2422 }
2422 add_weak++; 2423 add_weak++;
2423 } 2424 }
2424 } 2425 }
2425 2426
2426 /* Copy slots into a list, mangle names and sort them. 2427 /* Copy slots into a list, mangle names and sort them.
2427 Sorted names are needed for __class__ assignment. 2428 Sorted names are needed for __class__ assignment.
2428 Convert them back to tuple at the end. 2429 Convert them back to tuple at the end.
2429 */ 2430 */
2430 newslots = PyList_New(nslots - add_dict - add_weak); 2431 newslots = PyList_New(nslots - add_dict - add_weak);
2431 if (newslots == NULL) 2432 if (newslots == NULL)
2432 goto error; 2433 goto error;
2433 for (i = j = 0; i < nslots; i++) { 2434 for (i = j = 0; i < nslots; i++) {
2434 tmp = PyTuple_GET_ITEM(slots, i); 2435 tmp = PyTuple_GET_ITEM(slots, i);
2435 if ((add_dict && 2436 if ((add_dict &&
2436 _PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) || 2437 _PyUnicode_EqualToASCIIId(tmp, &PyId___dict__)) ||
2437 (add_weak && 2438 (add_weak &&
2438 PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) 2439 _PyUnicode_EqualToASCIIString(tmp, "__weakref__")))
2439 continue; 2440 continue;
2440 tmp =_Py_Mangle(name, tmp); 2441 tmp =_Py_Mangle(name, tmp);
2441 if (!tmp) { 2442 if (!tmp) {
2442 Py_DECREF(newslots); 2443 Py_DECREF(newslots);
2443 goto error; 2444 goto error;
2444 } 2445 }
2445 PyList_SET_ITEM(newslots, j, tmp); 2446 PyList_SET_ITEM(newslots, j, tmp);
2446 if (PyDict_GetItem(dict, tmp)) { 2447 if (PyDict_GetItem(dict, tmp)) {
2447 PyErr_Format(PyExc_ValueError, 2448 PyErr_Format(PyExc_ValueError,
2448 "%R in __slots__ conflicts with class variable", 2449 "%R in __slots__ conflicts with class variable",
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
2567 and is a string. The __doc__ accessor will first look for tp_doc; 2568 and is a string. The __doc__ accessor will first look for tp_doc;
2568 if that fails, it will still look into __dict__. 2569 if that fails, it will still look into __dict__.
2569 */ 2570 */
2570 { 2571 {
2571 PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__); 2572 PyObject *doc = _PyDict_GetItemId(dict, &PyId___doc__);
2572 if (doc != NULL && PyUnicode_Check(doc)) { 2573 if (doc != NULL && PyUnicode_Check(doc)) {
2573 Py_ssize_t len; 2574 Py_ssize_t len;
2574 char *doc_str; 2575 char *doc_str;
2575 char *tp_doc; 2576 char *tp_doc;
2576 2577
2577 doc_str = _PyUnicode_AsString(doc); 2578 doc_str = PyUnicode_AsUTF8(doc);
2578 if (doc_str == NULL) 2579 if (doc_str == NULL)
2579 goto error; 2580 goto error;
2580 /* Silently truncate the docstring if it contains null bytes. */ 2581 /* Silently truncate the docstring if it contains null bytes. */
2581 len = strlen(doc_str); 2582 len = strlen(doc_str);
2582 tp_doc = (char *)PyObject_MALLOC(len + 1); 2583 tp_doc = (char *)PyObject_MALLOC(len + 1);
2583 if (tp_doc == NULL) { 2584 if (tp_doc == NULL) {
2584 PyErr_NoMemory(); 2585 PyErr_NoMemory();
2585 goto error; 2586 goto error;
2586 } 2587 }
2587 memcpy(tp_doc, doc_str, len + 1); 2588 memcpy(tp_doc, doc_str, len + 1);
(...skipping 27 matching lines...) Expand all
2615 goto error; 2616 goto error;
2616 } 2617 }
2617 Py_DECREF(tmp); 2618 Py_DECREF(tmp);
2618 } 2619 }
2619 2620
2620 /* Add descriptors for custom slots from __slots__, or for __dict__ */ 2621 /* Add descriptors for custom slots from __slots__, or for __dict__ */
2621 mp = PyHeapType_GET_MEMBERS(et); 2622 mp = PyHeapType_GET_MEMBERS(et);
2622 slotoffset = base->tp_basicsize; 2623 slotoffset = base->tp_basicsize;
2623 if (et->ht_slots != NULL) { 2624 if (et->ht_slots != NULL) {
2624 for (i = 0; i < nslots; i++, mp++) { 2625 for (i = 0; i < nslots; i++, mp++) {
2625 mp->name = _PyUnicode_AsString( 2626 mp->name = PyUnicode_AsUTF8(
2626 PyTuple_GET_ITEM(et->ht_slots, i)); 2627 PyTuple_GET_ITEM(et->ht_slots, i));
2627 if (mp->name == NULL) 2628 if (mp->name == NULL)
2628 goto error; 2629 goto error;
2629 mp->type = T_OBJECT_EX; 2630 mp->type = T_OBJECT_EX;
2630 mp->offset = slotoffset; 2631 mp->offset = slotoffset;
2631 2632
2632 /* __dict__ and __weakref__ are already filtered out */ 2633 /* __dict__ and __weakref__ are already filtered out */
2633 assert(strcmp(mp->name, "__dict__") != 0); 2634 assert(strcmp(mp->name, "__dict__") != 0);
2634 assert(strcmp(mp->name, "__weakref__") != 0); 2635 assert(strcmp(mp->name, "__weakref__") != 0);
2635 2636
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2679 /* Always override allocation strategy to use regular heap */ 2680 /* Always override allocation strategy to use regular heap */
2680 type->tp_alloc = PyType_GenericAlloc; 2681 type->tp_alloc = PyType_GenericAlloc;
2681 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { 2682 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
2682 type->tp_free = PyObject_GC_Del; 2683 type->tp_free = PyObject_GC_Del;
2683 type->tp_traverse = subtype_traverse; 2684 type->tp_traverse = subtype_traverse;
2684 type->tp_clear = subtype_clear; 2685 type->tp_clear = subtype_clear;
2685 } 2686 }
2686 else 2687 else
2687 type->tp_free = PyObject_Del; 2688 type->tp_free = PyObject_Del;
2688 2689
2689 /* store type in class' cell */ 2690 /* store type in class' cell if one is supplied */
2690 cell = _PyDict_GetItemId(dict, &PyId___classcell__); 2691 cell = _PyDict_GetItemId(dict, &PyId___classcell__);
2691 if (cell != NULL && PyCell_Check(cell)) { 2692 if (cell != NULL) {
2693 /* At least one method requires a reference to its defining class */
2694 if (!PyCell_Check(cell)) {
2695 PyErr_Format(PyExc_TypeError,
2696 "__classcell__ must be a nonlocal cell, not %.200R",
2697 Py_TYPE(cell));
2698 goto error;
2699 }
2692 PyCell_Set(cell, (PyObject *) type); 2700 PyCell_Set(cell, (PyObject *) type);
2693 _PyDict_DelItemId(dict, &PyId___classcell__); 2701 _PyDict_DelItemId(dict, &PyId___classcell__);
2694 PyErr_Clear(); 2702 PyErr_Clear();
2695 } 2703 }
2696 2704
2697 /* Initialize the rest */ 2705 /* Initialize the rest */
2698 if (PyType_Ready(type) < 0) 2706 if (PyType_Ready(type) < 0)
2699 goto error; 2707 goto error;
2700 2708
2701 /* Put the proper slots in place */ 2709 /* Put the proper slots in place */
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
2840 if (PyType_Ready(type) < 0) 2848 if (PyType_Ready(type) < 0)
2841 goto fail; 2849 goto fail;
2842 2850
2843 if (type->tp_dictoffset) { 2851 if (type->tp_dictoffset) {
2844 res->ht_cached_keys = _PyDict_NewKeysForClass(); 2852 res->ht_cached_keys = _PyDict_NewKeysForClass();
2845 } 2853 }
2846 2854
2847 /* Set type.__module__ */ 2855 /* Set type.__module__ */
2848 s = strrchr(spec->name, '.'); 2856 s = strrchr(spec->name, '.');
2849 if (s != NULL) { 2857 if (s != NULL) {
2858 int err;
2850 modname = PyUnicode_FromStringAndSize( 2859 modname = PyUnicode_FromStringAndSize(
2851 spec->name, (Py_ssize_t)(s - spec->name)); 2860 spec->name, (Py_ssize_t)(s - spec->name));
2852 if (modname == NULL) { 2861 if (modname == NULL) {
2853 goto fail; 2862 goto fail;
2854 } 2863 }
2855 _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname); 2864 err = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname);
2856 Py_DECREF(modname); 2865 Py_DECREF(modname);
2866 if (err != 0)
2867 goto fail;
2857 } else { 2868 } else {
2858 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, 2869 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
2859 "builtin type %.200s has no __module__ attribute", 2870 "builtin type %.200s has no __module__ attribute",
2860 spec->name)) 2871 spec->name))
2861 goto fail; 2872 goto fail;
2862 } 2873 }
2863 2874
2864 return (PyObject*)res; 2875 return (PyObject*)res;
2865 2876
2866 fail: 2877 fail:
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
2906 #if MCACHE_STATS 2917 #if MCACHE_STATS
2907 method_cache_hits++; 2918 method_cache_hits++;
2908 #endif 2919 #endif
2909 return method_cache[h].value; 2920 return method_cache[h].value;
2910 } 2921 }
2911 } 2922 }
2912 2923
2913 /* Look in tp_dict of types in MRO */ 2924 /* Look in tp_dict of types in MRO */
2914 mro = type->tp_mro; 2925 mro = type->tp_mro;
2915 2926
2916 /* If mro is NULL, the type is either not yet initialized 2927 if (mro == NULL) {
2917 by PyType_Ready(), or already cleared by type_clear(). 2928 if ((type->tp_flags & Py_TPFLAGS_READYING) == 0 &&
2918 Either way the safest thing to do is to return NULL. */ 2929 PyType_Ready(type) < 0) {
2919 if (mro == NULL) 2930 /* It's not ideal to clear the error condition,
2920 return NULL; 2931 but this function is documented as not setting
2932 an exception, and I don't want to change that.
2933 When PyType_Ready() can't proceed, it won't
2934 set the "ready" flag, so future attempts to ready
2935 the same type will call it again -- hopefully
2936 in a context that propagates the exception out.
2937 */
2938 PyErr_Clear();
2939 return NULL;
2940 }
2941 mro = type->tp_mro;
2942 if (mro == NULL) {
2943 return NULL;
2944 }
2945 }
2921 2946
2922 res = NULL; 2947 res = NULL;
2923 /* keep a strong reference to mro because type->tp_mro can be replaced 2948 /* keep a strong reference to mro because type->tp_mro can be replaced
2924 during PyDict_GetItem(dict, name) */ 2949 during PyDict_GetItem(dict, name) */
2925 Py_INCREF(mro); 2950 Py_INCREF(mro);
2926 assert(PyTuple_Check(mro)); 2951 assert(PyTuple_Check(mro));
2927 n = PyTuple_GET_SIZE(mro); 2952 n = PyTuple_GET_SIZE(mro);
2928 for (i = 0; i < n; i++) { 2953 for (i = 0; i < n; i++) {
2929 base = PyTuple_GET_ITEM(mro, i); 2954 base = PyTuple_GET_ITEM(mro, i);
2930 assert(PyType_Check(base)); 2955 assert(PyType_Check(base));
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after
3513 PyErr_Clear(); 3538 PyErr_Clear();
3514 else if (!PyUnicode_Check(mod)) { 3539 else if (!PyUnicode_Check(mod)) {
3515 Py_DECREF(mod); 3540 Py_DECREF(mod);
3516 mod = NULL; 3541 mod = NULL;
3517 } 3542 }
3518 name = type_qualname(type, NULL); 3543 name = type_qualname(type, NULL);
3519 if (name == NULL) { 3544 if (name == NULL) {
3520 Py_XDECREF(mod); 3545 Py_XDECREF(mod);
3521 return NULL; 3546 return NULL;
3522 } 3547 }
3523 if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) 3548 if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins))
3524 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); 3549 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
3525 else 3550 else
3526 rtn = PyUnicode_FromFormat("<%s object at %p>", 3551 rtn = PyUnicode_FromFormat("<%s object at %p>",
3527 type->tp_name, self); 3552 type->tp_name, self);
3528 Py_XDECREF(mod); 3553 Py_XDECREF(mod);
3529 Py_DECREF(name); 3554 Py_DECREF(name);
3530 return rtn; 3555 return rtn;
3531 } 3556 }
3532 3557
3533 static PyObject * 3558 static PyObject *
(...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after
4367 { 4392 {
4368 Py_RETURN_NONE; 4393 Py_RETURN_NONE;
4369 } 4394 }
4370 4395
4371 PyDoc_STRVAR(object_init_subclass_doc, 4396 PyDoc_STRVAR(object_init_subclass_doc,
4372 "This method is called when a class is subclassed.\n" 4397 "This method is called when a class is subclassed.\n"
4373 "\n" 4398 "\n"
4374 "The default implementation does nothing. It may be\n" 4399 "The default implementation does nothing. It may be\n"
4375 "overridden to extend subclasses.\n"); 4400 "overridden to extend subclasses.\n");
4376 4401
4377 /*
4378 from PEP 3101, this code implements:
4379
4380 class object:
4381 def __format__(self, format_spec):
4382 return format(str(self), format_spec)
4383 */
4384 static PyObject * 4402 static PyObject *
4385 object_format(PyObject *self, PyObject *args) 4403 object_format(PyObject *self, PyObject *args)
4386 { 4404 {
4387 PyObject *format_spec; 4405 PyObject *format_spec;
4388 PyObject *self_as_str = NULL; 4406 PyObject *self_as_str = NULL;
4389 PyObject *result = NULL; 4407 PyObject *result = NULL;
4390 4408
4391 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) 4409 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
4392 return NULL; 4410 return NULL;
4393 4411
4412 /* Issue 7994: If we're converting to a string, we
4413 should reject format specifications */
4414 if (PyUnicode_GET_LENGTH(format_spec) > 0) {
4415 PyErr_Format(PyExc_TypeError,
4416 "unsupported format string passed to %.200s.__format__",
4417 self->ob_type->tp_name);
4418 return NULL;
4419 }
4394 self_as_str = PyObject_Str(self); 4420 self_as_str = PyObject_Str(self);
4395 if (self_as_str != NULL) { 4421 if (self_as_str != NULL) {
4396 /* Issue 7994: If we're converting to a string, we
4397 should reject format specifications */
4398 if (PyUnicode_GET_LENGTH(format_spec) > 0) {
4399 PyErr_SetString(PyExc_TypeError,
4400 "non-empty format string passed to object.__format__");
4401 goto done;
4402 }
4403
4404 result = PyObject_Format(self_as_str, format_spec); 4422 result = PyObject_Format(self_as_str, format_spec);
4405 } 4423 Py_DECREF(self_as_str);
4406 4424 }
4407 done:
4408 Py_XDECREF(self_as_str);
4409
4410 return result; 4425 return result;
4411 } 4426 }
4412 4427
4413 static PyObject * 4428 static PyObject *
4414 object_sizeof(PyObject *self, PyObject *args) 4429 object_sizeof(PyObject *self, PyObject *args)
4415 { 4430 {
4416 Py_ssize_t res, isize; 4431 Py_ssize_t res, isize;
4417 4432
4418 res = 0; 4433 res = 0;
4419 isize = self->ob_type->tp_itemsize; 4434 isize = self->ob_type->tp_itemsize;
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
4884 type->tp_flags |= Py_TPFLAGS_READYING; 4899 type->tp_flags |= Py_TPFLAGS_READYING;
4885 4900
4886 #ifdef Py_TRACE_REFS 4901 #ifdef Py_TRACE_REFS
4887 /* PyType_Ready is the closest thing we have to a choke point 4902 /* PyType_Ready is the closest thing we have to a choke point
4888 * for type objects, so is the best place I can think of to try 4903 * for type objects, so is the best place I can think of to try
4889 * to get type objects into the doubly-linked list of all objects. 4904 * to get type objects into the doubly-linked list of all objects.
4890 * Still, not all type objects go thru PyType_Ready. 4905 * Still, not all type objects go thru PyType_Ready.
4891 */ 4906 */
4892 _Py_AddToAllObjects((PyObject *)type, 0); 4907 _Py_AddToAllObjects((PyObject *)type, 0);
4893 #endif 4908 #endif
4909
4910 if (type->tp_name == NULL) {
4911 PyErr_Format(PyExc_SystemError,
4912 "Type does not define the tp_name field.");
4913 goto error;
4914 }
4894 4915
4895 /* Initialize tp_base (defaults to BaseObject unless that's us) */ 4916 /* Initialize tp_base (defaults to BaseObject unless that's us) */
4896 base = type->tp_base; 4917 base = type->tp_base;
4897 if (base == NULL && type != &PyBaseObject_Type) { 4918 if (base == NULL && type != &PyBaseObject_Type) {
4898 base = type->tp_base = &PyBaseObject_Type; 4919 base = type->tp_base = &PyBaseObject_Type;
4899 Py_INCREF(base); 4920 Py_INCREF(base);
4900 } 4921 }
4901 4922
4902 /* Now the only way base can still be NULL is if type is 4923 /* Now the only way base can still be NULL is if type is
4903 * &PyBaseObject_Type. 4924 * &PyBaseObject_Type.
(...skipping 2079 matching lines...) Expand 10 before | Expand all | Expand 10 after
6983 for (p = slotdefs; p->name; p++) { 7004 for (p = slotdefs; p->name; p++) {
6984 /* update_slot returns int but can't actually fail */ 7005 /* update_slot returns int but can't actually fail */
6985 update_slot(type, p->name_strobj); 7006 update_slot(type, p->name_strobj);
6986 } 7007 }
6987 } 7008 }
6988 7009
6989 /* Call __set_name__ on all descriptors in a newly generated type */ 7010 /* Call __set_name__ on all descriptors in a newly generated type */
6990 static int 7011 static int
6991 set_names(PyTypeObject *type) 7012 set_names(PyTypeObject *type)
6992 { 7013 {
6993 PyObject *key, *value, *tmp; 7014 PyObject *names_to_set, *key, *value, *set_name, *tmp;
6994 Py_ssize_t i = 0; 7015 Py_ssize_t i = 0;
6995 7016
6996 while (PyDict_Next(type->tp_dict, &i, &key, &value)) { 7017 names_to_set = PyDict_Copy(type->tp_dict);
6997 if (PyObject_HasAttr(value, _PyUnicode_FromId(&PyId___set_name__))) { 7018 if (names_to_set == NULL)
6998 tmp = PyObject_CallMethodObjArgs( 7019 return -1;
6999 value, _PyUnicode_FromId(&PyId___set_name__), 7020
7000 type, key, NULL); 7021 while (PyDict_Next(names_to_set, &i, &key, &value)) {
7001 if (tmp == NULL) 7022 set_name = lookup_maybe(value, &PyId___set_name__);
7023 if (set_name != NULL) {
7024 tmp = PyObject_CallFunctionObjArgs(set_name, type, key, NULL);
7025 Py_DECREF(set_name);
7026 if (tmp == NULL) {
7027 _PyErr_FormatFromCause(PyExc_RuntimeError,
7028 "Error calling __set_name__ on '%.100s' instance %R "
7029 "in '%.100s'",
7030 value->ob_type->tp_name, key, type->tp_name);
7031 Py_DECREF(names_to_set);
7002 return -1; 7032 return -1;
7033 }
7003 else 7034 else
7004 Py_DECREF(tmp); 7035 Py_DECREF(tmp);
7005 } 7036 }
7006 } 7037 else if (PyErr_Occurred()) {
7007 7038 Py_DECREF(names_to_set);
7039 return -1;
7040 }
7041 }
7042
7043 Py_DECREF(names_to_set);
7008 return 0; 7044 return 0;
7009 } 7045 }
7010 7046
7011 /* Call __init_subclass__ on the parent of a newly generated type */ 7047 /* Call __init_subclass__ on the parent of a newly generated type */
7012 static int 7048 static int
7013 init_subclass(PyTypeObject *type, PyObject *kwds) 7049 init_subclass(PyTypeObject *type, PyObject *kwds)
7014 { 7050 {
7015 PyObject *super, *func, *result; 7051 PyObject *super, *func, *result;
7016 PyObject *args[2] = {(PyObject *)type, (PyObject *)type}; 7052 PyObject *args[2] = {(PyObject *)type, (PyObject *)type};
7017 7053
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
7210 Py_ssize_t i, n; 7246 Py_ssize_t i, n;
7211 7247
7212 starttype = su->obj_type; 7248 starttype = su->obj_type;
7213 if (starttype == NULL) 7249 if (starttype == NULL)
7214 goto skip; 7250 goto skip;
7215 7251
7216 /* We want __class__ to return the class of the super object 7252 /* We want __class__ to return the class of the super object
7217 (i.e. super, or a subclass), not the class of su->obj. */ 7253 (i.e. super, or a subclass), not the class of su->obj. */
7218 if (PyUnicode_Check(name) && 7254 if (PyUnicode_Check(name) &&
7219 PyUnicode_GET_LENGTH(name) == 9 && 7255 PyUnicode_GET_LENGTH(name) == 9 &&
7220 _PyUnicode_CompareWithId(name, &PyId___class__) == 0) 7256 _PyUnicode_EqualToASCIIId(name, &PyId___class__))
7221 goto skip; 7257 goto skip;
7222 7258
7223 mro = starttype->tp_mro; 7259 mro = starttype->tp_mro;
7224 if (mro == NULL) 7260 if (mro == NULL)
7225 goto skip; 7261 goto skip;
7226 7262
7227 assert(PyTuple_Check(mro)); 7263 assert(PyTuple_Check(mro));
7228 n = PyTuple_GET_SIZE(mro); 7264 n = PyTuple_GET_SIZE(mro);
7229 7265
7230 /* No need to check the last one: it's gonna be skipped anyway. */ 7266 /* No need to check the last one: it's gonna be skipped anyway. */
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
7422 } 7458 }
7423 if (co->co_freevars == NULL) 7459 if (co->co_freevars == NULL)
7424 n = 0; 7460 n = 0;
7425 else { 7461 else {
7426 assert(PyTuple_Check(co->co_freevars)); 7462 assert(PyTuple_Check(co->co_freevars));
7427 n = PyTuple_GET_SIZE(co->co_freevars); 7463 n = PyTuple_GET_SIZE(co->co_freevars);
7428 } 7464 }
7429 for (i = 0; i < n; i++) { 7465 for (i = 0; i < n; i++) {
7430 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 7466 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
7431 assert(PyUnicode_Check(name)); 7467 assert(PyUnicode_Check(name));
7432 if (!_PyUnicode_CompareWithId(name, &PyId___class__)) { 7468 if (_PyUnicode_EqualToASCIIId(name, &PyId___class__)) {
7433 Py_ssize_t index = co->co_nlocals + 7469 Py_ssize_t index = co->co_nlocals +
7434 PyTuple_GET_SIZE(co->co_cellvars) + i; 7470 PyTuple_GET_SIZE(co->co_cellvars) + i;
7435 PyObject *cell = f->f_localsplus[index]; 7471 PyObject *cell = f->f_localsplus[index];
7436 if (cell == NULL || !PyCell_Check(cell)) { 7472 if (cell == NULL || !PyCell_Check(cell)) {
7437 PyErr_SetString(PyExc_RuntimeError, 7473 PyErr_SetString(PyExc_RuntimeError,
7438 "super(): bad __class__ cell"); 7474 "super(): bad __class__ cell");
7439 return -1; 7475 return -1;
7440 } 7476 }
7441 type = (PyTypeObject *) PyCell_GET(cell); 7477 type = (PyTypeObject *) PyCell_GET(cell);
7442 if (type == NULL) { 7478 if (type == NULL) {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
7538 0, /* tp_base */ 7574 0, /* tp_base */
7539 0, /* tp_dict */ 7575 0, /* tp_dict */
7540 super_descr_get, /* tp_descr_get */ 7576 super_descr_get, /* tp_descr_get */
7541 0, /* tp_descr_set */ 7577 0, /* tp_descr_set */
7542 0, /* tp_dictoffset */ 7578 0, /* tp_dictoffset */
7543 super_init, /* tp_init */ 7579 super_init, /* tp_init */
7544 PyType_GenericAlloc, /* tp_alloc */ 7580 PyType_GenericAlloc, /* tp_alloc */
7545 PyType_GenericNew, /* tp_new */ 7581 PyType_GenericNew, /* tp_new */
7546 PyObject_GC_Del, /* tp_free */ 7582 PyObject_GC_Del, /* tp_free */
7547 }; 7583 };
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+