Debian Codegraph @

pypy/module/cpyext/test/comparisons.c

raw symbols SCIP
1 #include "Python.h"
2
3 #if PY_MAJOR_VERSION >= 3
4 #define PyInt_CheckExact PyLong_CheckExact
5 #endif
6
7 typedef struct CmpObject {
8 PyObject_HEAD
9 } CmpObject;
10
11
12 static PyObject* cmp_richcmp(PyObject *self, PyObject *other, int opid) {
13 long val;
14 if ((opid != Py_EQ && opid != Py_NE) || !PyInt_CheckExact(other)) {
15 Py_INCREF(Py_NotImplemented);
16 return Py_NotImplemented;
17 }
18 val = PyLong_AsLong(other);
19 if (opid == Py_EQ) {
20 return PyBool_FromLong(val == 3);
21 }
22 else if (opid == Py_NE) {
23 return PyBool_FromLong(val != 3);
24 }
25 return Py_NotImplemented;
26 }
27
28 static long cmp_hashfunc(PyObject *self) {
29 return 3;
30 }
31
32
33 PyTypeObject CmpType = {
34 PyVarObject_HEAD_INIT(NULL, 0)
35 "comparisons.CmpType", /* tp_name */
36 sizeof(CmpObject), /* tp_basicsize */
37 0, /* tp_itemsize */
38 0, /* tp_dealloc */
39 0, /* tp_print */
40 0, /* tp_getattr */
41 0, /* tp_setattr */
42 0, /* tp_compare */
43 0, /* tp_repr */
44 0, /* tp_as_number */
45 0, /* tp_as_sequence */
46 0, /* tp_as_mapping */
47 cmp_hashfunc, /* tp_hash */
48 0, /* tp_call */
49 0, /* tp_str */
50 0, /* tp_getattro */
51 0, /* tp_setattro */
52 0, /* tp_as_buffer */
53 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
54 0, /* tp_doc */
55 (traverseproc)0, /* tp_traverse */
56 0, /* tp_clear */
57 (richcmpfunc)cmp_richcmp, /* tp_richcompare */
58 0, /* tp_weaklistoffset */
59 0, /* tp_iter */
60 0, /* tp_iternext */
61 0, /* tp_methods */
62 0, /* tp_members */
63 0, /* tp_getset */
64 0, /* tp_base */
65 0, /* tp_dict */
66 0, /* tp_descr_get */
67 0, /* tp_descr_set */
68 0, /* tp_dictoffset */
69 0, /* tp_init */
70 0, /* tp_alloc */
71 PyType_GenericNew, /* tp_new */
72 0 /* tp_free */
73 };
74
75
76 static struct PyModuleDef moduledef = {
77 PyModuleDef_HEAD_INIT,
78 "comparisons",
79 "Module Doc",
80 -1,
81 NULL
82 };
83
84
85 PyMODINIT_FUNC
86 PyInit_comparisons(void)
87 {
88 PyObject *m, *d;
89
90 if (PyType_Ready(&CmpType) < 0)
91 return NULL;
92 m = PyModule_Create(&moduledef);
93 if (m == NULL)
94 return NULL;
95 d = PyModule_GetDict(m);
96 if (d == NULL)
97 return NULL;
98 if (PyDict_SetItemString(d, "CmpType", (PyObject *)&CmpType) < 0)
99 return NULL;
100 return m;
101 }