diff options
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 141 |
1 files changed, 87 insertions, 54 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 2f887aa..47010d6 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -74,7 +74,7 @@ PyObject_Length(PyObject *o) Py_ssize_t _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) { - static PyObject *hintstrobj = NULL; + _Py_IDENTIFIER(__length_hint__); PyObject *ro, *hintmeth; Py_ssize_t rv; @@ -89,7 +89,7 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) } /* try o.__length_hint__() */ - hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj); + hintmeth = _PyObject_LookupSpecial(o, &PyId___length_hint__); if (hintmeth == NULL) { if (PyErr_Occurred()) return -1; @@ -237,7 +237,8 @@ PyObject_AsCharBuffer(PyObject *obj, pb = obj->ob_type->tp_as_buffer; if (pb == NULL || pb->bf_getbuffer == NULL) { PyErr_SetString(PyExc_TypeError, - "expected an object with the buffer interface"); + "expected bytes, bytearray " + "or buffer compatible object"); return -1; } if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1; @@ -331,7 +332,7 @@ PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) { if (!PyObject_CheckBuffer(obj)) { PyErr_Format(PyExc_TypeError, - "'%100s' does not support the buffer interface", + "'%.100s' does not support the buffer interface", Py_TYPE(obj)->tp_name); return -1; } @@ -696,16 +697,16 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) PyObject *meth; PyObject *empty = NULL; PyObject *result = NULL; - static PyObject *format_cache = NULL; + _Py_IDENTIFIER(__format__); /* If no format_spec is provided, use an empty string */ if (format_spec == NULL) { - empty = PyUnicode_FromUnicode(NULL, 0); + empty = PyUnicode_New(0, 0); format_spec = empty; } /* Find the (unbound!) __format__ method (a borrowed reference) */ - meth = _PyObject_LookupSpecial(obj, "__format__", &format_cache); + meth = _PyObject_LookupSpecial(obj, &PyId___format__); if (meth == NULL) { if (!PyErr_Occurred()) PyErr_Format(PyExc_TypeError, @@ -792,8 +793,7 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot) return x; Py_DECREF(x); /* can't do it */ } - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; + Py_RETURN_NOTIMPLEMENTED; } static PyObject * @@ -1379,9 +1379,7 @@ PyNumber_Long(PyObject *o) PyBytes_GET_SIZE(o)); if (PyUnicode_Check(o)) /* The above check is done in PyLong_FromUnicode(). */ - return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), - PyUnicode_GET_SIZE(o), - 10); + return PyLong_FromUnicodeObject(o, 10); if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) return long_from_string(buffer, buffer_len); @@ -1986,7 +1984,7 @@ PySequence_Index(PyObject *s, PyObject *o) int PyMapping_Check(PyObject *o) { - return o && o->ob_type->tp_as_mapping && + return o && o->ob_type->tp_as_mapping && o->ob_type->tp_as_mapping->mp_subscript; } @@ -2084,10 +2082,11 @@ PyMapping_Keys(PyObject *o) { PyObject *keys; PyObject *fast; + _Py_IDENTIFIER(keys); if (PyDict_CheckExact(o)) return PyDict_Keys(o); - keys = PyObject_CallMethod(o, "keys", NULL); + keys = _PyObject_CallMethodId(o, &PyId_keys, NULL); if (keys == NULL) return NULL; fast = PySequence_Fast(keys, "o.keys() are not iterable"); @@ -2100,10 +2099,11 @@ PyMapping_Items(PyObject *o) { PyObject *items; PyObject *fast; + _Py_IDENTIFIER(items); if (PyDict_CheckExact(o)) return PyDict_Items(o); - items = PyObject_CallMethod(o, "items", NULL); + items = _PyObject_CallMethodId(o, &PyId_items, NULL); if (items == NULL) return NULL; fast = PySequence_Fast(items, "o.items() are not iterable"); @@ -2116,10 +2116,11 @@ PyMapping_Values(PyObject *o) { PyObject *values; PyObject *fast; + _Py_IDENTIFIER(values); if (PyDict_CheckExact(o)) return PyDict_Values(o); - values = PyObject_CallMethod(o, "values", NULL); + values = _PyObject_CallMethodId(o, &PyId_values, NULL); if (values == NULL) return NULL; fast = PySequence_Fast(values, "o.values() are not iterable"); @@ -2225,22 +2226,11 @@ _PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...) return call_function_tail(callable, args); } -PyObject * -PyObject_CallMethod(PyObject *o, char *name, char *format, ...) +static PyObject* +callmethod(PyObject* func, char *format, va_list va, int is_size_t) { - va_list va; - PyObject *args; - PyObject *func = NULL; PyObject *retval = NULL; - - if (o == NULL || name == NULL) - return null_error(); - - func = PyObject_GetAttrString(o, name); - if (func == NULL) { - PyErr_SetString(PyExc_AttributeError, name); - return 0; - } + PyObject *args; if (!PyCallable_Check(func)) { type_error("attribute of type '%.200s' is not callable", func); @@ -2248,9 +2238,10 @@ PyObject_CallMethod(PyObject *o, char *name, char *format, ...) } if (format && *format) { - va_start(va, format); - args = Py_VaBuildValue(format, va); - va_end(va); + if (is_size_t) + args = _Py_VaBuildValue_SizeT(format, va); + else + args = Py_VaBuildValue(format, va); } else args = PyTuple_New(0); @@ -2265,10 +2256,9 @@ PyObject_CallMethod(PyObject *o, char *name, char *format, ...) } PyObject * -_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) +PyObject_CallMethod(PyObject *o, char *name, char *format, ...) { va_list va; - PyObject *args; PyObject *func = NULL; PyObject *retval = NULL; @@ -2277,32 +2267,75 @@ _PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) func = PyObject_GetAttrString(o, name); if (func == NULL) { - PyErr_SetString(PyExc_AttributeError, name); return 0; } - if (!PyCallable_Check(func)) { - type_error("attribute of type '%.200s' is not callable", func); - goto exit; - } + va_start(va, format); + retval = callmethod(func, format, va, 0); + va_end(va); + return retval; +} - if (format && *format) { - va_start(va, format); - args = _Py_VaBuildValue_SizeT(format, va); - va_end(va); +PyObject * +_PyObject_CallMethodId(PyObject *o, _Py_Identifier *name, char *format, ...) +{ + va_list va; + PyObject *func = NULL; + PyObject *retval = NULL; + + if (o == NULL || name == NULL) + return null_error(); + + func = _PyObject_GetAttrId(o, name); + if (func == NULL) { + return 0; } - else - args = PyTuple_New(0); - retval = call_function_tail(func, args); + va_start(va, format); + retval = callmethod(func, format, va, 0); + va_end(va); + return retval; +} - exit: - /* args gets consumed in call_function_tail */ - Py_XDECREF(func); +PyObject * +_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) +{ + va_list va; + PyObject *func = NULL; + PyObject *retval; + if (o == NULL || name == NULL) + return null_error(); + + func = PyObject_GetAttrString(o, name); + if (func == NULL) { + return 0; + } + va_start(va, format); + retval = callmethod(func, format, va, 1); + va_end(va); return retval; } +PyObject * +_PyObject_CallMethodId_SizeT(PyObject *o, _Py_Identifier *name, char *format, ...) +{ + va_list va; + PyObject *func = NULL; + PyObject *retval; + + if (o == NULL || name == NULL) + return null_error(); + + func = _PyObject_GetAttrId(o, name); + if (func == NULL) { + return NULL; + } + va_start(va, format); + retval = callmethod(func, format, va, 1); + va_end(va); + return retval; +} static PyObject * objargs_mktuple(va_list va) @@ -2538,7 +2571,7 @@ recursive_isinstance(PyObject *inst, PyObject *cls) int PyObject_IsInstance(PyObject *inst, PyObject *cls) { - static PyObject *name = NULL; + _Py_IDENTIFIER(__instancecheck__); PyObject *checker; /* Quick test for an exact match */ @@ -2564,7 +2597,7 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls) return r; } - checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name); + checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__); if (checker != NULL) { PyObject *res; int ok = -1; @@ -2607,7 +2640,7 @@ recursive_issubclass(PyObject *derived, PyObject *cls) int PyObject_IsSubclass(PyObject *derived, PyObject *cls) { - static PyObject *name = NULL; + _Py_IDENTIFIER(__subclasscheck__); PyObject *checker; if (PyTuple_Check(cls)) { @@ -2629,7 +2662,7 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls) return r; } - checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name); + checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__); if (checker != NULL) { PyObject *res; int ok = -1; |