annotate Objects/methodobject.c @ 13255:916fb024ce94 legacy-trunk

Change copyright notice.
author Guido van Rossum <guido@python.org>
date Fri, 30 Jun 2000 23:50:40 +0000
parents 310d7cbba329
children 87ee82e5c56d
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 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
6
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13252
diff changeset
7 Copyright (c) 2000, BeOpen.com.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13252
diff changeset
8 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13252
diff changeset
9 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13252
diff changeset
10 All rights reserved.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
11
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13252
diff changeset
12 See the file "Misc/COPYRIGHT" for information on usage and
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13252
diff changeset
13 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
15 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
16
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 /* Method object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
19 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 #include "token.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
5934
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
23 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
24
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
25 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
26 PyCFunction_New(ml, self)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
27 PyMethodDef *ml;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
28 PyObject *self;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 {
5934
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
30 PyCFunctionObject *op;
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
31 op = free_list;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 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
33 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
34 PyObject_INIT(op, &PyCFunction_Type);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 }
5934
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
36 else {
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
37 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
38 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
39 return NULL;
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
40 }
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
41 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
42 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
43 op->m_self = self;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
44 return (PyObject *)op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
47 PyCFunction
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
48 PyCFunction_GetFunction(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
49 PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
51 if (!PyCFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
52 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
55 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
58 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
59 PyCFunction_GetSelf(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
60 PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
62 if (!PyCFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
63 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
66 return ((PyCFunctionObject *)op) -> m_self;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
69 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
70 PyCFunction_GetFlags(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
71 PyObject *op;
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
72 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
73 if (!PyCFunction_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
74 PyErr_BadInternalCall();
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
75 return -1;
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
76 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
77 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
78 }
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
79
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 /* Methods (the standard built-in methods, that is) */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 static void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 meth_dealloc(m)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
84 PyCFunctionObject *m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
86 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
87 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
88 free_list = m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
91 static PyObject *
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
92 meth_getattr(m, name)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
93 PyCFunctionObject *m;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
94 char *name;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
95 {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
96 if (strcmp(name, "__name__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
97 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
98 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
99 if (strcmp(name, "__doc__") == 0) {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
100 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
101 if (doc != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
102 return PyString_FromString(doc);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
103 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
104 return Py_None;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
105 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
106 if (strcmp(name, "__self__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
107 PyObject *self;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
108 if (PyEval_GetRestricted()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
109 PyErr_SetString(PyExc_RuntimeError,
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
110 "method.__self__ not accessible in restricted mode");
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
111 return NULL;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
112 }
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
113 self = m->m_self;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
114 if (self == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
115 self = Py_None;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
116 Py_INCREF(self);
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
117 return self;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
118 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
119 if (strcmp(name, "__members__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
120 return Py_BuildValue("[sss]",
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
121 "__doc__", "__name__", "__self__");
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
122 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
123 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
124 return NULL;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
125 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
126
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
127 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 meth_repr(m)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
129 PyCFunctionObject *m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 char buf[200];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 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
133 sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 else
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
135 sprintf(buf,
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13141
diff changeset
136 "<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
137 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
138 m->m_self);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
139 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
142 static int
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
143 meth_compare(a, b)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
144 PyCFunctionObject *a, *b;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
145 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
146 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
147 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
148 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
149 return 0;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
150 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
151 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
152 else
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
153 return 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
154 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
155
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
156 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
157 meth_hash(a)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
158 PyCFunctionObject *a;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
159 {
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
160 long x,y;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
161 if (a->m_self == NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
162 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
163 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
164 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
165 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
166 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
167 }
13252
310d7cbba329 Fix an error on AIX by using a proper cast.
Guido van Rossum <guido@python.org>
parents: 13210
diff changeset
168 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
169 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
170 return -1;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
171 x ^= y;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
172 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
173 x = -2;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
174 return x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
175 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
176
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
177 PyTypeObject PyCFunction_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
178 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 0,
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
180 "builtin_function_or_method",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
181 sizeof(PyCFunctionObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 0,
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
183 (destructor)meth_dealloc, /*tp_dealloc*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
184 0, /*tp_print*/
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
185 (getattrfunc)meth_getattr, /*tp_getattr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 0, /*tp_setattr*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
187 (cmpfunc)meth_compare, /*tp_compare*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
188 (reprfunc)meth_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 0, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 0, /*tp_as_mapping*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
192 (hashfunc)meth_hash, /*tp_hash*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 };
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
194
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
195 /* List all methods in a chain -- helper for findmethodinchain */
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
196
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
197 static PyObject *
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
198 listmethodchain(chain)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
199 PyMethodChain *chain;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
200 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
201 PyMethodChain *c;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
202 PyMethodDef *ml;
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
203 int i, n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
204 PyObject *v;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
205
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
206 n = 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++)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
209 n++;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
210 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
211 v = PyList_New(n);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
212 if (v == NULL)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
213 return NULL;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
214 i = 0;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
215 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
216 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
217 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
218 i++;
395
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 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
221 if (PyErr_Occurred()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
222 Py_DECREF(v);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
223 return NULL;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
224 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
225 PyList_Sort(v);
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
226 return v;
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
227 }
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
228
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
229 /* Find a method in a method chain */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
230
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
231 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
232 Py_FindMethodInChain(chain, self, name)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
233 PyMethodChain *chain;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
234 PyObject *self;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
235 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
236 {
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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 }
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
245 }
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
246 while (chain != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
247 PyMethodDef *ml = chain->methods;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
248 for (; ml->ml_name != NULL; ml++) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
249 if (name[0] == ml->ml_name[0] &&
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
250 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
251 return PyCFunction_New(ml, self);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
252 }
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
253 chain = chain->link;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
254 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
255 PyErr_SetString(PyExc_AttributeError, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
256 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
257 }
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
258
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
259 /* 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
260
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
261 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
262 Py_FindMethod(methods, self, name)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
263 PyMethodDef *methods;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
264 PyObject *self;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
265 char *name;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
266 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
267 PyMethodChain chain;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
268 chain.methods = methods;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
269 chain.link = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
270 return Py_FindMethodInChain(&chain, self, name);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
271 }
5934
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
272
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
273 /* 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
274
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
275 void
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
276 PyCFunction_Fini()
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
277 {
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
278 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
279 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
280 free_list = (PyCFunctionObject *)(v->m_self);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
281 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
282 }
0369d63b100b Added separate free list for cfunction (builtin method) objects, for a
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
283 }