From aeb4f05daa93e27516f621c183659d56b734cb42 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Fri, 29 Jul 2011 18:19:43 -0500 Subject: make the types of None and Ellipsis callable --- Objects/sliceobject.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'Objects/sliceobject.c') diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 51c53a8a11..53cc951a73 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -16,6 +16,17 @@ this type and there is exactly one in existence. #include "Python.h" #include "structmember.h" +static PyObject * +ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { + PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments"); + return NULL; + } + Py_INCREF(Py_Ellipsis); + return Py_Ellipsis; +} + static PyObject * ellipsis_repr(PyObject *op) { @@ -43,6 +54,24 @@ PyTypeObject PyEllipsis_Type = { 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ + 0, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + ellipsis_new, /* tp_new */ }; PyObject _Py_EllipsisObject = { -- cgit v1.2.1 From 174958263e0e4854bb560dd8609a76e797cc66f7 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Wed, 10 Aug 2011 20:28:54 -0500 Subject: Replace Py_NotImplemented returns with the macro form Py_RETURN_NOTIMPLEMENTED. The macro was introduced in #12724. --- Objects/sliceobject.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'Objects/sliceobject.c') diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 53cc951a73..92464abf7c 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -326,10 +326,8 @@ slice_richcompare(PyObject *v, PyObject *w, int op) PyObject *t2; PyObject *res; - if (!PySlice_Check(v) || !PySlice_Check(w)) { - Py_INCREF(Py_NotImplemented); - return Py_NotImplemented; - } + if (!PySlice_Check(v) || !PySlice_Check(w)) + Py_RETURN_NOTIMPLEMENTED; if (v == w) { /* XXX Do we really need this shortcut? -- cgit v1.2.1 From 20c31ce6ca51dcf5219497f8f4bb07110b9144a2 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 18 Nov 2011 20:14:34 +0100 Subject: Issue #10227: Add an allocation cache for a single slice object. Patch by Stefan Behnel. --- Objects/sliceobject.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'Objects/sliceobject.c') diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 2f5c045f36..c4a190755c 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -80,19 +80,38 @@ PyObject _Py_EllipsisObject = { }; -/* Slice object implementation +/* Slice object implementation */ - start, stop, and step are python objects with None indicating no +/* Using a cache is very effective since typically only a single slice is + * created and then deleted again + */ +static PySliceObject *slice_cache = NULL; +void PySlice_Fini(void) +{ + PySliceObject *obj = slice_cache; + if (obj != NULL) { + slice_cache = NULL; + PyObject_Del(obj); + } +} + +/* start, stop, and step are python objects with None indicating no index is present. */ PyObject * PySlice_New(PyObject *start, PyObject *stop, PyObject *step) { - PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type); - - if (obj == NULL) - return NULL; + PySliceObject *obj; + if (slice_cache != NULL) { + obj = slice_cache; + slice_cache = NULL; + _Py_NewReference((PyObject *)obj); + } else { + obj = PyObject_New(PySliceObject, &PySlice_Type); + if (obj == NULL) + return NULL; + } if (step == NULL) step = Py_None; Py_INCREF(step); @@ -260,7 +279,10 @@ slice_dealloc(PySliceObject *r) Py_DECREF(r->step); Py_DECREF(r->start); Py_DECREF(r->stop); - PyObject_Del(r); + if (slice_cache == NULL) + slice_cache = r; + else + PyObject_Del(r); } static PyObject * -- cgit v1.2.1