| 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; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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} |
| 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} |
| 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 |
|
| 201 |
if (t->tp_bases && (PyTuple_GET_SIZE(t->tp_bases) > 1)) |
| 202 |
{ |
| 203 |
PyTypeObject *sup; |
| 204 |
|
| 205 |
|
| 206 |
sup = (PyTypeObject *)PyTuple_GET_ITEM(t->tp_bases, 1); |
| 207 |
|
| 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} |
| 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", |
| 255 |
sizeof(fooobject), |
| 256 |
0, |
| 257 |
|
| 258 |
0, |
| 259 |
0, |
| 260 |
0, |
| 261 |
0, |
| 262 |
0, |
| 263 |
foo_repr, |
| 264 |
0, |
| 265 |
0, |
| 266 |
0, |
| 267 |
0, |
| 268 |
foo_call, |
| 269 |
0, |
| 270 |
0, |
| 271 |
(setattrofunc)foo_setattro, |
| 272 |
0, |
| 273 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 274 |
foo_doc, |
| 275 |
0, |
| 276 |
0, |
| 277 |
0, |
| 278 |
0, |
| 279 |
0, |
| 280 |
0, |
| 281 |
foo_methods, |
| 282 |
foo_members, |
| 283 |
foo_getseters, |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 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} |
| 314 |
}; |
| 315 |
|
| 316 |
PyTypeObject UnicodeSubtype = { |
| 317 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 318 |
"foo.fuu", |
| 319 |
sizeof(UnicodeSubclassObject), |
| 320 |
0, |
| 321 |
0, |
| 322 |
0, |
| 323 |
0, |
| 324 |
0, |
| 325 |
0, |
| 326 |
0, |
| 327 |
0, |
| 328 |
0, |
| 329 |
0, |
| 330 |
0, |
| 331 |
|
| 332 |
0, |
| 333 |
0, |
| 334 |
0, |
| 335 |
0, |
| 336 |
0, |
| 337 |
|
| 338 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 339 |
0, |
| 340 |
|
| 341 |
0, |
| 342 |
0, |
| 343 |
|
| 344 |
0, |
| 345 |
0, |
| 346 |
|
| 347 |
0, |
| 348 |
0, |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
UnicodeSubclass_methods, |
| 353 |
0, |
| 354 |
0, |
| 355 |
0, |
| 356 |
0, |
| 357 |
|
| 358 |
0, |
| 359 |
0, |
| 360 |
0, |
| 361 |
|
| 362 |
(initproc) UnicodeSubclass_init, |
| 363 |
0, |
| 364 |
0, |
| 365 |
0, |
| 366 |
0, |
| 367 |
0, |
| 368 |
0, |
| 369 |
0, |
| 370 |
0, |
| 371 |
0 |
| 372 |
}; |
| 373 |
|
| 374 |
PyTypeObject UnicodeSubtype2 = { |
| 375 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 376 |
"foo.fuu2", |
| 377 |
sizeof(UnicodeSubclassObject), |
| 378 |
0, |
| 379 |
0, |
| 380 |
0, |
| 381 |
0, |
| 382 |
0, |
| 383 |
0, |
| 384 |
0, |
| 385 |
0, |
| 386 |
0, |
| 387 |
0, |
| 388 |
0, |
| 389 |
|
| 390 |
0, |
| 391 |
0, |
| 392 |
0, |
| 393 |
0, |
| 394 |
0, |
| 395 |
|
| 396 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 397 |
0, |
| 398 |
|
| 399 |
0, |
| 400 |
0, |
| 401 |
|
| 402 |
0, |
| 403 |
0, |
| 404 |
|
| 405 |
0, |
| 406 |
0, |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
0, |
| 411 |
0, |
| 412 |
0, |
| 413 |
0, |
| 414 |
0, |
| 415 |
|
| 416 |
0, |
| 417 |
0, |
| 418 |
0, |
| 419 |
|
| 420 |
0, |
| 421 |
0, |
| 422 |
0, |
| 423 |
0, |
| 424 |
0, |
| 425 |
0, |
| 426 |
0, |
| 427 |
0, |
| 428 |
0, |
| 429 |
0 |
| 430 |
}; |
| 431 |
|
| 432 |
PyTypeObject UnicodeSubtype3 = { |
| 433 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 434 |
"foo.fuu3", |
| 435 |
sizeof(UnicodeSubclassObject) |
| 436 |
}; |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
PyTypeObject MetaType = { |
| 441 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 442 |
"foo.Meta", |
| 443 |
sizeof(PyHeapTypeObject), |
| 444 |
0, |
| 445 |
0, |
| 446 |
0, |
| 447 |
0, |
| 448 |
0, |
| 449 |
0, |
| 450 |
0, |
| 451 |
0, |
| 452 |
0, |
| 453 |
0, |
| 454 |
0, |
| 455 |
|
| 456 |
0, |
| 457 |
0, |
| 458 |
0, |
| 459 |
0, |
| 460 |
0, |
| 461 |
|
| 462 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 463 |
0, |
| 464 |
|
| 465 |
0, |
| 466 |
0, |
| 467 |
|
| 468 |
0, |
| 469 |
0, |
| 470 |
|
| 471 |
0, |
| 472 |
0, |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
0, |
| 477 |
0, |
| 478 |
0, |
| 479 |
0, |
| 480 |
0, |
| 481 |
|
| 482 |
0, |
| 483 |
0, |
| 484 |
0, |
| 485 |
|
| 486 |
0, |
| 487 |
0, |
| 488 |
0, |
| 489 |
0, |
| 490 |
0, |
| 491 |
0, |
| 492 |
0, |
| 493 |
0, |
| 494 |
0, |
| 495 |
0 |
| 496 |
}; |
| 497 |
|
| 498 |
|
| 499 |
|
| 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), |
| 523 |
0, |
| 524 |
0, |
| 525 |
0, |
| 526 |
0, |
| 527 |
0, |
| 528 |
0, |
| 529 |
0, |
| 530 |
0, |
| 531 |
0, |
| 532 |
0, |
| 533 |
0, |
| 534 |
|
| 535 |
0, |
| 536 |
0, |
| 537 |
0, |
| 538 |
0, |
| 539 |
0, |
| 540 |
|
| 541 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 542 |
0, |
| 543 |
|
| 544 |
0, |
| 545 |
0, |
| 546 |
|
| 547 |
0, |
| 548 |
0, |
| 549 |
|
| 550 |
0, |
| 551 |
0, |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
0, |
| 556 |
0, |
| 557 |
0, |
| 558 |
0, |
| 559 |
0, |
| 560 |
|
| 561 |
0, |
| 562 |
0, |
| 563 |
0, |
| 564 |
|
| 565 |
initerrtype_init, |
| 566 |
0, |
| 567 |
0, |
| 568 |
0, |
| 569 |
0, |
| 570 |
0, |
| 571 |
0, |
| 572 |
0, |
| 573 |
0, |
| 574 |
0 |
| 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, |
| 608 |
0, |
| 609 |
0, |
| 610 |
0, |
| 611 |
0, |
| 612 |
0, |
| 613 |
0, |
| 614 |
0, |
| 615 |
0, |
| 616 |
0, |
| 617 |
|
| 618 |
0, |
| 619 |
0, |
| 620 |
0, |
| 621 |
0, |
| 622 |
0, |
| 623 |
|
| 624 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 625 |
0, |
| 626 |
|
| 627 |
0, |
| 628 |
0, |
| 629 |
|
| 630 |
0, |
| 631 |
0, |
| 632 |
|
| 633 |
0, |
| 634 |
0, |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
0, |
| 639 |
0, |
| 640 |
0, |
| 641 |
0, |
| 642 |
0, |
| 643 |
|
| 644 |
prop_descr_get, |
| 645 |
prop_descr_set, |
| 646 |
0, |
| 647 |
|
| 648 |
0, |
| 649 |
0, |
| 650 |
PyType_GenericNew, |
| 651 |
0, |
| 652 |
0, |
| 653 |
0, |
| 654 |
0, |
| 655 |
0, |
| 656 |
0, |
| 657 |
0 |
| 658 |
}; |
| 659 |
|
| 660 |
|
| 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", |
| 679 |
sizeof(PyObject), |
| 680 |
0, |
| 681 |
|
| 682 |
(destructor)custom_dealloc, |
| 683 |
}; |
| 684 |
|
| 685 |
static PyTypeObject TupleLike = { |
| 686 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 687 |
"foo.TupleLike", |
| 688 |
sizeof(PyObject), |
| 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", |
| 709 |
sizeof(PyObject), |
| 710 |
}; |
| 711 |
static PyTypeObject GetType2 = { |
| 712 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 713 |
"foo.GetType2", |
| 714 |
sizeof(PyObject), |
| 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 |
|
| 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} |
| 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 |
|
| 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 |
} |