annotate Objects/methodobject.c @ 18816:50004bed4e65 legacy-trunk

Oh, ok, so plainpager is probably marginally better.
author Fred Drake <fdrake@acm.org>
date Mon, 23 Jul 2001 19:48:10 +0000
parents b2bf88e06fc5
children 09df3254b49d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Method object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
4 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "token.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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;
18512
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
27 PyObject_GC_Init(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
28 return (PyObject *)op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
31 PyCFunction
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
32 PyCFunction_GetFunction(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
34 if (!PyCFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
35 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
38 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
41 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
42 PyCFunction_GetSelf(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
44 if (!PyCFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
45 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
48 return ((PyCFunctionObject *)op) -> m_self;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
51 int
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
52 PyCFunction_GetFlags(PyObject *op)
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
53 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
54 if (!PyCFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
55 PyErr_BadInternalCall();
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
56 return -1;
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
57 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
58 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
59 }
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
60
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 /* Methods (the standard built-in methods, that is) */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 static void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
64 meth_dealloc(PyCFunctionObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 {
18512
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
66 PyObject_GC_Fini(m);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
67 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
68 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
69 free_list = m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
18512
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
72 static int
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
73 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
74 {
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
75 if (m->m_self != NULL)
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
76 return visit(m->m_self, arg);
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
77 else
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
78 return 0;
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
79 }
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
80
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
81 static PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
82 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
83 {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
84 if (strcmp(name, "__name__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
85 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
86 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
87 if (strcmp(name, "__doc__") == 0) {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
88 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
89 if (doc != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
90 return PyString_FromString(doc);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
91 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
92 return Py_None;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
93 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
94 if (strcmp(name, "__self__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
95 PyObject *self;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
96 if (PyEval_GetRestricted()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
97 PyErr_SetString(PyExc_RuntimeError,
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
98 "method.__self__ not accessible in restricted mode");
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
99 return NULL;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
100 }
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
101 self = m->m_self;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
102 if (self == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
103 self = Py_None;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
104 Py_INCREF(self);
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
105 return self;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
106 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
107 if (strcmp(name, "__members__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
108 return Py_BuildValue("[sss]",
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
109 "__doc__", "__name__", "__self__");
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
110 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
111 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
112 return NULL;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
113 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
114
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
115 static PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
116 meth_repr(PyCFunctionObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 char buf[200];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 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
120 sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 else
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
122 sprintf(buf,
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13141
diff changeset
123 "<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
124 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
125 m->m_self);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
126 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
129 static int
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
130 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
131 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
132 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
133 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
134 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
135 return 0;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
136 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
137 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
138 else
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
139 return 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
140 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
141
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
142 static long
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
143 meth_hash(PyCFunctionObject *a)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
144 {
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
145 long x,y;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
146 if (a->m_self == NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
147 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
148 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
149 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
150 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
151 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
152 }
13252
310d7cbba329 Fix an error on AIX by using a proper cast.
Guido van Rossum <guido@python.org>
parents: 13210
diff changeset
153 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
154 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
155 return -1;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
156 x ^= y;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
157 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
158 x = -2;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
159 return x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
160 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
161
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
162 PyTypeObject PyCFunction_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
163 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 0,
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
165 "builtin_function_or_method",
18512
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
166 sizeof(PyCFunctionObject) + PyGC_HEAD_SIZE,
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 0,
18512
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
168 (destructor)meth_dealloc, /* tp_dealloc */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
169 0, /* tp_print */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
170 (getattrfunc)meth_getattr, /* tp_getattr */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
171 0, /* tp_setattr */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
172 (cmpfunc)meth_compare, /* tp_compare */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
173 (reprfunc)meth_repr, /* tp_repr */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
174 0, /* tp_as_number */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
175 0, /* tp_as_sequence */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
176 0, /* tp_as_mapping */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
177 (hashfunc)meth_hash, /* tp_hash */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
178 0, /* tp_call */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
179 0, /* tp_str */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
180 0, /* tp_getattro */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
181 0, /* tp_setattro */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
182 0, /* tp_as_buffer */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
183 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
184 0, /* tp_doc */
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
185 (traverseproc)meth_traverse, /* tp_traverse */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 };
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
187
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
188 /* List all methods in a chain -- helper for findmethodinchain */
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
189
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
190 static PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
191 listmethodchain(PyMethodChain *chain)
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
192 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
193 PyMethodChain *c;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
194 PyMethodDef *ml;
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
195 int i, n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
196 PyObject *v;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
197
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
198 n = 0;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
199 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
200 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
201 n++;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
202 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
203 v = PyList_New(n);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
204 if (v == NULL)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
205 return NULL;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
206 i = 0;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
207 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
208 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
209 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
210 i++;
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
211 }
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
212 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
213 if (PyErr_Occurred()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
214 Py_DECREF(v);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
215 return NULL;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
216 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
217 PyList_Sort(v);
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
218 return v;
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
219 }
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
220
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
221 /* Find a method in a method chain */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
222
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
223 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
224 Py_FindMethodInChain(PyMethodChain *chain, PyObject *self, char *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
225 {
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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 }
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
234 }
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
235 while (chain != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
236 PyMethodDef *ml = chain->methods;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
237 for (; ml->ml_name != NULL; ml++) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
238 if (name[0] == ml->ml_name[0] &&
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
239 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
240 return PyCFunction_New(ml, self);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
241 }
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
242 chain = chain->link;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
243 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
244 PyErr_SetString(PyExc_AttributeError, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
245 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
246 }
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
247
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
248 /* 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
249
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
250 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
251 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
252 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
253 PyMethodChain chain;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
254 chain.methods = methods;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
255 chain.link = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
256 return Py_FindMethodInChain(&chain, self, name);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
257 }
5934
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
258
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
259 /* 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
260
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
261 void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
262 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
263 {
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
264 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
265 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
266 free_list = (PyCFunctionObject *)(v->m_self);
18512
b2bf88e06fc5 GC for method objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14375
diff changeset
267 v = (PyCFunctionObject *) PyObject_AS_GC(v);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
268 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
269 }
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
270 }