diff options
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 30 | ||||
-rw-r--r-- | Objects/classobject.c | 13 | ||||
-rw-r--r-- | Objects/complexobject.c | 47 | ||||
-rw-r--r-- | Objects/descrobject.c | 2 | ||||
-rw-r--r-- | Objects/frameobject.c | 14 | ||||
-rw-r--r-- | Objects/methodobject.c | 13 | ||||
-rw-r--r-- | Objects/tupleobject.c | 25 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 33 |
8 files changed, 139 insertions, 38 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index b50b43e7e69..bb6c301074f 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1405,13 +1405,19 @@ PyNumber_Float(PyObject *o) PyObject * PyNumber_ToBase(PyObject *n, int base) { - PyObject *res; + PyObject *res = NULL; PyObject *index = PyNumber_Index(n); if (!index) return NULL; - assert(PyLong_Check(index)); - res = _PyLong_Format(index, base); + if (PyLong_Check(index)) + res = _PyLong_Format(index, base); + else + /* It should not be possible to get here, as + PyNumber_Index already has a check for the same + condition */ + PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not " + "int or long"); Py_DECREF(index); return res; } @@ -2540,10 +2546,17 @@ recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth) int PyObject_IsInstance(PyObject *inst, PyObject *cls) { + static PyObject *name = NULL; PyObject *t, *v, *tb; PyObject *checker; PyErr_Fetch(&t, &v, &tb); - checker = PyObject_GetAttrString(cls, "__instancecheck__"); + + if (name == NULL) { + name = PyUnicode_InternFromString("__instancecheck__"); + if (name == NULL) + return -1; + } + checker = PyObject_GetAttr(cls, name); PyErr_Restore(t, v, tb); if (checker != NULL) { PyObject *res; @@ -2611,10 +2624,17 @@ recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth) int PyObject_IsSubclass(PyObject *derived, PyObject *cls) { + static PyObject *name = NULL; PyObject *t, *v, *tb; PyObject *checker; PyErr_Fetch(&t, &v, &tb); - checker = PyObject_GetAttrString(cls, "__subclasscheck__"); + + if (name == NULL) { + name = PyUnicode_InternFromString("__subclasscheck__"); + if (name == NULL) + return -1; + } + checker = PyObject_GetAttr(cls, name); PyErr_Restore(t, v, tb); if (checker != NULL) { PyObject *res; diff --git a/Objects/classobject.c b/Objects/classobject.c index be7ba2dbeb6..0e131eb1c1b 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -382,9 +382,11 @@ PyTypeObject PyMethod_Type = { /* Clear out the free list */ -void -PyMethod_Fini(void) +int +PyMethod_ClearFreeList(void) { + int freelist_size = numfree; + while (free_list) { PyMethodObject *im = free_list; free_list = (PyMethodObject *)(im->im_self); @@ -392,6 +394,13 @@ PyMethod_Fini(void) numfree--; } assert(numfree == 0); + return freelist_size; +} + +void +PyMethod_Fini(void) +{ + (void)PyMethod_ClearFreeList(); } /* ------------------------------------------------------------------------ diff --git a/Objects/complexobject.c b/Objects/complexobject.c index a08253e0dc5..a47cd549c55 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -314,16 +314,49 @@ complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision) { char format[32]; if (v->cval.real == 0.) { - PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); - PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag); - strncat(buf, "j", 1); + if (!Py_IS_FINITE(v->cval.imag)) { + if (Py_IS_NAN(v->cval.imag)) + strncpy(buf, "nan*j", 6); + /* else if (copysign(1, v->cval.imag) == 1) */ + else if (v->cval.imag > 0) + strncpy(buf, "inf*j", 6); + else + strncpy(buf, "-inf*j", 7); + } + else { + PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); + PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag); + strncat(buf, "j", 1); + } } else { char re[64], im[64]; /* Format imaginary part with sign, real part without */ - PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); - PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real); - PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision); - PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag); + if (!Py_IS_FINITE(v->cval.real)) { + if (Py_IS_NAN(v->cval.real)) + strncpy(re, "nan", 4); + /* else if (copysign(1, v->cval.real) == 1) */ + else if (v->cval.real > 0) + strncpy(re, "inf", 4); + else + strncpy(re, "-inf", 5); + } + else { + PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); + PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real); + } + if (!Py_IS_FINITE(v->cval.imag)) { + if (Py_IS_NAN(v->cval.imag)) + strncpy(im, "+nan*", 6); + /* else if (copysign(1, v->cval.imag) == 1) */ + else if (v->cval.imag > 0) + strncpy(im, "+inf*", 6); + else + strncpy(im, "-inf*", 6); + } + else { + PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision); + PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag); + } PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im); } } diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 0926817b78a..17bdf5cdb74 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -168,7 +168,7 @@ descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value, int *pres) { assert(obj != NULL); - if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) { + if (!PyObject_TypeCheck(obj, descr->d_type)) { PyErr_Format(PyExc_TypeError, "descriptor '%V' for '%.100s' objects " "doesn't apply to '%.100s' object", diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 658ce1da524..7815f928a42 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -897,10 +897,11 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear) } /* Clear out the free list */ - -void -PyFrame_Fini(void) +int +PyFrame_ClearFreeList(void) { + int freelist_size = numfree; + while (free_list != NULL) { PyFrameObject *f = free_list; free_list = free_list->f_back; @@ -908,6 +909,13 @@ PyFrame_Fini(void) --numfree; } assert(numfree == 0); + return freelist_size; +} + +void +PyFrame_Fini(void) +{ + (void)PyFrame_ClearFreeList(); Py_XDECREF(builtin_object); builtin_object = NULL; } diff --git a/Objects/methodobject.c b/Objects/methodobject.c index 7a82d8942af..2c1db739b6a 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -319,9 +319,11 @@ Py_FindMethod(PyMethodDef *methods, PyObject *self, const char *name) /* Clear out the free list */ -void -PyCFunction_Fini(void) +int +PyCFunction_ClearFreeList(void) { + int freelist_size = numfree; + while (free_list) { PyCFunctionObject *v = free_list; free_list = (PyCFunctionObject *)(v->m_self); @@ -329,6 +331,13 @@ PyCFunction_Fini(void) numfree--; } assert(numfree == 0); + return freelist_size; +} + +void +PyCFunction_Fini(void) +{ + (void)PyCFunction_ClearFreeList(); } /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(), diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index df69db929a0..9a53cfa32f9 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -807,19 +807,18 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) return 0; } -void -PyTuple_Fini(void) +int +PyTuple_ClearFreeList(void) { + int freelist_size = 0; #if PyTuple_MAXSAVESIZE > 0 int i; - - Py_XDECREF(free_list[0]); - free_list[0] = NULL; - for (i = 1; i < PyTuple_MAXSAVESIZE; i++) { PyTupleObject *p, *q; p = free_list[i]; + freelist_size += numfree[i]; free_list[i] = NULL; + numfree[i] = 0; while (p) { q = p; p = (PyTupleObject *)(p->ob_item[0]); @@ -827,6 +826,20 @@ PyTuple_Fini(void) } } #endif + return freelist_size; +} + +void +PyTuple_Fini(void) +{ +#if PyTuple_MAXSAVESIZE > 0 + /* empty tuples are used all over the place and applications may + * rely on the fact that an empty tuple is a singleton. */ + Py_XDECREF(free_list[0]); + free_list[0] = NULL; + + (void)PyTuple_ClearFreeList(); +#endif } /*********************** Tuple Iterator **************************/ diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 4f0de1e24a4..86d8b547bfd 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9111,10 +9111,29 @@ void _PyUnicode_Init(void) /* Finalize the Unicode implementation */ +int +PyUnicode_ClearFreeList(void) +{ + int freelist_size = numfree; + PyUnicodeObject *u; + + for (u = free_list; u != NULL;) { + PyUnicodeObject *v = u; + u = *(PyUnicodeObject **)u; + if (v->str) + PyMem_DEL(v->str); + Py_XDECREF(v->defenc); + PyObject_Del(v); + numfree--; + } + free_list = NULL; + assert(numfree == 0); + return freelist_size; +} + void _PyUnicode_Fini(void) { - PyUnicodeObject *u; int i; Py_XDECREF(unicode_empty); @@ -9126,17 +9145,7 @@ _PyUnicode_Fini(void) unicode_latin1[i] = NULL; } } - - for (u = free_list; u != NULL;) { - PyUnicodeObject *v = u; - u = *(PyUnicodeObject **)u; - if (v->str) - PyMem_DEL(v->str); - Py_XDECREF(v->defenc); - PyObject_Del(v); - } - free_list = NULL; - numfree = 0; + (void)PyUnicode_ClearFreeList(); } void |