1
from libc.stdio cimport FILE
4
cdef extern from "Python.h":
6
ctypedef struct PyObject # forward declaration
8
ctypedef object (*newfunc)(cpython.type.type, PyObject*, PyObject*) # (type, args|NULL, kwargs|NULL)
10
ctypedef object (*unaryfunc)(object)
11
ctypedef object (*binaryfunc)(object, object)
12
ctypedef object (*ternaryfunc)(object, object, object)
13
ctypedef int (*inquiry)(object) except -1
14
ctypedef Py_ssize_t (*lenfunc)(object) except -1
15
ctypedef object (*ssizeargfunc)(object, Py_ssize_t)
16
ctypedef object (*ssizessizeargfunc)(object, Py_ssize_t, Py_ssize_t)
17
ctypedef int (*ssizeobjargproc)(object, Py_ssize_t, object) except -1
18
ctypedef int (*ssizessizeobjargproc)(object, Py_ssize_t, Py_ssize_t, object) except -1
19
ctypedef int (*objobjargproc)(object, object, object) except -1
20
ctypedef int (*objobjproc)(object, object) except -1
22
ctypedef Py_hash_t (*hashfunc)(object) except -1
23
ctypedef object (*reprfunc)(object)
25
ctypedef int (*cmpfunc)(object, object) except -2
26
ctypedef object (*richcmpfunc)(object, object, int)
28
# The following functions use 'PyObject*' as first argument instead of 'object' to prevent
29
# accidental reference counting when calling them during a garbage collection run.
30
ctypedef void (*destructor)(PyObject*)
31
ctypedef int (*visitproc)(PyObject*, void *) except -1
32
ctypedef int (*traverseproc)(PyObject*, visitproc, void*) except -1
33
ctypedef void (*freefunc)(void*)
35
ctypedef object (*descrgetfunc)(object, object, object)
36
ctypedef int (*descrsetfunc)(object, object, object) except -1
38
ctypedef object (*PyCFunction)(object, object)
40
ctypedef struct PyMethodDef:
46
ctypedef struct PyTypeObject:
49
Py_ssize_t tp_basicsize
50
Py_ssize_t tp_itemsize
51
Py_ssize_t tp_dictoffset
52
unsigned long tp_flags
57
destructor tp_finalize
58
traverseproc tp_traverse
68
richcmpfunc tp_richcompare
70
PyMethodDef* tp_methods
75
descrgetfunc tp_descr_get
76
descrsetfunc tp_descr_set
78
unsigned int tp_version_tag
80
ctypedef struct PyObject:
84
cdef PyTypeObject *Py_TYPE(object)
86
void* PyObject_Malloc(size_t)
87
void* PyObject_Realloc(void *, size_t)
88
void PyObject_Free(void *)
90
#####################################################################
92
#####################################################################
93
int PyObject_Print(object o, FILE *fp, int flags) except -1
94
# Print an object o, on file fp. Returns -1 on error. The flags
95
# argument is used to enable certain printing options. The only
96
# option currently supported is Py_PRINT_RAW; if given, the str()
97
# of the object is written instead of the repr().
99
bint PyObject_HasAttrString(object o, const char *attr_name)
100
# Returns 1 if o has the attribute attr_name, and 0
101
# otherwise. This is equivalent to the Python expression
102
# "hasattr(o, attr_name)". This function always succeeds.
104
object PyObject_GetAttrString(object o, const char *attr_name)
105
# Return value: New reference. Retrieve an attribute named
106
# attr_name from object o. Returns the attribute value on success,
107
# or NULL on failure. This is the equivalent of the Python
108
# expression "o.attr_name".
110
bint PyObject_HasAttr(object o, object attr_name)
111
# Returns 1 if o has the attribute attr_name, and 0
112
# otherwise. This is equivalent to the Python expression
113
# "hasattr(o, attr_name)". This function always succeeds.
115
object PyObject_GetAttr(object o, object attr_name)
116
# Return value: New reference. Retrieve an attribute named
117
# attr_name from object o. Returns the attribute value on success,
118
# or NULL on failure. This is the equivalent of the Python
119
# expression "o.attr_name".
121
object PyObject_GenericGetAttr(object o, object attr_name)
123
int PyObject_SetAttrString(object o, const char *attr_name, object v) except -1
124
# Set the value of the attribute named attr_name, for object o, to
125
# the value v. Returns -1 on failure. This is the equivalent of
126
# the Python statement "o.attr_name = v".
128
int PyObject_SetAttr(object o, object attr_name, object v) except -1
129
# Set the value of the attribute named attr_name, for object o, to
130
# the value v. Returns -1 on failure. This is the equivalent of
131
# the Python statement "o.attr_name = v".
133
int PyObject_GenericSetAttr(object o, object attr_name, object v) except -1
135
int PyObject_DelAttrString(object o, const char *attr_name) except -1
136
# Delete attribute named attr_name, for object o. Returns -1 on
137
# failure. This is the equivalent of the Python statement: "del
140
int PyObject_DelAttr(object o, object attr_name) except -1
141
# Delete attribute named attr_name, for object o. Returns -1 on
142
# failure. This is the equivalent of the Python statement "del
145
object PyObject_GenericGetDict(object o, void *context)
146
# Return value: New reference.
147
# A generic implementation for the getter of a __dict__ descriptor. It
148
# creates the dictionary if necessary.
149
# New in version 3.3.
151
int PyObject_GenericSetDict(object o, object value, void *context) except -1
152
# A generic implementation for the setter of a __dict__ descriptor. This
153
# implementation does not allow the dictionary to be deleted.
154
# New in version 3.3.
156
int Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
158
object PyObject_RichCompare(object o1, object o2, int opid)
159
# Return value: New reference.
160
# Compare the values of o1 and o2 using the operation specified by
161
# opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
162
# Py_GE, corresponding to <, <=, ==, !=, >, or >=
163
# respectively. This is the equivalent of the Python expression
164
# "o1 op o2", where op is the operator corresponding to
165
# opid. Returns the value of the comparison on success, or NULL on
168
bint PyObject_RichCompareBool(object o1, object o2, int opid) except -1
169
# Compare the values of o1 and o2 using the operation specified by
170
# opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
171
# Py_GE, corresponding to <, <=, ==, !=, >, or >=
172
# respectively. Returns -1 on error, 0 if the result is false, 1
173
# otherwise. This is the equivalent of the Python expression "o1
174
# op o2", where op is the operator corresponding to opid.
176
int PyObject_Cmp(object o1, object o2, int *result) except -1
177
# Compare the values of o1 and o2 using a routine provided by o1,
178
# if one exists, otherwise with a routine provided by o2. The
179
# result of the comparison is returned in result. Returns -1 on
180
# failure. This is the equivalent of the Python statement "result
183
int PyObject_Compare(object o1, object o2) except *
184
# Compare the values of o1 and o2 using a routine provided by o1,
185
# if one exists, otherwise with a routine provided by o2. Returns
186
# the result of the comparison on success. On error, the value
187
# returned is undefined; use PyErr_Occurred() to detect an
188
# error. This is equivalent to the Python expression "cmp(o1,
191
object PyObject_Repr(object o)
192
# Return value: New reference.
193
# Compute a string representation of object o. Returns the string
194
# representation on success, NULL on failure. This is the
195
# equivalent of the Python expression "repr(o)". Called by the
196
# repr() built-in function and by reverse quotes.
198
object PyObject_Str(object o)
199
# Return value: New reference.
200
# Compute a string representation of object o. Returns the string
201
# representation on success, NULL on failure. This is the
202
# equivalent of the Python expression "str(o)". Called by the
203
# str() built-in function and by the print statement.
205
object PyObject_Bytes(object o)
206
# Return value: New reference.
207
# Compute a bytes representation of object o. Return NULL on
208
# failure and a bytes object on success. This is equivalent to
209
# the Python expression bytes(o), when o is not an integer.
210
# Unlike bytes(o), a TypeError is raised when o is an integer
211
# instead of a zero-initialized bytes object.
213
bint PyObject_IsInstance(object inst, object cls) except -1
214
# Returns 1 if inst is an instance of the class cls or a subclass
215
# of cls, or 0 if not. On error, returns -1 and sets an
216
# exception. If cls is a type object rather than a class object,
217
# PyObject_IsInstance() returns 1 if inst is of type cls. If cls
218
# is a tuple, the check will be done against every entry in
219
# cls. The result will be 1 when at least one of the checks
220
# returns 1, otherwise it will be 0. If inst is not a class
221
# instance and cls is neither a type object, nor a class object,
222
# nor a tuple, inst must have a __class__ attribute -- the class
223
# relationship of the value of that attribute with cls will be
224
# used to determine the result of this function.
226
# Subclass determination is done in a fairly straightforward way,
227
# but includes a wrinkle that implementors of extensions to the
228
# class system may want to be aware of. If A and B are class
229
# objects, B is a subclass of A if it inherits from A either
230
# directly or indirectly. If either is not a class object, a more
231
# general mechanism is used to determine the class relationship of
232
# the two objects. When testing if B is a subclass of A, if A is
233
# B, PyObject_IsSubclass() returns true. If A and B are different
234
# objects, B's __bases__ attribute is searched in a depth-first
235
# fashion for A -- the presence of the __bases__ attribute is
236
# considered sufficient for this determination.
238
bint PyObject_IsSubclass(object derived, object cls) except -1
239
# Returns 1 if the class derived is identical to or derived from
240
# the class cls, otherwise returns 0. In case of an error, returns
241
# -1. If cls is a tuple, the check will be done against every
242
# entry in cls. The result will be 1 when at least one of the
243
# checks returns 1, otherwise it will be 0. If either derived or
244
# cls is not an actual class object (or tuple), this function uses
245
# the generic algorithm described above. New in version
246
# 2.1. Changed in version 2.3: Older versions of Python did not
247
# support a tuple as the second argument.
249
bint PyCallable_Check(object o)
250
# Determine if the object o is callable. Return 1 if the object is
251
# callable and 0 otherwise. This function always succeeds.
253
object PyObject_Call(object callable_object, object args, object kw)
254
# Return value: New reference.
255
# Call a callable Python object callable_object, with arguments
256
# given by the tuple args, and named arguments given by the
257
# dictionary kw. If no named arguments are needed, kw may be
258
# NULL. args must not be NULL, use an empty tuple if no arguments
259
# are needed. Returns the result of the call on success, or NULL
260
# on failure. This is the equivalent of the Python expression
261
# "apply(callable_object, args, kw)" or "callable_object(*args,
264
object PyObject_CallObject(object callable_object, object args)
265
# Return value: New reference.
266
# Call a callable Python object callable_object, with arguments
267
# given by the tuple args. If no arguments are needed, then args
268
# may be NULL. Returns the result of the call on success, or NULL
269
# on failure. This is the equivalent of the Python expression
270
# "apply(callable_object, args)" or "callable_object(*args)".
272
object PyObject_CallFunction(object callable, char *format, ...)
273
# Return value: New reference.
274
# Call a callable Python object callable, with a variable number
275
# of C arguments. The C arguments are described using a
276
# Py_BuildValue() style format string. The format may be NULL,
277
# indicating that no arguments are provided. Returns the result of
278
# the call on success, or NULL on failure. This is the equivalent
279
# of the Python expression "apply(callable, args)" or
280
# "callable(*args)". Note that if you only pass object args,
281
# PyObject_CallFunctionObjArgs is a faster alternative.
283
object PyObject_CallMethod(object o, char *method, char *format, ...)
284
# Return value: New reference.
285
# Call the method named method of object o with a variable number
286
# of C arguments. The C arguments are described by a
287
# Py_BuildValue() format string that should produce a tuple. The
288
# format may be NULL, indicating that no arguments are
289
# provided. Returns the result of the call on success, or NULL on
290
# failure. This is the equivalent of the Python expression
291
# "o.method(args)". Note that if you only pass object args,
292
# PyObject_CallMethodObjArgs is a faster alternative.
294
#object PyObject_CallFunctionObjArgs(object callable, ..., NULL)
295
object PyObject_CallFunctionObjArgs(object callable, ...)
296
# Return value: New reference.
297
# Call a callable Python object callable, with a variable number
298
# of PyObject* arguments. The arguments are provided as a variable
299
# number of parameters followed by NULL. Returns the result of the
300
# call on success, or NULL on failure.
302
#PyObject* PyObject_CallMethodObjArgs(object o, object name, ..., NULL)
303
object PyObject_CallMethodObjArgs(object o, object name, ...)
304
# Return value: New reference.
305
# Calls a method of the object o, where the name of the method is
306
# given as a Python string object in name. It is called with a
307
# variable number of PyObject* arguments. The arguments are
308
# provided as a variable number of parameters followed by
309
# NULL. Returns the result of the call on success, or NULL on
312
long PyObject_Hash(object o) except? -1
313
# Compute and return the hash value of an object o. On failure,
314
# return -1. This is the equivalent of the Python expression
317
bint PyObject_IsTrue(object o) except -1
318
# Returns 1 if the object o is considered to be true, and 0
319
# otherwise. This is equivalent to the Python expression "not not
320
# o". On failure, return -1.
322
bint PyObject_Not(object o) except -1
323
# Returns 0 if the object o is considered to be true, and 1
324
# otherwise. This is equivalent to the Python expression "not
325
# o". On failure, return -1.
327
object PyObject_Type(object o)
328
# Return value: New reference.
329
# When o is non-NULL, returns a type object corresponding to the
330
# object type of object o. On failure, raises SystemError and
331
# returns NULL. This is equivalent to the Python expression
332
# type(o). This function increments the reference count of the
333
# return value. There's really no reason to use this function
334
# instead of the common expression o->ob_type, which returns a
335
# pointer of type PyTypeObject*, except when the incremented
336
# reference count is needed.
338
bint PyObject_TypeCheck(object o, PyTypeObject *type)
339
# Return true if the object o is of type type or a subtype of
340
# type. Both parameters must be non-NULL.
342
Py_ssize_t PyObject_Length(object o) except -1
343
Py_ssize_t PyObject_Size(object o) except -1
344
# Return the length of object o. If the object o provides either
345
# the sequence and mapping protocols, the sequence length is
346
# returned. On error, -1 is returned. This is the equivalent to
347
# the Python expression "len(o)".
349
Py_ssize_t PyObject_LengthHint(object o, Py_ssize_t default) except -1
350
# Return an estimated length for the object o. First try to return its
351
# actual length, then an estimate using __length_hint__(), and finally
352
# return the default value. On error, return -1. This is the equivalent to
353
# the Python expression "operator.length_hint(o, default)".
354
# New in version 3.4.
356
object PyObject_GetItem(object o, object key)
357
# Return value: New reference.
358
# Return element of o corresponding to the object key or NULL on
359
# failure. This is the equivalent of the Python expression
362
int PyObject_SetItem(object o, object key, object v) except -1
363
# Map the object key to the value v. Returns -1 on failure. This
364
# is the equivalent of the Python statement "o[key] = v".
366
int PyObject_DelItem(object o, object key) except -1
367
# Delete the mapping for key from o. Returns -1 on failure. This
368
# is the equivalent of the Python statement "del o[key]".
370
int PyObject_AsFileDescriptor(object o) except -1
371
# Derives a file-descriptor from a Python object. If the object is
372
# an integer or long integer, its value is returned. If not, the
373
# object's fileno() method is called if it exists; the method must
374
# return an integer or long integer, which is returned as the file
375
# descriptor value. Returns -1 on failure.
377
object PyObject_Dir(object o)
378
# Return value: New reference.
379
# This is equivalent to the Python expression "dir(o)", returning
380
# a (possibly empty) list of strings appropriate for the object
381
# argument, or NULL if there was an error. If the argument is
382
# NULL, this is like the Python "dir()", returning the names of
383
# the current locals; in this case, if no execution frame is
384
# active then NULL is returned but PyErr_Occurred() will return
387
object PyObject_GetIter(object o)
388
# Return value: New reference.
389
# This is equivalent to the Python expression "iter(o)". It
390
# returns a new iterator for the object argument, or the object
391
# itself if the object is already an iterator. Raises TypeError
392
# and returns NULL if the object cannot be iterated.
394
Py_ssize_t Py_SIZE(object o)
396
object PyObject_Format(object obj, object format_spec)
397
# Takes an arbitrary object and returns the result of calling
398
# obj.__format__(format_spec).
401
# Type flags (tp_flags of PyTypeObject)
402
long Py_TPFLAGS_HAVE_GETCHARBUFFER
403
long Py_TPFLAGS_HAVE_SEQUENCE_IN
404
long Py_TPFLAGS_HAVE_INPLACEOPS
405
long Py_TPFLAGS_CHECKTYPES
406
long Py_TPFLAGS_HAVE_RICHCOMPARE
407
long Py_TPFLAGS_HAVE_WEAKREFS
408
long Py_TPFLAGS_HAVE_ITER
409
long Py_TPFLAGS_HAVE_CLASS
410
long Py_TPFLAGS_HEAPTYPE
411
long Py_TPFLAGS_BASETYPE
412
long Py_TPFLAGS_READY
413
long Py_TPFLAGS_READYING
414
long Py_TPFLAGS_HAVE_GC
415
long Py_TPFLAGS_HAVE_STACKLESS_EXTENSION
416
long Py_TPFLAGS_HAVE_INDEX
417
long Py_TPFLAGS_HAVE_VERSION_TAG
418
long Py_TPFLAGS_VALID_VERSION_TAG
419
long Py_TPFLAGS_IS_ABSTRACT
420
long Py_TPFLAGS_HAVE_NEWBUFFER
421
long Py_TPFLAGS_INT_SUBCLASS
422
long Py_TPFLAGS_LONG_SUBCLASS
423
long Py_TPFLAGS_LIST_SUBCLASS
424
long Py_TPFLAGS_TUPLE_SUBCLASS
425
long Py_TPFLAGS_STRING_SUBCLASS
426
long Py_TPFLAGS_UNICODE_SUBCLASS
427
long Py_TPFLAGS_DICT_SUBCLASS
428
long Py_TPFLAGS_BASE_EXC_SUBCLASS
429
long Py_TPFLAGS_TYPE_SUBCLASS
430
long Py_TPFLAGS_DEFAULT_EXTERNAL
431
long Py_TPFLAGS_DEFAULT_CORE
432
long Py_TPFLAGS_DEFAULT
433
long Py_TPFLAGS_HAVE_FINALIZE