66
66
PyObject *table[0];
69
#define StaticTuple_CheckExact(op) (Py_TYPE(op) == &StaticTuple_Type)
70
69
#define StaticTuple_SET_ITEM(key, offset, val) \
71
70
((((StaticTuple*)(key))->items[(offset)]) = ((PyObject *)(val)))
72
71
#define StaticTuple_GET_ITEM(key, offset) (((StaticTuple*)key)->items[offset])
75
static const char *_C_API_NAME = "_C_API";
77
74
#ifdef STATIC_TUPLE_MODULE
78
75
/* Used when compiling _static_tuple_c.c */
80
77
static StaticTuple * StaticTuple_New(Py_ssize_t);
81
78
static StaticTuple * StaticTuple_intern(StaticTuple *self);
79
#define StaticTuple_CheckExact(op) (Py_TYPE(op) == &StaticTuple_Type)
84
82
/* Used as the foreign api */
84
#include "_import_c_api.h"
86
86
static StaticTuple *(*StaticTuple_New)(Py_ssize_t);
87
87
static StaticTuple *(*StaticTuple_intern)(StaticTuple *);
88
88
static PyTypeObject *_p_StaticTuple_Type;
91
91
static int (*_StaticTuple_CheckExact)(PyObject *);
94
static int _import_function(PyObject *module, char *funcname,
95
void **f, char *signature)
98
PyObject *c_obj = NULL;
101
d = PyObject_GetAttrString(module, _C_API_NAME);
103
// PyObject_GetAttrString sets an appropriate exception
106
c_obj = PyDict_GetItemString(d, funcname);
108
// PyDict_GetItemString does not set an exception
109
PyErr_Format(PyExc_AttributeError,
110
"Module %s did not export a function named %s\n",
111
PyModule_GetName(module), funcname);
114
desc = (char *)PyCObject_GetDesc(c_obj);
115
if (!desc || strcmp(desc, signature) != 0) {
119
PyErr_Format(PyExc_TypeError,
120
"C function %s.%s has wrong signature (expected %s, got %s)",
121
PyModule_GetName(module), funcname, signature, desc);
124
*f = PyCObject_AsVoidPtr(c_obj);
133
static PyTypeObject *
134
_import_type(PyObject *module, char *class_name)
136
PyObject *type = NULL;
138
type = PyObject_GetAttrString(module, class_name);
142
if (!PyType_Check(type)) {
143
PyErr_Format(PyExc_TypeError,
144
"%s.%s is not a type object",
145
PyModule_GetName(module), class_name);
148
return (PyTypeObject *)type;
155
94
/* Return -1 and set exception on error, 0 on success */
157
96
import_static_tuple_c(void)
159
/* This is modeled after the implementation in Pyrex, which uses a
160
* dictionary and descriptors, rather than using plain offsets into a
163
PyObject *module = NULL;
165
module = PyImport_ImportModule("bzrlib._static_tuple_c");
166
if (!module) goto bad;
167
if (_import_function(module, "StaticTuple_New", (void **)&StaticTuple_New,
168
"StaticTuple *(Py_ssize_t)") < 0)
170
if (_import_function(module, "StaticTuple_intern",
171
(void **)&StaticTuple_intern,
172
"StaticTuple *(StaticTuple *)") < 0)
174
if (_import_function(module, "_StaticTuple_CheckExact",
175
(void **)&_StaticTuple_CheckExact,
176
"int(PyObject *)") < 0)
178
_p_StaticTuple_Type = _import_type(module, "StaticTuple");
179
if (!_p_StaticTuple_Type) {
98
struct function_description functions[] = {
99
{"StaticTuple_New", (void **)&StaticTuple_New,
100
"StaticTuple *(Py_ssize_t)"},
101
{"StaticTuple_intern", (void **)&StaticTuple_intern,
102
"StaticTuple *(StaticTuple *)"},
103
{"_StaticTuple_CheckExact", (void **)&_StaticTuple_CheckExact,
106
struct type_description types[] = {
107
{"StaticTuple", &_p_StaticTuple_Type},
109
return _import_extension_module("bzrlib._static_tuple_c",
189
113
#endif // !STATIC_TUPLE_MODULE