object.pxd 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. from libc.stdio cimport FILE
  2. cimport cpython.type
  3. cdef extern from "Python.h":
  4. ctypedef struct PyObject # forward declaration
  5. ctypedef object (*newfunc)(cpython.type.type, object, object) # (type, args, kwargs)
  6. ctypedef object (*unaryfunc)(object)
  7. ctypedef object (*binaryfunc)(object, object)
  8. ctypedef object (*ternaryfunc)(object, object, object)
  9. ctypedef int (*inquiry)(object) except -1
  10. ctypedef Py_ssize_t (*lenfunc)(object) except -1
  11. ctypedef object (*ssizeargfunc)(object, Py_ssize_t)
  12. ctypedef object (*ssizessizeargfunc)(object, Py_ssize_t, Py_ssize_t)
  13. ctypedef int (*ssizeobjargproc)(object, Py_ssize_t, object) except -1
  14. ctypedef int (*ssizessizeobjargproc)(object, Py_ssize_t, Py_ssize_t, object) except -1
  15. ctypedef int (*objobjargproc)(object, object, object) except -1
  16. ctypedef int (*objobjproc)(object, object) except -1
  17. ctypedef Py_hash_t (*hashfunc)(object) except -1
  18. ctypedef object (*reprfunc)(object)
  19. ctypedef int (*cmpfunc)(object, object) except -2
  20. ctypedef object (*richcmpfunc)(object, object, int)
  21. # The following functions use 'PyObject*' as first argument instead of 'object' to prevent
  22. # accidental reference counting when calling them during a garbage collection run.
  23. ctypedef void (*destructor)(PyObject*)
  24. ctypedef int (*visitproc)(PyObject*, void *) except -1
  25. ctypedef int (*traverseproc)(PyObject*, visitproc, void*) except -1
  26. ctypedef void (*freefunc)(void*)
  27. ctypedef object (*descrgetfunc)(object, object, object)
  28. ctypedef int (*descrsetfunc)(object, object, object) except -1
  29. ctypedef struct PyTypeObject:
  30. const char* tp_name
  31. const char* tp_doc
  32. Py_ssize_t tp_basicsize
  33. Py_ssize_t tp_itemsize
  34. Py_ssize_t tp_dictoffset
  35. unsigned long tp_flags
  36. newfunc tp_new
  37. destructor tp_dealloc
  38. traverseproc tp_traverse
  39. inquiry tp_clear
  40. freefunc tp_free
  41. ternaryfunc tp_call
  42. hashfunc tp_hash
  43. reprfunc tp_str
  44. reprfunc tp_repr
  45. cmpfunc tp_compare
  46. richcmpfunc tp_richcompare
  47. PyTypeObject* tp_base
  48. PyObject* tp_dict
  49. descrgetfunc tp_descr_get
  50. descrsetfunc tp_descr_set
  51. ctypedef struct PyObject:
  52. Py_ssize_t ob_refcnt
  53. PyTypeObject *ob_type
  54. cdef PyTypeObject *Py_TYPE(object)
  55. void* PyObject_Malloc(size_t)
  56. void* PyObject_Realloc(void *, size_t)
  57. void PyObject_Free(void *)
  58. #####################################################################
  59. # 6.1 Object Protocol
  60. #####################################################################
  61. int PyObject_Print(object o, FILE *fp, int flags) except -1
  62. # Print an object o, on file fp. Returns -1 on error. The flags
  63. # argument is used to enable certain printing options. The only
  64. # option currently supported is Py_PRINT_RAW; if given, the str()
  65. # of the object is written instead of the repr().
  66. bint PyObject_HasAttrString(object o, const char *attr_name)
  67. # Returns 1 if o has the attribute attr_name, and 0
  68. # otherwise. This is equivalent to the Python expression
  69. # "hasattr(o, attr_name)". This function always succeeds.
  70. object PyObject_GetAttrString(object o, const char *attr_name)
  71. # Return value: New reference. Retrieve an attribute named
  72. # attr_name from object o. Returns the attribute value on success,
  73. # or NULL on failure. This is the equivalent of the Python
  74. # expression "o.attr_name".
  75. bint PyObject_HasAttr(object o, object attr_name)
  76. # Returns 1 if o has the attribute attr_name, and 0
  77. # otherwise. This is equivalent to the Python expression
  78. # "hasattr(o, attr_name)". This function always succeeds.
  79. object PyObject_GetAttr(object o, object attr_name)
  80. # Return value: New reference. Retrieve an attribute named
  81. # attr_name from object o. Returns the attribute value on success,
  82. # or NULL on failure. This is the equivalent of the Python
  83. # expression "o.attr_name".
  84. object PyObject_GenericGetAttr(object o, object attr_name)
  85. int PyObject_SetAttrString(object o, const char *attr_name, object v) except -1
  86. # Set the value of the attribute named attr_name, for object o, to
  87. # the value v. Returns -1 on failure. This is the equivalent of
  88. # the Python statement "o.attr_name = v".
  89. int PyObject_SetAttr(object o, object attr_name, object v) except -1
  90. # Set the value of the attribute named attr_name, for object o, to
  91. # the value v. Returns -1 on failure. This is the equivalent of
  92. # the Python statement "o.attr_name = v".
  93. int PyObject_GenericSetAttr(object o, object attr_name, object v) except -1
  94. int PyObject_DelAttrString(object o, const char *attr_name) except -1
  95. # Delete attribute named attr_name, for object o. Returns -1 on
  96. # failure. This is the equivalent of the Python statement: "del
  97. # o.attr_name".
  98. int PyObject_DelAttr(object o, object attr_name) except -1
  99. # Delete attribute named attr_name, for object o. Returns -1 on
  100. # failure. This is the equivalent of the Python statement "del
  101. # o.attr_name".
  102. int Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
  103. object PyObject_RichCompare(object o1, object o2, int opid)
  104. # Return value: New reference.
  105. # Compare the values of o1 and o2 using the operation specified by
  106. # opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
  107. # Py_GE, corresponding to <, <=, ==, !=, >, or >=
  108. # respectively. This is the equivalent of the Python expression
  109. # "o1 op o2", where op is the operator corresponding to
  110. # opid. Returns the value of the comparison on success, or NULL on
  111. # failure.
  112. bint PyObject_RichCompareBool(object o1, object o2, int opid) except -1
  113. # Compare the values of o1 and o2 using the operation specified by
  114. # opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
  115. # Py_GE, corresponding to <, <=, ==, !=, >, or >=
  116. # respectively. Returns -1 on error, 0 if the result is false, 1
  117. # otherwise. This is the equivalent of the Python expression "o1
  118. # op o2", where op is the operator corresponding to opid.
  119. int PyObject_Cmp(object o1, object o2, int *result) except -1
  120. # Compare the values of o1 and o2 using a routine provided by o1,
  121. # if one exists, otherwise with a routine provided by o2. The
  122. # result of the comparison is returned in result. Returns -1 on
  123. # failure. This is the equivalent of the Python statement "result
  124. # = cmp(o1, o2)".
  125. int PyObject_Compare(object o1, object o2) except *
  126. # Compare the values of o1 and o2 using a routine provided by o1,
  127. # if one exists, otherwise with a routine provided by o2. Returns
  128. # the result of the comparison on success. On error, the value
  129. # returned is undefined; use PyErr_Occurred() to detect an
  130. # error. This is equivalent to the Python expression "cmp(o1,
  131. # o2)".
  132. object PyObject_Repr(object o)
  133. # Return value: New reference.
  134. # Compute a string representation of object o. Returns the string
  135. # representation on success, NULL on failure. This is the
  136. # equivalent of the Python expression "repr(o)". Called by the
  137. # repr() built-in function and by reverse quotes.
  138. object PyObject_Str(object o)
  139. # Return value: New reference.
  140. # Compute a string representation of object o. Returns the string
  141. # representation on success, NULL on failure. This is the
  142. # equivalent of the Python expression "str(o)". Called by the
  143. # str() built-in function and by the print statement.
  144. object PyObject_Unicode(object o)
  145. # Return value: New reference.
  146. # Compute a Unicode string representation of object o. Returns the
  147. # Unicode string representation on success, NULL on failure. This
  148. # is the equivalent of the Python expression "unicode(o)". Called
  149. # by the unicode() built-in function.
  150. bint PyObject_IsInstance(object inst, object cls) except -1
  151. # Returns 1 if inst is an instance of the class cls or a subclass
  152. # of cls, or 0 if not. On error, returns -1 and sets an
  153. # exception. If cls is a type object rather than a class object,
  154. # PyObject_IsInstance() returns 1 if inst is of type cls. If cls
  155. # is a tuple, the check will be done against every entry in
  156. # cls. The result will be 1 when at least one of the checks
  157. # returns 1, otherwise it will be 0. If inst is not a class
  158. # instance and cls is neither a type object, nor a class object,
  159. # nor a tuple, inst must have a __class__ attribute -- the class
  160. # relationship of the value of that attribute with cls will be
  161. # used to determine the result of this function.
  162. # Subclass determination is done in a fairly straightforward way,
  163. # but includes a wrinkle that implementors of extensions to the
  164. # class system may want to be aware of. If A and B are class
  165. # objects, B is a subclass of A if it inherits from A either
  166. # directly or indirectly. If either is not a class object, a more
  167. # general mechanism is used to determine the class relationship of
  168. # the two objects. When testing if B is a subclass of A, if A is
  169. # B, PyObject_IsSubclass() returns true. If A and B are different
  170. # objects, B's __bases__ attribute is searched in a depth-first
  171. # fashion for A -- the presence of the __bases__ attribute is
  172. # considered sufficient for this determination.
  173. bint PyObject_IsSubclass(object derived, object cls) except -1
  174. # Returns 1 if the class derived is identical to or derived from
  175. # the class cls, otherwise returns 0. In case of an error, returns
  176. # -1. If cls is a tuple, the check will be done against every
  177. # entry in cls. The result will be 1 when at least one of the
  178. # checks returns 1, otherwise it will be 0. If either derived or
  179. # cls is not an actual class object (or tuple), this function uses
  180. # the generic algorithm described above. New in version
  181. # 2.1. Changed in version 2.3: Older versions of Python did not
  182. # support a tuple as the second argument.
  183. bint PyCallable_Check(object o)
  184. # Determine if the object o is callable. Return 1 if the object is
  185. # callable and 0 otherwise. This function always succeeds.
  186. object PyObject_Call(object callable_object, object args, object kw)
  187. # Return value: New reference.
  188. # Call a callable Python object callable_object, with arguments
  189. # given by the tuple args, and named arguments given by the
  190. # dictionary kw. If no named arguments are needed, kw may be
  191. # NULL. args must not be NULL, use an empty tuple if no arguments
  192. # are needed. Returns the result of the call on success, or NULL
  193. # on failure. This is the equivalent of the Python expression
  194. # "apply(callable_object, args, kw)" or "callable_object(*args,
  195. # **kw)".
  196. object PyObject_CallObject(object callable_object, object args)
  197. # Return value: New reference.
  198. # Call a callable Python object callable_object, with arguments
  199. # given by the tuple args. If no arguments are needed, then args
  200. # may be NULL. Returns the result of the call on success, or NULL
  201. # on failure. This is the equivalent of the Python expression
  202. # "apply(callable_object, args)" or "callable_object(*args)".
  203. object PyObject_CallFunction(object callable, char *format, ...)
  204. # Return value: New reference.
  205. # Call a callable Python object callable, with a variable number
  206. # of C arguments. The C arguments are described using a
  207. # Py_BuildValue() style format string. The format may be NULL,
  208. # indicating that no arguments are provided. Returns the result of
  209. # the call on success, or NULL on failure. This is the equivalent
  210. # of the Python expression "apply(callable, args)" or
  211. # "callable(*args)". Note that if you only pass object args,
  212. # PyObject_CallFunctionObjArgs is a faster alternative.
  213. object PyObject_CallMethod(object o, char *method, char *format, ...)
  214. # Return value: New reference.
  215. # Call the method named method of object o with a variable number
  216. # of C arguments. The C arguments are described by a
  217. # Py_BuildValue() format string that should produce a tuple. The
  218. # format may be NULL, indicating that no arguments are
  219. # provided. Returns the result of the call on success, or NULL on
  220. # failure. This is the equivalent of the Python expression
  221. # "o.method(args)". Note that if you only pass object args,
  222. # PyObject_CallMethodObjArgs is a faster alternative.
  223. #object PyObject_CallFunctionObjArgs(object callable, ..., NULL)
  224. object PyObject_CallFunctionObjArgs(object callable, ...)
  225. # Return value: New reference.
  226. # Call a callable Python object callable, with a variable number
  227. # of PyObject* arguments. The arguments are provided as a variable
  228. # number of parameters followed by NULL. Returns the result of the
  229. # call on success, or NULL on failure.
  230. #PyObject* PyObject_CallMethodObjArgs(object o, object name, ..., NULL)
  231. object PyObject_CallMethodObjArgs(object o, object name, ...)
  232. # Return value: New reference.
  233. # Calls a method of the object o, where the name of the method is
  234. # given as a Python string object in name. It is called with a
  235. # variable number of PyObject* arguments. The arguments are
  236. # provided as a variable number of parameters followed by
  237. # NULL. Returns the result of the call on success, or NULL on
  238. # failure.
  239. long PyObject_Hash(object o) except? -1
  240. # Compute and return the hash value of an object o. On failure,
  241. # return -1. This is the equivalent of the Python expression
  242. # "hash(o)".
  243. bint PyObject_IsTrue(object o) except -1
  244. # Returns 1 if the object o is considered to be true, and 0
  245. # otherwise. This is equivalent to the Python expression "not not
  246. # o". On failure, return -1.
  247. bint PyObject_Not(object o) except -1
  248. # Returns 0 if the object o is considered to be true, and 1
  249. # otherwise. This is equivalent to the Python expression "not
  250. # o". On failure, return -1.
  251. object PyObject_Type(object o)
  252. # Return value: New reference.
  253. # When o is non-NULL, returns a type object corresponding to the
  254. # object type of object o. On failure, raises SystemError and
  255. # returns NULL. This is equivalent to the Python expression
  256. # type(o). This function increments the reference count of the
  257. # return value. There's really no reason to use this function
  258. # instead of the common expression o->ob_type, which returns a
  259. # pointer of type PyTypeObject*, except when the incremented
  260. # reference count is needed.
  261. bint PyObject_TypeCheck(object o, PyTypeObject *type)
  262. # Return true if the object o is of type type or a subtype of
  263. # type. Both parameters must be non-NULL.
  264. Py_ssize_t PyObject_Length(object o) except -1
  265. Py_ssize_t PyObject_Size(object o) except -1
  266. # Return the length of object o. If the object o provides either
  267. # the sequence and mapping protocols, the sequence length is
  268. # returned. On error, -1 is returned. This is the equivalent to
  269. # the Python expression "len(o)".
  270. object PyObject_GetItem(object o, object key)
  271. # Return value: New reference.
  272. # Return element of o corresponding to the object key or NULL on
  273. # failure. This is the equivalent of the Python expression
  274. # "o[key]".
  275. int PyObject_SetItem(object o, object key, object v) except -1
  276. # Map the object key to the value v. Returns -1 on failure. This
  277. # is the equivalent of the Python statement "o[key] = v".
  278. int PyObject_DelItem(object o, object key) except -1
  279. # Delete the mapping for key from o. Returns -1 on failure. This
  280. # is the equivalent of the Python statement "del o[key]".
  281. int PyObject_AsFileDescriptor(object o) except -1
  282. # Derives a file-descriptor from a Python object. If the object is
  283. # an integer or long integer, its value is returned. If not, the
  284. # object's fileno() method is called if it exists; the method must
  285. # return an integer or long integer, which is returned as the file
  286. # descriptor value. Returns -1 on failure.
  287. object PyObject_Dir(object o)
  288. # Return value: New reference.
  289. # This is equivalent to the Python expression "dir(o)", returning
  290. # a (possibly empty) list of strings appropriate for the object
  291. # argument, or NULL if there was an error. If the argument is
  292. # NULL, this is like the Python "dir()", returning the names of
  293. # the current locals; in this case, if no execution frame is
  294. # active then NULL is returned but PyErr_Occurred() will return
  295. # false.
  296. object PyObject_GetIter(object o)
  297. # Return value: New reference.
  298. # This is equivalent to the Python expression "iter(o)". It
  299. # returns a new iterator for the object argument, or the object
  300. # itself if the object is already an iterator. Raises TypeError
  301. # and returns NULL if the object cannot be iterated.
  302. Py_ssize_t Py_SIZE(object o)
  303. object PyObject_Format(object obj, object format_spec)
  304. # Takes an arbitrary object and returns the result of calling
  305. # obj.__format__(format_spec).
  306. # Added in Py2.6
  307. # Type flags (tp_flags of PyTypeObject)
  308. long Py_TPFLAGS_HAVE_GETCHARBUFFER
  309. long Py_TPFLAGS_HAVE_SEQUENCE_IN
  310. long Py_TPFLAGS_HAVE_INPLACEOPS
  311. long Py_TPFLAGS_CHECKTYPES
  312. long Py_TPFLAGS_HAVE_RICHCOMPARE
  313. long Py_TPFLAGS_HAVE_WEAKREFS
  314. long Py_TPFLAGS_HAVE_ITER
  315. long Py_TPFLAGS_HAVE_CLASS
  316. long Py_TPFLAGS_HEAPTYPE
  317. long Py_TPFLAGS_BASETYPE
  318. long Py_TPFLAGS_READY
  319. long Py_TPFLAGS_READYING
  320. long Py_TPFLAGS_HAVE_GC
  321. long Py_TPFLAGS_HAVE_STACKLESS_EXTENSION
  322. long Py_TPFLAGS_HAVE_INDEX
  323. long Py_TPFLAGS_HAVE_VERSION_TAG
  324. long Py_TPFLAGS_VALID_VERSION_TAG
  325. long Py_TPFLAGS_IS_ABSTRACT
  326. long Py_TPFLAGS_HAVE_NEWBUFFER
  327. long Py_TPFLAGS_INT_SUBCLASS
  328. long Py_TPFLAGS_LONG_SUBCLASS
  329. long Py_TPFLAGS_LIST_SUBCLASS
  330. long Py_TPFLAGS_TUPLE_SUBCLASS
  331. long Py_TPFLAGS_STRING_SUBCLASS
  332. long Py_TPFLAGS_UNICODE_SUBCLASS
  333. long Py_TPFLAGS_DICT_SUBCLASS
  334. long Py_TPFLAGS_BASE_EXC_SUBCLASS
  335. long Py_TPFLAGS_TYPE_SUBCLASS
  336. long Py_TPFLAGS_DEFAULT_EXTERNAL
  337. long Py_TPFLAGS_DEFAULT_CORE
  338. long Py_TPFLAGS_DEFAULT