Mercurial > cpython-lru_cache-weakref
annotate Objects/methodobject.c @ 33526:175eee09cb98 legacy-trunk
Raymond reminded me to use DSU key
| author | Skip Montanaro <skip@pobox.com> |
|---|---|
| date | Mon, 20 Sep 2004 16:43:30 +0000 |
| parents | e4fb6164158c |
| children | 9541369b0dca |
| 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" |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
5 #include "structmember.h" |
| 6 | 6 |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
7 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
|
8 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
9 PyObject * |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
10 PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
| 6 | 11 { |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
12 PyCFunctionObject *op; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
13 op = free_list; |
| 6 | 14 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
|
15 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
|
16 PyObject_INIT(op, &PyCFunction_Type); |
| 6 | 17 } |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
18 else { |
|
22397
132a064da656
Re-enable GC of method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20015
diff
changeset
|
19 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
20 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
|
21 return NULL; |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
22 } |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
23 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
|
24 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
|
25 op->m_self = self; |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
26 Py_XINCREF(module); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
27 op->m_module = module; |
|
22397
132a064da656
Re-enable GC of method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20015
diff
changeset
|
28 _PyObject_GC_TRACK(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
29 return (PyObject *)op; |
| 6 | 30 } |
| 31 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
32 PyCFunction |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
33 PyCFunction_GetFunction(PyObject *op) |
| 6 | 34 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
35 if (!PyCFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
36 PyErr_BadInternalCall(); |
| 6 | 37 return NULL; |
| 38 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
39 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth; |
| 6 | 40 } |
| 41 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
42 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
43 PyCFunction_GetSelf(PyObject *op) |
| 6 | 44 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
45 if (!PyCFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
46 PyErr_BadInternalCall(); |
| 6 | 47 return NULL; |
| 48 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
49 return ((PyCFunctionObject *)op) -> m_self; |
| 6 | 50 } |
| 51 | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
52 int |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
53 PyCFunction_GetFlags(PyObject *op) |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
54 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
55 if (!PyCFunction_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
56 PyErr_BadInternalCall(); |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
57 return -1; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
58 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
59 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
60 } |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
61 |
|
19198
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
62 PyObject * |
|
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
63 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) |
|
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
64 { |
|
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
65 PyCFunctionObject* f = (PyCFunctionObject*)func; |
|
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
66 PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
|
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
67 PyObject *self = PyCFunction_GET_SELF(func); |
|
27072
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
68 int size; |
|
19198
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
69 |
|
31172
e4fb6164158c
* Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents:
27941
diff
changeset
|
70 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19198
diff
changeset
|
71 case METH_VARARGS: |
|
27072
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
72 if (kw == NULL || PyDict_Size(kw) == 0) |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
73 return (*meth)(self, arg); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
74 break; |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
75 case METH_VARARGS | METH_KEYWORDS: |
|
27093
bd750378d5c1
PyCFunction_Call(): Combined two switch cases w/ identical bodies.
Tim Peters <tim.peters@gmail.com>
parents:
27072
diff
changeset
|
76 case METH_OLDARGS | METH_KEYWORDS: |
|
27072
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
77 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19198
diff
changeset
|
78 case METH_NOARGS: |
|
27072
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
79 if (kw == NULL || PyDict_Size(kw) == 0) { |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
80 size = PyTuple_GET_SIZE(arg); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
81 if (size == 0) |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
82 return (*meth)(self, NULL); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
83 PyErr_Format(PyExc_TypeError, |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
84 "%.200s() takes no arguments (%d given)", |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
85 f->m_ml->ml_name, size); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
86 return NULL; |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
87 } |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
88 break; |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19198
diff
changeset
|
89 case METH_O: |
|
27072
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
90 if (kw == NULL || PyDict_Size(kw) == 0) { |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
91 size = PyTuple_GET_SIZE(arg); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
92 if (size == 1) |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
93 return (*meth)(self, PyTuple_GET_ITEM(arg, 0)); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
94 PyErr_Format(PyExc_TypeError, |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
95 "%.200s() takes exactly one argument (%d given)", |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
96 f->m_ml->ml_name, size); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
97 return NULL; |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
98 } |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
99 break; |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19198
diff
changeset
|
100 case METH_OLDARGS: |
|
19198
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
101 /* the really old style */ |
|
27072
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
102 if (kw == NULL || PyDict_Size(kw) == 0) { |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
103 size = PyTuple_GET_SIZE(arg); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
104 if (size == 1) |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
105 arg = PyTuple_GET_ITEM(arg, 0); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
106 else if (size == 0) |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
107 arg = NULL; |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
108 return (*meth)(self, arg); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
109 } |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
110 break; |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19198
diff
changeset
|
111 default: |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19198
diff
changeset
|
112 PyErr_BadInternalCall(); |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19198
diff
changeset
|
113 return NULL; |
|
19198
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
114 } |
|
27072
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
115 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
116 f->m_ml->ml_name); |
|
1564c6839e6b
SF Patch #661440: Refactor and streamline PyCFunction_Call
Raymond Hettinger <python@rcn.com>
parents:
22613
diff
changeset
|
117 return NULL; |
|
19198
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
118 } |
|
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
119 |
| 6 | 120 /* Methods (the standard built-in methods, that is) */ |
| 121 | |
| 122 static void | |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
123 meth_dealloc(PyCFunctionObject *m) |
| 6 | 124 { |
|
22397
132a064da656
Re-enable GC of method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20015
diff
changeset
|
125 _PyObject_GC_UNTRACK(m); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
126 Py_XDECREF(m->m_self); |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
127 Py_XDECREF(m->m_module); |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
128 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
|
129 free_list = m; |
| 6 | 130 } |
| 131 | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
132 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
133 meth_get__doc__(PyCFunctionObject *m, void *closure) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
134 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
135 char *doc = m->m_ml->ml_doc; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
136 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
137 if (doc != NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
138 return PyString_FromString(doc); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
139 Py_INCREF(Py_None); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
140 return Py_None; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
141 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
142 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
143 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
144 meth_get__name__(PyCFunctionObject *m, void *closure) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
145 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
146 return PyString_FromString(m->m_ml->ml_name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
147 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
148 |
|
18512
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
149 static int |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
150 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
151 { |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
152 int err; |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
153 if (m->m_self != NULL) { |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
154 err = visit(m->m_self, arg); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
155 if (err) |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
156 return err; |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
157 } |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
158 if (m->m_module != NULL) { |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
159 err = visit(m->m_module, arg); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
160 if (err) |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
161 return err; |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
162 } |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
163 return 0; |
|
18512
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
164 } |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
165 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
166 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
167 meth_get__self__(PyCFunctionObject *m, void *closure) |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
168 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
169 PyObject *self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
170 if (PyEval_GetRestricted()) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
171 PyErr_SetString(PyExc_RuntimeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
172 "method.__self__ not accessible in restricted mode"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
173 return NULL; |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
174 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
175 self = m->m_self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
176 if (self == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
177 self = Py_None; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
178 Py_INCREF(self); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
179 return self; |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
180 } |
|
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
181 |
|
20015
ca7af2a5d915
Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents:
19599
diff
changeset
|
182 static PyGetSetDef meth_getsets [] = { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
183 {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
184 {"__name__", (getter)meth_get__name__, NULL, NULL}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
185 {"__self__", (getter)meth_get__self__, NULL, NULL}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
186 {0} |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
187 }; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
188 |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
189 #define OFF(x) offsetof(PyCFunctionObject, x) |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
190 |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
191 static PyMemberDef meth_members[] = { |
|
27941
14451dd3acb0
Make __module__ writable except in restricted mode (like for classic classes).
Guido van Rossum <guido@python.org>
parents:
27940
diff
changeset
|
192 {"__module__", T_OBJECT, OFF(m_module), WRITE_RESTRICTED}, |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
193 {NULL} |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
194 }; |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
195 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
196 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
197 meth_repr(PyCFunctionObject *m) |
| 6 | 198 { |
| 199 if (m->m_self == NULL) | |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
200 return PyString_FromFormat("<built-in function %s>", |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
201 m->m_ml->ml_name); |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
202 return PyString_FromFormat("<built-in method %s of %s object at %p>", |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
203 m->m_ml->ml_name, |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
204 m->m_self->ob_type->tp_name, |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
205 m->m_self); |
| 6 | 206 } |
| 207 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
208 static int |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
209 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
|
210 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
211 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
|
212 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
|
213 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
|
214 return 0; |
|
2083
f2d267edbfd6
* Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
215 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
|
216 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
217 else |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
218 return 1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
219 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
220 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
221 static long |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
222 meth_hash(PyCFunctionObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
223 { |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
224 long x,y; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
225 if (a->m_self == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
226 x = 0; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
227 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
228 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
|
229 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
230 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
231 } |
|
13252
310d7cbba329
Fix an error on AIX by using a proper cast.
Guido van Rossum <guido@python.org>
parents:
13210
diff
changeset
|
232 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
|
233 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
|
234 return -1; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
235 x ^= y; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
236 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
|
237 x = -2; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
238 return x; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
239 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
240 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
241 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
242 PyTypeObject PyCFunction_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
243 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 244 0, |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
245 "builtin_function_or_method", |
|
22397
132a064da656
Re-enable GC of method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20015
diff
changeset
|
246 sizeof(PyCFunctionObject), |
| 6 | 247 0, |
|
18512
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
248 (destructor)meth_dealloc, /* tp_dealloc */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
249 0, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
250 0, /* tp_getattr */ |
|
18512
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
251 0, /* tp_setattr */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
252 (cmpfunc)meth_compare, /* tp_compare */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
253 (reprfunc)meth_repr, /* tp_repr */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
254 0, /* tp_as_number */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
255 0, /* tp_as_sequence */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
256 0, /* tp_as_mapping */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
257 (hashfunc)meth_hash, /* tp_hash */ |
|
19198
3c1c9948ff65
Remove much dead code from ceval.c
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
18933
diff
changeset
|
258 PyCFunction_Call, /* tp_call */ |
|
18512
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
259 0, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
260 PyObject_GenericGetAttr, /* tp_getattro */ |
|
18512
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
261 0, /* tp_setattro */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
262 0, /* tp_as_buffer */ |
|
22397
132a064da656
Re-enable GC of method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20015
diff
changeset
|
263 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
18512
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
264 0, /* tp_doc */ |
|
b2bf88e06fc5
GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14375
diff
changeset
|
265 (traverseproc)meth_traverse, /* tp_traverse */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
266 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
267 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
268 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
269 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
270 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
271 0, /* tp_methods */ |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
272 meth_members, /* tp_members */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
273 meth_getsets, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
274 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18512
diff
changeset
|
275 0, /* tp_dict */ |
| 6 | 276 }; |
| 86 | 277 |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
278 /* List all methods in a chain -- helper for findmethodinchain */ |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
279 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
280 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
281 listmethodchain(PyMethodChain *chain) |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
282 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
283 PyMethodChain *c; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
284 PyMethodDef *ml; |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
285 int i, n; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
286 PyObject *v; |
|
27093
bd750378d5c1
PyCFunction_Call(): Combined two switch cases w/ identical bodies.
Tim Peters <tim.peters@gmail.com>
parents:
27072
diff
changeset
|
287 |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
288 n = 0; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
289 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
|
290 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
|
291 n++; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
292 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
293 v = PyList_New(n); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
294 if (v == NULL) |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
295 return NULL; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
296 i = 0; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
297 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
|
298 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
|
299 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
|
300 i++; |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
301 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
302 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
303 if (PyErr_Occurred()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
304 Py_DECREF(v); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
305 return NULL; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
306 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
307 PyList_Sort(v); |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
308 return v; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
309 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
310 |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
311 /* Find a method in a method chain */ |
| 86 | 312 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
313 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
314 Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name) |
| 86 | 315 { |
|
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
|
316 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
|
317 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
|
318 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
|
319 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
|
320 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
|
321 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
|
322 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
|
323 } |
|
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
|
324 } |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
325 while (chain != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
326 PyMethodDef *ml = chain->methods; |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
327 for (; ml->ml_name != NULL; ml++) { |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
328 if (name[0] == ml->ml_name[0] && |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
329 strcmp(name+1, ml->ml_name+1) == 0) |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
330 /* XXX */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
331 return PyCFunction_New(ml, self); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
332 } |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
333 chain = chain->link; |
| 86 | 334 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
335 PyErr_SetString(PyExc_AttributeError, name); |
| 86 | 336 return NULL; |
| 337 } | |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
338 |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
339 /* 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
|
340 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
341 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
342 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
|
343 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
344 PyMethodChain chain; |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
345 chain.methods = methods; |
|
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
346 chain.link = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
347 return Py_FindMethodInChain(&chain, self, name); |
|
2256
d091b246a3a7
added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
348 } |
|
5934
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
349 |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
350 /* 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
|
351 |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
352 void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
353 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
|
354 { |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
355 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
|
356 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
|
357 free_list = (PyCFunctionObject *)(v->m_self); |
|
22397
132a064da656
Re-enable GC of method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
20015
diff
changeset
|
358 PyObject_GC_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
|
359 } |
|
0369d63b100b
Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
360 } |
|
27559
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
361 |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
362 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(), |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
363 but it's part of the API so we need to keep a function around that |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
364 existing C extensions can call. |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
365 */ |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
366 |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
367 #undef PyCFunction_New |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
368 PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
369 |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
370 PyObject * |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
371 PyCFunction_New(PyMethodDef *ml, PyObject *self) |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
372 { |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
373 return PyCFunction_NewEx(ml, self, NULL); |
|
faabd9867fb8
Provide __module__ attributes for functions defined in C and Python.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27093
diff
changeset
|
374 } |
