| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1676,12 +1676,15 @@ def iden(self): | |||
| 1676 | 1676 | return self | |
| 1677 | 1677 | def hello(self): | |
| 1678 | 1678 | return "hello" | |
| 1679 | + def empty_seq(self): | ||
| 1680 | + return [] | ||
| 1679 | 1681 | ||
| 1680 | 1682 | # It would be nice to have every special method tested here, but I'm | |
| 1681 | 1683 | # only listing the ones I can remember outside of typeobject.c, since it | |
| 1682 | 1684 | # does it right. | |
| 1683 | 1685 | specials = [ | |
| 1684 | 1686 | ("__unicode__", unicode, hello), | |
| 1687 | + ("__reversed__", reversed, empty_seq), | ||
| 1685 | 1688 | # These two fail because the compiler generates LOAD_ATTR to look | |
| 1686 | 1689 | # them up. We'd have to add a new opcode to fix this, and it's | |
| 1687 | 1690 | # probably not worth it. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -222,7 +222,8 @@ static PyObject * | |||
| 222 | 222 | reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| 223 | 223 | { | |
| 224 | 224 | Py_ssize_t n; | |
| 225 | - PyObject *seq; | ||
| 225 | + PyObject *seq, *reversed_meth; | ||
| 226 | + static PyObject *reversed_cache = NULL; | ||
| 226 | 227 | reversedobject *ro; | |
| 227 | 228 | ||
| 228 | 229 | if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds)) | |
@@ -231,8 +232,12 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||
| 231 | 232 | if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) ) | |
| 232 | 233 | return NULL; | |
| 233 | 234 | ||
| 234 | - if (PyObject_HasAttrString(seq, "__reversed__")) | ||
| 235 | - return PyObject_CallMethod(seq, "__reversed__", NULL); | ||
| 235 | + reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache); | ||
| 236 | + if (reversed_meth != NULL) { | ||
| 237 | + PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL); | ||
| 238 | + Py_DECREF(reversed_meth); | ||
| 239 | + return res; | ||
| 240 | + } | ||
| 236 | 241 | ||
| 237 | 242 | if (!PySequence_Check(seq)) { | |
| 238 | 243 | PyErr_SetString(PyExc_TypeError, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments