87
81
static StaticTuple * StaticTuple_intern(StaticTuple *self);
90
/* Used by foriegn callers */
91
static void **StaticTuple_API;
84
/* Used as the foreign api */
93
86
static StaticTuple *(*StaticTuple_New)(Py_ssize_t);
94
87
static StaticTuple *(*StaticTuple_intern)(StaticTuple *);
95
#undef StaticTuple_CheckExact
96
static int (*StaticTuple_CheckExact)(PyObject *);
88
static PyTypeObject *_p_StaticTuple_Type;
90
#define StaticTuple_CheckExact(op) (Py_TYPE(op) == _p_StaticTuple_Type)
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);
125
fprintf(stderr, "Imported function %s @%x\n", funcname, *f);
129
fprintf(stderr, "Returning -1\n");
135
static PyTypeObject *
136
_import_type(PyObject *module, char *class_name)
138
PyObject *type = NULL;
140
type = PyObject_GetAttrString(module, class_name);
144
if (!PyType_Check(type)) {
145
PyErr_Format(PyExc_TypeError,
146
"%s.%s is not a type object",
147
PyModule_GetName(module), class_name);
150
return (PyTypeObject *)type;
99
157
/* Return -1 and set exception on error, 0 on success */
101
import_static_tuple(void)
159
import_static_tuple_c(void)
103
PyObject *module = PyImport_ImportModule("bzrlib._static_tuple_c");
104
PyObject *c_api_object;
106
if (module == NULL) {
107
fprintf(stderr, "Failed to find module _static_tuple_c.\n");
110
c_api_object = PyObject_GetAttrString(module, "_C_API");
111
if (c_api_object == NULL) {
112
fprintf(stderr, "Failed to find _static_tuple_c._C_API.\n");
115
if (!PyCObject_Check(c_api_object)) {
116
fprintf(stderr, "_static_tuple_c._C_API not a CObject.\n");
117
Py_DECREF(c_api_object);
120
StaticTuple_API = (void **)PyCObject_AsVoidPtr(c_api_object);
121
StaticTuple_New = StaticTuple_API[StaticTuple_New_NUM];
122
StaticTuple_intern = StaticTuple_API[StaticTuple_intern_NUM];
123
StaticTuple_CheckExact = StaticTuple_API[StaticTuple_CheckExact_NUM];
124
Py_DECREF(c_api_object);
161
/* This is modeled after the implementation in Pyrex, which uses a
162
* dictionary and descriptors, rather than using plain offsets into a
165
PyObject *module = NULL;
167
module = PyImport_ImportModule("bzrlib._static_tuple_c");
168
if (!module) goto bad;
169
if (_import_function(module, "StaticTuple_New", (void **)&StaticTuple_New,
170
"StaticTuple *(Py_ssize_t)") < 0)
172
if (_import_function(module, "StaticTuple_intern",
173
(void **)&StaticTuple_intern,
174
"StaticTuple *(StaticTuple *)") < 0)
176
if (_import_function(module, "_StaticTuple_CheckExact",
177
(void **)&_StaticTuple_CheckExact,
178
"int(PyObject *)") < 0)
180
_p_StaticTuple_Type = _import_type(module, "StaticTuple");
181
if (!_p_StaticTuple_Type) {
129
#endif // _STATIC_TUPLE_H_
191
#endif // !STATIC_TUPLE_MODULE
192
#endif // !_STATIC_TUPLE_H_