Mercurial > cpython-lru_cache-weakref
annotate Objects/methodobject.c @ 17581:c1937faabb23 2.1
Added support for optional explanation parameter to the \versionadded
macro.
Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most
of the work in a helper function, with the do_cmd_*() wrappers just supplying
a portion of the replacement text.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 18 Apr 2001 17:29:14 +0000 |
| parents | 6f6f965c5e2d |
| children | b2bf88e06fc5 |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Method object implementation */ |
| 3 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
4 #include "Python.h" |
| 6 | 5 |
| 6 #include "token.h" | |
| 7 | |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
8 static PyCFunctionObject *free_list = NULL; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
9 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
10 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
11 PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 6 | 12 { |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
13 PyCFunctionObject *op; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
14 op = free_list; |
| 6 | 15 if (op != NULL) { |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
16 free_list = (PyCFunctionObject *)(op->m_self); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
17 PyObject_INIT(op, &PyCFunction_Type); |
| 6 | 18 } |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
19 else { |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
20 op = PyObject_NEW(PyCFunctionObject, &PyCFunction_Type); |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
21 if (op == NULL) |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
22 return NULL; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
23 } |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
24 op->m_ml = ml; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
25 Py_XINCREF(self); |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
26 op->m_self = self; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
27 return (PyObject *)op; |
| 6 | 28 } |
| 29 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
30 PyCFunction |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
31 PyCFunction_GetFunction(PyObject *op) |
| 6 | 32 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
33 if (!PyCFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
34 PyErr_BadInternalCall(); |
| 6 | 35 return NULL; |
| 36 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
37 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth; |
| 6 | 38 } |
| 39 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
40 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
41 PyCFunction_GetSelf(PyObject *op) |
| 6 | 42 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
43 if (!PyCFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
44 PyErr_BadInternalCall(); |
| 6 | 45 return NULL; |
| 46 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
47 return ((PyCFunctionObject *)op) -> m_self; |
| 6 | 48 } |
| 49 | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
50 int |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
51 PyCFunction_GetFlags(PyObject *op) |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
52 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
53 if (!PyCFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
54 PyErr_BadInternalCall(); |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
55 return -1; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
56 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
57 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
58 } |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
59 |
| 6 | 60 /* Methods (the standard built-in methods, that is) */ |
| 61 | |
| 62 static void | |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
63 meth_dealloc(PyCFunctionObject *m) |
| 6 | 64 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
65 Py_XDECREF(m->m_self); |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
66 m->m_self = (PyObject *)free_list; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
67 free_list = m; |
| 6 | 68 } |
| 69 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
70 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
71 meth_getattr(PyCFunctionObject *m, char *name) |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
72 { |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
73 if (strcmp(name, "__name__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
74 return PyString_FromString(m->m_ml->ml_name); |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
75 } |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
76 if (strcmp(name, "__doc__") == 0) { |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
77 char *doc = m->m_ml->ml_doc; |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
78 if (doc != NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
79 return PyString_FromString(doc); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
80 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
81 return Py_None; |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
82 } |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
83 if (strcmp(name, "__self__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
84 PyObject *self; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
85 if (PyEval_GetRestricted()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
86 PyErr_SetString(PyExc_RuntimeError, |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2083
diff
changeset
|
87 "method.__self__ not accessible in restricted mode"); |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2083
diff
changeset
|
88 return NULL; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2083
diff
changeset
|
89 } |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2083
diff
changeset
|
90 self = m->m_self; |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
91 if (self == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
92 self = Py_None; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
93 Py_INCREF(self); |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
94 return self; |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
95 } |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
96 if (strcmp(name, "__members__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
97 return Py_BuildValue("[sss]", |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
98 "__doc__", "__name__", "__self__"); |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
99 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
100 PyErr_SetString(PyExc_AttributeError, name); |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
101 return NULL; |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
102 } |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
103 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
104 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
105 meth_repr(PyCFunctionObject *m) |
| 6 | 106 { |
| 107 char buf[200]; | |
| 108 if (m->m_self == NULL) | |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
109 sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name); |
| 6 | 110 else |
| 86 | 111 sprintf(buf, |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13141
diff
changeset
|
112 "<built-in method %.80s of %.80s object at %p>", |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
113 m->m_ml->ml_name, m->m_self->ob_type->tp_name, |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13141
diff
changeset
|
114 m->m_self); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
115 return PyString_FromString(buf); |
| 6 | 116 } |
| 117 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
118 static int |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
119 meth_compare(PyCFunctionObject *a, PyCFunctionObject *b) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
120 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
121 if (a->m_self != b->m_self) |
|
9592
2aafcee220bc
When comparing bound methods, use identity test on the objects,
Guido van Rossum <guido@python.org>
parents:
8656
diff
changeset
|
122 return (a->m_self < b->m_self) ? -1 : 1; |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
123 if (a->m_ml->ml_meth == b->m_ml->ml_meth) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
124 return 0; |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
125 if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
126 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
127 else |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
128 return 1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
129 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
130 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
131 static long |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
132 meth_hash(PyCFunctionObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
133 { |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
134 long x,y; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
135 if (a->m_self == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
136 x = 0; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
137 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
138 x = PyObject_Hash(a->m_self); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
139 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
140 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
141 } |
|
13252
310d7cbba329
Fix an error on AIX by using a proper cast.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
142 y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
143 if (y == -1) |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
144 return -1; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
145 x ^= y; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
146 if (x == -1) |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
147 x = -2; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
148 return x; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
149 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
150 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
151 PyTypeObject PyCFunction_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
152 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 153 0, |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
154 "builtin_function_or_method", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
155 sizeof(PyCFunctionObject), |
| 6 | 156 0, |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
157 (destructor)meth_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
158 0, /*tp_print*/ |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
159 (getattrfunc)meth_getattr, /*tp_getattr*/ |
| 6 | 160 0, /*tp_setattr*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
161 (cmpfunc)meth_compare, /*tp_compare*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
162 (reprfunc)meth_repr, /*tp_repr*/ |
| 6 | 163 0, /*tp_as_number*/ |
| 164 0, /*tp_as_sequence*/ | |
| 165 0, /*tp_as_mapping*/ | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
166 (hashfunc)meth_hash, /*tp_hash*/ |
| 6 | 167 }; |
| 86 | 168 |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
169 /* List all methods in a chain -- helper for findmethodinchain */ |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
170 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
171 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
172 listmethodchain(PyMethodChain *chain) |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
173 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
174 PyMethodChain *c; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
175 PyMethodDef *ml; |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
176 int i, n; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
177 PyObject *v; |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
178 |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
179 n = 0; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
180 for (c = chain; c != NULL; c = c->link) { |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
181 for (ml = c->methods; ml->ml_name != NULL; ml++) |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
182 n++; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
183 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
184 v = PyList_New(n); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
185 if (v == NULL) |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
186 return NULL; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
187 i = 0; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
188 for (c = chain; c != NULL; c = c->link) { |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
189 for (ml = c->methods; ml->ml_name != NULL; ml++) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
190 PyList_SetItem(v, i, PyString_FromString(ml->ml_name)); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
191 i++; |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
192 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
193 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
194 if (PyErr_Occurred()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
195 Py_DECREF(v); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
196 return NULL; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
197 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
198 PyList_Sort(v); |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
199 return v; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
200 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
201 |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
202 /* Find a method in a method chain */ |
| 86 | 203 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
204 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
205 Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name) |
| 86 | 206 { |
|
8565
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
207 if (name[0] == '_' && name[1] == '_') { |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
208 if (strcmp(name, "__methods__") == 0) |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
209 return listmethodchain(chain); |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
210 if (strcmp(name, "__doc__") == 0) { |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
211 char *doc = self->ob_type->tp_doc; |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
212 if (doc != NULL) |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
213 return PyString_FromString(doc); |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
214 } |
|
19e8016cd38c
New feature: if the object's type has a non-NULL tp_doc field, that
Guido van Rossum <guido@python.org>
parents:
5934
diff
changeset
|
215 } |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
216 while (chain != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
217 PyMethodDef *ml = chain->methods; |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
218 for (; ml->ml_name != NULL; ml++) { |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
219 if (name[0] == ml->ml_name[0] && |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
220 strcmp(name+1, ml->ml_name+1) == 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
221 return PyCFunction_New(ml, self); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
222 } |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
223 chain = chain->link; |
| 86 | 224 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
225 PyErr_SetString(PyExc_AttributeError, name); |
| 86 | 226 return NULL; |
| 227 } | |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
228 |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
229 /* Find a method in a single method list */ |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
230 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
231 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
232 Py_FindMethod(PyMethodDef *methods, PyObject *self, char *name) |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
233 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
234 PyMethodChain chain; |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
235 chain.methods = methods; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
236 chain.link = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
237 return Py_FindMethodInChain(&chain, self, name); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
238 } |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
239 |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
240 /* Clear out the free list */ |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
241 |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
242 void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
243 PyCFunction_Fini(void) |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
244 { |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
245 while (free_list) { |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
246 PyCFunctionObject *v = free_list; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
247 free_list = (PyCFunctionObject *)(v->m_self); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
248 PyObject_DEL(v); |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
249 } |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
250 } |
