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

Delta Between Two Patch Sets: Objects/typeobject.c

Issue 29259: Add tp_fastcall to PyTypeObject: support FASTCALL calling convention for all callable objects
Left Patch Set: Created 3 years ago
Right Patch Set: Created 2 years, 11 months 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 if ((*doc == *SIGNATURE_END_MARKER) && 114 if ((*doc == *SIGNATURE_END_MARKER) &&
115 !strncmp(doc, SIGNATURE_END_MARKER, SIGNATURE_END_MARKER_LENGTH)) 115 !strncmp(doc, SIGNATURE_END_MARKER, SIGNATURE_END_MARKER_LENGTH))
116 return doc + SIGNATURE_END_MARKER_LENGTH; 116 return doc + SIGNATURE_END_MARKER_LENGTH;
117 if ((*doc == '\n') && (doc[1] == '\n')) 117 if ((*doc == '\n') && (doc[1] == '\n'))
118 return NULL; 118 return NULL;
119 doc++; 119 doc++;
120 } 120 }
121 return NULL; 121 return NULL;
122 } 122 }
123 123
124 #ifdef Py_DEBUG
125 static int
126 _PyType_CheckConsistency(PyTypeObject *type)
127 {
128 if (!(type->tp_flags & Py_TPFLAGS_READY)) {
129 /* don't check types before PyType_Ready() */
130 return 1;
131 }
132
133 assert(!(type->tp_flags & Py_TPFLAGS_READYING));
134 assert(type->tp_mro != NULL && PyTuple_Check(type->tp_mro));
135 assert(type->tp_dict != NULL);
136 return 1;
137 }
138 #endif
139
124 static const char * 140 static const char *
125 _PyType_DocWithoutSignature(const char *name, const char *internal_doc) 141 _PyType_DocWithoutSignature(const char *name, const char *internal_doc)
126 { 142 {
127 const char *doc = find_signature(name, internal_doc); 143 const char *doc = find_signature(name, internal_doc);
128 144
129 if (doc) { 145 if (doc) {
130 doc = skip_signature(doc); 146 doc = skip_signature(doc);
131 if (doc) 147 if (doc)
132 return doc; 148 return doc;
133 } 149 }
134 return internal_doc; 150 return internal_doc;
135 } 151 }
136 152
137 PyObject * 153 PyObject *
138 _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc) 154 _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc)
139 { 155 {
140 const char *doc = _PyType_DocWithoutSignature(name, internal_doc); 156 const char *doc = _PyType_DocWithoutSignature(name, internal_doc);
141 157
142 if (!doc || *doc == '\0') { 158 if (!doc || *doc == '\0') {
143 Py_INCREF(Py_None); 159 Py_RETURN_NONE;
144 return Py_None;
145 } 160 }
146 161
147 return PyUnicode_FromString(doc); 162 return PyUnicode_FromString(doc);
148 } 163 }
149 164
150 PyObject * 165 PyObject *
151 _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_d oc) 166 _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_d oc)
152 { 167 {
153 const char *start = find_signature(name, internal_doc); 168 const char *start = find_signature(name, internal_doc);
154 const char *end; 169 const char *end;
155 170
156 if (start) 171 if (start)
157 end = skip_signature(start); 172 end = skip_signature(start);
158 else 173 else
159 end = NULL; 174 end = NULL;
160 if (!end) { 175 if (!end) {
161 Py_INCREF(Py_None); 176 Py_RETURN_NONE;
162 return Py_None;
163 } 177 }
164 178
165 /* back "end" up until it points just past the final ')' */ 179 /* back "end" up until it points just past the final ')' */
166 end -= SIGNATURE_END_MARKER_LENGTH - 1; 180 end -= SIGNATURE_END_MARKER_LENGTH - 1;
167 assert((end - start) >= 2); /* should be "()" at least */ 181 assert((end - start) >= 2); /* should be "()" at least */
168 assert(end[-1] == ')'); 182 assert(end[-1] == ')');
169 assert(end[0] == '\n'); 183 assert(end[0] == '\n');
170 return PyUnicode_FromStringAndSize(start, end - start); 184 return PyUnicode_FromStringAndSize(start, end - start);
171 } 185 }
172 186
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 /* for now, sod that: just remove from all old_bases, 728 /* for now, sod that: just remove from all old_bases,
715 add to all new_bases */ 729 add to all new_bases */
716 remove_all_subclasses(type, old_bases); 730 remove_all_subclasses(type, old_bases);
717 res = add_all_subclasses(type, new_bases); 731 res = add_all_subclasses(type, new_bases);
718 update_all_slots(type); 732 update_all_slots(type);
719 } 733 }
720 734
721 Py_DECREF(old_bases); 735 Py_DECREF(old_bases);
722 Py_DECREF(old_base); 736 Py_DECREF(old_base);
723 737
738 assert(_PyType_CheckConsistency(type));
724 return res; 739 return res;
725 740
726 undo: 741 undo:
727 for (i = PyList_GET_SIZE(temp) - 1; i >= 0; i--) { 742 for (i = PyList_GET_SIZE(temp) - 1; i >= 0; i--) {
728 PyTypeObject *cls; 743 PyTypeObject *cls;
729 PyObject *new_mro, *old_mro = NULL; 744 PyObject *new_mro, *old_mro = NULL;
730 745
731 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), 746 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
732 "", 2, 3, &cls, &new_mro, &old_mro); 747 "", 2, 3, &cls, &new_mro, &old_mro);
733 /* Do not rollback if cls has a newer version of MRO. */ 748 /* Do not rollback if cls has a newer version of MRO. */
(...skipping 13 matching lines...) Expand all
747 type->tp_base = old_base; 762 type->tp_base = old_base;
748 763
749 Py_DECREF(new_bases); 764 Py_DECREF(new_bases);
750 Py_DECREF(new_base); 765 Py_DECREF(new_base);
751 } 766 }
752 else { 767 else {
753 Py_DECREF(old_bases); 768 Py_DECREF(old_bases);
754 Py_DECREF(old_base); 769 Py_DECREF(old_base);
755 } 770 }
756 771
772 assert(_PyType_CheckConsistency(type));
757 return -1; 773 return -1;
758 } 774 }
759 775
760 static PyObject * 776 static PyObject *
761 type_dict(PyTypeObject *type, void *context) 777 type_dict(PyTypeObject *type, void *context)
762 { 778 {
763 if (type->tp_dict == NULL) { 779 if (type->tp_dict == NULL) {
764 Py_INCREF(Py_None); 780 Py_RETURN_NONE;
765 return Py_None;
766 } 781 }
767 return PyDictProxy_New(type->tp_dict); 782 return PyDictProxy_New(type->tp_dict);
768 } 783 }
769 784
770 static PyObject * 785 static PyObject *
771 type_get_doc(PyTypeObject *type, void *context) 786 type_get_doc(PyTypeObject *type, void *context)
772 { 787 {
773 PyObject *result; 788 PyObject *result;
774 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) { 789 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) {
775 return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); 790 return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 895
881 if (type->tp_new == NULL) { 896 if (type->tp_new == NULL) {
882 PyErr_Format(PyExc_TypeError, 897 PyErr_Format(PyExc_TypeError,
883 "cannot create '%.100s' instances", 898 "cannot create '%.100s' instances",
884 type->tp_name); 899 type->tp_name);
885 return NULL; 900 return NULL;
886 } 901 }
887 902
888 #ifdef Py_DEBUG 903 #ifdef Py_DEBUG
889 /* type_call() must not be called with an exception set, 904 /* type_call() must not be called with an exception set,
890 because it may clear it (directly or indirectly) and so the 905 because it can clear it (directly or indirectly) and so the
891 caller loses its exception */ 906 caller loses its exception */
892 assert(!PyErr_Occurred()); 907 assert(!PyErr_Occurred());
893 #endif 908 #endif
894 909
895 obj = type->tp_new(type, args, kwds); 910 obj = type->tp_new(type, args, kwds);
896 obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL); 911 obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL);
897 if (obj == NULL) 912 if (obj == NULL)
898 return NULL; 913 return NULL;
899 914
900 /* Ugly exception: when the call was type(something), 915 /* Ugly exception: when the call was type(something),
(...skipping 2129 matching lines...) Expand 10 before | Expand all | Expand 10 after
3030 /* Give up */ 3045 /* Give up */
3031 PyErr_Format(PyExc_AttributeError, 3046 PyErr_Format(PyExc_AttributeError,
3032 "type object '%.50s' has no attribute '%U'", 3047 "type object '%.50s' has no attribute '%U'",
3033 type->tp_name, name); 3048 type->tp_name, name);
3034 return NULL; 3049 return NULL;
3035 } 3050 }
3036 3051
3037 static int 3052 static int
3038 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) 3053 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
3039 { 3054 {
3055 int res;
3040 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { 3056 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3041 PyErr_Format( 3057 PyErr_Format(
3042 PyExc_TypeError, 3058 PyExc_TypeError,
3043 "can't set attributes of built-in/extension type '%s'", 3059 "can't set attributes of built-in/extension type '%s'",
3044 type->tp_name); 3060 type->tp_name);
3045 return -1; 3061 return -1;
3046 } 3062 }
3047 if (_PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL) < 0) 3063 if (_PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL) < 0)
3048 return -1; 3064 return -1;
3049 return update_slot(type, name); 3065 res = update_slot(type, name);
3066 assert(_PyType_CheckConsistency(type));
3067 return res;
3050 } 3068 }
3051 3069
3052 extern void 3070 extern void
3053 _PyDictKeys_DecRef(PyDictKeysObject *keys); 3071 _PyDictKeys_DecRef(PyDictKeysObject *keys);
3054 3072
3055 static void 3073 static void
3056 type_dealloc(PyTypeObject *type) 3074 type_dealloc(PyTypeObject *type)
3057 { 3075 {
3058 PyHeapTypeObject *et; 3076 PyHeapTypeObject *et;
3059 PyObject *tp, *val, *tb; 3077 PyObject *tp, *val, *tb;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
3105 if (PyList_Append(list, ref) < 0) { 3123 if (PyList_Append(list, ref) < 0) {
3106 Py_DECREF(list); 3124 Py_DECREF(list);
3107 return NULL; 3125 return NULL;
3108 } 3126 }
3109 } 3127 }
3110 } 3128 }
3111 return list; 3129 return list;
3112 } 3130 }
3113 3131
3114 static PyObject * 3132 static PyObject *
3115 type_prepare(PyObject *self, PyObject *args, PyObject *kwds) 3133 type_prepare(PyObject *self, PyObject **args, Py_ssize_t nargs,
3134 PyObject *kwnames)
3116 { 3135 {
3117 return PyDict_New(); 3136 return PyDict_New();
3118 } 3137 }
3119 3138
3120 /* 3139 /*
3121 Merge the __dict__ of aclass into dict, and recursively also all 3140 Merge the __dict__ of aclass into dict, and recursively also all
3122 the __dict__s of aclass's base classes. The order of merging isn't 3141 the __dict__s of aclass's base classes. The order of merging isn't
3123 defined, as it's expected that only the final set of dict keys is 3142 defined, as it's expected that only the final set of dict keys is
3124 interesting. 3143 interesting.
3125 Return 0 on success, -1 on error. 3144 Return 0 on success, -1 on error.
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
3209 size = sizeof(PyTypeObject); 3228 size = sizeof(PyTypeObject);
3210 return PyLong_FromSsize_t(size); 3229 return PyLong_FromSsize_t(size);
3211 } 3230 }
3212 3231
3213 static PyMethodDef type_methods[] = { 3232 static PyMethodDef type_methods[] = {
3214 {"mro", (PyCFunction)mro_external, METH_NOARGS, 3233 {"mro", (PyCFunction)mro_external, METH_NOARGS,
3215 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")}, 3234 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
3216 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, 3235 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
3217 PyDoc_STR("__subclasses__() -> list of immediate subclasses")}, 3236 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
3218 {"__prepare__", (PyCFunction)type_prepare, 3237 {"__prepare__", (PyCFunction)type_prepare,
3219 METH_VARARGS | METH_KEYWORDS | METH_CLASS, 3238 METH_FASTCALL | METH_CLASS,
3220 PyDoc_STR("__prepare__() -> dict\n" 3239 PyDoc_STR("__prepare__() -> dict\n"
3221 "used to create the namespace for the class statement")}, 3240 "used to create the namespace for the class statement")},
3222 {"__instancecheck__", type___instancecheck__, METH_O, 3241 {"__instancecheck__", type___instancecheck__, METH_O,
3223 PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance") }, 3242 PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance") },
3224 {"__subclasscheck__", type___subclasscheck__, METH_O, 3243 {"__subclasscheck__", type___subclasscheck__, METH_O,
3225 PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")}, 3244 PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")},
3226 {"__dir__", type_dir, METH_NOARGS, 3245 {"__dir__", type_dir, METH_NOARGS,
3227 PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types" )}, 3246 PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types" )},
3228 {"__sizeof__", type_sizeof, METH_NOARGS, 3247 {"__sizeof__", type_sizeof, METH_NOARGS,
3229 "__sizeof__() -> int\nreturn memory consumption of the type object"}, 3248 "__sizeof__() -> int\nreturn memory consumption of the type object"},
(...skipping 1434 matching lines...) Expand 10 before | Expand all | Expand 10 after
4664 _Py_IDENTIFIER(__eq__); 4683 _Py_IDENTIFIER(__eq__);
4665 4684
4666 assert(dict != NULL); 4685 assert(dict != NULL);
4667 if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) 4686 if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL)
4668 return 1; 4687 return 1;
4669 if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) 4688 if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL)
4670 return 1; 4689 return 1;
4671 return 0; 4690 return 0;
4672 } 4691 }
4673 4692
4674 /* tp_call slot which calls tp_fastcall */ 4693 /* tp_call slot calling tp_fastcall */
4675 static PyObject * 4694 static PyObject *
4676 fastcall_wrapper(PyObject *callable, PyObject *args_tuple, PyObject *kwargs) 4695 fastcall_wrapper(PyObject *callable, PyObject *args_tuple, PyObject *kwargs)
4677 { 4696 {
4678 return _Py_FastCall_FromArgs(callable, 4697 PyTypeObject *type;
4679 Py_TYPE(callable)->tp_fastcall, 4698 PyObject *result;
4699
4700 type = Py_TYPE(callable);
4701 while (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)
4702 || type->tp_fastcall == NULL) {
4703 PyTypeObject *base = type->tp_base;
4704 if (base == type || base == NULL) {
4705 PyErr_SetString(PyExc_SystemError,
4706 "unable to find tp_fastcall in a base classes");
4707 return NULL;
4708 }
4709 type = base;
4710 }
4711
4712 result = _Py_RawFastCallDict(callable,
4713 type->tp_fastcall,
4680 &PyTuple_GET_ITEM(args_tuple, 0), 4714 &PyTuple_GET_ITEM(args_tuple, 0),
4681 PyTuple_GET_SIZE(args_tuple), 4715 PyTuple_GET_SIZE(args_tuple),
4682 kwargs); 4716 kwargs);
4717
4718 result = _Py_CheckFunctionResult(callable, result, NULL);
4719 return result;
4683 } 4720 }
4684 4721
4685 static void 4722 static void
4686 inherit_slots(PyTypeObject *type, PyTypeObject *base) 4723 inherit_slots(PyTypeObject *type, PyTypeObject *base)
4687 { 4724 {
4688 PyTypeObject *basebase; 4725 PyTypeObject *basebase;
4689 4726
4690 #undef SLOTDEFINED 4727 #undef SLOTDEFINED
4691 #undef COPYSLOT 4728 #undef COPYSLOT
4692 #undef COPYNUM 4729 #undef COPYNUM
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
4799 type->tp_getattro = base->tp_getattro; 4836 type->tp_getattro = base->tp_getattro;
4800 } 4837 }
4801 if (type->tp_setattr == NULL && type->tp_setattro == NULL) { 4838 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
4802 type->tp_setattr = base->tp_setattr; 4839 type->tp_setattr = base->tp_setattr;
4803 type->tp_setattro = base->tp_setattro; 4840 type->tp_setattro = base->tp_setattro;
4804 } 4841 }
4805 /* tp_reserved is ignored */ 4842 /* tp_reserved is ignored */
4806 COPYSLOT(tp_repr); 4843 COPYSLOT(tp_repr);
4807 /* tp_hash see tp_richcompare */ 4844 /* tp_hash see tp_richcompare */
4808 4845
4809 if (type->tp_call != fastcall_wrapper) { 4846 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)
4810 /* don't inherit tp_fastcall if tp_call is defined but 4847 && PyType_HasFeature(base, Py_TPFLAGS_HAVE_FASTCALL)
4811 not tp_fastcall */ 4848 /* don't inherit tp_fastcall if tp_call is defined */
4812 if (!type->tp_call && !type->tp_fastcall) { 4849 && (type->tp_call == NULL || type->tp_call != fastcall_wrapper)) {
4813 COPYSLOT(tp_fastcall); 4850 COPYSLOT(tp_fastcall);
4814 } 4851 }
4815 4852
4853 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)
4854 || type->tp_call != fastcall_wrapper) {
4816 COPYSLOT(tp_call); 4855 COPYSLOT(tp_call);
4817 } 4856 }
4818 4857
4819 COPYSLOT(tp_str); 4858 COPYSLOT(tp_str);
4820 { 4859 {
4821 /* Copy comparison-related slots only when 4860 /* Copy comparison-related slots only when
4822 not overriding them anywhere */ 4861 not overriding them anywhere */
4823 if (type->tp_richcompare == NULL && 4862 if (type->tp_richcompare == NULL &&
4824 type->tp_hash == NULL && 4863 type->tp_hash == NULL &&
4825 !overrides_hash(type)) 4864 !overrides_hash(type))
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
4867 static int add_operators(PyTypeObject *); 4906 static int add_operators(PyTypeObject *);
4868 4907
4869 int 4908 int
4870 PyType_Ready(PyTypeObject *type) 4909 PyType_Ready(PyTypeObject *type)
4871 { 4910 {
4872 PyObject *dict, *bases; 4911 PyObject *dict, *bases;
4873 PyTypeObject *base; 4912 PyTypeObject *base;
4874 Py_ssize_t i, n; 4913 Py_ssize_t i, n;
4875 4914
4876 if (type->tp_flags & Py_TPFLAGS_READY) { 4915 if (type->tp_flags & Py_TPFLAGS_READY) {
4877 assert(type->tp_dict != NULL); 4916 assert(_PyType_CheckConsistency(type));
4878 return 0; 4917 return 0;
4879 } 4918 }
4880 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); 4919 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
4881 4920
4882 type->tp_flags |= Py_TPFLAGS_READYING; 4921 type->tp_flags |= Py_TPFLAGS_READYING;
4883 4922
4884 #ifdef Py_TRACE_REFS 4923 #ifdef Py_TRACE_REFS
4885 /* PyType_Ready is the closest thing we have to a choke point 4924 /* PyType_Ready is the closest thing we have to a choke point
4886 * for type objects, so is the best place I can think of to try 4925 * for type objects, so is the best place I can think of to try
4887 * to get type objects into the doubly-linked list of all objects. 4926 * to get type objects into the doubly-linked list of all objects.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
4937 4976
4938 /* Initialize tp_dict */ 4977 /* Initialize tp_dict */
4939 dict = type->tp_dict; 4978 dict = type->tp_dict;
4940 if (dict == NULL) { 4979 if (dict == NULL) {
4941 dict = PyDict_New(); 4980 dict = PyDict_New();
4942 if (dict == NULL) 4981 if (dict == NULL)
4943 goto error; 4982 goto error;
4944 type->tp_dict = dict; 4983 type->tp_dict = dict;
4945 } 4984 }
4946 4985
4947 if (type->tp_fastcall && !type->tp_call) { 4986 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)
4987 && type->tp_fastcall && !type->tp_call) {
4948 /* tp_fastcall defined, but not tp_call: tp_call will use a wrapper 4988 /* tp_fastcall defined, but not tp_call: tp_call will use a wrapper
4949 calling tp_fastcall. We need to define tp_call before calling 4989 calling tp_fastcall. We need to define tp_call before calling
4950 add_operators(), otherwise the __call__ descriptor is not 4990 add_operators(), otherwise the __call__ descriptor is not
4951 defined. */ 4991 defined. */
4952 type->tp_call = fastcall_wrapper; 4992 type->tp_call = fastcall_wrapper;
4953 } 4993 }
4954 4994
4955 /* Add type-specific descriptors to tp_dict */ 4995 /* Add type-specific descriptors to tp_dict */
4956 if (add_operators(type) < 0) 4996 if (add_operators(type) < 0)
4957 goto error; 4997 goto error;
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
5069 bases = type->tp_bases; 5109 bases = type->tp_bases;
5070 n = PyTuple_GET_SIZE(bases); 5110 n = PyTuple_GET_SIZE(bases);
5071 for (i = 0; i < n; i++) { 5111 for (i = 0; i < n; i++) {
5072 PyObject *b = PyTuple_GET_ITEM(bases, i); 5112 PyObject *b = PyTuple_GET_ITEM(bases, i);
5073 if (PyType_Check(b) && 5113 if (PyType_Check(b) &&
5074 add_subclass((PyTypeObject *)b, type) < 0) 5114 add_subclass((PyTypeObject *)b, type) < 0)
5075 goto error; 5115 goto error;
5076 } 5116 }
5077 5117
5078 /* All done -- set the ready flag */ 5118 /* All done -- set the ready flag */
5079 assert(type->tp_dict != NULL);
5080 type->tp_flags = 5119 type->tp_flags =
5081 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; 5120 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
5121 assert(_PyType_CheckConsistency(type));
5082 return 0; 5122 return 0;
5083 5123
5084 error: 5124 error:
5085 type->tp_flags &= ~Py_TPFLAGS_READYING; 5125 type->tp_flags &= ~Py_TPFLAGS_READYING;
5086 return -1; 5126 return -1;
5087 } 5127 }
5088 5128
5089 static int 5129 static int
5090 add_subclass(PyTypeObject *base, PyTypeObject *type) 5130 add_subclass(PyTypeObject *base, PyTypeObject *type)
5091 { 5131 {
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
5351 PyObject *arg, *value; 5391 PyObject *arg, *value;
5352 5392
5353 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) 5393 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
5354 return NULL; 5394 return NULL;
5355 i = getindex(self, arg); 5395 i = getindex(self, arg);
5356 if (i == -1 && PyErr_Occurred()) 5396 if (i == -1 && PyErr_Occurred())
5357 return NULL; 5397 return NULL;
5358 res = (*func)(self, i, value); 5398 res = (*func)(self, i, value);
5359 if (res == -1 && PyErr_Occurred()) 5399 if (res == -1 && PyErr_Occurred())
5360 return NULL; 5400 return NULL;
5361 Py_INCREF(Py_None); 5401 Py_RETURN_NONE;
5362 return Py_None;
5363 } 5402 }
5364 5403
5365 static PyObject * 5404 static PyObject *
5366 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) 5405 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
5367 { 5406 {
5368 ssizeobjargproc func = (ssizeobjargproc)wrapped; 5407 ssizeobjargproc func = (ssizeobjargproc)wrapped;
5369 Py_ssize_t i; 5408 Py_ssize_t i;
5370 int res; 5409 int res;
5371 PyObject *arg; 5410 PyObject *arg;
5372 5411
5373 if (!check_num_args(args, 1)) 5412 if (!check_num_args(args, 1))
5374 return NULL; 5413 return NULL;
5375 arg = PyTuple_GET_ITEM(args, 0); 5414 arg = PyTuple_GET_ITEM(args, 0);
5376 i = getindex(self, arg); 5415 i = getindex(self, arg);
5377 if (i == -1 && PyErr_Occurred()) 5416 if (i == -1 && PyErr_Occurred())
5378 return NULL; 5417 return NULL;
5379 res = (*func)(self, i, NULL); 5418 res = (*func)(self, i, NULL);
5380 if (res == -1 && PyErr_Occurred()) 5419 if (res == -1 && PyErr_Occurred())
5381 return NULL; 5420 return NULL;
5382 Py_INCREF(Py_None); 5421 Py_RETURN_NONE;
5383 return Py_None;
5384 } 5422 }
5385 5423
5386 /* XXX objobjproc is a misnomer; should be objargpred */ 5424 /* XXX objobjproc is a misnomer; should be objargpred */
5387 static PyObject * 5425 static PyObject *
5388 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) 5426 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
5389 { 5427 {
5390 objobjproc func = (objobjproc)wrapped; 5428 objobjproc func = (objobjproc)wrapped;
5391 int res; 5429 int res;
5392 PyObject *value; 5430 PyObject *value;
5393 5431
(...skipping 12 matching lines...) Expand all
5406 { 5444 {
5407 objobjargproc func = (objobjargproc)wrapped; 5445 objobjargproc func = (objobjargproc)wrapped;
5408 int res; 5446 int res;
5409 PyObject *key, *value; 5447 PyObject *key, *value;
5410 5448
5411 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) 5449 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
5412 return NULL; 5450 return NULL;
5413 res = (*func)(self, key, value); 5451 res = (*func)(self, key, value);
5414 if (res == -1 && PyErr_Occurred()) 5452 if (res == -1 && PyErr_Occurred())
5415 return NULL; 5453 return NULL;
5416 Py_INCREF(Py_None); 5454 Py_RETURN_NONE;
5417 return Py_None;
5418 } 5455 }
5419 5456
5420 static PyObject * 5457 static PyObject *
5421 wrap_delitem(PyObject *self, PyObject *args, void *wrapped) 5458 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
5422 { 5459 {
5423 objobjargproc func = (objobjargproc)wrapped; 5460 objobjargproc func = (objobjargproc)wrapped;
5424 int res; 5461 int res;
5425 PyObject *key; 5462 PyObject *key;
5426 5463
5427 if (!check_num_args(args, 1)) 5464 if (!check_num_args(args, 1))
5428 return NULL; 5465 return NULL;
5429 key = PyTuple_GET_ITEM(args, 0); 5466 key = PyTuple_GET_ITEM(args, 0);
5430 res = (*func)(self, key, NULL); 5467 res = (*func)(self, key, NULL);
5431 if (res == -1 && PyErr_Occurred()) 5468 if (res == -1 && PyErr_Occurred())
5432 return NULL; 5469 return NULL;
5433 Py_INCREF(Py_None); 5470 Py_RETURN_NONE;
5434 return Py_None;
5435 } 5471 }
5436 5472
5437 /* Helper to check for object.__setattr__ or __delattr__ applied to a type. 5473 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
5438 This is called the Carlo Verre hack after its discoverer. */ 5474 This is called the Carlo Verre hack after its discoverer. */
5439 static int 5475 static int
5440 hackcheck(PyObject *self, setattrofunc func, const char *what) 5476 hackcheck(PyObject *self, setattrofunc func, const char *what)
5441 { 5477 {
5442 PyTypeObject *type = Py_TYPE(self); 5478 PyTypeObject *type = Py_TYPE(self);
5443 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) 5479 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
5444 type = type->tp_base; 5480 type = type->tp_base;
(...skipping 16 matching lines...) Expand all
5461 int res; 5497 int res;
5462 PyObject *name, *value; 5498 PyObject *name, *value;
5463 5499
5464 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) 5500 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
5465 return NULL; 5501 return NULL;
5466 if (!hackcheck(self, func, "__setattr__")) 5502 if (!hackcheck(self, func, "__setattr__"))
5467 return NULL; 5503 return NULL;
5468 res = (*func)(self, name, value); 5504 res = (*func)(self, name, value);
5469 if (res < 0) 5505 if (res < 0)
5470 return NULL; 5506 return NULL;
5471 Py_INCREF(Py_None); 5507 Py_RETURN_NONE;
5472 return Py_None;
5473 } 5508 }
5474 5509
5475 static PyObject * 5510 static PyObject *
5476 wrap_delattr(PyObject *self, PyObject *args, void *wrapped) 5511 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
5477 { 5512 {
5478 setattrofunc func = (setattrofunc)wrapped; 5513 setattrofunc func = (setattrofunc)wrapped;
5479 int res; 5514 int res;
5480 PyObject *name; 5515 PyObject *name;
5481 5516
5482 if (!check_num_args(args, 1)) 5517 if (!check_num_args(args, 1))
5483 return NULL; 5518 return NULL;
5484 name = PyTuple_GET_ITEM(args, 0); 5519 name = PyTuple_GET_ITEM(args, 0);
5485 if (!hackcheck(self, func, "__delattr__")) 5520 if (!hackcheck(self, func, "__delattr__"))
5486 return NULL; 5521 return NULL;
5487 res = (*func)(self, name, NULL); 5522 res = (*func)(self, name, NULL);
5488 if (res < 0) 5523 if (res < 0)
5489 return NULL; 5524 return NULL;
5490 Py_INCREF(Py_None); 5525 Py_RETURN_NONE;
5491 return Py_None;
5492 } 5526 }
5493 5527
5494 static PyObject * 5528 static PyObject *
5495 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) 5529 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
5496 { 5530 {
5497 hashfunc func = (hashfunc)wrapped; 5531 hashfunc func = (hashfunc)wrapped;
5498 Py_hash_t res; 5532 Py_hash_t res;
5499 5533
5500 if (!check_num_args(args, 0)) 5534 if (!check_num_args(args, 0))
5501 return NULL; 5535 return NULL;
5502 res = (*func)(self); 5536 res = (*func)(self);
5503 if (res == -1 && PyErr_Occurred()) 5537 if (res == -1 && PyErr_Occurred())
5504 return NULL; 5538 return NULL;
5505 return PyLong_FromSsize_t(res); 5539 return PyLong_FromSsize_t(res);
5506 } 5540 }
5507 5541
5508 static PyObject * 5542 static PyObject *
5509 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) 5543 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
5510 { 5544 {
5511 ternaryfunc func = (ternaryfunc)wrapped; 5545 ternaryfunc func = (ternaryfunc)wrapped;
5512 5546
5513 return (*func)(self, args, kwds); 5547 return (*func)(self, args, kwds);
5548 }
5549
5550 static PyObject *
5551 wrap_fastcall(PyObject *self, void *wrapped,
5552 PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
5553 {
5554 fastternaryfunc func = (fastternaryfunc)wrapped;
5555
5556 return (*func)(self, args, nargs, kwnames);
5514 } 5557 }
5515 5558
5516 static PyObject * 5559 static PyObject *
5517 wrap_del(PyObject *self, PyObject *args, void *wrapped) 5560 wrap_del(PyObject *self, PyObject *args, void *wrapped)
5518 { 5561 {
5519 destructor func = (destructor)wrapped; 5562 destructor func = (destructor)wrapped;
5520 5563
5521 if (!check_num_args(args, 0)) 5564 if (!check_num_args(args, 0))
5522 return NULL; 5565 return NULL;
5523 5566
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
5592 { 5635 {
5593 descrsetfunc func = (descrsetfunc)wrapped; 5636 descrsetfunc func = (descrsetfunc)wrapped;
5594 PyObject *obj, *value; 5637 PyObject *obj, *value;
5595 int ret; 5638 int ret;
5596 5639
5597 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) 5640 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
5598 return NULL; 5641 return NULL;
5599 ret = (*func)(self, obj, value); 5642 ret = (*func)(self, obj, value);
5600 if (ret < 0) 5643 if (ret < 0)
5601 return NULL; 5644 return NULL;
5602 Py_INCREF(Py_None); 5645 Py_RETURN_NONE;
5603 return Py_None;
5604 } 5646 }
5605 5647
5606 static PyObject * 5648 static PyObject *
5607 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) 5649 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
5608 { 5650 {
5609 descrsetfunc func = (descrsetfunc)wrapped; 5651 descrsetfunc func = (descrsetfunc)wrapped;
5610 PyObject *obj; 5652 PyObject *obj;
5611 int ret; 5653 int ret;
5612 5654
5613 if (!check_num_args(args, 1)) 5655 if (!check_num_args(args, 1))
5614 return NULL; 5656 return NULL;
5615 obj = PyTuple_GET_ITEM(args, 0); 5657 obj = PyTuple_GET_ITEM(args, 0);
5616 ret = (*func)(self, obj, NULL); 5658 ret = (*func)(self, obj, NULL);
5617 if (ret < 0) 5659 if (ret < 0)
5618 return NULL; 5660 return NULL;
5619 Py_INCREF(Py_None); 5661 Py_RETURN_NONE;
5620 return Py_None;
5621 } 5662 }
5622 5663
5623 static PyObject * 5664 static PyObject *
5624 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) 5665 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
5625 { 5666 {
5626 initproc func = (initproc)wrapped; 5667 initproc func = (initproc)wrapped;
5627 5668
5628 if (func(self, args, kwds) < 0) 5669 if (func(self, args, kwds) < 0)
5629 return NULL; 5670 return NULL;
5630 Py_INCREF(Py_None); 5671 Py_RETURN_NONE;
5631 return Py_None;
5632 } 5672 }
5633 5673
5634 static PyObject * 5674 static PyObject *
5635 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) 5675 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
5636 { 5676 {
5637 PyTypeObject *type, *subtype, *staticbase; 5677 PyTypeObject *type, *subtype, *staticbase;
5638 PyObject *arg0, *res; 5678 PyObject *arg0, *res;
5639 5679
5640 if (self == NULL || !PyType_Check(self)) 5680 if (self == NULL || !PyType_Check(self))
5641 Py_FatalError("__new__() called with non-type 'self'"); 5681 Py_FatalError("__new__() called with non-type 'self'");
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
6172 6212
6173 if (meth == NULL) 6213 if (meth == NULL)
6174 return NULL; 6214 return NULL;
6175 6215
6176 res = PyObject_Call(meth, args, kwds); 6216 res = PyObject_Call(meth, args, kwds);
6177 6217
6178 Py_DECREF(meth); 6218 Py_DECREF(meth);
6179 return res; 6219 return res;
6180 } 6220 }
6181 6221
6222 static PyObject *
6223 slot_tp_fastcall(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kw names)
6224 {
6225 _Py_IDENTIFIER(__call__);
6226 PyObject *meth = lookup_method(self, &PyId___call__);
inada.naoki 2017/01/26 10:10:38 slot_tp_call has _Py_IDENTIFIER(__call__) too. Let
6227 PyObject *res;
6228
6229 if (meth == NULL)
6230 return NULL;
6231
6232 res = _PyObject_FastCallKeywords(meth, args, nargs, kwnames);
6233
6234 Py_DECREF(meth);
6235 return res;
6236 }
6237
6182 /* There are two slot dispatch functions for tp_getattro. 6238 /* There are two slot dispatch functions for tp_getattro.
6183 6239
6184 - slot_tp_getattro() is used when __getattribute__ is overridden 6240 - slot_tp_getattro() is used when __getattribute__ is overridden
6185 but no __getattr__ hook is present; 6241 but no __getattr__ hook is present;
6186 6242
6187 - slot_tp_getattr_hook() is used when a __getattr__ hook is present. 6243 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
6188 6244
6189 The code in update_one_slot() always installs slot_tp_getattr_hook(); this 6245 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
6190 detects the absence of __getattr__ and then installs the simpler slot if 6246 detects the absence of __getattr__ and then installs the simpler slot if
6191 necessary. */ 6247 necessary. */
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
6525 #undef ETSLOT 6581 #undef ETSLOT
6526 #undef SQSLOT 6582 #undef SQSLOT
6527 #undef MPSLOT 6583 #undef MPSLOT
6528 #undef NBSLOT 6584 #undef NBSLOT
6529 #undef UNSLOT 6585 #undef UNSLOT
6530 #undef IBSLOT 6586 #undef IBSLOT
6531 #undef BINSLOT 6587 #undef BINSLOT
6532 #undef RBINSLOT 6588 #undef RBINSLOT
6533 6589
6534 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6590 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6535 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 6591 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, NULL, \
6536 PyDoc_STR(DOC)} 6592 PyDoc_STR(DOC)}
6537 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ 6593 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
6538 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 6594 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, NULL, \
6595 PyDoc_STR(DOC), FLAGS}
6596 #define FLSLOT_FAST(NAME, SLOT, FUNCTION, WRAPPER, FAST_WRAPPER, DOC, FLAGS) \
6597 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, FAST_WRAPP ER, \
6539 PyDoc_STR(DOC), FLAGS} 6598 PyDoc_STR(DOC), FLAGS}
6540 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6599 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6541 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 6600 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, NULL, \
6542 PyDoc_STR(DOC)} 6601 PyDoc_STR(DOC)}
6543 #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6602 #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6544 ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC) 6603 ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC)
6545 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6604 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6546 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) 6605 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
6547 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6606 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6548 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) 6607 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
6549 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6608 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6550 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) 6609 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
6551 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6610 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
(...skipping 17 matching lines...) Expand all
6569 6628
6570 static slotdef slotdefs[] = { 6629 static slotdef slotdefs[] = {
6571 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), 6630 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
6572 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), 6631 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
6573 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), 6632 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
6574 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), 6633 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
6575 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, 6634 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
6576 "__repr__($self, /)\n--\n\nReturn repr(self)."), 6635 "__repr__($self, /)\n--\n\nReturn repr(self)."),
6577 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, 6636 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
6578 "__hash__($self, /)\n--\n\nReturn hash(self)."), 6637 "__hash__($self, /)\n--\n\nReturn hash(self)."),
6579 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, 6638 FLSLOT_FAST("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, wrap_ fastcall,
6580 "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function." , 6639 "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function." ,
6581 PyWrapperFlag_KEYWORDS), 6640 PyWrapperFlag_KEYWORDS),
6582 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, 6641 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
6583 "__str__($self, /)\n--\n\nReturn str(self)."), 6642 "__str__($self, /)\n--\n\nReturn str(self)."),
6584 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, 6643 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
6585 wrap_binaryfunc, 6644 wrap_binaryfunc,
6586 "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)." ), 6645 "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)." ),
6587 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), 6646 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
6588 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, 6647 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
6589 "__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, na me, value)."), 6648 "__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, na me, value)."),
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
6843 the slot.) Return a pointer to the next slotdef with a different offset, 6902 the slot.) Return a pointer to the next slotdef with a different offset,
6844 because that's convenient for fixup_slot_dispatchers(). */ 6903 because that's convenient for fixup_slot_dispatchers(). */
6845 static slotdef * 6904 static slotdef *
6846 update_one_slot(PyTypeObject *type, slotdef *p) 6905 update_one_slot(PyTypeObject *type, slotdef *p)
6847 { 6906 {
6848 PyObject *descr; 6907 PyObject *descr;
6849 PyWrapperDescrObject *d; 6908 PyWrapperDescrObject *d;
6850 void *generic = NULL, *specific = NULL; 6909 void *generic = NULL, *specific = NULL;
6851 int use_generic = 0; 6910 int use_generic = 0;
6852 int offset = p->offset; 6911 int offset = p->offset;
6853 void **ptr = slotptr(type, offset); 6912 void **ptr = slotptr(type, offset), **ptr2 = NULL;
6854 6913
6855 if (ptr == NULL) { 6914 if (ptr == NULL) {
6856 do { 6915 do {
6857 ++p; 6916 ++p;
6858 } while (p->offset == offset); 6917 } while (p->offset == offset);
6859 return p; 6918 return p;
6860 } 6919 }
6920
6861 do { 6921 do {
6862 descr = _PyType_Lookup(type, p->name_strobj); 6922 descr = _PyType_Lookup(type, p->name_strobj);
6863 if (descr == NULL) { 6923 if (descr == NULL) {
6864 if (ptr == (void**)&type->tp_iternext) { 6924 if (ptr == (void**)&type->tp_iternext) {
6925 ptr2 = NULL;
6865 specific = (void *)_PyObject_NextNotImplemented; 6926 specific = (void *)_PyObject_NextNotImplemented;
6866 } 6927 }
6867 continue; 6928 continue;
6868 } 6929 }
6869 if (Py_TYPE(descr) == &PyWrapperDescr_Type && 6930 if (Py_TYPE(descr) == &PyWrapperDescr_Type &&
6870 ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_stro bj) { 6931 ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_stro bj) {
6871 void **tptr = resolve_slotdups(type, p->name_strobj); 6932 void **tptr = resolve_slotdups(type, p->name_strobj);
6933 int same_wrapper;
6934
6872 if (tptr == NULL || tptr == ptr) 6935 if (tptr == NULL || tptr == ptr)
6873 generic = p->function; 6936 generic = p->function;
6874 d = (PyWrapperDescrObject *)descr; 6937 d = (PyWrapperDescrObject *)descr;
6875 if (d->d_base->wrapper == p->wrapper && 6938
6939 if (d->d_use_fastwrapper) {
6940 same_wrapper = (d->d_base->fastwrapper == p->fastwrapper);
6941
6942 assert(p->wrapper == (void *)wrap_call);
6943 ptr2 = (void **)&type->tp_fastcall;
6944 }
6945 else {
6946 same_wrapper = (d->d_base->wrapper == p->wrapper);
6947 }
6948
6949 if (same_wrapper &&
6876 PyType_IsSubtype(type, PyDescr_TYPE(d))) 6950 PyType_IsSubtype(type, PyDescr_TYPE(d)))
6877 { 6951 {
6878 if (specific == NULL || 6952 if (specific == NULL ||
6879 specific == d->d_wrapped) 6953 specific == d->d_wrapped)
6880 specific = d->d_wrapped; 6954 specific = d->d_wrapped;
6881 else 6955 else
6882 use_generic = 1; 6956 use_generic = 1;
6883 } 6957 }
6884 } 6958 }
6885 else if (Py_TYPE(descr) == &PyCFunction_Type && 6959 else if (Py_TYPE(descr) == &PyCFunction_Type &&
6886 PyCFunction_GET_FUNCTION(descr) == 6960 PyCFunction_GET_FUNCTION(descr) ==
6887 (PyCFunction)tp_new_wrapper && 6961 (PyCFunction)tp_new_wrapper &&
6888 ptr == (void**)&type->tp_new) 6962 ptr == (void**)&type->tp_new)
6889 { 6963 {
6890 /* The __new__ wrapper is not a wrapper descriptor, 6964 /* The __new__ wrapper is not a wrapper descriptor,
6891 so must be special-cased differently. 6965 so must be special-cased differently.
6892 If we don't do this, creating an instance will 6966 If we don't do this, creating an instance will
6893 always use slot_tp_new which will look up 6967 always use slot_tp_new which will look up
6894 __new__ in the MRO which will call tp_new_wrapper 6968 __new__ in the MRO which will call tp_new_wrapper
6895 which will look through the base classes looking 6969 which will look through the base classes looking
6896 for a static base and call its tp_new (usually 6970 for a static base and call its tp_new (usually
6897 PyType_GenericNew), after performing various 6971 PyType_GenericNew), after performing various
6898 sanity checks and constructing a new argument 6972 sanity checks and constructing a new argument
6899 list. Cut all that nonsense short -- this speeds 6973 list. Cut all that nonsense short -- this speeds
6900 up instance creation tremendously. */ 6974 up instance creation tremendously. */
6975
6901 specific = (void *)type->tp_new; 6976 specific = (void *)type->tp_new;
6977 ptr2 = NULL;
6978
6902 /* XXX I'm not 100% sure that there isn't a hole 6979 /* XXX I'm not 100% sure that there isn't a hole
6903 in this reasoning that requires additional 6980 in this reasoning that requires additional
6904 sanity checks. I'll buy the first person to 6981 sanity checks. I'll buy the first person to
6905 point out a bug in this reasoning a beer. */ 6982 point out a bug in this reasoning a beer. */
6906 } 6983 }
6907 else if (descr == Py_None && 6984 else if (descr == Py_None &&
6908 ptr == (void**)&type->tp_hash) { 6985 ptr == (void**)&type->tp_hash) {
6909 /* We specifically allow __hash__ to be set to None 6986 /* We specifically allow __hash__ to be set to None
6910 to prevent inheritance of the default 6987 to prevent inheritance of the default
6911 implementation from object.__hash__ */ 6988 implementation from object.__hash__ */
6912 specific = (void *)PyObject_HashNotImplemented; 6989 specific = (void *)PyObject_HashNotImplemented;
6990 ptr2 = NULL;
6913 } 6991 }
6914 else { 6992 else {
6915 use_generic = 1; 6993 use_generic = 1;
6916 generic = p->function; 6994 generic = p->function;
6917 } 6995 }
6918 } while ((++p)->offset == offset); 6996 } while ((++p)->offset == offset);
6919 if (specific && !use_generic) 6997
6998 if (specific && !use_generic) {
6999 if (ptr2 != NULL) {
7000 ptr = ptr2;
7001 }
6920 *ptr = specific; 7002 *ptr = specific;
6921 else 7003 }
7004 else {
6922 *ptr = generic; 7005 *ptr = generic;
7006 }
7007
7008 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)) {
7009 /* synchronize tp_call and tp_fastcall slots */
7010 if (ptr == (void **)&type->tp_call) {
7011 if (type->tp_call == slot_tp_call) {
7012 type->tp_fastcall = slot_tp_fastcall;
7013 }
7014 }
7015 else if (ptr == (void **)&type->tp_fastcall) {
7016 type->tp_call = fastcall_wrapper;
7017 }
7018 }
7019
6923 return p; 7020 return p;
6924 } 7021 }
6925 7022
6926 /* In the type, update the slots whose slotdefs are gathered in the pp array. 7023 /* In the type, update the slots whose slotdefs are gathered in the pp array.
6927 This is a callback for update_subclasses(). */ 7024 This is a callback for update_subclasses(). */
6928 static int 7025 static int
6929 update_slots_callback(PyTypeObject *type, void *data) 7026 update_slots_callback(PyTypeObject *type, void *data)
6930 { 7027 {
6931 slotdef **pp = (slotdef **)data; 7028 slotdef **pp = (slotdef **)data;
6932 7029
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
7004 7101
7005 /* Store the proper functions in the slot dispatches at class (type) 7102 /* Store the proper functions in the slot dispatches at class (type)
7006 definition time, based upon which operations the class overrides in its 7103 definition time, based upon which operations the class overrides in its
7007 dict. */ 7104 dict. */
7008 static void 7105 static void
7009 fixup_slot_dispatchers(PyTypeObject *type) 7106 fixup_slot_dispatchers(PyTypeObject *type)
7010 { 7107 {
7011 slotdef *p; 7108 slotdef *p;
7012 7109
7013 init_slotdefs(); 7110 init_slotdefs();
7014 for (p = slotdefs; p->name; ) 7111 for (p = slotdefs; p->name; ) {
7015 p = update_one_slot(type, p); 7112 p = update_one_slot(type, p);
7113 }
7016 } 7114 }
7017 7115
7018 static void 7116 static void
7019 update_all_slots(PyTypeObject* type) 7117 update_all_slots(PyTypeObject* type)
7020 { 7118 {
7021 slotdef *p; 7119 slotdef *p;
7022 7120
7023 init_slotdefs(); 7121 init_slotdefs();
7024 for (p = slotdefs; p->name; p++) { 7122 for (p = slotdefs; p->name; p++) {
7025 /* update_slot returns int but can't actually fail */ 7123 /* update_slot returns int but can't actually fail */
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
7185 if (PyDict_GetItem(dict, p->name_strobj)) 7283 if (PyDict_GetItem(dict, p->name_strobj))
7186 continue; 7284 continue;
7187 if (*ptr == (void *)PyObject_HashNotImplemented) { 7285 if (*ptr == (void *)PyObject_HashNotImplemented) {
7188 /* Classes may prevent the inheritance of the tp_hash 7286 /* Classes may prevent the inheritance of the tp_hash
7189 slot by storing PyObject_HashNotImplemented in it. Make it 7287 slot by storing PyObject_HashNotImplemented in it. Make it
7190 visible as a None value for the __hash__ attribute. */ 7288 visible as a None value for the __hash__ attribute. */
7191 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) 7289 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
7192 return -1; 7290 return -1;
7193 } 7291 }
7194 else { 7292 else {
7195 descr = PyDescr_NewWrapper(type, p, *ptr); 7293 int use_fastwrapper;
7294 void *wrapped;
7295
7296 if (*ptr == fastcall_wrapper) {
7297 /* A wrapper must not call fastcall_wrapper() but the specific
7298 tp_fastcall. fastcall_wrapper() is called with an instance.
7299 From an instance, it's not possible to find the specific
7300 function. */
haypo 2017/01/26 03:35:21 This comment should be rephrased.
7301 use_fastwrapper = 1;
7302 wrapped = type->tp_fastcall;
7303 }
7304 else {
7305 use_fastwrapper = 0;
7306 wrapped = *ptr;
7307 }
7308 assert(wrapped != NULL);
7309
7310 descr = _PyDescr_NewWrapperEx(type, p, wrapped, use_fastwrapper);
7196 if (descr == NULL) 7311 if (descr == NULL)
7197 return -1; 7312 return -1;
7198 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) { 7313 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) {
7199 Py_DECREF(descr); 7314 Py_DECREF(descr);
7200 return -1; 7315 return -1;
7201 } 7316 }
7202 Py_DECREF(descr); 7317 Py_DECREF(descr);
7203 } 7318 }
7204 } 7319 }
7205 if (type->tp_new != NULL) { 7320 if (type->tp_new != NULL) {
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
7594 0, /* tp_base */ 7709 0, /* tp_base */
7595 0, /* tp_dict */ 7710 0, /* tp_dict */
7596 super_descr_get, /* tp_descr_get */ 7711 super_descr_get, /* tp_descr_get */
7597 0, /* tp_descr_set */ 7712 0, /* tp_descr_set */
7598 0, /* tp_dictoffset */ 7713 0, /* tp_dictoffset */
7599 super_init, /* tp_init */ 7714 super_init, /* tp_init */
7600 PyType_GenericAlloc, /* tp_alloc */ 7715 PyType_GenericAlloc, /* tp_alloc */
7601 PyType_GenericNew, /* tp_new */ 7716 PyType_GenericNew, /* tp_new */
7602 PyObject_GC_Del, /* tp_free */ 7717 PyObject_GC_Del, /* tp_free */
7603 }; 7718 };
LEFTRIGHT

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