~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Objects/classobject.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Class object implementation (dead now except for methods) */
 
2
 
 
3
#include "Python.h"
 
4
#include "structmember.h"
 
5
 
 
6
#define TP_DESCR_GET(t) ((t)->tp_descr_get)
 
7
 
 
8
/* Free list for method objects to safe malloc/free overhead
 
9
 * The im_self element is used to chain the elements.
 
10
 */
 
11
static PyMethodObject *free_list;
 
12
static int numfree = 0;
 
13
#ifndef PyMethod_MAXFREELIST
 
14
#define PyMethod_MAXFREELIST 256
 
15
#endif
 
16
 
 
17
_Py_IDENTIFIER(__name__);
 
18
 
 
19
PyObject *
 
20
PyMethod_Function(PyObject *im)
 
21
{
 
22
    if (!PyMethod_Check(im)) {
 
23
        PyErr_BadInternalCall();
 
24
        return NULL;
 
25
    }
 
26
    return ((PyMethodObject *)im)->im_func;
 
27
}
 
28
 
 
29
PyObject *
 
30
PyMethod_Self(PyObject *im)
 
31
{
 
32
    if (!PyMethod_Check(im)) {
 
33
        PyErr_BadInternalCall();
 
34
        return NULL;
 
35
    }
 
36
    return ((PyMethodObject *)im)->im_self;
 
37
}
 
38
 
 
39
/* Method objects are used for bound instance methods returned by
 
40
   instancename.methodname. ClassName.methodname returns an ordinary
 
41
   function.
 
42
*/
 
43
 
 
44
PyObject *
 
45
PyMethod_New(PyObject *func, PyObject *self)
 
46
{
 
47
    PyMethodObject *im;
 
48
    if (self == NULL) {
 
49
        PyErr_BadInternalCall();
 
50
        return NULL;
 
51
    }
 
52
    im = free_list;
 
53
    if (im != NULL) {
 
54
        free_list = (PyMethodObject *)(im->im_self);
 
55
        PyObject_INIT(im, &PyMethod_Type);
 
56
        numfree--;
 
57
    }
 
58
    else {
 
59
        im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
 
60
        if (im == NULL)
 
61
            return NULL;
 
62
    }
 
63
    im->im_weakreflist = NULL;
 
64
    Py_INCREF(func);
 
65
    im->im_func = func;
 
66
    Py_XINCREF(self);
 
67
    im->im_self = self;
 
68
    _PyObject_GC_TRACK(im);
 
69
    return (PyObject *)im;
 
70
}
 
71
 
 
72
static PyObject *
 
73
method_reduce(PyMethodObject *im)
 
74
{
 
75
    PyObject *self = PyMethod_GET_SELF(im);
 
76
    PyObject *func = PyMethod_GET_FUNCTION(im);
 
77
    PyObject *builtins;
 
78
    PyObject *getattr;
 
79
    PyObject *funcname;
 
80
    _Py_IDENTIFIER(getattr);
 
81
 
 
82
    funcname = _PyObject_GetAttrId(func, &PyId___name__);
 
83
    if (funcname == NULL) {
 
84
        return NULL;
 
85
    }
 
86
    builtins = PyEval_GetBuiltins();
 
87
    getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
 
88
    return Py_BuildValue("O(ON)", getattr, self, funcname);
 
89
}
 
90
 
 
91
static PyMethodDef method_methods[] = {
 
92
    {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
 
93
    {NULL, NULL}
 
94
};
 
95
 
 
96
/* Descriptors for PyMethod attributes */
 
97
 
 
98
/* im_func and im_self are stored in the PyMethod object */
 
99
 
 
100
#define MO_OFF(x) offsetof(PyMethodObject, x)
 
101
 
 
102
static PyMemberDef method_memberlist[] = {
 
103
    {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
 
104
     "the function (or other callable) implementing a method"},
 
105
    {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
 
106
     "the instance to which a method is bound"},
 
107
    {NULL}      /* Sentinel */
 
108
};
 
109
 
 
110
/* Christian Tismer argued convincingly that method attributes should
 
111
   (nearly) always override function attributes.
 
112
   The one exception is __doc__; there's a default __doc__ which
 
113
   should only be used for the class, not for instances */
 
114
 
 
115
static PyObject *
 
116
method_get_doc(PyMethodObject *im, void *context)
 
117
{
 
118
    static PyObject *docstr;
 
119
    if (docstr == NULL) {
 
120
        docstr= PyUnicode_InternFromString("__doc__");
 
121
        if (docstr == NULL)
 
122
            return NULL;
 
123
    }
 
124
    return PyObject_GetAttr(im->im_func, docstr);
 
125
}
 
126
 
 
127
static PyGetSetDef method_getset[] = {
 
128
    {"__doc__", (getter)method_get_doc, NULL, NULL},
 
129
    {0}
 
130
};
 
131
 
 
132
static PyObject *
 
133
method_getattro(PyObject *obj, PyObject *name)
 
134
{
 
135
    PyMethodObject *im = (PyMethodObject *)obj;
 
136
    PyTypeObject *tp = obj->ob_type;
 
137
    PyObject *descr = NULL;
 
138
 
 
139
    {
 
140
        if (tp->tp_dict == NULL) {
 
141
            if (PyType_Ready(tp) < 0)
 
142
                return NULL;
 
143
        }
 
144
        descr = _PyType_Lookup(tp, name);
 
145
    }
 
146
 
 
147
    if (descr != NULL) {
 
148
        descrgetfunc f = TP_DESCR_GET(descr->ob_type);
 
149
        if (f != NULL)
 
150
            return f(descr, obj, (PyObject *)obj->ob_type);
 
151
        else {
 
152
            Py_INCREF(descr);
 
153
            return descr;
 
154
        }
 
155
    }
 
156
 
 
157
    return PyObject_GetAttr(im->im_func, name);
 
158
}
 
159
 
 
160
PyDoc_STRVAR(method_doc,
 
161
"method(function, instance)\n\
 
162
\n\
 
163
Create a bound instance method object.");
 
164
 
 
165
static PyObject *
 
166
method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
 
167
{
 
168
    PyObject *func;
 
169
    PyObject *self;
 
170
 
 
171
    if (!_PyArg_NoKeywords("method", kw))
 
172
        return NULL;
 
173
    if (!PyArg_UnpackTuple(args, "method", 2, 2,
 
174
                          &func, &self))
 
175
        return NULL;
 
176
    if (!PyCallable_Check(func)) {
 
177
        PyErr_SetString(PyExc_TypeError,
 
178
                        "first argument must be callable");
 
179
        return NULL;
 
180
    }
 
181
    if (self == NULL || self == Py_None) {
 
182
        PyErr_SetString(PyExc_TypeError,
 
183
            "self must not be None");
 
184
        return NULL;
 
185
    }
 
186
 
 
187
    return PyMethod_New(func, self);
 
188
}
 
189
 
 
190
static void
 
191
method_dealloc(PyMethodObject *im)
 
192
{
 
193
    _PyObject_GC_UNTRACK(im);
 
194
    if (im->im_weakreflist != NULL)
 
195
        PyObject_ClearWeakRefs((PyObject *)im);
 
196
    Py_DECREF(im->im_func);
 
197
    Py_XDECREF(im->im_self);
 
198
    if (numfree < PyMethod_MAXFREELIST) {
 
199
        im->im_self = (PyObject *)free_list;
 
200
        free_list = im;
 
201
        numfree++;
 
202
    }
 
203
    else {
 
204
        PyObject_GC_Del(im);
 
205
    }
 
206
}
 
207
 
 
208
static PyObject *
 
209
method_richcompare(PyObject *self, PyObject *other, int op)
 
210
{
 
211
    PyMethodObject *a, *b;
 
212
    PyObject *res;
 
213
    int eq;
 
214
 
 
215
    if ((op != Py_EQ && op != Py_NE) ||
 
216
        !PyMethod_Check(self) ||
 
217
        !PyMethod_Check(other))
 
218
    {
 
219
        Py_RETURN_NOTIMPLEMENTED;
 
220
    }
 
221
    a = (PyMethodObject *)self;
 
222
    b = (PyMethodObject *)other;
 
223
    eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
 
224
    if (eq == 1) {
 
225
        if (a->im_self == NULL || b->im_self == NULL)
 
226
            eq = a->im_self == b->im_self;
 
227
        else
 
228
            eq = PyObject_RichCompareBool(a->im_self, b->im_self,
 
229
                                          Py_EQ);
 
230
    }
 
231
    if (eq < 0)
 
232
        return NULL;
 
233
    if (op == Py_EQ)
 
234
        res = eq ? Py_True : Py_False;
 
235
    else
 
236
        res = eq ? Py_False : Py_True;
 
237
    Py_INCREF(res);
 
238
    return res;
 
239
}
 
240
 
 
241
static PyObject *
 
242
method_repr(PyMethodObject *a)
 
243
{
 
244
    PyObject *self = a->im_self;
 
245
    PyObject *func = a->im_func;
 
246
    PyObject *klass;
 
247
    PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
 
248
    char *defname = "?";
 
249
 
 
250
    if (self == NULL) {
 
251
        PyErr_BadInternalCall();
 
252
        return NULL;
 
253
    }
 
254
    klass = (PyObject*)Py_TYPE(self);
 
255
 
 
256
    funcname = _PyObject_GetAttrId(func, &PyId___name__);
 
257
    if (funcname == NULL) {
 
258
        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
 
259
            return NULL;
 
260
        PyErr_Clear();
 
261
    }
 
262
    else if (!PyUnicode_Check(funcname)) {
 
263
        Py_DECREF(funcname);
 
264
        funcname = NULL;
 
265
    }
 
266
 
 
267
    if (klass == NULL)
 
268
        klassname = NULL;
 
269
    else {
 
270
        klassname = _PyObject_GetAttrId(klass, &PyId___name__);
 
271
        if (klassname == NULL) {
 
272
            if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
 
273
                Py_XDECREF(funcname);
 
274
                return NULL;
 
275
            }
 
276
            PyErr_Clear();
 
277
        }
 
278
        else if (!PyUnicode_Check(klassname)) {
 
279
            Py_DECREF(klassname);
 
280
            klassname = NULL;
 
281
        }
 
282
    }
 
283
 
 
284
    /* XXX Shouldn't use repr()/%R here! */
 
285
    result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
 
286
                                  klassname, defname,
 
287
                                  funcname, defname, self);
 
288
 
 
289
    Py_XDECREF(funcname);
 
290
    Py_XDECREF(klassname);
 
291
    return result;
 
292
}
 
293
 
 
294
static Py_hash_t
 
295
method_hash(PyMethodObject *a)
 
296
{
 
297
    Py_hash_t x, y;
 
298
    if (a->im_self == NULL)
 
299
        x = PyObject_Hash(Py_None);
 
300
    else
 
301
        x = PyObject_Hash(a->im_self);
 
302
    if (x == -1)
 
303
        return -1;
 
304
    y = PyObject_Hash(a->im_func);
 
305
    if (y == -1)
 
306
        return -1;
 
307
    x = x ^ y;
 
308
    if (x == -1)
 
309
        x = -2;
 
310
    return x;
 
311
}
 
312
 
 
313
static int
 
314
method_traverse(PyMethodObject *im, visitproc visit, void *arg)
 
315
{
 
316
    Py_VISIT(im->im_func);
 
317
    Py_VISIT(im->im_self);
 
318
    return 0;
 
319
}
 
320
 
 
321
static PyObject *
 
322
method_call(PyObject *func, PyObject *arg, PyObject *kw)
 
323
{
 
324
    PyObject *self = PyMethod_GET_SELF(func);
 
325
    PyObject *result;
 
326
 
 
327
    func = PyMethod_GET_FUNCTION(func);
 
328
    if (self == NULL) {
 
329
        PyErr_BadInternalCall();
 
330
        return NULL;
 
331
    }
 
332
    else {
 
333
        Py_ssize_t argcount = PyTuple_Size(arg);
 
334
        PyObject *newarg = PyTuple_New(argcount + 1);
 
335
        int i;
 
336
        if (newarg == NULL)
 
337
            return NULL;
 
338
        Py_INCREF(self);
 
339
        PyTuple_SET_ITEM(newarg, 0, self);
 
340
        for (i = 0; i < argcount; i++) {
 
341
            PyObject *v = PyTuple_GET_ITEM(arg, i);
 
342
            Py_XINCREF(v);
 
343
            PyTuple_SET_ITEM(newarg, i+1, v);
 
344
        }
 
345
        arg = newarg;
 
346
    }
 
347
    result = PyObject_Call((PyObject *)func, arg, kw);
 
348
    Py_DECREF(arg);
 
349
    return result;
 
350
}
 
