| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,6 +117,35 @@ typedef struct { | |||
| 117 | 117 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) | |
| 118 | 118 | #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size) | |
| 119 | 119 | ||
| 120 | + /********************* String Literals ****************************************/ | ||
| 121 | + /* This structure helps managing static strings. The basic usage goes like this: | ||
| 122 | + Instead of doing | ||
| 123 | + | ||
| 124 | + r = PyObject_CallMethod(o, "foo", "args", ...); | ||
| 125 | + | ||
| 126 | + do | ||
| 127 | + | ||
| 128 | + _Py_IDENTIFIER(foo); | ||
| 129 | + ... | ||
| 130 | + r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...); | ||
| 131 | + | ||
| 132 | + PyId_foo is a static variable, either on block level or file level. On first | ||
| 133 | + usage, the string "foo" is interned, and the structures are linked. On interpreter | ||
| 134 | + shutdown, all strings are released (through _PyUnicode_ClearStaticStrings). | ||
| 135 | + | ||
| 136 | + Alternatively, _Py_static_string allows to choose the variable name. | ||
| 137 | + _PyUnicode_FromId returns a borrowed reference to the interned string. | ||
| 138 | + _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*. | ||
| 139 | + */ | ||
| 140 | + typedef struct _Py_Identifier { | ||
| 141 | + struct _Py_Identifier *next; | ||
| 142 | + const char* string; | ||
| 143 | + PyObject *object; | ||
| 144 | + } _Py_Identifier; | ||
| 145 | + | ||
| 146 | + #define _Py_static_string(varname, value) static _Py_Identifier varname = { 0, value, 0 } | ||
| 147 | + #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname) | ||
| 148 | + | ||
| 120 | 149 | /* | |
| 121 | 150 | Type objects contain a string containing the type name (to help somewhat | |
| 122 | 151 | in debugging), the allocation parameters (see PyObject_New() and | |
@@ -448,7 +477,7 @@ PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *, | |||
| 448 | 477 | PyObject *, PyObject *); | |
| 449 | 478 | #ifndef Py_LIMITED_API | |
| 450 | 479 | PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *); | |
| 451 | - PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, char *, PyObject **); | ||
| 480 | + PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *); | ||
| 452 | 481 | PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *); | |
| 453 | 482 | #endif | |
| 454 | 483 | PyAPI_FUNC(unsigned int) PyType_ClearCache(void); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2120,35 +2120,6 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency( | |||
| 2120 | 2120 | int check_content); | |
| 2121 | 2121 | #endif | |
| 2122 | 2122 | ||
| 2123 | - /********************* String Literals ****************************************/ | ||
| 2124 | - /* This structure helps managing static strings. The basic usage goes like this: | ||
| 2125 | - Instead of doing | ||
| 2126 | - | ||
| 2127 | - r = PyObject_CallMethod(o, "foo", "args", ...); | ||
| 2128 | - | ||
| 2129 | - do | ||
| 2130 | - | ||
| 2131 | - _Py_IDENTIFIER(foo); | ||
| 2132 | - ... | ||
| 2133 | - r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...); | ||
| 2134 | - | ||
| 2135 | - PyId_foo is a static variable, either on block level or file level. On first | ||
| 2136 | - usage, the string "foo" is interned, and the structures are linked. On interpreter | ||
| 2137 | - shutdown, all strings are released (through _PyUnicode_ClearStaticStrings). | ||
| 2138 | - | ||
| 2139 | - Alternatively, _Py_static_string allows to choose the variable name. | ||
| 2140 | - _PyUnicode_FromId returns a borrowed reference to the interned string. | ||
| 2141 | - _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*. | ||
| 2142 | - */ | ||
| 2143 | - typedef struct _Py_Identifier { | ||
| 2144 | - struct _Py_Identifier *next; | ||
| 2145 | - const char* string; | ||
| 2146 | - PyObject *object; | ||
| 2147 | - } _Py_Identifier; | ||
| 2148 | - | ||
| 2149 | - #define _Py_static_string(varname, value) static _Py_Identifier varname = { 0, value, 0 } | ||
| 2150 | - #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname) | ||
| 2151 | - | ||
| 2152 | 2123 | /* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/ | |
| 2153 | 2124 | PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*); | |
| 2154 | 2125 | /* Clear all static strings. */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -887,10 +887,10 @@ FUNC1(atanh, m_atanh, 0, | |||
| 887 | 887 | "atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.") | |
| 888 | 888 | ||
| 889 | 889 | static PyObject * math_ceil(PyObject *self, PyObject *number) { | |
| 890 | - static PyObject *ceil_str = NULL; | ||
| 890 | + _Py_IDENTIFIER(__ceil__); | ||
| 891 | 891 | PyObject *method, *result; | |
| 892 | 892 | ||
| 893 | - method = _PyObject_LookupSpecial(number, "__ceil__", &ceil_str); | ||
| 893 | + method = _PyObject_LookupSpecial(number, &PyId___ceil__); | ||
| 894 | 894 | if (method == NULL) { | |
| 895 | 895 | if (PyErr_Occurred()) | |
| 896 | 896 | return NULL; | |
@@ -925,10 +925,10 @@ FUNC1(fabs, fabs, 0, | |||
| 925 | 925 | "fabs(x)\n\nReturn the absolute value of the float x.") | |
| 926 | 926 | ||
| 927 | 927 | static PyObject * math_floor(PyObject *self, PyObject *number) { | |
| 928 | - static PyObject *floor_str = NULL; | ||
| 928 | + _Py_IDENTIFIER(__floor__); | ||
| 929 | 929 | PyObject *method, *result; | |
| 930 | 930 | ||
| 931 | - method = _PyObject_LookupSpecial(number, "__floor__", &floor_str); | ||
| 931 | + method = _PyObject_LookupSpecial(number, &PyId___floor__); | ||
| 932 | 932 | if (method == NULL) { | |
| 933 | 933 | if (PyErr_Occurred()) | |
| 934 | 934 | return NULL; | |
@@ -1462,15 +1462,15 @@ PyDoc_STRVAR(math_factorial_doc, | |||
| 1462 | 1462 | static PyObject * | |
| 1463 | 1463 | math_trunc(PyObject *self, PyObject *number) | |
| 1464 | 1464 | { | |
| 1465 | - static PyObject *trunc_str = NULL; | ||
| 1465 | + _Py_IDENTIFIER(__trunc__); | ||
| 1466 | 1466 | PyObject *trunc, *result; | |
| 1467 | 1467 | ||
| 1468 | 1468 | if (Py_TYPE(number)->tp_dict == NULL) { | |
| 1469 | 1469 | if (PyType_Ready(Py_TYPE(number)) < 0) | |
| 1470 | 1470 | return NULL; | |
| 1471 | 1471 | } | |
| 1472 | 1472 | ||
| 1473 | - trunc = _PyObject_LookupSpecial(number, "__trunc__", &trunc_str); | ||
| 1473 | + trunc = _PyObject_LookupSpecial(number, &PyId___trunc__); | ||
| 1474 | 1474 | if (trunc == NULL) { | |
| 1475 | 1475 | if (!PyErr_Occurred()) | |
| 1476 | 1476 | PyErr_Format(PyExc_TypeError, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,7 +74,7 @@ PyObject_Length(PyObject *o) | |||
| 74 | 74 | Py_ssize_t | |
| 75 | 75 | _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) | |
| 76 | 76 | { | |
| 77 | - static PyObject *hintstrobj = NULL; | ||
| 77 | + _Py_IDENTIFIER(__length_hint__); | ||
| 78 | 78 | PyObject *ro, *hintmeth; | |
| 79 | 79 | Py_ssize_t rv; | |
| 80 | 80 | ||
@@ -89,7 +89,7 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) | |||
| 89 | 89 | } | |
| 90 | 90 | ||
| 91 | 91 | /* try o.__length_hint__() */ | |
| 92 | - hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj); | ||
| 92 | + hintmeth = _PyObject_LookupSpecial(o, &PyId___length_hint__); | ||
| 93 | 93 | if (hintmeth == NULL) { | |
| 94 | 94 | if (PyErr_Occurred()) | |
| 95 | 95 | return -1; | |
@@ -697,7 +697,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) | |||
| 697 | 697 | PyObject *meth; | |
| 698 | 698 | PyObject *empty = NULL; | |
| 699 | 699 | PyObject *result = NULL; | |
| 700 | - static PyObject *format_cache = NULL; | ||
| 700 | + _Py_IDENTIFIER(__format__); | ||
| 701 | 701 | ||
| 702 | 702 | /* If no format_spec is provided, use an empty string */ | |
| 703 | 703 | if (format_spec == NULL) { | |
@@ -706,7 +706,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) | |||
| 706 | 706 | } | |
| 707 | 707 | ||
| 708 | 708 | /* Find the (unbound!) __format__ method (a borrowed reference) */ | |
| 709 | - meth = _PyObject_LookupSpecial(obj, "__format__", &format_cache); | ||
| 709 | + meth = _PyObject_LookupSpecial(obj, &PyId___format__); | ||
| 710 | 710 | if (meth == NULL) { | |
| 711 | 711 | if (!PyErr_Occurred()) | |
| 712 | 712 | PyErr_Format(PyExc_TypeError, | |
@@ -2571,7 +2571,7 @@ recursive_isinstance(PyObject *inst, PyObject *cls) | |||
| 2571 | 2571 | int | |
| 2572 | 2572 | PyObject_IsInstance(PyObject *inst, PyObject *cls) | |
| 2573 | 2573 | { | |
| 2574 | - static PyObject *name = NULL; | ||
| 2574 | + _Py_IDENTIFIER(__instancecheck__); | ||
| 2575 | 2575 | PyObject *checker; | |
| 2576 | 2576 | ||
| 2577 | 2577 | /* Quick test for an exact match */ | |
@@ -2597,7 +2597,7 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls) | |||
| 2597 | 2597 | return r; | |
| 2598 | 2598 | } | |
| 2599 | 2599 | ||
| 2600 | - checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name); | ||
| 2600 | + checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__); | ||
| 2601 | 2601 | if (checker != NULL) { | |
| 2602 | 2602 | PyObject *res; | |
| 2603 | 2603 | int ok = -1; | |
@@ -2640,7 +2640,7 @@ recursive_issubclass(PyObject *derived, PyObject *cls) | |||
| 2640 | 2640 | int | |
| 2641 | 2641 | PyObject_IsSubclass(PyObject *derived, PyObject *cls) | |
| 2642 | 2642 | { | |
| 2643 | - static PyObject *name = NULL; | ||
| 2643 | + _Py_IDENTIFIER(__subclasscheck__); | ||
| 2644 | 2644 | PyObject *checker; | |
| 2645 | 2645 | ||
| 2646 | 2646 | if (PyTuple_Check(cls)) { | |
@@ -2662,7 +2662,7 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls) | |||
| 2662 | 2662 | return r; | |
| 2663 | 2663 | } | |
| 2664 | 2664 | ||
| 2665 | - checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name); | ||
| 2665 | + checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__); | ||
| 2666 | 2666 | if (checker != NULL) { | |
| 2667 | 2667 | PyObject *res; | |
| 2668 | 2668 | int ok = -1; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -265,9 +265,9 @@ PyComplex_ImagAsDouble(PyObject *op) | |||
| 265 | 265 | static PyObject * | |
| 266 | 266 | try_complex_special_method(PyObject *op) { | |
| 267 | 267 | PyObject *f; | |
| 268 | - static PyObject *complexstr; | ||
| 268 | + _Py_IDENTIFIER(__complex__); | ||
| 269 | 269 | ||
| 270 | - f = _PyObject_LookupSpecial(op, "__complex__", &complexstr); | ||
| 270 | + f = _PyObject_LookupSpecial(op, &PyId___complex__); | ||
| 271 | 271 | if (f) { | |
| 272 | 272 | PyObject *res = PyObject_CallFunctionObjArgs(f, NULL); | |
| 273 | 273 | Py_DECREF(f); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1142,10 +1142,8 @@ dict_subscript(PyDictObject *mp, register PyObject *key) | |||
| 1142 | 1142 | if (!PyDict_CheckExact(mp)) { | |
| 1143 | 1143 | /* Look up __missing__ method if we're a subclass. */ | |
| 1144 | 1144 | PyObject *missing, *res; | |
| 1145 | - static PyObject *missing_str = NULL; | ||
| 1146 | - missing = _PyObject_LookupSpecial((PyObject *)mp, | ||
| 1147 | - "__missing__", | ||
| 1148 | - &missing_str); | ||
| 1145 | + _Py_IDENTIFIER(__missing__); | ||
| 1146 | + missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__); | ||
| 1149 | 1147 | if (missing != NULL) { | |
| 1150 | 1148 | res = PyObject_CallFunctionObjArgs(missing, | |
| 1151 | 1149 | key, NULL); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -224,16 +224,16 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||
| 224 | 224 | { | |
| 225 | 225 | Py_ssize_t n; | |
| 226 | 226 | PyObject *seq, *reversed_meth; | |
| 227 | - static PyObject *reversed_cache = NULL; | ||
| 228 | 227 | reversedobject *ro; | |
| 228 | + _Py_IDENTIFIER(__reversed__); | ||
| 229 | 229 | ||
| 230 | 230 | if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds)) | |
| 231 | 231 | return NULL; | |
| 232 | 232 | ||
| 233 | 233 | if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) ) | |
| 234 | 234 | return NULL; | |
| 235 | 235 | ||
| 236 | - reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache); | ||
| 236 | + reversed_meth = _PyObject_LookupSpecial(seq, &PyId___reversed__); | ||
| 237 | 237 | if (reversed_meth != NULL) { | |
| 238 | 238 | PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL); | |
| 239 | 239 | Py_DECREF(reversed_meth); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -470,7 +470,7 @@ PyObject * | |||
| 470 | 470 | PyObject_Bytes(PyObject *v) | |
| 471 | 471 | { | |
| 472 | 472 | PyObject *result, *func; | |
| 473 | - static PyObject *bytesstring = NULL; | ||
| 473 | + _Py_IDENTIFIER(__bytes__); | ||
| 474 | 474 | ||
| 475 | 475 | if (v == NULL) | |
| 476 | 476 | return PyBytes_FromString("<NULL>"); | |
@@ -480,7 +480,7 @@ PyObject_Bytes(PyObject *v) | |||
| 480 | 480 | return v; | |
| 481 | 481 | } | |
| 482 | 482 | ||
| 483 | - func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring); | ||
| 483 | + func = _PyObject_LookupSpecial(v, &PyId___bytes__); | ||
| 484 | 484 | if (func != NULL) { | |
| 485 | 485 | result = PyObject_CallFunctionObjArgs(func, NULL); | |
| 486 | 486 | Py_DECREF(func); | |
@@ -1298,8 +1298,8 @@ static PyObject * | |||
| 1298 | 1298 | _dir_object(PyObject *obj) | |
| 1299 | 1299 | { | |
| 1300 | 1300 | PyObject *result, *sorted; | |
| 1301 | - static PyObject *dir_str = NULL; | ||
| 1302 | - PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str); | ||
| 1301 | + _Py_IDENTIFIER(__dir__); | ||
| 1302 | + PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__); | ||
| 1303 | 1303 | ||
| 1304 | 1304 | assert(obj); | |
| 1305 | 1305 | if (dirfunc == NULL) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments