summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-05-09 16:36:39 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-05-09 16:36:39 (GMT)
commitaf1692a266d5c83d9367557f9642bbd618066318 (patch)
treeb2b3628e8868a81e6b1b23fb330c1887529c7214
parentf9b01fe692515eabf555bbc40b458b195f46a807 (diff)
downloadcpython-af1692a266d5c83d9367557f9642bbd618066318.zip
cpython-af1692a266d5c83d9367557f9642bbd618066318.tar.gz
cpython-af1692a266d5c83d9367557f9642bbd618066318.tar.bz2
convert some more special methods to use _PyObject_LookupSpecial
-rw-r--r--Lib/test/test_descr.py18
-rw-r--r--Objects/abstract.c21
-rw-r--r--Python/sysmodule.c19
3 files changed, 30 insertions, 28 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index fe4eaea..e66b550 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1,4 +1,5 @@
import __builtin__
+import sys
import types
import unittest
import warnings
@@ -1678,13 +1679,20 @@ order (MRO) for bases """
return "hello"
def empty_seq(self):
return []
+ def zero(self):
+ return 0
+ def stop(self):
+ raise StopIteration
# It would be nice to have every special method tested here, but I'm
# only listing the ones I can remember outside of typeobject.c, since it
# does it right.
specials = [
- ("__unicode__", unicode, hello),
- ("__reversed__", reversed, empty_seq),
+ ("__unicode__", unicode, hello, {}),
+ ("__reversed__", reversed, empty_seq, {}),
+ ("__length_hint__", list, zero,
+ {"__iter__" : iden, "next" : stop}),
+ ("__sizeof__", sys.getsizeof, zero, {}),
# These two fail because the compiler generates LOAD_ATTR to look
# them up. We'd have to add a new opcode to fix this, and it's
# probably not worth it.
@@ -1705,15 +1713,19 @@ order (MRO) for bases """
return self.impl.__get__(obj, owner)
- for name, runner, meth_impl in specials:
+ for name, runner, meth_impl, env in specials:
class X(Checker):
pass
+ for attr, obj in env.iteritems():
+ setattr(X, attr, obj)
setattr(X, name, meth_impl)
runner(X())
record = []
class X(Checker):
pass
+ for attr, obj in env.iteritems():
+ setattr(X, attr, obj)
setattr(X, name, SpecialDescr(meth_impl))
runner(X())
self.assertEqual(record, [1], name)
diff --git a/Objects/abstract.c b/Objects/abstract.c
index cd14386..c2d8db7 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -93,7 +93,7 @@ Py_ssize_t
_PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
{
static PyObject *hintstrobj = NULL;
- PyObject *ro;
+ PyObject *ro, *hintmeth;
Py_ssize_t rv;
/* try o.__len__() */
@@ -107,20 +107,15 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
PyErr_Clear();
}
- /* cache a hashed version of the attribute string */
- if (hintstrobj == NULL) {
- hintstrobj = PyString_InternFromString("__length_hint__");
- if (hintstrobj == NULL)
- return -1;
- }
-
/* try o.__length_hint__() */
- ro = PyObject_CallMethodObjArgs(o, hintstrobj, NULL);
+ hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj);
+ if (hintmeth == NULL)
+ return defaultvalue;
+ ro = PyObject_CallFunctionObjArgs(hintmeth, NULL);
+ Py_DECREF(hintmeth);
if (ro == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
- !PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_Clear();
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
+ return -1;
return defaultvalue;
}
rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 4e52282..6c22b8f 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -643,7 +643,7 @@ static PyObject *
sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *res = NULL;
- static PyObject *str__sizeof__, *gc_head_size = NULL;
+ static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
static char *kwlist[] = {"object", "default", 0};
PyObject *o, *dflt = NULL;
@@ -651,13 +651,6 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
kwlist, &o, &dflt))
return NULL;
- /* Initialize static variable needed by _PyType_Lookup */
- if (str__sizeof__ == NULL) {
- str__sizeof__ = PyString_InternFromString("__sizeof__");
- if (str__sizeof__ == NULL)
- return NULL;
- }
-
/* Initialize static variable for GC head size */
if (gc_head_size == NULL) {
gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head));
@@ -674,14 +667,16 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
res = PyInt_FromSsize_t(PyInstance_Type.tp_basicsize);
/* all other objects */
else {
- PyObject *method = _PyType_Lookup(Py_TYPE(o),
- str__sizeof__);
+ PyObject *method = _PyObject_LookupSpecial(o, "__sizeof__",
+ &str__sizeof__);
if (method == NULL)
PyErr_Format(PyExc_TypeError,
"Type %.100s doesn't define __sizeof__",
Py_TYPE(o)->tp_name);
- else
- res = PyObject_CallFunctionObjArgs(method, o, NULL);
+ else {
+ res = PyObject_CallFunctionObjArgs(method, NULL);
+ Py_DECREF(method);
+ }
}
/* Has a default value been given? */