351
 
 
352
static PyObject *
 
353
method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
 
354
{
 
355
    /* Don't rebind an already bound method of a class that's not a base
 
356
       class of cls. */
 
357
    if (PyMethod_GET_SELF(meth) != NULL) {
 
358
        /* Already bound */
 
359
        Py_INCREF(meth);
 
360
        return meth;
 
361
    }
 
362
    /* Bind it to obj */
 
363
    return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
 
364
}
 
365
 
 
366
PyTypeObject PyMethod_Type = {
 
367
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
 
368
    "method",
 
369
    sizeof(PyMethodObject),
 
370
    0,
 
371
    (destructor)method_dealloc,                 /* tp_dealloc */
 
372
    0,                                          /* tp_print */
 
373
    0,                                          /* tp_getattr */
 
374
    0,                                          /* tp_setattr */
 
375
    0,                                          /* tp_reserved */
 
376
    (reprfunc)method_repr,                      /* tp_repr */
 
377
    0,                                          /* tp_as_number */
 
378
    0,                                          /* tp_as_sequence */
 
379
    0,                                          /* tp_as_mapping */
 
380
    (hashfunc)method_hash,                      /* tp_hash */
 
381
    method_call,                                /* tp_call */
 
382
    0,                                          /* tp_str */
 
383
    method_getattro,                            /* tp_getattro */
 
384
    PyObject_GenericSetAttr,                    /* tp_setattro */
 
385
    0,                                          /* tp_as_buffer */
 
386
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
 
387
    method_doc,                                 /* tp_doc */
 
388
    (traverseproc)method_traverse,              /* tp_traverse */
 
389
    0,                                          /* tp_clear */
 
390
    method_richcompare,                         /* tp_richcompare */
 
391
    offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
 
392
    0,                                          /* tp_iter */
 
393
    0,                                          /* tp_iternext */
 
394
    method_methods,                             /* tp_methods */
 
395
    method_memberlist,                          /* tp_members */
 
396
    method_getset,                              /* tp_getset */
 
397
    0,                                          /* tp_base */
 
398
    0,                                          /* tp_dict */
 
399
    method_descr_get,                           /* tp_descr_get */
 
400
    0,                                          /* tp_descr_set */
 
401
    0,                                          /* tp_dictoffset */
 
402
    0,                                          /* tp_init */
 
403
    0,                                          /* tp_alloc */
 
404
    method_new,                                 /* tp_new */
 
405
};
 
406
 
 
407
/* Clear out the free list */
 
408
 
 
409
int
 
410
PyMethod_ClearFreeList(void)
 
411
{
 
412
    int freelist_size = numfree;
 
413
 
 
414
    while (free_list) {
 
415
        PyMethodObject *im = free_list;
 
416
        free_list = (PyMethodObject *)(im->im_self);
 
417
        PyObject_GC_Del(im);
 
418
        numfree--;
 
419
    }
 
420
    assert(numfree == 0);
 
421
    return freelist_size;
 
422
}
 
423
 
 
424
void
 
425
PyMethod_Fini(void)
 
426
{
 
427
    (void)PyMethod_ClearFreeList();
 
428
}
 
429
 
 
430
/* Print summary info about the state of the optimized allocator */
 
431
void
 
432
_PyMethod_DebugMallocStats(FILE *out)
 
433
{
 
434
    _PyDebugAllocatorStats(out,
 
435
                           "free PyMethodObject",
 
436
                           numfree, sizeof(PyMethodObject));
 
437
}
 
438
 
 
439
/* ------------------------------------------------------------------------
 
440
 * instance method
 
441
 */
 
442
 
 
443
PyObject *
 
444
PyInstanceMethod_New(PyObject *func) {
 
445
    PyInstanceMethodObject *method;
 
446
    method = PyObject_GC_New(PyInstanceMethodObject,
 
447
                             &PyInstanceMethod_Type);
 
448
    if (method == NULL) return NULL;
 
449
    Py_INCREF(func);
 
450
    method->func = func;
 
451
    _PyObject_GC_TRACK(method);
 
452
    return (PyObject *)method;
 
453
}
 
