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

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, 12 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 1559 matching lines...) Expand 10 before | Expand all | Expand 10 after
2460 2475
2461 /* Keep name and slots alive in the extended type object */ 2476 /* Keep name and slots alive in the extended type object */
2462 et = (PyHeapTypeObject *)type; 2477 et = (PyHeapTypeObject *)type;
2463 Py_INCREF(name); 2478 Py_INCREF(name);
2464 et->ht_name = name; 2479 et->ht_name = name;
2465 et->ht_slots = slots; 2480 et->ht_slots = slots;
2466 slots = NULL; 2481 slots = NULL;
2467 2482
2468 /* Initialize tp_flags */ 2483 /* Initialize tp_flags */
2469 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | 2484 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2470 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE | 2485 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE;
2471 Py_TPFLAGS_HAVE_FASTCALL;
2472 if (base->tp_flags & Py_TPFLAGS_HAVE_GC) 2486 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2473 type->tp_flags |= Py_TPFLAGS_HAVE_GC; 2487 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2474 2488
2475 /* Initialize essential fields */ 2489 /* Initialize essential fields */
2476 type->tp_as_async = &et->as_async; 2490 type->tp_as_async = &et->as_async;
2477 type->tp_as_number = &et->as_number; 2491 type->tp_as_number = &et->as_number;
2478 type->tp_as_sequence = &et->as_sequence; 2492 type->tp_as_sequence = &et->as_sequence;
2479 type->tp_as_mapping = &et->as_mapping; 2493 type->tp_as_mapping = &et->as_mapping;
2480 type->tp_as_buffer = &et->as_buffer; 2494 type->tp_as_buffer = &et->as_buffer;
2481 type->tp_name = PyUnicode_AsUTF8AndSize(name, &name_size); 2495 type->tp_name = PyUnicode_AsUTF8AndSize(name, &name_size);
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
3031 /* Give up */ 3045 /* Give up */
3032 PyErr_Format(PyExc_AttributeError, 3046 PyErr_Format(PyExc_AttributeError,
3033 "type object '%.50s' has no attribute '%U'", 3047 "type object '%.50s' has no attribute '%U'",
3034 type->tp_name, name); 3048 type->tp_name, name);
3035 return NULL; 3049 return NULL;
3036 } 3050 }
3037 3051
3038 static int 3052 static int
3039 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) 3053 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
3040 { 3054 {
3055 int res;
3041 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { 3056 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3042 PyErr_Format( 3057 PyErr_Format(
3043 PyExc_TypeError, 3058 PyExc_TypeError,
3044 "can't set attributes of built-in/extension type '%s'", 3059 "can't set attributes of built-in/extension type '%s'",
3045 type->tp_name); 3060 type->tp_name);
3046 return -1; 3061 return -1;
3047 } 3062 }
3048 if (_PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL) < 0) 3063 if (_PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL) < 0)
3049 return -1; 3064 return -1;
3050 return update_slot(type, name); 3065 res = update_slot(type, name);
3066 assert(_PyType_CheckConsistency(type));
3067 return res;
3051 } 3068 }
3052 3069
3053 extern void 3070 extern void
3054 _PyDictKeys_DecRef(PyDictKeysObject *keys); 3071 _PyDictKeys_DecRef(PyDictKeysObject *keys);
3055 3072
3056 static void 3073 static void
3057 type_dealloc(PyTypeObject *type) 3074 type_dealloc(PyTypeObject *type)
3058 { 3075 {
3059 PyHeapTypeObject *et; 3076 PyHeapTypeObject *et;
3060 PyObject *tp, *val, *tb; 3077 PyObject *tp, *val, *tb;
(...skipping 1605 matching lines...) Expand 10 before | Expand all | Expand 10 after
4666 _Py_IDENTIFIER(__eq__); 4683 _Py_IDENTIFIER(__eq__);
4667 4684
4668 assert(dict != NULL); 4685 assert(dict != NULL);
4669 if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL) 4686 if (_PyDict_GetItemId(dict, &PyId___eq__) != NULL)
4670 return 1; 4687 return 1;
4671 if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL) 4688 if (_PyDict_GetItemId(dict, &PyId___hash__) != NULL)
4672 return 1; 4689 return 1;
4673 return 0; 4690 return 0;
4674 } 4691 }
4675 4692
4676 /* tp_call slot which calls tp_fastcall */ 4693 /* tp_call slot calling tp_fastcall */
4677 static PyObject * 4694 static PyObject *
4678 fastcall_wrapper(PyObject *callable, PyObject *args_tuple, PyObject *kwargs) 4695 fastcall_wrapper(PyObject *callable, PyObject *args_tuple, PyObject *kwargs)
4679 { 4696 {
4680 return _Py_FastCall_FromArgs(callable, 4697 PyTypeObject *type;
4681 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,
4682 &PyTuple_GET_ITEM(args_tuple, 0), 4714 &PyTuple_GET_ITEM(args_tuple, 0),
4683 PyTuple_GET_SIZE(args_tuple), 4715 PyTuple_GET_SIZE(args_tuple),
4684 kwargs); 4716 kwargs);
4717
4718 result = _Py_CheckFunctionResult(callable, result, NULL);
4719 return result;
4685 } 4720 }
4686 4721
4687 static void 4722 static void
4688 inherit_slots(PyTypeObject *type, PyTypeObject *base) 4723 inherit_slots(PyTypeObject *type, PyTypeObject *base)
4689 { 4724 {
4690 PyTypeObject *basebase; 4725 PyTypeObject *basebase;
4691 4726
4692 #undef SLOTDEFINED 4727 #undef SLOTDEFINED
4693 #undef COPYSLOT 4728 #undef COPYSLOT
4694 #undef COPYNUM 4729 #undef COPYNUM
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
4801 type->tp_getattro = base->tp_getattro; 4836 type->tp_getattro = base->tp_getattro;
4802 } 4837 }
4803 if (type->tp_setattr == NULL && type->tp_setattro == NULL) { 4838 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
4804 type->tp_setattr = base->tp_setattr; 4839 type->tp_setattr = base->tp_setattr;
4805 type->tp_setattro = base->tp_setattro; 4840 type->tp_setattro = base->tp_setattro;
4806 } 4841 }
4807 /* tp_reserved is ignored */ 4842 /* tp_reserved is ignored */
4808 COPYSLOT(tp_repr); 4843 COPYSLOT(tp_repr);
4809 /* tp_hash see tp_richcompare */ 4844 /* tp_hash see tp_richcompare */
4810 4845
4811 if (type->tp_call != fastcall_wrapper) { 4846 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)
4812 /* don't inherit tp_fastcall if tp_call is defined but 4847 && PyType_HasFeature(base, Py_TPFLAGS_HAVE_FASTCALL)
4813 not tp_fastcall */ 4848 /* don't inherit tp_fastcall if tp_call is defined */
4814 if (!type->tp_call && !type->tp_fastcall) { 4849 && (type->tp_call == NULL || type->tp_call != fastcall_wrapper)) {
4815 /* FIXME: do something with Py_TPFLAGS_HAVE_FASTCALL */ 4850 COPYSLOT(tp_fastcall);
4816 COPYSLOT(tp_fastcall); 4851 }
4817 } 4852
4818 4853 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)
4854 || type->tp_call != fastcall_wrapper) {
4819 COPYSLOT(tp_call); 4855 COPYSLOT(tp_call);
4820 } 4856 }
4821 4857
4822 COPYSLOT(tp_str); 4858 COPYSLOT(tp_str);
4823 { 4859 {
4824 /* Copy comparison-related slots only when 4860 /* Copy comparison-related slots only when
4825 not overriding them anywhere */ 4861 not overriding them anywhere */
4826 if (type->tp_richcompare == NULL && 4862 if (type->tp_richcompare == NULL &&
4827 type->tp_hash == NULL && 4863 type->tp_hash == NULL &&
4828 !overrides_hash(type)) 4864 !overrides_hash(type))
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
4870 static int add_operators(PyTypeObject *); 4906 static int add_operators(PyTypeObject *);
4871 4907
4872 int 4908 int
4873 PyType_Ready(PyTypeObject *type) 4909 PyType_Ready(PyTypeObject *type)
4874 { 4910 {
4875 PyObject *dict, *bases; 4911 PyObject *dict, *bases;
4876 PyTypeObject *base; 4912 PyTypeObject *base;
4877 Py_ssize_t i, n; 4913 Py_ssize_t i, n;
4878 4914
4879 if (type->tp_flags & Py_TPFLAGS_READY) { 4915 if (type->tp_flags & Py_TPFLAGS_READY) {
4880 assert(type->tp_dict != NULL); 4916 assert(_PyType_CheckConsistency(type));
4881 return 0; 4917 return 0;
4882 } 4918 }
4883 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); 4919 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
4884 4920
4885 type->tp_flags |= Py_TPFLAGS_READYING; 4921 type->tp_flags |= Py_TPFLAGS_READYING;
4886 4922
4887 #ifdef Py_TRACE_REFS 4923 #ifdef Py_TRACE_REFS
4888 /* 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
4889 * 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
4890 * 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
4940 4976
4941 /* Initialize tp_dict */ 4977 /* Initialize tp_dict */
4942 dict = type->tp_dict; 4978 dict = type->tp_dict;
4943 if (dict == NULL) { 4979 if (dict == NULL) {
4944 dict = PyDict_New(); 4980 dict = PyDict_New();
4945 if (dict == NULL) 4981 if (dict == NULL)
4946 goto error; 4982 goto error;
4947 type->tp_dict = dict; 4983 type->tp_dict = dict;
4948 } 4984 }
4949 4985
4950 if (_Py_TYPE_HAS_FASTCALL(type) && !type->tp_call) { 4986 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_FASTCALL)
4987 && type->tp_fastcall && !type->tp_call) {
4951 /* 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
4952 calling tp_fastcall. We need to define tp_call before calling 4989 calling tp_fastcall. We need to define tp_call before calling
4953 add_operators(), otherwise the __call__ descriptor is not 4990 add_operators(), otherwise the __call__ descriptor is not
4954 defined. */ 4991 defined. */
4955 type->tp_call = fastcall_wrapper; 4992 type->tp_call = fastcall_wrapper;
4956 } 4993 }
4957 4994
4958 /* Add type-specific descriptors to tp_dict */ 4995 /* Add type-specific descriptors to tp_dict */
4959 if (add_operators(type) < 0) 4996 if (add_operators(type) < 0)
4960 goto error; 4997 goto error;
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
5072 bases = type->tp_bases; 5109 bases = type->tp_bases;
5073 n = PyTuple_GET_SIZE(bases); 5110 n = PyTuple_GET_SIZE(bases);
5074 for (i = 0; i < n; i++) { 5111 for (i = 0; i < n; i++) {
5075 PyObject *b = PyTuple_GET_ITEM(bases, i); 5112 PyObject *b = PyTuple_GET_ITEM(bases, i);
5076 if (PyType_Check(b) && 5113 if (PyType_Check(b) &&
5077 add_subclass((PyTypeObject *)b, type) < 0) 5114 add_subclass((PyTypeObject *)b, type) < 0)
5078 goto error; 5115 goto error;
5079 } 5116 }
5080 5117
5081 /* All done -- set the ready flag */ 5118 /* All done -- set the ready flag */
5082 assert(type->tp_dict != NULL);
5083 type->tp_flags = 5119 type->tp_flags =
5084 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; 5120 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
5121 assert(_PyType_CheckConsistency(type));
5085 return 0; 5122 return 0;
5086 5123
5087 error: 5124 error:
5088 type->tp_flags &= ~Py_TPFLAGS_READYING; 5125 type->tp_flags &= ~Py_TPFLAGS_READYING;
5089 return -1; 5126 return -1;
5090 } 5127 }
5091 5128
5092 static int 5129 static int
5093 add_subclass(PyTypeObject *base, PyTypeObject *type) 5130 add_subclass(PyTypeObject *base, PyTypeObject *type)
5094 { 5131 {
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
5354 PyObject *arg, *value; 5391 PyObject *arg, *value;
5355 5392
5356 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) 5393 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
5357 return NULL; 5394 return NULL;
5358 i = getindex(self, arg); 5395 i = getindex(self, arg);
5359 if (i == -1 && PyErr_Occurred()) 5396 if (i == -1 && PyErr_Occurred())
5360 return NULL; 5397 return NULL;
5361 res = (*func)(self, i, value); 5398 res = (*func)(self, i, value);
5362 if (res == -1 && PyErr_Occurred()) 5399 if (res == -1 && PyErr_Occurred())
5363 return NULL; 5400 return NULL;
5364 Py_INCREF(Py_None); 5401 Py_RETURN_NONE;
5365 return Py_None;
5366 } 5402 }
5367 5403
5368 static PyObject * 5404 static PyObject *
5369 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) 5405 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
5370 { 5406 {
5371 ssizeobjargproc func = (ssizeobjargproc)wrapped; 5407 ssizeobjargproc func = (ssizeobjargproc)wrapped;
5372 Py_ssize_t i; 5408 Py_ssize_t i;
5373 int res; 5409 int res;
5374 PyObject *arg; 5410 PyObject *arg;
5375 5411
5376 if (!check_num_args(args, 1)) 5412 if (!check_num_args(args, 1))
5377 return NULL; 5413 return NULL;
5378 arg = PyTuple_GET_ITEM(args, 0); 5414 arg = PyTuple_GET_ITEM(args, 0);
5379 i = getindex(self, arg); 5415 i = getindex(self, arg);
5380 if (i == -1 && PyErr_Occurred()) 5416 if (i == -1 && PyErr_Occurred())
5381 return NULL; 5417 return NULL;
5382 res = (*func)(self, i, NULL); 5418 res = (*func)(self, i, NULL);
5383 if (res == -1 && PyErr_Occurred()) 5419 if (res == -1 && PyErr_Occurred())
5384 return NULL; 5420 return NULL;
5385 Py_INCREF(Py_None); 5421 Py_RETURN_NONE;
5386 return Py_None;
5387 } 5422 }
5388 5423
5389 /* XXX objobjproc is a misnomer; should be objargpred */ 5424 /* XXX objobjproc is a misnomer; should be objargpred */
5390 static PyObject * 5425 static PyObject *
5391 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) 5426 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
5392 { 5427 {
5393 objobjproc func = (objobjproc)wrapped; 5428 objobjproc func = (objobjproc)wrapped;
5394 int res; 5429 int res;
5395 PyObject *value; 5430 PyObject *value;
5396 5431
(...skipping 12 matching lines...) Expand all
5409 { 5444 {
5410 objobjargproc func = (objobjargproc)wrapped; 5445 objobjargproc func = (objobjargproc)wrapped;
5411 int res; 5446 int res;
5412 PyObject *key, *value; 5447 PyObject *key, *value;
5413 5448
5414 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) 5449 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
5415 return NULL; 5450 return NULL;
5416 res = (*func)(self, key, value); 5451 res = (*func)(self, key, value);
5417 if (res == -1 && PyErr_Occurred()) 5452 if (res == -1 && PyErr_Occurred())
5418 return NULL; 5453 return NULL;
5419 Py_INCREF(Py_None); 5454 Py_RETURN_NONE;
5420 return Py_None;
5421 } 5455 }
5422 5456
5423 static PyObject * 5457 static PyObject *
5424 wrap_delitem(PyObject *self, PyObject *args, void *wrapped) 5458 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
5425 { 5459 {
5426 objobjargproc func = (objobjargproc)wrapped; 5460 objobjargproc func = (objobjargproc)wrapped;
5427 int res; 5461 int res;
5428 PyObject *key; 5462 PyObject *key;
5429 5463
5430 if (!check_num_args(args, 1)) 5464 if (!check_num_args(args, 1))
5431 return NULL; 5465 return NULL;
5432 key = PyTuple_GET_ITEM(args, 0); 5466 key = PyTuple_GET_ITEM(args, 0);
5433 res = (*func)(self, key, NULL); 5467 res = (*func)(self, key, NULL);
5434 if (res == -1 && PyErr_Occurred()) 5468 if (res == -1 && PyErr_Occurred())
5435 return NULL; 5469 return NULL;
5436 Py_INCREF(Py_None); 5470 Py_RETURN_NONE;
5437 return Py_None;
5438 } 5471 }
5439 5472
5440 /* 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.
5441 This is called the Carlo Verre hack after its discoverer. */ 5474 This is called the Carlo Verre hack after its discoverer. */
5442 static int 5475 static int
5443 hackcheck(PyObject *self, setattrofunc func, const char *what) 5476 hackcheck(PyObject *self, setattrofunc func, const char *what)
5444 { 5477 {
5445 PyTypeObject *type = Py_TYPE(self); 5478 PyTypeObject *type = Py_TYPE(self);
5446 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE) 5479 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
5447 type = type->tp_base; 5480 type = type->tp_base;
(...skipping 16 matching lines...) Expand all
5464 int res; 5497 int res;
5465 PyObject *name, *value; 5498 PyObject *name, *value;
5466 5499
5467 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) 5500 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
5468 return NULL; 5501 return NULL;
5469 if (!hackcheck(self, func, "__setattr__")) 5502 if (!hackcheck(self, func, "__setattr__"))
5470 return NULL; 5503 return NULL;
5471 res = (*func)(self, name, value); 5504 res = (*func)(self, name, value);
5472 if (res < 0) 5505 if (res < 0)
5473 return NULL; 5506 return NULL;
5474 Py_INCREF(Py_None); 5507 Py_RETURN_NONE;
5475 return Py_None;
5476 } 5508 }
5477 5509
5478 static PyObject * 5510 static PyObject *
5479 wrap_delattr(PyObject *self, PyObject *args, void *wrapped) 5511 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
5480 { 5512 {
5481 setattrofunc func = (setattrofunc)wrapped; 5513 setattrofunc func = (setattrofunc)wrapped;
5482 int res; 5514 int res;
5483 PyObject *name; 5515 PyObject *name;
5484 5516
5485 if (!check_num_args(args, 1)) 5517 if (!check_num_args(args, 1))
5486 return NULL; 5518 return NULL;
5487 name = PyTuple_GET_ITEM(args, 0); 5519 name = PyTuple_GET_ITEM(args, 0);
5488 if (!hackcheck(self, func, "__delattr__")) 5520 if (!hackcheck(self, func, "__delattr__"))
5489 return NULL; 5521 return NULL;
5490 res = (*func)(self, name, NULL); 5522 res = (*func)(self, name, NULL);
5491 if (res < 0) 5523 if (res < 0)
5492 return NULL; 5524 return NULL;
5493 Py_INCREF(Py_None); 5525 Py_RETURN_NONE;
5494 return Py_None;
5495 } 5526 }
5496 5527
5497 static PyObject * 5528 static PyObject *
5498 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) 5529 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
5499 { 5530 {
5500 hashfunc func = (hashfunc)wrapped; 5531 hashfunc func = (hashfunc)wrapped;
5501 Py_hash_t res; 5532 Py_hash_t res;
5502 5533
5503 if (!check_num_args(args, 0)) 5534 if (!check_num_args(args, 0))
5504 return NULL; 5535 return NULL;
5505 res = (*func)(self); 5536 res = (*func)(self);
5506 if (res == -1 && PyErr_Occurred()) 5537 if (res == -1 && PyErr_Occurred())
5507 return NULL; 5538 return NULL;
5508 return PyLong_FromSsize_t(res); 5539 return PyLong_FromSsize_t(res);
5509 } 5540 }
5510 5541
5511 static PyObject * 5542 static PyObject *
5512 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) 5543 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
5513 { 5544 {
5514 ternaryfunc func = (ternaryfunc)wrapped; 5545 ternaryfunc func = (ternaryfunc)wrapped;
5515 5546
5516 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);
5517 } 5557 }
5518 5558
5519 static PyObject * 5559 static PyObject *
5520 wrap_del(PyObject *self, PyObject *args, void *wrapped) 5560 wrap_del(PyObject *self, PyObject *args, void *wrapped)
5521 { 5561 {
5522 destructor func = (destructor)wrapped; 5562 destructor func = (destructor)wrapped;
5523 5563
5524 if (!check_num_args(args, 0)) 5564 if (!check_num_args(args, 0))
5525 return NULL; 5565 return NULL;
5526 5566
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
5595 { 5635 {
5596 descrsetfunc func = (descrsetfunc)wrapped; 5636 descrsetfunc func = (descrsetfunc)wrapped;
5597 PyObject *obj, *value; 5637 PyObject *obj, *value;
5598 int ret; 5638 int ret;
5599 5639
5600 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) 5640 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
5601 return NULL; 5641 return NULL;
5602 ret = (*func)(self, obj, value); 5642 ret = (*func)(self, obj, value);
5603 if (ret < 0) 5643 if (ret < 0)
5604 return NULL; 5644 return NULL;
5605 Py_INCREF(Py_None); 5645 Py_RETURN_NONE;
5606 return Py_None;
5607 } 5646 }
5608 5647
5609 static PyObject * 5648 static PyObject *
5610 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) 5649 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
5611 { 5650 {
5612 descrsetfunc func = (descrsetfunc)wrapped; 5651 descrsetfunc func = (descrsetfunc)wrapped;
5613 PyObject *obj; 5652 PyObject *obj;
5614 int ret; 5653 int ret;
5615 5654
5616 if (!check_num_args(args, 1)) 5655 if (!check_num_args(args, 1))
5617 return NULL; 5656 return NULL;
5618 obj = PyTuple_GET_ITEM(args, 0); 5657 obj = PyTuple_GET_ITEM(args, 0);
5619 ret = (*func)(self, obj, NULL); 5658 ret = (*func)(self, obj, NULL);
5620 if (ret < 0) 5659 if (ret < 0)
5621 return NULL; 5660 return NULL;
5622 Py_INCREF(Py_None); 5661 Py_RETURN_NONE;
5623 return Py_None;
5624 } 5662 }
5625 5663
5626 static PyObject * 5664 static PyObject *
5627 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) 5665 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
5628 { 5666 {
5629 initproc func = (initproc)wrapped; 5667 initproc func = (initproc)wrapped;
5630 5668
5631 if (func(self, args, kwds) < 0) 5669 if (func(self, args, kwds) < 0)
5632 return NULL; 5670 return NULL;
5633 Py_INCREF(Py_None); 5671 Py_RETURN_NONE;
5634 return Py_None;
5635 } 5672 }
5636 5673
5637 static PyObject * 5674 static PyObject *
5638 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) 5675 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
5639 { 5676 {
5640 PyTypeObject *type, *subtype, *staticbase; 5677 PyTypeObject *type, *subtype, *staticbase;
5641 PyObject *arg0, *res; 5678 PyObject *arg0, *res;
5642 5679
5643 if (self == NULL || !PyType_Check(self)) 5680 if (self == NULL || !PyType_Check(self))
5644 Py_FatalError("__new__() called with non-type 'self'"); 5681 Py_FatalError("__new__() called with non-type 'self'");
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after
6179 res = PyObject_Call(meth, args, kwds); 6216 res = PyObject_Call(meth, args, kwds);
6180 6217
6181 Py_DECREF(meth); 6218 Py_DECREF(meth);
6182 return res; 6219 return res;
6183 } 6220 }
6184 6221
6185 static PyObject * 6222 static PyObject *
6186 slot_tp_fastcall(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kw names) 6223 slot_tp_fastcall(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kw names)
6187 { 6224 {
6188 _Py_IDENTIFIER(__call__); 6225 _Py_IDENTIFIER(__call__);
6189 PyObject *meth = lookup_method(self, &PyId___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
6190 PyObject *res; 6227 PyObject *res;
6191 6228
6192 if (meth == NULL) 6229 if (meth == NULL)
6193 return NULL; 6230 return NULL;
6194 6231
6195 res = _PyObject_FastCallKeywords(meth, args, nargs, kwnames); 6232 res = _PyObject_FastCallKeywords(meth, args, nargs, kwnames);
6196 6233
6197 Py_DECREF(meth); 6234 Py_DECREF(meth);
6198 return res; 6235 return res;
6199 } 6236 }
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
6544 #undef ETSLOT 6581 #undef ETSLOT
6545 #undef SQSLOT 6582 #undef SQSLOT
6546 #undef MPSLOT 6583 #undef MPSLOT
6547 #undef NBSLOT 6584 #undef NBSLOT
6548 #undef UNSLOT 6585 #undef UNSLOT
6549 #undef IBSLOT 6586 #undef IBSLOT
6550 #undef BINSLOT 6587 #undef BINSLOT
6551 #undef RBINSLOT 6588 #undef RBINSLOT
6552 6589
6553 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6590 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6554 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 6591 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, NULL, \
6555 PyDoc_STR(DOC)} 6592 PyDoc_STR(DOC)}
6556 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ 6593 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
6557 {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, \
6558 PyDoc_STR(DOC), FLAGS} 6598 PyDoc_STR(DOC), FLAGS}
6559 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6599 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6560 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 6600 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, NULL, \
6561 PyDoc_STR(DOC)} 6601 PyDoc_STR(DOC)}
6562 #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6602 #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6563 ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC) 6603 ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC)
6564 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6604 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6565 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) 6605 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
6566 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6606 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6567 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) 6607 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
6568 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6608 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
6569 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) 6609 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
6570 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 6610 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
(...skipping 17 matching lines...) Expand all
6588 6628
6589 static slotdef slotdefs[] = { 6629 static slotdef slotdefs[] = {
6590 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), 6630 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
6591 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), 6631 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
6592 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), 6632 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
6593 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), 6633 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
6594 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, 6634 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
6595 "__repr__($self, /)\n--\n\nReturn repr(self)."), 6635 "__repr__($self, /)\n--\n\nReturn repr(self)."),
6596 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, 6636 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
6597 "__hash__($self, /)\n--\n\nReturn hash(self)."), 6637 "__hash__($self, /)\n--\n\nReturn hash(self)."),
6598 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, 6638 FLSLOT_FAST("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, wrap_ fastcall,
6599 "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function." , 6639 "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function." ,
6600 PyWrapperFlag_KEYWORDS), 6640 PyWrapperFlag_KEYWORDS),
6601 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, 6641 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
6602 "__str__($self, /)\n--\n\nReturn str(self)."), 6642 "__str__($self, /)\n--\n\nReturn str(self)."),
6603 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, 6643 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
6604 wrap_binaryfunc, 6644 wrap_binaryfunc,
6605 "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)." ), 6645 "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)." ),
6606 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), 6646 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
6607 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, 6647 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
6608 "__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
6862 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,
6863 because that's convenient for fixup_slot_dispatchers(). */ 6903 because that's convenient for fixup_slot_dispatchers(). */
6864 static slotdef * 6904 static slotdef *
6865 update_one_slot(PyTypeObject *type, slotdef *p) 6905 update_one_slot(PyTypeObject *type, slotdef *p)
6866 { 6906 {
6867 PyObject *descr; 6907 PyObject *descr;
6868 PyWrapperDescrObject *d; 6908 PyWrapperDescrObject *d;
6869 void *generic = NULL, *specific = NULL; 6909 void *generic = NULL, *specific = NULL;
6870 int use_generic = 0; 6910 int use_generic = 0;
6871 int offset = p->offset; 6911 int offset = p->offset;
6872 void **ptr = slotptr(type, offset); 6912 void **ptr = slotptr(type, offset), **ptr2 = NULL;
6873 6913
6874 if (ptr == NULL) { 6914 if (ptr == NULL) {
6875 do { 6915 do {
6876 ++p; 6916 ++p;
6877 } while (p->offset == offset); 6917 } while (p->offset == offset);
6878 return p; 6918 return p;
6879 } 6919 }
6920
6880 do { 6921 do {
6881 descr = _PyType_Lookup(type, p->name_strobj); 6922 descr = _PyType_Lookup(type, p->name_strobj);
6882 if (descr == NULL) { 6923 if (descr == NULL) {
6883 if (ptr == (void**)&type->tp_iternext) { 6924 if (ptr == (void**)&type->tp_iternext) {
6925 ptr2 = NULL;
6884 specific = (void *)_PyObject_NextNotImplemented; 6926 specific = (void *)_PyObject_NextNotImplemented;
6885 } 6927 }
6886 continue; 6928 continue;
6887 } 6929 }
6888 if (Py_TYPE(descr) == &PyWrapperDescr_Type && 6930 if (Py_TYPE(descr) == &PyWrapperDescr_Type &&
6889 ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_stro bj) { 6931 ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_stro bj) {
6890 void **tptr = resolve_slotdups(type, p->name_strobj); 6932 void **tptr = resolve_slotdups(type, p->name_strobj);
6933 int same_wrapper;
6934
6891 if (tptr == NULL || tptr == ptr) 6935 if (tptr == NULL || tptr == ptr)
6892 generic = p->function; 6936 generic = p->function;
6893 d = (PyWrapperDescrObject *)descr; 6937 d = (PyWrapperDescrObject *)descr;
6894 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 &&
6895 PyType_IsSubtype(type, PyDescr_TYPE(d))) 6950 PyType_IsSubtype(type, PyDescr_TYPE(d)))
6896 { 6951 {
6897 if (specific == NULL || 6952 if (specific == NULL ||
6898 specific == d->d_wrapped) 6953 specific == d->d_wrapped)
6899 specific = d->d_wrapped; 6954 specific = d->d_wrapped;
6900 else 6955 else
6901 use_generic = 1; 6956 use_generic = 1;
6902 } 6957 }
6903 } 6958 }
6904 else if (Py_TYPE(descr) == &PyCFunction_Type && 6959 else if (Py_TYPE(descr) == &PyCFunction_Type &&
6905 PyCFunction_GET_FUNCTION(descr) == 6960 PyCFunction_GET_FUNCTION(descr) ==
6906 (PyCFunction)tp_new_wrapper && 6961 (PyCFunction)tp_new_wrapper &&
6907 ptr == (void**)&type->tp_new) 6962 ptr == (void**)&type->tp_new)
6908 { 6963 {
6909 /* The __new__ wrapper is not a wrapper descriptor, 6964 /* The __new__ wrapper is not a wrapper descriptor,
6910 so must be special-cased differently. 6965 so must be special-cased differently.
6911 If we don't do this, creating an instance will 6966 If we don't do this, creating an instance will
6912 always use slot_tp_new which will look up 6967 always use slot_tp_new which will look up
6913 __new__ in the MRO which will call tp_new_wrapper 6968 __new__ in the MRO which will call tp_new_wrapper
6914 which will look through the base classes looking 6969 which will look through the base classes looking
6915 for a static base and call its tp_new (usually 6970 for a static base and call its tp_new (usually
6916 PyType_GenericNew), after performing various 6971 PyType_GenericNew), after performing various
6917 sanity checks and constructing a new argument 6972 sanity checks and constructing a new argument
6918 list. Cut all that nonsense short -- this speeds 6973 list. Cut all that nonsense short -- this speeds
6919 up instance creation tremendously. */ 6974 up instance creation tremendously. */
6975
6920 specific = (void *)type->tp_new; 6976 specific = (void *)type->tp_new;
6977 ptr2 = NULL;
6978
6921 /* 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
6922 in this reasoning that requires additional 6980 in this reasoning that requires additional
6923 sanity checks. I'll buy the first person to 6981 sanity checks. I'll buy the first person to
6924 point out a bug in this reasoning a beer. */ 6982 point out a bug in this reasoning a beer. */
6925 } 6983 }
6926 else if (descr == Py_None && 6984 else if (descr == Py_None &&
6927 ptr == (void**)&type->tp_hash) { 6985 ptr == (void**)&type->tp_hash) {
6928 /* We specifically allow __hash__ to be set to None 6986 /* We specifically allow __hash__ to be set to None
6929 to prevent inheritance of the default 6987 to prevent inheritance of the default
6930 implementation from object.__hash__ */ 6988 implementation from object.__hash__ */
6931 specific = (void *)PyObject_HashNotImplemented; 6989 specific = (void *)PyObject_HashNotImplemented;
6990 ptr2 = NULL;
6932 } 6991 }
6933 else { 6992 else {
6934 use_generic = 1; 6993 use_generic = 1;
6935 generic = p->function; 6994 generic = p->function;
6936 } 6995 }
6937 } while ((++p)->offset == offset); 6996 } while ((++p)->offset == offset);
6938 if (specific && !use_generic) 6997
6998 if (specific && !use_generic) {
6999 if (ptr2 != NULL) {
7000 ptr = ptr2;
7001 }
6939 *ptr = specific; 7002 *ptr = specific;
6940 else 7003 }
7004 else {
6941 *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
6942 return p; 7020 return p;
6943 } 7021 }
6944 7022
6945 /* 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.
6946 This is a callback for update_subclasses(). */ 7024 This is a callback for update_subclasses(). */
6947 static int 7025 static int
6948 update_slots_callback(PyTypeObject *type, void *data) 7026 update_slots_callback(PyTypeObject *type, void *data)
6949 { 7027 {
6950 slotdef **pp = (slotdef **)data; 7028 slotdef **pp = (slotdef **)data;
6951 7029
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
7023 7101
7024 /* Store the proper functions in the slot dispatches at class (type) 7102 /* Store the proper functions in the slot dispatches at class (type)
7025 definition time, based upon which operations the class overrides in its 7103 definition time, based upon which operations the class overrides in its
7026 dict. */ 7104 dict. */
7027 static void 7105 static void
7028 fixup_slot_dispatchers(PyTypeObject *type) 7106 fixup_slot_dispatchers(PyTypeObject *type)
7029 { 7107 {
7030 slotdef *p; 7108 slotdef *p;
7031 7109
7032 init_slotdefs(); 7110 init_slotdefs();
7033 for (p = slotdefs; p->name; ) 7111 for (p = slotdefs; p->name; ) {
7034 p = update_one_slot(type, p); 7112 p = update_one_slot(type, p);
7035
7036 if (type->tp_call == slot_tp_call && type->tp_fastcall == NULL) {
7037 /* FIXME: is it ok to set the flag here? */
7038 type->tp_flags |= Py_TPFLAGS_HAVE_FASTCALL;
7039 type->tp_fastcall = slot_tp_fastcall;
7040 type->tp_call = fastcall_wrapper;
7041 } 7113 }
7042 } 7114 }
7043 7115
7044 static void 7116 static void
7045 update_all_slots(PyTypeObject* type) 7117 update_all_slots(PyTypeObject* type)
7046 { 7118 {
7047 slotdef *p; 7119 slotdef *p;
7048 7120
7049 init_slotdefs(); 7121 init_slotdefs();
7050 for (p = slotdefs; p->name; p++) { 7122 for (p = slotdefs; p->name; p++) {
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
7211 if (PyDict_GetItem(dict, p->name_strobj)) 7283 if (PyDict_GetItem(dict, p->name_strobj))
7212 continue; 7284 continue;
7213 if (*ptr == (void *)PyObject_HashNotImplemented) { 7285 if (*ptr == (void *)PyObject_HashNotImplemented) {
7214 /* Classes may prevent the inheritance of the tp_hash 7286 /* Classes may prevent the inheritance of the tp_hash
7215 slot by storing PyObject_HashNotImplemented in it. Make it 7287 slot by storing PyObject_HashNotImplemented in it. Make it
7216 visible as a None value for the __hash__ attribute. */ 7288 visible as a None value for the __hash__ attribute. */
7217 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) 7289 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
7218 return -1; 7290 return -1;
7219 } 7291 }
7220 else { 7292 else {
7221 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);
7222 if (descr == NULL) 7311 if (descr == NULL)
7223 return -1; 7312 return -1;
7224 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) { 7313 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) {
7225 Py_DECREF(descr); 7314 Py_DECREF(descr);
7226 return -1; 7315 return -1;
7227 } 7316 }
7228 Py_DECREF(descr); 7317 Py_DECREF(descr);
7229 } 7318 }
7230 } 7319 }
7231 if (type->tp_new != NULL) { 7320 if (type->tp_new != NULL) {
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
7620 0, /* tp_base */ 7709 0, /* tp_base */
7621 0, /* tp_dict */ 7710 0, /* tp_dict */
7622 super_descr_get, /* tp_descr_get */ 7711 super_descr_get, /* tp_descr_get */
7623 0, /* tp_descr_set */ 7712 0, /* tp_descr_set */
7624 0, /* tp_dictoffset */ 7713 0, /* tp_dictoffset */
7625 super_init, /* tp_init */ 7714 super_init, /* tp_init */
7626 PyType_GenericAlloc, /* tp_alloc */ 7715 PyType_GenericAlloc, /* tp_alloc */
7627 PyType_GenericNew, /* tp_new */ 7716 PyType_GenericNew, /* tp_new */
7628 PyObject_GC_Del, /* tp_free */ 7717 PyObject_GC_Del, /* tp_free */
7629 }; 7718 };
LEFTRIGHT

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