Mercurial > cpython-lru_cache-weakref
comparison Objects/methodobject.c @ 61467:f197407bdba2 3.1
Recorded merge of revisions 81032 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
................
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 May 2010 16:14:21 +0000 |
| parents | 87fbbf22adf3 |
| children | e8969fb822fc |
comparison
equal
deleted
inserted
replaced
| 61459:f8c19c5f5dd2 | 61467:f197407bdba2 |
|---|---|
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 PyObject * | 16 PyObject * |
| 17 PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) | 17 PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
| 18 { | 18 { |
| 19 PyCFunctionObject *op; | 19 PyCFunctionObject *op; |
| 20 op = free_list; | 20 op = free_list; |
| 21 if (op != NULL) { | 21 if (op != NULL) { |
| 22 free_list = (PyCFunctionObject *)(op->m_self); | 22 free_list = (PyCFunctionObject *)(op->m_self); |
| 23 PyObject_INIT(op, &PyCFunction_Type); | 23 PyObject_INIT(op, &PyCFunction_Type); |
| 24 numfree--; | 24 numfree--; |
| 25 } | 25 } |
| 26 else { | 26 else { |
| 27 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); | 27 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
| 28 if (op == NULL) | 28 if (op == NULL) |
| 29 return NULL; | 29 return NULL; |
| 30 } | 30 } |
| 31 op->m_ml = ml; | 31 op->m_ml = ml; |
| 32 Py_XINCREF(self); | 32 Py_XINCREF(self); |
| 33 op->m_self = self; | 33 op->m_self = self; |
| 34 Py_XINCREF(module); | 34 Py_XINCREF(module); |
| 35 op->m_module = module; | 35 op->m_module = module; |
| 36 _PyObject_GC_TRACK(op); | 36 _PyObject_GC_TRACK(op); |
| 37 return (PyObject *)op; | 37 return (PyObject *)op; |
| 38 } | 38 } |
| 39 | 39 |
| 40 PyCFunction | 40 PyCFunction |
| 41 PyCFunction_GetFunction(PyObject *op) | 41 PyCFunction_GetFunction(PyObject *op) |
| 42 { | 42 { |
| 43 if (!PyCFunction_Check(op)) { | 43 if (!PyCFunction_Check(op)) { |
| 44 PyErr_BadInternalCall(); | 44 PyErr_BadInternalCall(); |
| 45 return NULL; | 45 return NULL; |
| 46 } | 46 } |
| 47 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth; | 47 return ((PyCFunctionObject *)op) -> m_ml -> ml_meth; |
| 48 } | 48 } |
| 49 | 49 |
| 50 PyObject * | 50 PyObject * |
| 51 PyCFunction_GetSelf(PyObject *op) | 51 PyCFunction_GetSelf(PyObject *op) |
| 52 { | 52 { |
| 53 if (!PyCFunction_Check(op)) { | 53 if (!PyCFunction_Check(op)) { |
| 54 PyErr_BadInternalCall(); | 54 PyErr_BadInternalCall(); |
| 55 return NULL; | 55 return NULL; |
| 56 } | 56 } |
| 57 return ((PyCFunctionObject *)op) -> m_self; | 57 return ((PyCFunctionObject *)op) -> m_self; |
| 58 } | 58 } |
| 59 | 59 |
| 60 int | 60 int |
| 61 PyCFunction_GetFlags(PyObject *op) | 61 PyCFunction_GetFlags(PyObject *op) |
| 62 { | 62 { |
| 63 if (!PyCFunction_Check(op)) { | 63 if (!PyCFunction_Check(op)) { |
| 64 PyErr_BadInternalCall(); | 64 PyErr_BadInternalCall(); |
| 65 return -1; | 65 return -1; |
| 66 } | 66 } |
| 67 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; | 67 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; |
| 68 } | 68 } |
| 69 | 69 |
| 70 PyObject * | 70 PyObject * |
| 71 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) | 71 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 72 { | 72 { |
| 73 PyCFunctionObject* f = (PyCFunctionObject*)func; | 73 PyCFunctionObject* f = (PyCFunctionObject*)func; |
| 74 PyCFunction meth = PyCFunction_GET_FUNCTION(func); | 74 PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 75 PyObject *self = PyCFunction_GET_SELF(func); | 75 PyObject *self = PyCFunction_GET_SELF(func); |
| 76 Py_ssize_t size; | 76 Py_ssize_t size; |
| 77 | 77 |
| 78 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { | 78 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { |
| 79 case METH_VARARGS: | 79 case METH_VARARGS: |
| 80 if (kw == NULL || PyDict_Size(kw) == 0) | 80 if (kw == NULL || PyDict_Size(kw) == 0) |
| 81 return (*meth)(self, arg); | 81 return (*meth)(self, arg); |
| 82 break; | 82 break; |
| 83 case METH_VARARGS | METH_KEYWORDS: | 83 case METH_VARARGS | METH_KEYWORDS: |
| 84 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); | 84 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); |
| 85 case METH_NOARGS: | 85 case METH_NOARGS: |
| 86 if (kw == NULL || PyDict_Size(kw) == 0) { | 86 if (kw == NULL || PyDict_Size(kw) == 0) { |
| 87 size = PyTuple_GET_SIZE(arg); | 87 size = PyTuple_GET_SIZE(arg); |
| 88 if (size == 0) | 88 if (size == 0) |
| 89 return (*meth)(self, NULL); | 89 return (*meth)(self, NULL); |
| 90 PyErr_Format(PyExc_TypeError, | 90 PyErr_Format(PyExc_TypeError, |
| 91 "%.200s() takes no arguments (%zd given)", | 91 "%.200s() takes no arguments (%zd given)", |
| 92 f->m_ml->ml_name, size); | 92 f->m_ml->ml_name, size); |
| 93 return NULL; | 93 return NULL; |
| 94 } | 94 } |
| 95 break; | 95 break; |
| 96 case METH_O: | 96 case METH_O: |
| 97 if (kw == NULL || PyDict_Size(kw) == 0) { | 97 if (kw == NULL || PyDict_Size(kw) == 0) { |
| 98 size = PyTuple_GET_SIZE(arg); | 98 size = PyTuple_GET_SIZE(arg); |
| 99 if (size == 1) | 99 if (size == 1) |
| 100 return (*meth)(self, PyTuple_GET_ITEM(arg, 0)); | 100 return (*meth)(self, PyTuple_GET_ITEM(arg, 0)); |
| 101 PyErr_Format(PyExc_TypeError, | 101 PyErr_Format(PyExc_TypeError, |
| 102 "%.200s() takes exactly one argument (%zd given)", | 102 "%.200s() takes exactly one argument (%zd given)", |
| 103 f->m_ml->ml_name, size); | 103 f->m_ml->ml_name, size); |
| 104 return NULL; | 104 return NULL; |
| 105 } | 105 } |
| 106 break; | 106 break; |
| 107 default: | 107 default: |
| 108 PyErr_SetString(PyExc_SystemError, "Bad call flags in " | 108 PyErr_SetString(PyExc_SystemError, "Bad call flags in " |
| 109 "PyCFunction_Call. METH_OLDARGS is no " | 109 "PyCFunction_Call. METH_OLDARGS is no " |
| 110 "longer supported!"); | 110 "longer supported!"); |
| 111 | 111 |
| 112 return NULL; | 112 return NULL; |
| 113 } | 113 } |
| 114 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", | 114 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 115 f->m_ml->ml_name); | 115 f->m_ml->ml_name); |
| 116 return NULL; | 116 return NULL; |
| 117 } | 117 } |
| 118 | 118 |
| 119 /* Methods (the standard built-in methods, that is) */ | 119 /* Methods (the standard built-in methods, that is) */ |
| 120 | 120 |
| 121 static void | 121 static void |
| 122 meth_dealloc(PyCFunctionObject *m) | 122 meth_dealloc(PyCFunctionObject *m) |
| 123 { | 123 { |
| 124 _PyObject_GC_UNTRACK(m); | 124 _PyObject_GC_UNTRACK(m); |
| 125 Py_XDECREF(m->m_self); | 125 Py_XDECREF(m->m_self); |
| 126 Py_XDECREF(m->m_module); | 126 Py_XDECREF(m->m_module); |
| 127 if (numfree < PyCFunction_MAXFREELIST) { | 127 if (numfree < PyCFunction_MAXFREELIST) { |
| 128 m->m_self = (PyObject *)free_list; | 128 m->m_self = (PyObject *)free_list; |
| 129 free_list = m; | 129 free_list = m; |
| 130 numfree++; | 130 numfree++; |
| 131 } | 131 } |
| 132 else { | 132 else { |
| 133 PyObject_GC_Del(m); | 133 PyObject_GC_Del(m); |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 static PyObject * | 137 static PyObject * |
| 138 meth_get__doc__(PyCFunctionObject *m, void *closure) | 138 meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 139 { | 139 { |
| 140 const char *doc = m->m_ml->ml_doc; | 140 const char *doc = m->m_ml->ml_doc; |
| 141 | 141 |
| 142 if (doc != NULL) | 142 if (doc != NULL) |
| 143 return PyUnicode_FromString(doc); | 143 return PyUnicode_FromString(doc); |
| 144 Py_INCREF(Py_None); | 144 Py_INCREF(Py_None); |
| 145 return Py_None; | 145 return Py_None; |
| 146 } | 146 } |
| 147 | 147 |
| 148 static PyObject * | 148 static PyObject * |
| 149 meth_get__name__(PyCFunctionObject *m, void *closure) | 149 meth_get__name__(PyCFunctionObject *m, void *closure) |
| 150 { | 150 { |
| 151 return PyUnicode_FromString(m->m_ml->ml_name); | 151 return PyUnicode_FromString(m->m_ml->ml_name); |
| 152 } | 152 } |
| 153 | 153 |
| 154 static int | 154 static int |
| 155 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) | 155 meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 156 { | 156 { |
| 157 Py_VISIT(m->m_self); | 157 Py_VISIT(m->m_self); |
| 158 Py_VISIT(m->m_module); | 158 Py_VISIT(m->m_module); |
| 159 return 0; | 159 return 0; |
| 160 } | 160 } |
| 161 | 161 |
| 162 static PyObject * | 162 static PyObject * |
| 163 meth_get__self__(PyCFunctionObject *m, void *closure) | 163 meth_get__self__(PyCFunctionObject *m, void *closure) |
| 164 { | 164 { |
| 165 PyObject *self; | 165 PyObject *self; |
| 166 | 166 |
| 167 self = m->m_self; | 167 self = m->m_self; |
| 168 if (self == NULL) | 168 if (self == NULL) |
| 169 self = Py_None; | 169 self = Py_None; |
| 170 Py_INCREF(self); | 170 Py_INCREF(self); |
| 171 return self; | 171 return self; |
| 172 } | 172 } |
| 173 | 173 |
| 174 static PyGetSetDef meth_getsets [] = { | 174 static PyGetSetDef meth_getsets [] = { |
| 175 {"__doc__", (getter)meth_get__doc__, NULL, NULL}, | 175 {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
| 176 {"__name__", (getter)meth_get__name__, NULL, NULL}, | 176 {"__name__", (getter)meth_get__name__, NULL, NULL}, |
| 177 {"__self__", (getter)meth_get__self__, NULL, NULL}, | 177 {"__self__", (getter)meth_get__self__, NULL, NULL}, |
| 178 {0} | 178 {0} |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 #define OFF(x) offsetof(PyCFunctionObject, x) | 181 #define OFF(x) offsetof(PyCFunctionObject, x) |
| 182 | 182 |
| 183 static PyMemberDef meth_members[] = { | 183 static PyMemberDef meth_members[] = { |
| 184 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, | 184 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
| 185 {NULL} | 185 {NULL} |
| 186 }; | 186 }; |
| 187 | 187 |
| 188 static PyObject * | 188 static PyObject * |
| 189 meth_repr(PyCFunctionObject *m) | 189 meth_repr(PyCFunctionObject *m) |
| 190 { | 190 { |
| 191 if (m->m_self == NULL || PyModule_Check(m->m_self)) | 191 if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 192 return PyUnicode_FromFormat("<built-in function %s>", | 192 return PyUnicode_FromFormat("<built-in function %s>", |
| 193 m->m_ml->ml_name); | 193 m->m_ml->ml_name); |
| 194 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", | 194 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
| 195 m->m_ml->ml_name, | 195 m->m_ml->ml_name, |
| 196 m->m_self->ob_type->tp_name, | 196 m->m_self->ob_type->tp_name, |
| 197 m->m_self); | 197 m->m_self); |
| 198 } | 198 } |
| 199 | 199 |
| 200 static PyObject * | 200 static PyObject * |
| 201 meth_richcompare(PyObject *self, PyObject *other, int op) | 201 meth_richcompare(PyObject *self, PyObject *other, int op) |
| 202 { | 202 { |
| 203 PyCFunctionObject *a, *b; | 203 PyCFunctionObject *a, *b; |
| 204 PyObject *res; | 204 PyObject *res; |
| 205 int eq; | 205 int eq; |
| 206 | 206 |
| 207 if ((op != Py_EQ && op != Py_NE) || | 207 if ((op != Py_EQ && op != Py_NE) || |
| 208 !PyCFunction_Check(self) || | 208 !PyCFunction_Check(self) || |
| 209 !PyCFunction_Check(other)) | 209 !PyCFunction_Check(other)) |
| 210 { | 210 { |
| 211 Py_INCREF(Py_NotImplemented); | 211 Py_INCREF(Py_NotImplemented); |
| 212 return Py_NotImplemented; | 212 return Py_NotImplemented; |
| 213 } | 213 } |
| 214 a = (PyCFunctionObject *)self; | 214 a = (PyCFunctionObject *)self; |
| 215 b = (PyCFunctionObject *)other; | 215 b = (PyCFunctionObject *)other; |
| 216 eq = a->m_self == b->m_self; | 216 eq = a->m_self == b->m_self; |
| 217 if (eq) | 217 if (eq) |
| 218 eq = a->m_ml->ml_meth == b->m_ml->ml_meth; | 218 eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
| 219 if (op == Py_EQ) | 219 if (op == Py_EQ) |
| 220 res = eq ? Py_True : Py_False; | 220 res = eq ? Py_True : Py_False; |
| 221 else | 221 else |
| 222 res = eq ? Py_False : Py_True; | 222 res = eq ? Py_False : Py_True; |
| 223 Py_INCREF(res); | 223 Py_INCREF(res); |
| 224 return res; | 224 return res; |
| 225 } | 225 } |
| 226 | 226 |
| 227 static long | 227 static long |
| 228 meth_hash(PyCFunctionObject *a) | 228 meth_hash(PyCFunctionObject *a) |
| 229 { | 229 { |
| 230 long x,y; | 230 long x,y; |
| 231 if (a->m_self == NULL) | 231 if (a->m_self == NULL) |
| 232 x = 0; | 232 x = 0; |
| 233 else { | 233 else { |
| 234 x = PyObject_Hash(a->m_self); | 234 x = PyObject_Hash(a->m_self); |
| 235 if (x == -1) | 235 if (x == -1) |
| 236 return -1; | 236 return -1; |
| 237 } | 237 } |
| 238 y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); | 238 y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
| 239 if (y == -1) | 239 if (y == -1) |
| 240 return -1; | 240 return -1; |
| 241 x ^= y; | 241 x ^= y; |
| 242 if (x == -1) | 242 if (x == -1) |
| 243 x = -2; | 243 x = -2; |
| 244 return x; | 244 return x; |
| 245 } | 245 } |
| 246 | 246 |
| 247 | 247 |
| 248 PyTypeObject PyCFunction_Type = { | 248 PyTypeObject PyCFunction_Type = { |
| 249 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 249 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 250 "builtin_function_or_method", | 250 "builtin_function_or_method", |
| 251 sizeof(PyCFunctionObject), | 251 sizeof(PyCFunctionObject), |
| 252 0, | 252 0, |
| 253 (destructor)meth_dealloc, /* tp_dealloc */ | 253 (destructor)meth_dealloc, /* tp_dealloc */ |
| 254 0, /* tp_print */ | 254 0, /* tp_print */ |
| 255 0, /* tp_getattr */ | 255 0, /* tp_getattr */ |
| 256 0, /* tp_setattr */ | 256 0, /* tp_setattr */ |
| 257 0, /* tp_reserved */ | 257 0, /* tp_reserved */ |
| 258 (reprfunc)meth_repr, /* tp_repr */ | 258 (reprfunc)meth_repr, /* tp_repr */ |
| 259 0, /* tp_as_number */ | 259 0, /* tp_as_number */ |
| 260 0, /* tp_as_sequence */ | 260 0, /* tp_as_sequence */ |
| 261 0, /* tp_as_mapping */ | 261 0, /* tp_as_mapping */ |
| 262 (hashfunc)meth_hash, /* tp_hash */ | 262 (hashfunc)meth_hash, /* tp_hash */ |
| 263 PyCFunction_Call, /* tp_call */ | 263 PyCFunction_Call, /* tp_call */ |
| 264 0, /* tp_str */ | 264 0, /* tp_str */ |
| 265 PyObject_GenericGetAttr, /* tp_getattro */ | 265 PyObject_GenericGetAttr, /* tp_getattro */ |
| 266 0, /* tp_setattro */ | 266 0, /* tp_setattro */ |
| 267 0, /* tp_as_buffer */ | 267 0, /* tp_as_buffer */ |
| 268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 269 0, /* tp_doc */ | 269 0, /* tp_doc */ |
| 270 (traverseproc)meth_traverse, /* tp_traverse */ | 270 (traverseproc)meth_traverse, /* tp_traverse */ |
| 271 0, /* tp_clear */ | 271 0, /* tp_clear */ |
| 272 meth_richcompare, /* tp_richcompare */ | 272 meth_richcompare, /* tp_richcompare */ |
| 273 0, /* tp_weaklistoffset */ | 273 0, /* tp_weaklistoffset */ |
| 274 0, /* tp_iter */ | 274 0, /* tp_iter */ |
| 275 0, /* tp_iternext */ | 275 0, /* tp_iternext */ |
| 276 0, /* tp_methods */ | 276 0, /* tp_methods */ |
| 277 meth_members, /* tp_members */ | 277 meth_members, /* tp_members */ |
| 278 meth_getsets, /* tp_getset */ | 278 meth_getsets, /* tp_getset */ |
| 279 0, /* tp_base */ | 279 0, /* tp_base */ |
| 280 0, /* tp_dict */ | 280 0, /* tp_dict */ |
| 281 }; | 281 }; |
| 282 | 282 |
| 283 /* Clear out the free list */ | 283 /* Clear out the free list */ |
| 284 | 284 |
| 285 int | 285 int |
| 286 PyCFunction_ClearFreeList(void) | 286 PyCFunction_ClearFreeList(void) |
| 287 { | 287 { |
| 288 int freelist_size = numfree; | 288 int freelist_size = numfree; |
| 289 | 289 |
| 290 while (free_list) { | 290 while (free_list) { |
| 291 PyCFunctionObject *v = free_list; | 291 PyCFunctionObject *v = free_list; |
| 292 free_list = (PyCFunctionObject *)(v->m_self); | 292 free_list = (PyCFunctionObject *)(v->m_self); |
| 293 PyObject_GC_Del(v); | 293 PyObject_GC_Del(v); |
| 294 numfree--; | 294 numfree--; |
| 295 } | 295 } |
| 296 assert(numfree == 0); | 296 assert(numfree == 0); |
| 297 return freelist_size; | 297 return freelist_size; |
| 298 } | 298 } |
| 299 | 299 |
| 300 void | 300 void |
| 301 PyCFunction_Fini(void) | 301 PyCFunction_Fini(void) |
| 302 { | 302 { |
| 303 (void)PyCFunction_ClearFreeList(); | 303 (void)PyCFunction_ClearFreeList(); |
| 304 } | 304 } |
| 305 | 305 |
| 306 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(), | 306 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(), |
| 307 but it's part of the API so we need to keep a function around that | 307 but it's part of the API so we need to keep a function around that |
| 308 existing C extensions can call. | 308 existing C extensions can call. |
| 312 PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); | 312 PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); |
| 313 | 313 |
| 314 PyObject * | 314 PyObject * |
| 315 PyCFunction_New(PyMethodDef *ml, PyObject *self) | 315 PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 316 { | 316 { |
| 317 return PyCFunction_NewEx(ml, self, NULL); | 317 return PyCFunction_NewEx(ml, self, NULL); |
| 318 } | 318 } |
