Skip to content

Commit bea18cc

Browse files
committed
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]
1 parent 57454e5 commit bea18cc

17 files changed

Lines changed: 287 additions & 130 deletions

File tree

‎Lib/types.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
pass
2424

2525
StringType = str
26+
27+
# StringTypes is already outdated. Instead of writing "type(x) in
28+
# types.StringTypes", you should use "isinstance(x, basestring)". But
29+
# we keep around for compatibility with Python 2.2.
2630
try:
2731
UnicodeType = unicode
2832
StringTypes = (StringType, UnicodeType)
2933
except NameError:
3034
StringTypes = (StringType,)
3135

32-
BufferType = type(buffer(''))
36+
BufferType = buffer
3337

3438
TupleType = tuple
3539
ListType = list
@@ -77,7 +81,7 @@ def _m(self): pass
7781
pass
7882
tb = None; del tb
7983

80-
SliceType = type(slice(0))
84+
SliceType = slice
8185
EllipsisType = type(Ellipsis)
8286

8387
DictProxyType = type(TypeType.__dict__)

‎Mac/Distributions/dev.include‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@
501501
(':Modules:md5module.c', None)
502502
(':Modules:mmapmodule.c', None)
503503
(':Modules:mpzmodule.c', None)
504-
(':Modules:newmodule.c', None)
505504
(':Modules:nismodule.c', None)
506505
(':Modules:operator.c', None)
507506
(':Modules:parsermodule.c', None)

‎Mac/MPW/Makefile‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ ALL =
7777
"{Objs}"moduleobject.c.o ¶
7878
"{Objs}"myreadline.c.o ¶
7979
"{Objs}"mystrtoul.c.o ¶
80-
"{Objs}"newmodule.c.o ¶
8180
"{Objs}"nfullpath.c.o ¶
8281
"{Objs}"node.c.o ¶
8382
"{Objs}"object.c.o ¶
@@ -473,9 +472,6 @@ xxmodule.slb
473472
"{Objs}"mathmodule.c.o Ä "{Top}"Modules:mathmodule.c
474473
{CC} "{Top}"Modules:mathmodule.c -o "{Objs}"mathmodule.c.o -s mathmodule.c {CFlags}
475474

476-
"{Objs}"newmodule.c.o Ä "{Top}"Modules:newmodule.c
477-
{CC} "{Top}"Modules:newmodule.c -o "{Objs}"newmodule.c.o -s newmodule.c {CFlags}
478-
479475
"{Objs}"parsermodule.c.o Ä "{Top}"Modules:parsermodule.c
480476
{CC} "{Top}"Modules:parsermodule.c -o "{Objs}"parsermodule.c.o -s parsermodule.c {CFlags}
481477

‎Misc/BeOS-setup.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ def detect_modules(self):
232232
# (NIST's Secure Hash Algorithm.)
233233
exts.append( Extension('sha', ['shamodule.c']) )
234234

235-
# Tommy Burnette's 'new' module (creates new empty objects of certain
236-
# kinds):
237-
exts.append( Extension('new', ['newmodule.c']) )
238-
239235
# Helper module for various ascii-encoders
240236
exts.append( Extension('binascii', ['binascii.c']) )
241237

‎Modules/Setup.dist‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ PYTHONPATH=$(COREPYTHONPATH)
110110

111111
posix posixmodule.c # posix (UNIX) system calls
112112
_sre _sre.c # Fredrik Lundh's new regular expressions
113-
new newmodule.c # Tommy Burnette's 'new' module
114113

115114
# The rest of the modules listed in this file are all commented out by
116115
# default. Usually they can be detected and built as dynamically

‎Objects/bufferobject.c‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,27 @@ PyBuffer_New(int size)
155155

156156
/* Methods */
157157

