annotate Objects/classobject.c @ 44239:369a4e06fd30

Added another test case for kwonly methods
author Christian Heimes <christian@cheimes.de>
date Sat, 08 Dec 2007 22:32:47 +0000
parents 50a50e752142
children 3e0867a07765
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1 /* Class object implementation (dead now except for methods) */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
3 #include "Python.h"
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
4 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
6 #define TP_DESCR_GET(t) ((t)->tp_descr_get)
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
7
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
8
19751
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
9 PyObject *
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
10 PyMethod_Function(PyObject *im)
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
11 {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
12 if (!PyMethod_Check(im)) {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
13 PyErr_BadInternalCall();
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
14 return NULL;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
15 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
16 return ((PyMethodObject *)im)->im_func;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
17 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
18
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
19 PyObject *
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
20 PyMethod_Self(PyObject *im)
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
21 {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
22 if (!PyMethod_Check(im)) {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
23 PyErr_BadInternalCall();
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
24 return NULL;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
25 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
26 return ((PyMethodObject *)im)->im_self;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
27 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
28
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
29 /* Method objects are used for bound instance methods returned by
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
30 instancename.methodname. ClassName.methodname returns an ordinary
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
31 function.
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
32 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
34 static PyMethodObject *free_list;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
35
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
36 PyObject *
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
37 PyMethod_New(PyObject *func, PyObject *self)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
39 register PyMethodObject *im;
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8649
diff changeset
40 if (!PyCallable_Check(func)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
41 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 }
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
44 if (self == NULL) {
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
45 PyErr_BadInternalCall();
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
46 return NULL;
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
47 }
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
48 im = free_list;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
49 if (im != NULL) {
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
50 free_list = (PyMethodObject *)(im->im_self);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12504
diff changeset
51 PyObject_INIT(im, &PyMethod_Type);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
52 }
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
53 else {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
54 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
55 if (im == NULL)
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
56 return NULL;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
57 }
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 17154
diff changeset
58 im->im_weakreflist = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
59 Py_INCREF(func);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
60 im->im_func = func;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
61 Py_XINCREF(self);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
62 im->im_self = self;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
63 _PyObject_GC_TRACK(im);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
64 return (PyObject *)im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
67 /* Descriptors for PyMethod attributes */
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
68
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
69 /* im_func and im_self are stored in the PyMethod object */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
71 #define OFF(x) offsetof(PyMethodObject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
72
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
73 static PyMemberDef method_memberlist[] = {
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
74 {"__func__", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
75 "the function (or other callable) implementing a method"},
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
76 {"__self__", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
77 "the instance to which a method is bound"},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
78 {NULL} /* Sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
79 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
80
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
81 /* Christian Tismer argued convincingly that method attributes should
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
82 (nearly) always override function attributes.
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
83 The one exception is __doc__; there's a default __doc__ which
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
84 should only be used for the class, not for instances */
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
85
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
86 static PyObject *
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
87 method_get_doc(PyMethodObject *im, void *context)
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
88 {
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
89 static PyObject *docstr;
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
90 if (docstr == NULL) {
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41782
diff changeset
91 docstr= PyUnicode_InternFromString("__doc__");
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
92 if (docstr == NULL)
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
93 return NULL;
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
94 }
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
95 return PyObject_GetAttr(im->im_func, docstr);
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
96 }
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
97
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
98 static PyGetSetDef method_getset[] = {
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
99 {"__doc__", (getter)method_get_doc, NULL, NULL},
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
100 {0}
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
101 };
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 15722
diff changeset
102
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
103 static PyObject *
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
104 method_getattro(PyObject *obj, PyObject *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
105 {
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
106 PyMethodObject *im = (PyMethodObject *)obj;
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
107 PyTypeObject *tp = obj->ob_type;
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
108 PyObject *descr = NULL;
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
109
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
110 {
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
111 if (tp->tp_dict == NULL) {
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
112 if (PyType_Ready(tp) < 0)
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
113 return NULL;
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
114 }
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
115 descr = _PyType_Lookup(tp, name);
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
116 }
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
117
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
118 if (descr != NULL) {
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
119 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
120 if (f != NULL)
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
121 return f(descr, obj, (PyObject *)obj->ob_type);
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
122 else {
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
123 Py_INCREF(descr);
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
124 return descr;
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
125 }
3549
f66306bc0663 Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents: 3096
diff changeset
126 }
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
127
31003
278976b19dcc - When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents: 30801
diff changeset
128 return PyObject_GetAttr(im->im_func, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
129 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
130
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
131 PyDoc_STRVAR(method_doc,
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
132 "method(function, instance)\n\
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
133 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
134 Create an instance method object.");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
135
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
136 static PyObject *
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
137 method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
138 {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
139 PyObject *func;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
140 PyObject *self;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
141
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
142 if (!_PyArg_NoKeywords("instancemethod", kw))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
143 return NULL;
44233
50a50e752142 Fixed a bug found by Marcin Kowalczyk
Christian Heimes <christian@cheimes.de>
parents: 44026
diff changeset
144 if (!PyArg_UnpackTuple(args, "method", 2, 2,
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
145 &func, &self))
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
146 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
147 if (!PyCallable_Check(func)) {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
148 PyErr_SetString(PyExc_TypeError,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
149 "first argument must be callable");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
150 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
151 }
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
152 if (self == NULL || self == Py_None) {
34850
aef713de7a23 Backport:
Michael W. Hudson <mwh@python.net>
parents: 33541
diff changeset
153 PyErr_SetString(PyExc_TypeError,
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
154 "self must not be None");
34850
aef713de7a23 Backport:
Michael W. Hudson <mwh@python.net>
parents: 33541
diff changeset
155 return NULL;
aef713de7a23 Backport:
Michael W. Hudson <mwh@python.net>
parents: 33541
diff changeset
156 }
aef713de7a23 Backport:
Michael W. Hudson <mwh@python.net>
parents: 33541
diff changeset
157
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
158 return PyMethod_New(func, self);
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
159 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
160
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 static void
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
162 method_dealloc(register PyMethodObject *im)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
164 _PyObject_GC_UNTRACK(im);
20698
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20601
diff changeset
165 if (im->im_weakreflist != NULL)
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20601
diff changeset
166 PyObject_ClearWeakRefs((PyObject *)im);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
167 Py_DECREF(im->im_func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
168 Py_XDECREF(im->im_self);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
169 im->im_self = (PyObject *)free_list;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
170 free_list = im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
173 static PyObject *
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
174 method_richcompare(PyObject *self, PyObject *other, int op)
1021
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
175 {
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
176 PyMethodObject *a, *b;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
177 PyObject *res;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
178 int eq;
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
179
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
180 if ((op != Py_EQ && op != Py_NE) ||
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
181 !PyMethod_Check(self) ||
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
182 !PyMethod_Check(other))
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
183 {
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
184 Py_INCREF(Py_NotImplemented);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
185 return Py_NotImplemented;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
186 }
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
187 a = (PyMethodObject *)self;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
188 b = (PyMethodObject *)other;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
189 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
190 if (eq == 1) {
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
191 if (a->im_self == NULL || b->im_self == NULL)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
192 eq = a->im_self == b->im_self;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
193 else
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
194 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
195 Py_EQ);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
196 }
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
197 if (eq < 0)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
198 return NULL;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
199 if (op == Py_EQ)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
200 res = eq ? Py_True : Py_False;
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
201 else
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
202 res = eq ? Py_False : Py_True;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
203 Py_INCREF(res);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
204 return res;
1021
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
205 }
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
206
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
207 static PyObject *
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
208 method_repr(PyMethodObject *a)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
209 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
210 PyObject *self = a->im_self;
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8649
diff changeset
211 PyObject *func = a->im_func;
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
212 PyObject *klass = (PyObject*)Py_Type(self);
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
213 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
42047
d678ba0177da Add a format specifier %V to PyUnicode_FromFormat(), that works similar to %U,
Walter Dörwald <walter@livinglogic.de>
parents: 42018
diff changeset
214 char *defname = "?";
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
215
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
216 if (self == NULL) {
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
217 PyErr_BadInternalCall();
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
218 return NULL;
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
219 }
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
220
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
221 funcname = PyObject_GetAttrString(func, "__name__");
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
222 if (funcname == NULL) {
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
223 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
224 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
225 PyErr_Clear();
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
226 }
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41782
diff changeset
227 else if (!PyUnicode_Check(funcname)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
228 Py_DECREF(funcname);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
229 funcname = NULL;
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8649
diff changeset
230 }
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
231
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
232 if (klass == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
233 klassname = NULL;
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
234 else {
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
235 klassname = PyObject_GetAttrString(klass, "__name__");
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
236 if (klassname == NULL) {
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
237 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
238 return NULL;
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
239 PyErr_Clear();
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
240 }
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41782
diff changeset
241 else if (!PyUnicode_Check(klassname)) {
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
242 Py_DECREF(klassname);
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
243 klassname = NULL;
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
244 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
245 }
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
246
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
247 /* XXX Shouldn't use repr()/%R here! */
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
248 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
249 klassname, defname,
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
250 funcname, defname, self);
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
251
11378
dc9f1f964690 Fix for PR#98 (Adrian Eyre) -- in instancemethod_repr, the funcname
Guido van Rossum <guido@python.org>
parents: 8806
diff changeset
252 Py_XDECREF(funcname);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
253 Py_XDECREF(klassname);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
254 return result;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
255 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
256
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
257 static long
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
258 method_hash(PyMethodObject *a)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
259 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
260 long x, y;
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
261 if (a->im_self == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
262 x = PyObject_Hash(Py_None);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
263 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
264 x = PyObject_Hash(a->im_self);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
265 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
266 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
267 y = PyObject_Hash(a->im_func);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
268 if (y == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
269 return -1;
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
270 x = x ^ y;
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
271 if (x == -1)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
272 x = -2;
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
273 return x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
274 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
275
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
276 static int
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
277 method_traverse(PyMethodObject *im, visitproc visit, void *arg)
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
278 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
279 Py_VISIT(im->im_func);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
280 Py_VISIT(im->im_self);
12992
3f990ef66667 traverse functions should return 0 on success
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12990
diff changeset
281 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
282 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
283
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
284 static PyObject *
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
285 method_call(PyObject *func, PyObject *arg, PyObject *kw)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
286 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
287 PyObject *self = PyMethod_GET_SELF(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
288 PyObject *result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
289
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
290 func = PyMethod_GET_FUNCTION(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
291 if (self == NULL) {
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
292 PyErr_BadInternalCall();
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
293 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
294 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
295 else {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
296 Py_ssize_t argcount = PyTuple_Size(arg);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
297 PyObject *newarg = PyTuple_New(argcount + 1);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
298 int i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
299 if (newarg == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
300 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
301 Py_INCREF(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
302 PyTuple_SET_ITEM(newarg, 0, self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
303 for (i = 0; i < argcount; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
304 PyObject *v = PyTuple_GET_ITEM(arg, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
305 Py_XINCREF(v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
306 PyTuple_SET_ITEM(newarg, i+1, v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
307 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
308 arg = newarg;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
309 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
310 result = PyObject_Call((PyObject *)func, arg, kw);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
311 Py_DECREF(arg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
312 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
313 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
314
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
315 static PyObject *
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
316 method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
317 {
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
318 /* Don't rebind an already bound method of a class that's not a base
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
319 class of cls. */
27817
bf083e35185f Refactor instancemethod_descr_get() to (a) be more clear, (b) be safe
Guido van Rossum <guido@python.org>
parents: 26818
diff changeset
320 if (PyMethod_GET_SELF(meth) != NULL) {
bf083e35185f Refactor instancemethod_descr_get() to (a) be more clear, (b) be safe
Guido van Rossum <guido@python.org>
parents: 26818
diff changeset
321 /* Already bound */
19323
261e28e7fa01 classobject.c:instancemethod_descr_get(): when a bound method is
Guido van Rossum <guido@python.org>
parents: 19280
diff changeset
322 Py_INCREF(meth);
261e28e7fa01 classobject.c:instancemethod_descr_get(): when a bound method is
Guido van Rossum <guido@python.org>
parents: 19280
diff changeset
323 return meth;
261e28e7fa01 classobject.c:instancemethod_descr_get(): when a bound method is
Guido van Rossum <guido@python.org>
parents: 19280
diff changeset
324 }
27817
bf083e35185f Refactor instancemethod_descr_get() to (a) be more clear, (b) be safe
Guido van Rossum <guido@python.org>
parents: 26818
diff changeset
325 /* Bind it to obj */
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
326 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
327 }
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
328
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
329 PyTypeObject PyMethod_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42047
diff changeset
330 PyVarObject_HEAD_INIT(&PyType_Type, 0)
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
331 "method",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
332 sizeof(PyMethodObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 0,
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
334 (destructor)method_dealloc, /* tp_dealloc */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
335 0, /* tp_print */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
336 0, /* tp_getattr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
337 0, /* tp_setattr */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
338 0, /* tp_compare */
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
339 (reprfunc)method_repr, /* tp_repr */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
340 0, /* tp_as_number */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
341 0, /* tp_as_sequence */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
342 0, /* tp_as_mapping */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
343 (hashfunc)method_hash, /* tp_hash */
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
344 method_call, /* tp_call */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
345 0, /* tp_str */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
346 method_getattro, /* tp_getattro */
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
347 PyObject_GenericSetAttr, /* tp_setattro */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
348 0, /* tp_as_buffer */
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
349 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
350 method_doc, /* tp_doc */
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
351 (traverseproc)method_traverse, /* tp_traverse */
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 17154
diff changeset
352 0, /* tp_clear */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
353 method_richcompare, /* tp_richcompare */
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
354 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
355 0, /* tp_iter */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
356 0, /* tp_iternext */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
357 0, /* tp_methods */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
358 method_memberlist, /* tp_members */
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
359 method_getset, /* tp_getset */
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
360 0, /* tp_base */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
361 0, /* tp_dict */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
362 method_descr_get, /* tp_descr_get */
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
363 0, /* tp_descr_set */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
364 0, /* tp_dictoffset */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
365 0, /* tp_init */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
366 0, /* tp_alloc */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39536
diff changeset
367 method_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 };
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
369
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
370 /* Clear out the free list */
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
371
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
372 void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
373 PyMethod_Fini(void)
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
374 {
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
375 while (free_list) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12504
diff changeset
376 PyMethodObject *im = free_list;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12504
diff changeset
377 free_list = (PyMethodObject *)(im->im_self);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
378 PyObject_GC_Del(im);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
379 }
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
380 }