diff options
author | Guido van Rossum <guido@python.org> | 2002-06-14 20:41:17 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-06-14 20:41:17 (GMT) |
commit | bea18ccde6bc12e061c21bb6b944379d8b123845 (patch) | |
tree | d5366d2bba31bde0cf6d05e1b55cde64cf3d3864 /Objects | |
parent | 57454e57f83b407dd2653cbfcead7c9801beeff0 (diff) | |
download | cpython-bea18ccde6bc12e061c21bb6b944379d8b123845.zip cpython-bea18ccde6bc12e061c21bb6b944379d8b123845.tar.gz cpython-bea18ccde6bc12e061c21bb6b944379d8b123845.tar.bz2 |
SF patch 568629 by Oren Tirosh: types made callable.
These built-in functions are replaced by their (now callable) type:
slice()
buffer()
and these types can also be called (but have no built-in named
function named after them)
classobj (type name used to be "class")
code
function
instance
instancemethod (type name used to be "instance method")
The module "new" has been replaced with a small backward compatibility
placeholder in Python.
A large portion of the patch simply removes the new module from
various platform-specific build recipes. The following binary Mac
project files still have references to it:
Mac/Build/PythonCore.mcp
Mac/Build/PythonStandSmall.mcp
Mac/Build/PythonStandalone.mcp
[I've tweaked the code layout and the doc strings here and there, and
added a comment to types.py about StringTypes vs. basestring. --Guido]
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/bufferobject.c | 40 | ||||
-rw-r--r-- | Objects/classobject.c | 98 | ||||
-rw-r--r-- | Objects/descrobject.c | 2 | ||||
-rw-r--r-- | Objects/funcobject.c | 51 | ||||
-rw-r--r-- | Objects/sliceobject.c | 32 |
5 files changed, 214 insertions, 9 deletions
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c index 51b9ef9..031c000 100644 --- a/Objects/bufferobject.c +++ b/Objects/bufferobject.c @@ -155,6 +155,27 @@ PyBuffer_New(int size) /* Methods */ +static PyObject * +buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) +{ + PyObject *ob; + int offset = 0; + int size = Py_END_OF_BUFFER; + + if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) ) + return NULL; + return PyBuffer_FromObject(ob, offset, size); +} + +PyDoc_STRVAR(buffer_doc, +"buffer(object [, offset[, size]])\n\ +\n\ +Create a new buffer object which references the given object.\n\ +The buffer will reference a slice of the target object from the\n\ +start of the object (or at the specified offset). The slice will\n\ +extend to the end of the target object (or with the specified size)."); + + static void buffer_dealloc(PyBufferObject *self) { @@ -539,5 +560,22 @@ PyTypeObject PyBuffer_Type = { 0, /* tp_setattro */ &buffer_as_buffer, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ - 0, /* tp_doc */ + buffer_doc, /* 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 */ + buffer_new, /* tp_new */ }; diff --git a/Objects/classobject.c b/Objects/classobject.c index 1033ab6..979c967 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -149,6 +149,27 @@ PyMethod_Class(PyObject *im) return ((PyMethodObject *)im)->im_class; } +PyDoc_STRVAR(class_doc, +"classobj(name, bases, dict)\n\ +\n\ +Create a class object. The name must be a string; the second argument\n\ +a tuple of classes, and the third a dictionary."); + +static PyObject * +new_class(PyObject* unused, PyObject* args) +{ + PyObject *name; + PyObject *classes; + PyObject *dict; + + if (!PyArg_ParseTuple(args, "SO!O!:class", + &name, + &PyTuple_Type, &classes, + &PyDict_Type, &dict)) + return NULL; + return PyClass_New(classes, dict, name); +} + static PyObject * class_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -435,7 +456,7 @@ class_traverse(PyClassObject *o, visitproc visit, void *arg) PyTypeObject PyClass_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, - "class", + "classobj", sizeof(PyClassObject), 0, (destructor)class_dealloc, /* tp_dealloc */ @@ -454,7 +475,7 @@ PyTypeObject PyClass_Type = { (setattrofunc)class_setattr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ - 0, /* tp_doc */ + class_doc, /* tp_doc */ (traverseproc)class_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -575,6 +596,34 @@ PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw) /* Instance methods */ +PyDoc_STRVAR(instance_doc, +"instance(class[, dict])\n\ +\n\ +Create an instance without calling its __init__() method.\n\ +The class must be a classic class.\n\ +If present, dict must be a dictionary or None."); + +static PyObject * +instance_new(PyTypeObject* type, PyObject* args, PyObject *kw) +{ + PyObject *klass; + PyObject *dict = Py_None; + + if (!PyArg_ParseTuple(args, "O!|O:instance", + &PyClass_Type, &klass, &dict)) + return NULL; + + if (dict == Py_None) + dict = NULL; + else if (!PyDict_Check(dict)) { + PyErr_SetString(PyExc_TypeError, + "instance() second arg must be dictionary or None"); + return NULL; + } + return PyInstance_NewRaw(klass, dict); +} + + static void instance_dealloc(register PyInstanceObject *inst) { @@ -2014,13 +2063,24 @@ PyTypeObject PyInstance_Type = { (setattrofunc)instance_setattr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/ - 0, /* tp_doc */ + instance_doc, /* tp_doc */ (traverseproc)instance_traverse, /* tp_traverse */ 0, /* tp_clear */ instance_richcompare, /* tp_richcompare */ offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ (getiterfunc)instance_getiter, /* tp_iter */ (iternextfunc)instance_iternext, /* 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 */ + instance_new, /* tp_new */ }; @@ -2125,6 +2185,31 @@ instancemethod_getattro(PyObject *obj, PyObject *name) return NULL; } +PyDoc_STRVAR(instancemethod_doc, +"instancemethod(function, instance, class)\n\ +\n\ +Create an instance method object."); + +static PyObject * +instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) +{ + PyObject *func; + PyObject *self; + PyObject *classObj; + + if (!PyArg_ParseTuple(args, "OOO:instancemethod", + &func, &self, &classObj)) + return NULL; + if (!PyCallable_Check(func)) { + PyErr_SetString(PyExc_TypeError, + "first argument must be callable"); + return NULL; + } + if (self == Py_None) + self = NULL; + return PyMethod_New(func, self, classObj); +} + static void instancemethod_dealloc(register PyMethodObject *im) { @@ -2362,7 +2447,7 @@ instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *class) PyTypeObject PyMethod_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, - "instance method", + "instancemethod", sizeof(PyMethodObject), 0, (destructor)instancemethod_dealloc, /* tp_dealloc */ @@ -2381,7 +2466,7 @@ PyTypeObject PyMethod_Type = { PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ - 0, /* tp_doc */ + instancemethod_doc, /* tp_doc */ (traverseproc)instancemethod_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -2396,6 +2481,9 @@ PyTypeObject PyMethod_Type = { instancemethod_descr_get, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + instancemethod_new, /* tp_new */ }; /* Clear out the free list */ diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 1130f86..2d926b4 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -725,7 +725,7 @@ proxy_traverse(PyObject *self, visitproc visit, void *arg) static PyTypeObject proxytype = { PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ - "dict-proxy", /* tp_name */ + "dictproxy", /* tp_name */ sizeof(proxyobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ diff --git a/Objects/funcobject.c b/Objects/funcobject.c index f96d0dd..4eac035 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -266,6 +266,52 @@ static PyGetSetDef func_getsetlist[] = { {NULL} /* Sentinel */ }; +PyDoc_STRVAR(func_doc, +"function(code, globals[, name[, argdefs]])\n\ +\n\ +Create a function object from a code object and a dictionary.\n\ +The optional name string overrides the name from the code object.\n\ +The optional argdefs tuple specifies the default argument values."); + +static PyObject * +func_new(PyTypeObject* type, PyObject* args, PyObject* kw) +{ + PyObject *code; + PyObject *globals; + PyObject *name = Py_None; + PyObject *defaults = Py_None; + PyFunctionObject *newfunc; + + if (!PyArg_ParseTuple(args, "O!O!|OO!:function", + &PyCode_Type, &code, + &PyDict_Type, &globals, + &name, + &PyTuple_Type, &defaults)) + return NULL; + if (name != Py_None && !PyString_Check(name)) { + PyErr_SetString(PyExc_TypeError, + "arg 3 (name) must be None or string"); + return NULL; + } + + newfunc = (PyFunctionObject *)PyFunction_New(code, globals); + if (newfunc == NULL) + return NULL; + + if (name != Py_None) { + Py_XINCREF(name); + Py_XDECREF(newfunc->func_name); + newfunc->func_name = name; + } + if (defaults != Py_None) { + Py_XINCREF(defaults); + Py_XDECREF(newfunc->func_defaults); + newfunc->func_defaults = defaults; + } + + return (PyObject *)newfunc; +} + static void func_dealloc(PyFunctionObject *op) { @@ -415,7 +461,7 @@ PyTypeObject PyFunction_Type = { PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ - 0, /* tp_doc */ + func_doc, /* tp_doc */ (traverseproc)func_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -430,6 +476,9 @@ PyTypeObject PyFunction_Type = { func_descr_get, /* tp_descr_get */ 0, /* tp_descr_set */ offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + func_new, /* tp_new */ }; diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 9a268b7..a43644d 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -164,6 +164,30 @@ PySlice_GetIndicesEx(PySliceObject *r, int length, return 0; } +static PyObject * +slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) +{ + PyObject *start, *stop, *step; + + start = stop = step = NULL; + + if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step)) + return NULL; + + /* This swapping of stop and start is to maintain similarity with + range(). */ + if (stop == NULL) { + stop = start; + start = NULL; + } + return PySlice_New(start, stop, step); +} + +PyDoc_STRVAR(slice_doc, +"slice([start,] stop[, step])\n\ +\n\ +Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); + static void slice_dealloc(PySliceObject *r) { @@ -240,7 +264,7 @@ PyTypeObject PySlice_Type = { 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ - 0, /* tp_doc */ + slice_doc, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -252,4 +276,10 @@ PyTypeObject PySlice_Type = { 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 */ + slice_new, /* tp_new */ }; |