454
 
 
455
PyObject *
 
456
PyInstanceMethod_Function(PyObject *im)
 
457
{
 
458
    if (!PyInstanceMethod_Check(im)) {
 
459
        PyErr_BadInternalCall();
 
460
        return NULL;
 
461
    }
 
462
    return PyInstanceMethod_GET_FUNCTION(im);
 
463
}
 
464
 
 
465
#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
 
466
 
 
467
static PyMemberDef instancemethod_memberlist[] = {
 
468
    {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
 
469
     "the function (or other callable) implementing a method"},
 
470
    {NULL}      /* Sentinel */
 
471
};
 
472
 
 
473
static PyObject *
 
474
instancemethod_get_doc(PyObject *self, void *context)
 
475
{
 
476
    static PyObject *docstr;
 
477
    if (docstr == NULL) {
 
478
        docstr = PyUnicode_InternFromString("__doc__");
 
479
        if (docstr == NULL)
 
480
            return NULL;
 
481
    }
 
482
    return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
 
483
}
 
484
 
 
485
static PyGetSetDef instancemethod_getset[] = {
 
486
    {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
 
487
    {0}
 
488
};
 
489
 
 
490
static PyObject *
 
491
instancemethod_getattro(PyObject *self, PyObject *name)
 
492
{
 
493
    PyTypeObject *tp = self->ob_type;
 
494
    PyObject *descr = NULL;
 
495
 
 
496
    if (tp->tp_dict == NULL) {
 
497
        if (PyType_Ready(tp) < 0)
 
498
            return NULL;
 
499
    }
 
500
    descr = _PyType_Lookup(tp, name);
 
501
 
 
502
    if (descr != NULL) {
 
503
        descrgetfunc f = TP_DESCR_GET(descr->ob_type);
 
504
        if (f != NULL)
 
505
            return f(descr, self, (PyObject *)self->ob_type);
 
506
        else {
 
507
            Py_INCREF(descr);
 
508
            return descr;
 
509
        }
 
510
    }
 
511
 
 
512
    return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
 
513
}
 
514
 
 
515
static void
 
516
instancemethod_dealloc(PyObject *self) {
 
517
    _PyObject_GC_UNTRACK(self);
 
518
    Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
 
519
    PyObject_GC_Del(self);
 
520
}
 
521
 
 
522
static int
 
523
instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
 
524
    Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
 
525
    return 0;
 
526
}
 
527
 
 
528
static PyObject *
 
529
instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
 
530
{
 
531
    return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
 
532
}
 
533
 
 
534
static PyObject *
 
535
instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
 
536
    PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
 
537
    if (obj == NULL) {
 
538
        Py_INCREF(func);
 
539
        return func;
 
540
    }
 
541
    else
 
542
        return PyMethod_New(func, obj);
 
543
}
 
544
 
 
545
static PyObject *
 
546
instancemethod_richcompare(PyObject *self, PyObject *other, int op)
 
547
{
 
548
    PyInstanceMethodObject *a, *b;
 
549
    PyObject *res;
 
550
    int eq;
 
551
 
 
552
    if ((op != Py_EQ && op != Py_NE) ||
 
553
        !PyInstanceMethod_Check(self) ||
 
554
        !PyInstanceMethod_Check(other))
 
555
    {
 
556
        Py_RETURN_NOTIMPLEMENTED;
 
557
    }
 
558
    a = (PyInstanceMethodObject *)self;
 
559
    b = (PyInstanceMethodObject *)other;
 
560
    eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
 
561
    if (eq < 0)
 
562
        return NULL;
 
563
    if (op == Py_EQ)
 
564
        res = eq ? Py_True : Py_False;
 
565
    else
 
566
        res = eq ? Py_False : Py_True;
 
567
    Py_INCREF(res);
 
568
    return res;
 
569
}
 
570
 
 
571
static PyObject *
 
572
instancemethod_repr(PyObject *self)
 