158+
static PyObject *
159+
buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
160+
{
161+
PyObject *ob;
162+
int offset = 0;
163+
int size = Py_END_OF_BUFFER;
164+
165+
if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
166+
return NULL;
167+
return PyBuffer_FromObject(ob, offset, size);
168+
}
169+
170+
PyDoc_STRVAR(buffer_doc,
171+
"buffer(object [, offset[, size]])\n\
172+
\n\
173+
Create a new buffer object which references the given object.\n\
174+
The buffer will reference a slice of the target object from the\n\
175+
start of the object (or at the specified offset). The slice will\n\
176+
extend to the end of the target object (or with the specified size).");
177+
178+
158179
static void
159180
buffer_dealloc(PyBufferObject *self)
160181
{
@@ -539,5 +560,22 @@ PyTypeObject PyBuffer_Type = {
539560
0, /* tp_setattro */
540561
&buffer_as_buffer, /* tp_as_buffer */
541562
Py_TPFLAGS_DEFAULT, /* tp_flags */
542-
0, /* tp_doc */
563+
buffer_doc, /* tp_doc */
564+
0, /* tp_traverse */
565+
0, /* tp_clear */
566+
0, /* tp_richcompare */
567+
0, /* tp_weaklistoffset */
568+
0, /* tp_iter */
569+
0, /* tp_iternext */
570+
0, /* tp_methods */
571+
0, /* tp_members */
572+
0, /* tp_getset */
573+
0, /* tp_base */
574+
0, /* tp_dict */
575+
0, /* tp_descr_get */
576+
0, /* tp_descr_set */
577+
0, /* tp_dictoffset */
578+
0, /* tp_init */
579+
0, /* tp_alloc */
580+
buffer_new, /* tp_new */
543581
};

‎Objects/classobject.c‎

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,27 @@ PyMethod_Class(PyObject *im)
149149
return ((PyMethodObject *)im)->im_class;
150150
}
151151

152+
PyDoc_STRVAR(class_doc,
153+
"classobj(name, bases, dict)\n\
154+
\n\
155+
Create a class object. The name must be a string; the second argument\n\
156+
a tuple of classes, and the third a dictionary.");
157+
158+
static PyObject *
159+
new_class(PyObject* unused, PyObject* args)
160+
{
161+
PyObject *name;
162+
PyObject *classes;
163+
PyObject *dict;
164+
165+
if (!PyArg_ParseTuple(args, "SO!O!:class",
166+
&name,
167+
&PyTuple_Type, &classes,
168+
&PyDict_Type, &dict))
169+
return NULL;
170+
return PyClass_New(classes, dict, name);
171+
}
172+
152173
static PyObject *
153174
class_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
154175
{
@@ -435,7 +456,7 @@ class_traverse(PyClassObject *o, visitproc visit, void *arg)
435456
PyTypeObject PyClass_Type = {
436457
PyObject_HEAD_INIT(&PyType_Type)
437458
0,
438-
"class",
459+
"classobj",
439460
sizeof(PyClassObject),
440461
0,
441462
(destructor)class_dealloc, /* tp_dealloc */
@@ -454,7 +475,7 @@ PyTypeObject PyClass_Type = {
454475
(setattrofunc)class_setattr, /* tp_setattro */
455476
0, /* tp_as_buffer */
456477
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
457-
0, /* tp_doc */
478+
class_doc, /* tp_doc */
458479
(traverseproc)class_traverse, /* tp_traverse */
459480
0, /* tp_clear */
460481
0, /* tp_richcompare */
@@ -575,6 +596,34 @@ PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
575596

576597
/* Instance methods */
577598

599+
PyDoc_STRVAR(instance_doc,
600+
"instance(class[, dict])\n\
601+
\n\
602+
Create an instance without calling its __init__() method.\n\
603+
The class must be a classic class.\n\
604+
If present, dict must be a dictionary or None.");
605+
606+
static PyObject *
607+
instance_new(PyTypeObject* type, PyObject* args, PyObject *kw)
608+
{
609+
PyObject *klass;
610+
PyObject *dict = Py_None;
611+
612+
if (!PyArg_ParseTuple(args, "O!|O:instance",
613+
&PyClass_Type, &klass, &dict))
614+
return NULL;
615+
616+
if (dict == Py_None)
617+
dict = NULL;
618+
else if (!PyDict_Check(dict)) {
619+
PyErr_SetString(PyExc_TypeError,
620+
"instance() second arg must be dictionary or None");
621+
return NULL;
622+
}
623+
return PyInstance_NewRaw(klass, dict);
624+
}
625+
626+
578627
static void
579628
instance_dealloc(register PyInstanceObject *inst)
580629
{
@@ -2014,13 +2063,24 @@ PyTypeObject PyInstance_Type = {
20142063
(setattrofunc)instance_setattr, /* tp_setattro */
20152064
0, /* tp_as_buffer */
20162065
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/
2017-
0, /* tp_doc */
2066+
instance_doc, /* tp_doc */
20182067
(traverseproc)instance_traverse, /* tp_traverse */
20192068
0, /* tp_clear */
20202069
instance_richcompare, /* tp_richcompare */
20212070
offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
20222071
(getiterfunc)instance_getiter, /* tp_iter */
20232072
(iternextfunc)instance_iternext, /* tp_iternext */
2073+
0, /* tp_methods */
2074+
0, /* tp_members */
2075+
0, /* tp_getset */
2076+
0, /* tp_base */
2077+
0, /* tp_dict */
2078+
0, /* tp_descr_get */
2079+
0, /* tp_descr_set */
2080+
0, /* tp_dictoffset */
2081+
0, /* tp_init */
2082+
0, /* tp_alloc */
2083+
instance_new, /* tp_new */
20242084
};
20252085

20262086

@@ -2125,6 +2185,31 @@ instancemethod_getattro(PyObject *obj, PyObject *name)
21252185
return NULL;
21262186
}
21272187

2188+
PyDoc_STRVAR(instancemethod_doc,
2189+
"instancemethod(function, instance, class)\n\
2190+
\n\
2191+
Create an instance method object.");
2192+
2193+
static PyObject *
2194+
instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
2195+
{
2196+
PyObject *func;
2197+
PyObject *self;
2198+
PyObject *classObj;
2199+
2200+
if (!PyArg_ParseTuple(args, "OOO:instancemethod",
2201+
&func, &self, &classObj))
2202+
return NULL;
2203+
if (!PyCallable_Check(func)) {
2204+
PyErr_SetString(PyExc_TypeError,
2205+
"first argument must be callable");
2206+
return NULL;
2207+
}
2208+
if (self == Py_None)
2209+
self = NULL;
2210+
return PyMethod_New(func, self, classObj);
2211+
}
2212+
21282213
static void
21292214
instancemethod_dealloc(register PyMethodObject *im)
21302215
{
@@ -2362,7 +2447,7 @@ instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *class)
23622447
PyTypeObject PyMethod_Type = {
23632448
PyObject_HEAD_INIT(&PyType_Type)
23642449
0,
2365-
"instance method",
2450+
"instancemethod",
23662451
sizeof(PyMethodObject),
23672452
0,
23682453
(destructor)instancemethod_dealloc, /* tp_dealloc */
@@ -2381,7 +2466,7 @@ PyTypeObject PyMethod_Type = {
23812466
PyObject_GenericSetAttr, /* tp_setattro */
23822467
0, /* tp_as_buffer */
23832468
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2384-
0, /* tp_doc */
2469+
instancemethod_doc, /* tp_doc */
23852470
(traverseproc)instancemethod_traverse, /* tp_traverse */
23862471
0, /* tp_clear */
23872472
0, /* tp_richcompare */
@@ -2396,6 +2481,9 @@ PyTypeObject PyMethod_Type = {
23962481
instancemethod_descr_get, /* tp_descr_get */
23972482
0, /* tp_descr_set */
23982483
0, /* tp_dictoffset */
2484+
0, /* tp_init */
2485+
0, /* tp_alloc */
2486+
instancemethod_new, /* tp_new */
23992487
};
24002488

24012489
/* Clear out the free list */

‎Objects/descrobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ proxy_traverse(PyObject *self, visitproc visit, void *arg)
725725
static PyTypeObject proxytype = {
726726
PyObject_HEAD_INIT(&PyType_Type)
727727
0, /* ob_size */
728-
"dict-proxy", /* tp_name */
728+
"dictproxy", /* tp_name */
729729
sizeof(proxyobject), /* tp_basicsize */
730730
0, /* tp_itemsize */
731731
/* methods */

‎Objects/funcobject.c‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,52 @@ static PyGetSetDef func_getsetlist[] = {
266266
{NULL} /* Sentinel */
267267
};
268268

269+
PyDoc_STRVAR(func_doc,
270+
"function(code, globals[, name[, argdefs]])\n\
271+
\n\
272+
Create a function object from a code object and a dictionary.\n\
273+
The optional name string overrides the name from the code object.\n\
274+
The optional argdefs tuple specifies the default argument values.");
275+
276+
static PyObject *
277+
func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
278+
{
279+
PyObject *code;
280+
PyObject *globals;
281+
PyObject *name = Py_None;
282+
PyObject *defaults = Py_None;
283+
PyFunctionObject *newfunc;
284+
285+
if (!PyArg_ParseTuple(args, "O!O!|OO!:function",
286+
&PyCode_Type, &code,
287+
&PyDict_Type, &globals,
288+
&name,
289+
&PyTuple_Type, &defaults))
290+
return NULL;
291+
if (name != Py_None && !PyString_Check(name)) {
292+
PyErr_SetString(PyExc_TypeError,
293+
"arg 3 (name) must be None or string");
294+
return NULL;
295+
}
296+
297+
newfunc = (PyFunctionObject *)PyFunction_New(code, globals);
298+
if (newfunc == NULL)
299+
return NULL;
300+
301+
if (name != Py_None) {
302+
Py_XINCREF(name);
303+
Py_XDECREF(newfunc->func_name);
304+
newfunc->func_name = name;
305+
}
306+
if (defaults != Py_None) {
307+
Py_XINCREF(defaults);
308+
Py_XDECREF(newfunc->func_defaults);
309+
newfunc->func_defaults = defaults;
310+
}
311+
312+
return (PyObject *)newfunc;
313+
}
314+
269315
static void
270316
func_dealloc(PyFunctionObject *op)
271317
{
@@ -415,7 +461,7 @@ PyTypeObject PyFunction_Type = {
415461
PyObject_GenericSetAttr, /* tp_setattro */
416462
0, /* tp_as_buffer */
417463
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
418-
0, /* tp_doc */
464+
func_doc, /* tp_doc */
419465
(traverseproc)func_traverse, /* tp_traverse */
420466
0, /* tp_clear */
421467
0, /* tp_richcompare */
@@ -430,6 +476,9 @@ PyTypeObject PyFunction_Type = {
430476
func_descr_get, /* tp_descr_get */
431477
0, /* tp_descr_set */
432478
offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
479+
0, /* tp_init */
480+
0, /* tp_alloc */
481+
func_new, /* tp_new */
433482
};
434483

435484

0 commit comments

Comments
 (0)