Debian Codegraph @

pypy/module/cpyext/test/foo.c

raw symbols SCIP
1 #include "Python.h"
2 #include "structmember.h"
3
4 #if PY_MAJOR_VERSION >= 3
5 #define PyInt_FromLong PyLong_FromLong
6 #define PyInt_AsLong PyLong_AsLong
7 #define PyThing_FromStringAndSize PyUnicode_FromStringAndSize
8 #define PyThing_FromString PyUnicode_FromString
9 #define PyThing_Check PyUnicode_Check
10 #define _PyThing_AsString _PyUnicode_AsString
11 #else
12 #define PyThing_FromStringAndSize PyString_FromStringAndSize
13 #define PyThing_FromString PyString_FromString
14 #define PyThing_Check PyString_Check
15 #define _PyThing_AsString PyString_AsString
16 #endif
17
18 typedef struct {
19 PyObject_HEAD
20 int foo; /* the context holder */
21 PyObject *foo_object;
22 char *foo_string;
23 char foo_string_inplace[5];
24 short foo_short;
25 long foo_long;
26 unsigned short foo_ushort;
27 unsigned int foo_uint;
28 unsigned long foo_ulong;
29 signed char foo_byte;
30 unsigned char foo_ubyte;
31 unsigned char foo_bool;
32 float foo_float;
33 double foo_double;
34 long long foo_longlong;
35 unsigned long long foo_ulonglong;
36 Py_ssize_t foo_ssizet;
37 } fooobject;
38
39 static PyTypeObject fooType;
40
41 static fooobject *
42 newfooobject(void)
43 {
44 fooobject *foop;
45
46 foop = PyObject_New(fooobject, &fooType);
47 if (foop == NULL)
48 return NULL;
49
50 foop->foo = 42;
51 foop->foo_object = NULL;
52 foop->foo_string = "Hello from PyPy";
53 strncpy(foop->foo_string_inplace, "spam", 5);
54 return foop;
55 }
56
57
58 /* foo methods */
59
60 static PyObject *
61 foo_copy(fooobject *self)
62 {
63 fooobject *foop;
64
65 if ((foop = newfooobject()) == NULL)
66 return NULL;
67
68 foop->foo = self->foo;
69
70 return (PyObject *)foop;
71 }
72
73 static PyObject *
74 foo_create(fooobject *self)
75 {
76 return (PyObject*)newfooobject();
77 }
78
79 static PyObject *
80 foo_classmeth(PyObject *cls)
81 {
82 Py_INCREF(cls);
83 return cls;
84 }
85
86 // for CPython
87 #ifndef PyMethodDescr_Check
88 int PyMethodDescr_Check(PyObject* method)
89 {
90 PyObject *meth = PyObject_GetAttrString((PyObject*)&PyList_Type, "append");
91 if (!meth) return 0;
92 int res = PyObject_TypeCheck(method, meth->ob_type);
93 Py_DECREF(meth);
94 return res;
95 }
96 #endif
97
98 PyObject* make_classmethod(PyObject* method)
99 {
100 // adapted from __Pyx_Method_ClassMethod
101 if (PyMethodDescr_Check(method)) {
102 PyMethodDescrObject *descr = (PyMethodDescrObject *)method;
103 PyTypeObject *d_type = descr->d_common.d_type;
104 return PyDescr_NewClassMethod(d_type, descr->d_method);
105 }
106 else if (PyMethod_Check(method)) {
107 return PyClassMethod_New(PyMethod_GET_FUNCTION(method));
108 }
109 else {
110 PyErr_SetString(PyExc_TypeError, "unknown method kind");
111 return NULL;
112 }
113 }
114
115 static PyObject *
116 foo_unset(fooobject *self)
117 {
118 self->foo_string = NULL;
119 Py_RETURN_NONE;
120 }
121
122
123 static PyMethodDef foo_methods[] = {
124 {"copy", (PyCFunction)foo_copy, METH_NOARGS, "copy($self, /)\n--\n\nCopy the foo."},
125 {"create", (PyCFunction)foo_create, METH_NOARGS|METH_STATIC, NULL},
126 {"classmeth", (PyCFunction)foo_classmeth, METH_NOARGS|METH_CLASS, NULL},
127 {"fake_classmeth", (PyCFunction)foo_classmeth, METH_NOARGS, NULL},
128 {"unset_string_member", (PyCFunction)foo_unset, METH_NOARGS, NULL},
129 #if PY_VERSION_HEX >= 0x03090000
130 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, "__class_getitem__($type, x, /)\n--\n\nSee PEP 585"},
131 #endif
132 {NULL, NULL} /* sentinel */
133 };
134
135 static PyObject *
136 foo_get_name(PyObject *self, void *closure)
137 {
138 return PyThing_FromStringAndSize("Foo Example", 11);
139 }
140
141 static PyObject *
142 foo_get_foo(PyObject *self, void *closure)
143 {
144 return PyInt_FromLong(((fooobject*)self)->foo);
145 }
146
147 static PyGetSetDef foo_getseters[] = {
148 {"name",
149 (getter)foo_get_name, NULL,
150 NULL,
151 NULL},
152 {"foo",
153 (getter)foo_get_foo, NULL,
154 NULL,
155 NULL},
156 {NULL} /* Sentinel */
157 };
158
159 static PyObject *
160 foo_repr(PyObject *self)
161 {
162 PyObject *format;
163
164 format = PyThing_FromString("<Foo>");
165 if (format == NULL) return NULL;
166 return format;
167 }
168
169 static PyObject *
170 foo_call(PyObject *self, PyObject *args, PyObject *kwds)
171 {
172 Py_INCREF(kwds);
173 return kwds;
174 }
175
176 static int
177 foo_setattro(fooobject *self, PyObject *name, PyObject *value)
178 {
179 const char *name_str;
180 if (!PyThing_Check(name)) {
181 PyErr_SetObject(PyExc_AttributeError, name);
182 return -1;
183 }
184 name_str = _PyThing_AsString(name);
185 if (strcmp(name_str, "set_foo") == 0)
186 {
187 long v = PyInt_AsLong(value);
188 if (v == -1 && PyErr_Occurred())
189 return -1;
190 self->foo = v;
191 return 0;
192 }
193 return PyObject_GenericSetAttr((PyObject *)self, name, value);
194 }
195
196 static PyObject *
197 new_fooType(PyTypeObject * t, PyObject *args, PyObject *kwds)
198 {
199 PyObject * o;
200 /* copied from numpy scalartypes.c for inherited classes */
201 if (t->tp_bases && (PyTuple_GET_SIZE(t->tp_bases) > 1))
202 {
203 PyTypeObject *sup;
204 /* We are inheriting from a Python type as well so
205 give it first dibs on conversion */
206 sup = (PyTypeObject *)PyTuple_GET_ITEM(t->tp_bases, 1);
207 /* Prevent recursion */
208 if (new_fooType != sup->tp_new)
209 {
210 o = sup->tp_new(t, args, kwds);
211 return o;
212 }
213 }
214 o = t->tp_alloc(t, 0);
215 return o;
216 };
217
218 static PyMemberDef foo_members[] = {
219 {"int_member", T_INT, offsetof(fooobject, foo), 0,
220 "A helpful docstring."},
221 {"int_member_readonly", T_INT, offsetof(fooobject, foo), READONLY,
222 "A helpful docstring."},
223 {"broken_member", 0xaffe, 0, 0, NULL},
224 {"object_member", T_OBJECT, offsetof(fooobject, foo_object), 0,
225 "A Python object."},
226 {"object_member_ex", T_OBJECT_EX, offsetof(fooobject, foo_object), 0,
227 "A Python object."},
228 {"string_member", T_STRING, offsetof(fooobject, foo_string), 0,
229 "A string."},
230 {"string_member_inplace", T_STRING_INPLACE,
231 offsetof(fooobject, foo_string_inplace), 0, "An inplace string."},
232 {"char_member", T_CHAR, offsetof(fooobject, foo_string_inplace), 0, NULL},
233
234 {"short_member", T_SHORT, offsetof(fooobject, foo_short), 0, NULL},
235 {"long_member", T_LONG, offsetof(fooobject, foo_long), 0, NULL},
236 {"ushort_member", T_USHORT, offsetof(fooobject, foo_ushort), 0, NULL},
237 {"uint_member", T_UINT, offsetof(fooobject, foo_uint), 0, NULL},
238 {"ulong_member", T_ULONG, offsetof(fooobject, foo_ulong), 0, NULL},
239 {"byte_member", T_BYTE, offsetof(fooobject, foo_byte), 0, NULL},
240 {"ubyte_member", T_UBYTE, offsetof(fooobject, foo_ubyte), 0, NULL},
241 {"bool_member", T_BOOL, offsetof(fooobject, foo_bool), 0, NULL},
242 {"float_member", T_FLOAT, offsetof(fooobject, foo_float), 0, NULL},
243 {"double_member", T_DOUBLE, offsetof(fooobject, foo_double), 0, NULL},
244 {"longlong_member", T_LONGLONG, offsetof(fooobject, foo_longlong), 0, NULL},
245 {"ulonglong_member", T_ULONGLONG, offsetof(fooobject, foo_ulonglong), 0, NULL},
246 {"ssizet_member", T_PYSSIZET, offsetof(fooobject, foo_ssizet), 0, NULL},
247 {NULL} /* Sentinel */
248 };
249
250 PyDoc_STRVAR(foo_doc, "foo is for testing.");
251
252 static PyTypeObject fooType = {
253 PyVarObject_HEAD_INIT(NULL, 0)
254 "foo.foo", /*tp_name*/
255 sizeof(fooobject), /*tp_size*/
256 0, /*tp_itemsize*/
257 /* methods */
258 0, /*tp_dealloc*/
259 0, /*tp_print*/
260 0, /*tp_getattr*/
261 0, /*tp_setattr*/
262 0, /*tp_compare*/
263 foo_repr, /*tp_repr*/
264 0, /*tp_as_number*/
265 0, /*tp_as_sequence*/
266 0, /*tp_as_mapping*/
267 0, /*tp_hash*/
268 foo_call, /*tp_call*/
269 0, /*tp_str*/
270 0, /*tp_getattro*/
271 (setattrofunc)foo_setattro, /*tp_setattro*/
272 0, /*tp_as_buffer*/
273 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
274 foo_doc, /*tp_doc*/
275 0, /*tp_traverse*/
276 0, /*tp_clear*/
277 0, /*tp_richcompare*/
278 0, /*tp_weaklistoffset*/
279 0, /*tp_iter*/
280 0, /*tp_iternext*/
281 foo_methods, /*tp_methods*/
282 foo_members, /*tp_members*/
283 foo_getseters, /*tp_getset*/
284 };
285
286 /* A type that inherits from 'unicode */
287
288 typedef struct {
289 PyUnicodeObject HEAD;
290 int val;
291 } UnicodeSubclassObject;
292
293
294 static int UnicodeSubclass_init(UnicodeSubclassObject *self, PyObject *args, PyObject *kwargs) {
295 self->val = 42;
296 return 0;
297 }
298
299 static PyObject *
300 UnicodeSubclass_escape(PyTypeObject* type, PyObject *args)
301 {
302 Py_RETURN_TRUE;
303 }
304
305 static PyObject *
306 UnicodeSubclass_get_val(UnicodeSubclassObject *self) {
307 return PyInt_FromLong(self->val);
308 }
309
310 static PyMethodDef UnicodeSubclass_methods[] = {
311 {"escape", (PyCFunction) UnicodeSubclass_escape, METH_VARARGS, NULL},
312 {"get_val", (PyCFunction) UnicodeSubclass_get_val, METH_NOARGS, NULL},
313 {NULL} /* Sentinel */
314 };
315
316 PyTypeObject UnicodeSubtype = {
317 PyVarObject_HEAD_INIT(NULL, 0)
318 "foo.fuu",
319 sizeof(UnicodeSubclassObject),
320 0,
321 0, /*tp_dealloc*/
322 0, /*tp_print*/
323 0, /*tp_getattr*/
324 0, /*tp_setattr*/
325 0, /*tp_compare*/
326 0, /*tp_repr*/
327 0, /*tp_as_number*/
328 0, /*tp_as_sequence*/
329 0, /*tp_as_mapping*/
330 0, /*tp_hash */
331
332 0, /*tp_call*/
333 0, /*tp_str*/
334 0, /*tp_getattro*/
335 0, /*tp_setattro*/
336 0, /*tp_as_buffer*/
337
338 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
339 0, /*tp_doc*/
340
341 0, /*tp_traverse*/
342 0, /*tp_clear*/
343
344 0, /*tp_richcompare*/
345 0, /*tp_weaklistoffset*/
346
347 0, /*tp_iter*/
348 0, /*tp_iternext*/
349
350 /* Attribute descriptor and subclassing stuff */
351
352 UnicodeSubclass_methods,/*tp_methods*/
353 0, /*tp_members*/
354 0, /*tp_getset*/
355 0, /*tp_base*/
356 0, /*tp_dict*/
357
358 0, /*tp_descr_get*/
359 0, /*tp_descr_set*/
360 0, /*tp_dictoffset*/
361
362 (initproc) UnicodeSubclass_init, /*tp_init*/
363 0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
364 0, /*tp_new*/
365 0, /*tp_free Low-level free-memory routine */
366 0, /*tp_is_gc For PyObject_IS_GC */
367 0, /*tp_bases*/
368 0, /*tp_mro method resolution order */
369 0, /*tp_cache*/
370 0, /*tp_subclasses*/
371 0 /*tp_weaklist*/
372 };
373
374 PyTypeObject UnicodeSubtype2 = {
375 PyVarObject_HEAD_INIT(NULL, 0)
376 "foo.fuu2",
377 sizeof(UnicodeSubclassObject),
378 0,
379 0, /*tp_dealloc*/
380 0, /*tp_print*/
381 0, /*tp_getattr*/
382 0, /*tp_setattr*/
383 0, /*tp_compare*/
384 0, /*tp_repr*/
385 0, /*tp_as_number*/
386 0, /*tp_as_sequence*/
387 0, /*tp_as_mapping*/
388 0, /*tp_hash */
389
390 0, /*tp_call*/
391 0, /*tp_str*/
392 0, /*tp_getattro*/
393 0, /*tp_setattro*/
394 0, /*tp_as_buffer*/
395
396 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
397 0, /*tp_doc*/
398
399 0, /*tp_traverse*/
400 0, /*tp_clear*/
401
402 0, /*tp_richcompare*/
403 0, /*tp_weaklistoffset*/
404
405 0, /*tp_iter*/
406 0, /*tp_iternext*/
407
408 /* Attribute descriptor and subclassing stuff */
409
410 0, /*tp_methods*/
411 0, /*tp_members*/
412 0, /*tp_getset*/
413 0, /*tp_base*/
414 0, /*tp_dict*/
415
416 0, /*tp_descr_get*/
417 0, /*tp_descr_set*/
418 0, /*tp_dictoffset*/
419
420 0, /*tp_init*/
421 0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
422 0, /*tp_new*/
423 0, /*tp_free Low-level free-memory routine */
424 0, /*tp_is_gc For PyObject_IS_GC */
425 0, /*tp_bases*/
426 0, /*tp_mro method resolution order */
427 0, /*tp_cache*/
428 0, /*tp_subclasses*/
429 0 /*tp_weaklist*/
430 };
431
432 PyTypeObject UnicodeSubtype3 = {
433 PyVarObject_HEAD_INIT(NULL, 0)
434 "foo.fuu3",
435 sizeof(UnicodeSubclassObject)
436 };
437
438 /* A Metatype */
439
440 PyTypeObject MetaType = {
441 PyVarObject_HEAD_INIT(NULL, 0)
442 "foo.Meta",
443 sizeof(PyHeapTypeObject),/*tp_basicsize*/
444 0, /*tp_itemsize*/
445 0, /*tp_dealloc*/
446 0, /*tp_print*/
447 0, /*tp_getattr*/
448 0, /*tp_setattr*/
449 0, /*tp_compare*/
450 0, /*tp_repr*/
451 0, /*tp_as_number*/
452 0, /*tp_as_sequence*/
453 0, /*tp_as_mapping*/
454 0, /*tp_hash */
455
456 0, /*tp_call*/
457 0, /*tp_str*/
458 0, /*tp_getattro*/
459 0, /*tp_setattro*/
460 0, /*tp_as_buffer*/
461
462 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
463 0, /*tp_doc*/
464
465 0, /*tp_traverse*/
466 0, /*tp_clear*/
467
468 0, /*tp_richcompare*/
469 0, /*tp_weaklistoffset*/
470
471 0, /*tp_iter*/
472 0, /*tp_iternext*/
473
474 /* Attribute descriptor and subclassing stuff */
475
476 0, /*tp_methods*/
477 0, /*tp_members*/
478 0, /*tp_getset*/
479 0, /*tp_base*/
480 0, /*tp_dict*/
481
482 0, /*tp_descr_get*/
483 0, /*tp_descr_set*/
484 0, /*tp_dictoffset*/
485
486 0, /*tp_init*/
487 0, /*tp_alloc*/
488 0, /*tp_new*/
489 0, /*tp_free*/
490 0, /*tp_is_gc*/
491 0, /*tp_bases*/
492 0, /*tp_mro*/
493 0, /*tp_cache*/
494 0, /*tp_subclasses*/
495 0 /*tp_weaklist*/
496 };
497
498
499 /* foo functions */
500
501 static PyObject *
502 foo_new(PyObject *self, PyObject *args)
503 {
504 fooobject *foop;
505
506 if ((foop = newfooobject()) == NULL) {
507 return NULL;
508 }
509
510 return (PyObject *)foop;
511 }
512
513 static int initerrtype_init(PyObject *self, PyObject *args, PyObject *kwargs) {
514 PyErr_SetString(PyExc_ValueError, "init raised an error!");
515 return -1;
516 }
517
518
519 PyTypeObject InitErrType = {
520 PyVarObject_HEAD_INIT(NULL, 0)
521 "foo.InitErrType",
522 sizeof(PyObject),/*tp_basicsize*/
523 0, /*tp_itemsize*/
524 0, /*tp_dealloc*/
525 0, /*tp_print*/
526 0, /*tp_getattr*/
527 0, /*tp_setattr*/
528 0, /*tp_compare*/
529 0, /*tp_repr*/
530 0, /*tp_as_number*/
531 0, /*tp_as_sequence*/
532 0, /*tp_as_mapping*/
533 0, /*tp_hash */
534
535 0, /*tp_call*/
536 0, /*tp_str*/
537 0, /*tp_getattro*/
538 0, /*tp_setattro*/
539 0, /*tp_as_buffer*/
540
541 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
542 0, /*tp_doc*/
543
544 0, /*tp_traverse*/
545 0, /*tp_clear*/
546
547 0, /*tp_richcompare*/
548 0, /*tp_weaklistoffset*/
549
550 0, /*tp_iter*/
551 0, /*tp_iternext*/
552
553 /* Attribute descriptor and subclassing stuff */
554
555 0, /*tp_methods*/
556 0, /*tp_members*/
557 0, /*tp_getset*/
558 0, /*tp_base*/
559 0, /*tp_dict*/
560
561 0, /*tp_descr_get*/
562 0, /*tp_descr_set*/
563 0, /*tp_dictoffset*/
564
565 initerrtype_init, /*tp_init*/
566 0, /*tp_alloc*/
567 0, /*tp_new*/
568 0, /*tp_free*/
569 0, /*tp_is_gc*/
570 0, /*tp_bases*/
571 0, /*tp_mro*/
572 0, /*tp_cache*/
573 0, /*tp_subclasses*/
574 0 /*tp_weaklist*/
575 };
576
577 PyObject * prop_descr_get(PyObject *self, PyObject *obj, PyObject *type)
578 {
579 if (obj == NULL)
580 obj = Py_None;
581 if (type == NULL)
582 type = Py_None;
583
584 return PyTuple_Pack(3, self, obj, type);
585 }
586
587 int prop_descr_set(PyObject *self, PyObject *obj, PyObject *value)
588 {
589 int res;
590 if (value != NULL) {
591 PyObject *result = PyTuple_Pack(2, self, value);
592 res = PyObject_SetAttrString(obj, "y", result);
593 Py_DECREF(result);
594 }
595 else {
596 res = PyObject_SetAttrString(obj, "z", self);
597 }
598 return res;
599 }
600
601
602 PyTypeObject SimplePropertyType = {
603 PyVarObject_HEAD_INIT(NULL, 0)
604 "foo.Property",
605 sizeof(PyObject),
606 0,
607 0, /*tp_dealloc*/
608 0, /*tp_print*/
609 0, /*tp_getattr*/
610 0, /*tp_setattr*/
611 0, /*tp_compare*/
612 0, /*tp_repr*/
613 0, /*tp_as_number*/
614 0, /*tp_as_sequence*/
615 0, /*tp_as_mapping*/
616 0, /*tp_hash */
617
618 0, /*tp_call*/
619 0, /*tp_str*/
620 0, /*tp_getattro*/
621 0, /*tp_setattro*/
622 0, /*tp_as_buffer*/
623
624 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
625 0, /*tp_doc*/
626
627 0, /*tp_traverse*/
628 0, /*tp_clear*/
629
630 0, /*tp_richcompare*/
631 0, /*tp_weaklistoffset*/
632
633 0, /*tp_iter*/
634 0, /*tp_iternext*/
635
636 /* Attribute descriptor and subclassing stuff */
637
638 0, /*tp_methods*/
639 0, /*tp_members*/
640 0, /*tp_getset*/
641 0, /*tp_base*/
642 0, /*tp_dict*/
643
644 prop_descr_get, /*tp_descr_get*/
645 prop_descr_set, /*tp_descr_set*/
646 0, /*tp_dictoffset*/
647
648 0, /*tp_init*/
649 0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
650 PyType_GenericNew, /*tp_new*/
651 0, /*tp_free Low-level free-memory routine */
652 0, /*tp_is_gc For PyObject_IS_GC */
653 0, /*tp_bases*/
654 0, /*tp_mro method resolution order */
655 0, /*tp_cache*/
656 0, /*tp_subclasses*/
657 0 /*tp_weaklist*/
658 };
659
660 /* A type with a custom allocator */
661 static void custom_dealloc(PyObject *ob)
662 {
663 free(ob);
664 }
665
666 static PyTypeObject CustomType;
667
668 static PyObject *newCustom(PyObject *self, PyObject *args)
669 {
670 PyObject *obj = calloc(1, sizeof(PyObject));
671 Py_SET_TYPE(obj, &CustomType);
672 _Py_NewReference(obj);
673 return obj;
674 }
675
676 static PyTypeObject CustomType = {
677 PyVarObject_HEAD_INIT(NULL, 0)
678 "foo.Custom", /*tp_name*/
679 sizeof(PyObject), /*tp_size*/
680 0, /*tp_itemsize*/
681 /* methods */
682 (destructor)custom_dealloc, /*tp_dealloc*/
683 };
684
685 static PyTypeObject TupleLike = {
686 PyVarObject_HEAD_INIT(NULL, 0)
687 "foo.TupleLike", /*tp_name*/
688 sizeof(PyObject), /*tp_size*/
689 };
690
691
692 static PyObject *size_of_instances(PyObject *self, PyObject *t)
693 {
694 return PyLong_FromSsize_t(((PyTypeObject *)t)->tp_basicsize);
695 }
696
697
698 static PyObject * is_TupleLike(PyObject *self, PyObject * t)
699 {
700 int tf = t->ob_type == &TupleLike;
701 if (t->ob_type->tp_itemsize == 0)
702 return PyLong_FromLong(-1);
703 return PyLong_FromLong(tf);
704 }
705
706 static PyTypeObject GetType1 = {
707 PyVarObject_HEAD_INIT(NULL, 0)
708 "foo.GetType1", /*tp_name*/
709 sizeof(PyObject), /*tp_size*/
710 };
711 static PyTypeObject GetType2 = {
712 PyVarObject_HEAD_INIT(NULL, 0)
713 "foo.GetType2", /*tp_name*/
714 sizeof(PyObject), /*tp_size*/
715 };
716 static PyObject *gettype1, *gettype2;
717
718 static PyObject *gettype1_getattr(PyObject *self, char *name)
719 {
720 char buf[200];
721 strcpy(buf, "getattr:");
722 strcat(buf, name);
723 return PyBytes_FromString(buf);
724 }
725 static PyObject *gettype2_getattro(PyObject *self, PyObject *name)
726 {
727 char buf[200];
728 PyObject* temp;
729 temp = PyUnicode_AsASCIIString(name);
730 if (temp == NULL) return NULL;
731 strcpy(buf, "getattro:");
732 strcat(buf, PyBytes_AS_STRING(temp));
733 return PyBytes_FromString(buf);
734 }
735
736
737 /* List of functions exported by this module */
738
739 static PyMethodDef foo_functions[] = {
740 {"new", (PyCFunction)foo_new, METH_NOARGS, NULL},
741 {"newCustom", (PyCFunction)newCustom, METH_NOARGS, NULL},
742 {"size_of_instances", (PyCFunction)size_of_instances, METH_O, NULL},
743 {"is_TupleLike", (PyCFunction)is_TupleLike, METH_O, NULL},
744 {NULL, NULL} /* Sentinel */
745 };
746
747 #if PY_MAJOR_VERSION >= 3
748 static struct PyModuleDef moduledef = {
749 PyModuleDef_HEAD_INIT,
750 "foo",
751 "Module Doc",
752 -1,
753 foo_functions,
754 NULL,
755 NULL,
756 NULL,
757 NULL,
758 };
759 #define INITERROR return NULL
760
761 PyMODINIT_FUNC
762 PyInit_foo(void)
763
764 #else
765
766 #define INITERROR return
767
768 /* Initialize this module. */
769 #ifdef __GNUC__
770 extern __attribute__((visibility("default")))
771 #else
772 extern __declspec(dllexport)
773 #endif
774
775 PyMODINIT_FUNC
776 initfoo(void)
777 #endif
778 {
779 PyObject *d;
780 PyObject *fake_classmeth, *classmeth;
781 #if PY_MAJOR_VERSION >= 3
782 PyObject *module = PyModule_Create(&moduledef);
783 #else
784 PyObject *module = Py_InitModule("foo", foo_functions);
785 #endif
786 if (module == NULL)
787 INITERROR;
788
789 UnicodeSubtype.tp_base = &PyUnicode_Type;
790 UnicodeSubtype2.tp_base = &UnicodeSubtype;
791 MetaType.tp_base = &PyType_Type;
792
793 fooType.tp_new = &new_fooType;
794 InitErrType.tp_new = PyType_GenericNew;
795
796 if (PyType_Ready(&fooType) < 0)
797 INITERROR;
798 if (PyType_Ready(&UnicodeSubtype) < 0)
799 INITERROR;
800 if (PyType_Ready(&UnicodeSubtype2) < 0)
801 INITERROR;
802 if (PyType_Ready(&MetaType) < 0)
803 INITERROR;
804 if (PyType_Ready(&InitErrType) < 0)
805 INITERROR;
806 if (PyType_Ready(&SimplePropertyType) < 0)
807 INITERROR;
808
809
810 Py_SET_TYPE(&CustomType, &MetaType);
811 if (PyType_Ready(&CustomType) < 0)
812 INITERROR;
813
814 UnicodeSubtype3.tp_flags = Py_TPFLAGS_DEFAULT;
815 UnicodeSubtype3.tp_base = &UnicodeSubtype;
816 UnicodeSubtype3.tp_bases = Py_BuildValue("(OO)", &UnicodeSubtype,
817 &CustomType);
818 if (PyType_Ready(&UnicodeSubtype3) < 0)
819 INITERROR;
820
821 TupleLike.tp_flags = Py_TPFLAGS_DEFAULT;
822 TupleLike.tp_base = &PyTuple_Type;
823 if (PyType_Ready(&TupleLike) < 0)
824 INITERROR;
825
826 GetType1.tp_flags = Py_TPFLAGS_DEFAULT;
827 GetType1.tp_getattr = &gettype1_getattr;
828 if (PyType_Ready(&GetType1) < 0)
829 INITERROR;
830 gettype1 = PyObject_New(PyObject, &GetType1);
831
832 GetType2.tp_flags = Py_TPFLAGS_DEFAULT;
833 GetType2.tp_getattro = &gettype2_getattro;
834 if (PyType_Ready(&GetType2) < 0)
835 INITERROR;
836 gettype2 = PyObject_New(PyObject, &GetType2);
837
838 fake_classmeth = PyDict_GetItemString((PyObject *)fooType.tp_dict, "fake_classmeth");
839 classmeth = make_classmethod(fake_classmeth);
840 if (classmeth == NULL)
841 INITERROR;
842 if (PyDict_SetItemString((PyObject *)fooType.tp_dict, "fake_classmeth", classmeth) < 0)
843 INITERROR;
844
845 d = PyModule_GetDict(module);
846 if (d == NULL)
847 INITERROR;
848 if (PyDict_SetItemString(d, "fooType", (PyObject *)&fooType) < 0)
849 INITERROR;
850 if (PyDict_SetItemString(d, "UnicodeSubtype", (PyObject *) &UnicodeSubtype) < 0)
851 INITERROR;
852 if (PyDict_SetItemString(d, "UnicodeSubtype2", (PyObject *) &UnicodeSubtype2) < 0)
853 INITERROR;
854 if (PyDict_SetItemString(d, "UnicodeSubtype3", (PyObject *) &UnicodeSubtype3) < 0)
855 INITERROR;
856 if (PyDict_SetItemString(d, "MetaType", (PyObject *) &MetaType) < 0)
857 INITERROR;
858 if (PyDict_SetItemString(d, "InitErrType", (PyObject *) &InitErrType) < 0)
859 INITERROR;
860 if (PyDict_SetItemString(d, "Property", (PyObject *) &SimplePropertyType) < 0)
861 INITERROR;
862 if (PyDict_SetItemString(d, "Custom", (PyObject *) &CustomType) < 0)
863 INITERROR;
864 if (PyDict_SetItemString(d, "TupleLike", (PyObject *) &TupleLike) < 0)
865 INITERROR;
866 if (PyDict_SetItemString(d, "gettype1", gettype1) < 0)
867 INITERROR;
868 if (PyDict_SetItemString(d, "gettype2", gettype2) < 0)
869 INITERROR;
870 #if PY_MAJOR_VERSION >=3
871 return module;
872 #endif
873 }