573
{
 
574
    PyObject *func = PyInstanceMethod_Function(self);
 
575
    PyObject *funcname = NULL , *result = NULL;
 
576
    char *defname = "?";
 
577
 
 
578
    if (func == NULL) {
 
579
        PyErr_BadInternalCall();
 
580
        return NULL;
 
581
    }
 
582
 
 
583
    funcname = _PyObject_GetAttrId(func, &PyId___name__);
 
584
    if (funcname == NULL) {
 
585
        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
 
586
            return NULL;
 
587
        PyErr_Clear();
 
588
    }
 
589
    else if (!PyUnicode_Check(funcname)) {
 
590
        Py_DECREF(funcname);
 
591
        funcname = NULL;
 
592
    }
 
593
 
 
594
    result = PyUnicode_FromFormat("<instancemethod %V at %p>",
 
595
                                  funcname, defname, self);
 
596
 
 
597
    Py_XDECREF(funcname);
 
598
    return result;
 
599
}
 
600
 
 
601
/*
 
602
static long
 
603
instancemethod_hash(PyObject *self)
 
604
{
 
605
    long x, y;
 
606
    x = (long)self;
 
607
    y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
 
608
    if (y == -1)
 
609
        return -1;
 
610
    x = x ^ y;
 
611
    if (x == -1)
 
612
        x = -2;
 
613
    return x;
 
614
}
 
615
*/
 
616
 
 
617
PyDoc_STRVAR(instancemethod_doc,
 
618
"instancemethod(function)\n\
 
619
\n\
 
620
Bind a function to a class.");
 
621
 
 
622
static PyObject *
 
623
instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
 
624
{
 
625
    PyObject *func;
 
626
 
 
627
    if (!_PyArg_NoKeywords("instancemethod", kw))
 
628
        return NULL;
 
629
    if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
 
630
        return NULL;
 
631
    if (!PyCallable_Check(func)) {
 
632
        PyErr_SetString(PyExc_TypeError,
 
633
                        "first argument must be callable");
 
634
        return NULL;
 
635
    }
 
636
 
 
637
    return PyInstanceMethod_New(func);
 
638
}
 
639
 
 
640
PyTypeObject PyInstanceMethod_Type = {
 
641
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
 
642
    "instancemethod",                           /* tp_name */
 
643
    sizeof(PyInstanceMethodObject),             /* tp_basicsize */
 
644
    0,                                          /* tp_itemsize */
 
645
    instancemethod_dealloc,                     /* tp_dealloc */
 
646
    0,                                          /* tp_print */
 
647
    0,                                          /* tp_getattr */
 
648
    0,                                          /* tp_setattr */
 
649
    0,                                          /* tp_reserved */
 
650
    (reprfunc)instancemethod_repr,              /* tp_repr */
 
651
    0,                                          /* tp_as_number */
 
652
    0,                                          /* tp_as_sequence */
 
653
    0,                                          /* tp_as_mapping */
 
654
    0, /*(hashfunc)instancemethod_hash,         tp_hash  */
 
655
    instancemethod_call,                        /* tp_call */
 
656
    0,                                          /* tp_str */
 
657
    instancemethod_getattro,                    /* tp_getattro */
 
658
    PyObject_GenericSetAttr,                    /* tp_setattro */
 
659
    0,                                          /* tp_as_buffer */
 
660
    Py_TPFLAGS_DEFAULT
 
661
        | Py_TPFLAGS_HAVE_GC,                   /* tp_flags */
 
662
    instancemethod_doc,                         /* tp_doc */
 
663
    instancemethod_traverse,                    /* tp_traverse */
 
664
    0,                                          /* tp_clear */
 
665
    instancemethod_richcompare,                 /* tp_richcompare */
 
666
    0,                                          /* tp_weaklistoffset */
 
667
    0,                                          /* tp_iter */
 
668
    0,                                          /* tp_iternext */
 
669
    0,                                          /* tp_methods */
 
670
    instancemethod_memberlist,                  /* tp_members */
 
671
    instancemethod_getset,                      /* tp_getset */
 
672
    0,                                          /* tp_base */
 
673
    0,                                          /* tp_dict */
 
674
    instancemethod_descr_get,                   /* tp_descr_get */
 
675
    0,                                          /* tp_descr_set */
 
676
    0,                                          /* tp_dictoffset */
 
677
    0,                                          /* tp_init */
 
678
    0,                                          /* tp_alloc */
 
679
    instancemethod_new,                         /* tp_new */
 
680
};