abstract.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /* Abstract Object Interface (many thanks to Jim Fulton) */
  2. #ifndef Py_ABSTRACTOBJECT_H
  3. #define Py_ABSTRACTOBJECT_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /* === Object Protocol ================================================== */
  8. /* Implemented elsewhere:
  9. int PyObject_Print(PyObject *o, FILE *fp, int flags);
  10. Print an object 'o' on file 'fp'. Returns -1 on error. The flags argument
  11. is used to enable certain printing options. The only option currently
  12. supported is Py_Print_RAW.
  13. (What should be said about Py_Print_RAW?). */
  14. /* Implemented elsewhere:
  15. int PyObject_HasAttrString(PyObject *o, const char *attr_name);
  16. Returns 1 if object 'o' has the attribute attr_name, and 0 otherwise.
  17. This is equivalent to the Python expression: hasattr(o,attr_name).
  18. This function always succeeds. */
  19. /* Implemented elsewhere:
  20. PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
  21. Retrieve an attributed named attr_name form object o.
  22. Returns the attribute value on success, or NULL on failure.
  23. This is the equivalent of the Python expression: o.attr_name. */
  24. /* Implemented elsewhere:
  25. int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
  26. Returns 1 if o has the attribute attr_name, and 0 otherwise.
  27. This is equivalent to the Python expression: hasattr(o,attr_name).
  28. This function always succeeds. */
  29. /* Implemented elsewhere:
  30. PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
  31. Retrieve an attributed named 'attr_name' form object 'o'.
  32. Returns the attribute value on success, or NULL on failure.
  33. This is the equivalent of the Python expression: o.attr_name. */
  34. /* Implemented elsewhere:
  35. int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
  36. Set the value of the attribute named attr_name, for object 'o',
  37. to the value 'v'. Raise an exception and return -1 on failure; return 0 on
  38. success.
  39. This is the equivalent of the Python statement o.attr_name=v. */
  40. /* Implemented elsewhere:
  41. int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
  42. Set the value of the attribute named attr_name, for object 'o', to the value
  43. 'v'. an exception and return -1 on failure; return 0 on success.
  44. This is the equivalent of the Python statement o.attr_name=v. */
  45. /* Implemented as a macro:
  46. int PyObject_DelAttrString(PyObject *o, const char *attr_name);
  47. Delete attribute named attr_name, for object o. Returns
  48. -1 on failure.
  49. This is the equivalent of the Python statement: del o.attr_name. */
  50. #define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A), NULL)
  51. /* Implemented as a macro:
  52. int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
  53. Delete attribute named attr_name, for object o. Returns -1
  54. on failure. This is the equivalent of the Python
  55. statement: del o.attr_name. */
  56. #define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A), NULL)
  57. /* Implemented elsewhere:
  58. PyObject *PyObject_Repr(PyObject *o);
  59. Compute the string representation of object 'o'. Returns the
  60. string representation on success, NULL on failure.
  61. This is the equivalent of the Python expression: repr(o).
  62. Called by the repr() built-in function. */
  63. /* Implemented elsewhere:
  64. PyObject *PyObject_Str(PyObject *o);
  65. Compute the string representation of object, o. Returns the
  66. string representation on success, NULL on failure.
  67. This is the equivalent of the Python expression: str(o).
  68. Called by the str() and print() built-in functions. */
  69. /* Declared elsewhere
  70. PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
  71. Determine if the object, o, is callable. Return 1 if the object is callable
  72. and 0 otherwise.
  73. This function always succeeds. */
  74. #ifdef PY_SSIZE_T_CLEAN
  75. # define PyObject_CallFunction _PyObject_CallFunction_SizeT
  76. # define PyObject_CallMethod _PyObject_CallMethod_SizeT
  77. # ifndef Py_LIMITED_API
  78. # define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
  79. # endif /* !Py_LIMITED_API */
  80. #endif
  81. /* Call a callable Python object 'callable' with arguments given by the
  82. tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
  83. 'args' must not be NULL, use an empty tuple if no arguments are
  84. needed. If no named arguments are needed, 'kwargs' can be NULL.
  85. This is the equivalent of the Python expression:
  86. callable(*args, **kwargs). */
  87. PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable,
  88. PyObject *args, PyObject *kwargs);
  89. #ifndef Py_LIMITED_API
  90. PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
  91. PyObject *const *stack,
  92. Py_ssize_t nargs);
  93. PyAPI_FUNC(PyObject*) _PyStack_AsTupleSlice(
  94. PyObject *const *stack,
  95. Py_ssize_t nargs,
  96. Py_ssize_t start,
  97. Py_ssize_t end);
  98. /* Convert keyword arguments from the FASTCALL (stack: C array, kwnames: tuple)
  99. format to a Python dictionary ("kwargs" dict).
  100. The type of kwnames keys is not checked. The final function getting
  101. arguments is responsible to check if all keys are strings, for example using
  102. PyArg_ParseTupleAndKeywords() or PyArg_ValidateKeywordArguments().
  103. Duplicate keys are merged using the last value. If duplicate keys must raise
  104. an exception, the caller is responsible to implement an explicit keys on
  105. kwnames. */
  106. PyAPI_FUNC(PyObject *) _PyStack_AsDict(
  107. PyObject *const *values,
  108. PyObject *kwnames);
  109. /* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
  110. Return 0 on success, raise an exception and return -1 on error.
  111. Write the new stack into *p_stack. If *p_stack is differen than args, it
  112. must be released by PyMem_Free().
  113. The stack uses borrowed references.
  114. The type of keyword keys is not checked, these checks should be done
  115. later (ex: _PyArg_ParseStackAndKeywords). */
  116. PyAPI_FUNC(int) _PyStack_UnpackDict(
  117. PyObject *const *args,
  118. Py_ssize_t nargs,
  119. PyObject *kwargs,
  120. PyObject *const **p_stack,
  121. PyObject **p_kwnames);
  122. /* Suggested size (number of positional arguments) for arrays of PyObject*
  123. allocated on a C stack to avoid allocating memory on the heap memory. Such
  124. array is used to pass positional arguments to call functions of the
  125. _PyObject_FastCall() family.
  126. The size is chosen to not abuse the C stack and so limit the risk of stack
  127. overflow. The size is also chosen to allow using the small stack for most
  128. function calls of the Python standard library. On 64-bit CPU, it allocates
  129. 40 bytes on the stack. */
  130. #define _PY_FASTCALL_SMALL_STACK 5
  131. /* Return 1 if callable supports FASTCALL calling convention for positional
  132. arguments: see _PyObject_FastCallDict() and _PyObject_FastCallKeywords() */
  133. PyAPI_FUNC(int) _PyObject_HasFastCall(PyObject *callable);
  134. /* Call the callable object 'callable' with the "fast call" calling convention:
  135. args is a C array for positional arguments (nargs is the number of
  136. positional arguments), kwargs is a dictionary for keyword arguments.
  137. If nargs is equal to zero, args can be NULL. kwargs can be NULL.
  138. nargs must be greater or equal to zero.
  139. Return the result on success. Raise an exception and return NULL on
  140. error. */
  141. PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(
  142. PyObject *callable,
  143. PyObject *const *args,
  144. Py_ssize_t nargs,
  145. PyObject *kwargs);
  146. /* Call the callable object 'callable' with the "fast call" calling convention:
  147. args is a C array for positional arguments followed by values of
  148. keyword arguments. Keys of keyword arguments are stored as a tuple
  149. of strings in kwnames. nargs is the number of positional parameters at
  150. the beginning of stack. The size of kwnames gives the number of keyword
  151. values in the stack after positional arguments.
  152. kwnames must only contains str strings, no subclass, and all keys must
  153. be unique.
  154. If nargs is equal to zero and there is no keyword argument (kwnames is
  155. NULL or its size is zero), args can be NULL.
  156. Return the result on success. Raise an exception and return NULL on
  157. error. */
  158. PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords(
  159. PyObject *callable,
  160. PyObject *const *args,
  161. Py_ssize_t nargs,
  162. PyObject *kwnames);
  163. #define _PyObject_FastCall(func, args, nargs) \
  164. _PyObject_FastCallDict((func), (args), (nargs), NULL)
  165. #define _PyObject_CallNoArg(func) \
  166. _PyObject_FastCallDict((func), NULL, 0, NULL)
  167. PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
  168. PyObject *callable,
  169. PyObject *obj,
  170. PyObject *args,
  171. PyObject *kwargs);
  172. PyAPI_FUNC(PyObject *) _PyObject_FastCall_Prepend(
  173. PyObject *callable,
  174. PyObject *obj,
  175. PyObject *const *args,
  176. Py_ssize_t nargs);
  177. PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *callable,
  178. PyObject *result,
  179. const char *where);
  180. #endif /* Py_LIMITED_API */
  181. /* Call a callable Python object 'callable', with arguments given by the
  182. tuple 'args'. If no arguments are needed, then 'args' can be NULL.
  183. Returns the result of the call on success, or NULL on failure.
  184. This is the equivalent of the Python expression:
  185. callable(*args). */
  186. PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable,
  187. PyObject *args);
  188. /* Call a callable Python object, callable, with a variable number of C
  189. arguments. The C arguments are described using a mkvalue-style format
  190. string.
  191. The format may be NULL, indicating that no arguments are provided.
  192. Returns the result of the call on success, or NULL on failure.
  193. This is the equivalent of the Python expression:
  194. callable(arg1, arg2, ...). */
  195. PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable,
  196. const char *format, ...);
  197. /* Call the method named 'name' of object 'obj' with a variable number of
  198. C arguments. The C arguments are described by a mkvalue format string.
  199. The format can be NULL, indicating that no arguments are provided.
  200. Returns the result of the call on success, or NULL on failure.
  201. This is the equivalent of the Python expression:
  202. obj.name(arg1, arg2, ...). */
  203. PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *obj,
  204. const char *name,
  205. const char *format, ...);
  206. #ifndef Py_LIMITED_API
  207. /* Like PyObject_CallMethod(), but expect a _Py_Identifier*
  208. as the method name. */
  209. PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
  210. _Py_Identifier *name,
  211. const char *format, ...);
  212. #endif /* !Py_LIMITED_API */
  213. PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
  214. const char *format,
  215. ...);
  216. PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *obj,
  217. const char *name,
  218. const char *format,
  219. ...);
  220. #ifndef Py_LIMITED_API
  221. PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
  222. _Py_Identifier *name,
  223. const char *format,
  224. ...);
  225. #endif /* !Py_LIMITED_API */
  226. /* Call a callable Python object 'callable' with a variable number of C
  227. arguments. The C arguments are provided as PyObject* values, terminated
  228. by a NULL.
  229. Returns the result of the call on success, or NULL on failure.
  230. This is the equivalent of the Python expression:
  231. callable(arg1, arg2, ...). */
  232. PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
  233. ...);
  234. /* Call the method named 'name' of object 'obj' with a variable number of
  235. C arguments. The C arguments are provided as PyObject* values, terminated
  236. by NULL.
  237. Returns the result of the call on success, or NULL on failure.
  238. This is the equivalent of the Python expression: obj.name(*args). */
  239. PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(
  240. PyObject *obj,
  241. PyObject *name,
  242. ...);
  243. #ifndef Py_LIMITED_API
  244. PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
  245. PyObject *obj,
  246. struct _Py_Identifier *name,
  247. ...);
  248. #endif /* !Py_LIMITED_API */
  249. /* Implemented elsewhere:
  250. Py_hash_t PyObject_Hash(PyObject *o);
  251. Compute and return the hash, hash_value, of an object, o. On
  252. failure, return -1.
  253. This is the equivalent of the Python expression: hash(o). */
  254. /* Implemented elsewhere:
  255. int PyObject_IsTrue(PyObject *o);
  256. Returns 1 if the object, o, is considered to be true, 0 if o is
  257. considered to be false and -1 on failure.
  258. This is equivalent to the Python expression: not not o. */
  259. /* Implemented elsewhere:
  260. int PyObject_Not(PyObject *o);
  261. Returns 0 if the object, o, is considered to be true, 1 if o is
  262. considered to be false and -1 on failure.
  263. This is equivalent to the Python expression: not o. */
  264. /* Get the type of an object.
  265. On success, returns a type object corresponding to the object type of object
  266. 'o'. On failure, returns NULL.
  267. This is equivalent to the Python expression: type(o) */
  268. PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
  269. /* Return the size of object 'o'. If the object 'o' provides both sequence and
  270. mapping protocols, the sequence size is returned.
  271. On error, -1 is returned.
  272. This is the equivalent to the Python expression: len(o) */
  273. PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
  274. /* For DLL compatibility */
  275. #undef PyObject_Length
  276. PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
  277. #define PyObject_Length PyObject_Size
  278. #ifndef Py_LIMITED_API
  279. PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
  280. /* Guess the size of object 'o' using len(o) or o.__length_hint__().
  281. If neither of those return a non-negative value, then return the default
  282. value. If one of the calls fails, this function returns -1. */
  283. PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
  284. #endif
  285. /* Return element of 'o' corresponding to the object 'key'. Return NULL
  286. on failure.
  287. This is the equivalent of the Python expression: o[key] */
  288. PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
  289. /* Map the object 'key' to the value 'v' into 'o'.
  290. Raise an exception and return -1 on failure; return 0 on success.
  291. This is the equivalent of the Python statement: o[key]=v. */
  292. PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
  293. /* Remove the mapping for the string 'key' from the object 'o'.
  294. Returns -1 on failure.
  295. This is equivalent to the Python statement: del o[key]. */
  296. PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
  297. /* Delete the mapping for the object 'key' from the object 'o'.
  298. Returns -1 on failure.
  299. This is the equivalent of the Python statement: del o[key]. */
  300. PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
  301. /* === Old Buffer API ============================================ */
  302. /* FIXME: usage of these should all be replaced in Python itself
  303. but for backwards compatibility we will implement them.
  304. Their usage without a corresponding "unlock" mechanism
  305. may create issues (but they would already be there). */
  306. /* Takes an arbitrary object which must support the (character, single segment)
  307. buffer interface and returns a pointer to a read-only memory location
  308. useable as character based input for subsequent processing.
  309. Return 0 on success. buffer and buffer_len are only set in case no error
  310. occurs. Otherwise, -1 is returned and an exception set. */
  311. PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
  312. const char **buffer,
  313. Py_ssize_t *buffer_len)
  314. Py_DEPRECATED(3.0);
  315. /* Checks whether an arbitrary object supports the (character, single segment)
  316. buffer interface.
  317. Returns 1 on success, 0 on failure. */
  318. PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj)
  319. Py_DEPRECATED(3.0);
  320. /* Same as PyObject_AsCharBuffer() except that this API expects (readable,
  321. single segment) buffer interface and returns a pointer to a read-only memory
  322. location which can contain arbitrary data.
  323. 0 is returned on success. buffer and buffer_len are only set in case no
  324. error occurs. Otherwise, -1 is returned and an exception set. */
  325. PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj,
  326. const void **buffer,
  327. Py_ssize_t *buffer_len)
  328. Py_DEPRECATED(3.0);
  329. /* Takes an arbitrary object which must support the (writable, single segment)
  330. buffer interface and returns a pointer to a writable memory location in
  331. buffer of size 'buffer_len'.
  332. Return 0 on success. buffer and buffer_len are only set in case no error
  333. occurs. Otherwise, -1 is returned and an exception set. */
  334. PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj,
  335. void **buffer,
  336. Py_ssize_t *buffer_len)
  337. Py_DEPRECATED(3.0);
  338. /* === New Buffer API ============================================ */
  339. #ifndef Py_LIMITED_API
  340. /* Return 1 if the getbuffer function is available, otherwise return 0. */
  341. #define PyObject_CheckBuffer(obj) \
  342. (((obj)->ob_type->tp_as_buffer != NULL) && \
  343. ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
  344. /* This is a C-API version of the getbuffer function call. It checks
  345. to make sure object has the required function pointer and issues the
  346. call.
  347. Returns -1 and raises an error on failure and returns 0 on success. */
  348. PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
  349. int flags);
  350. /* Get the memory area pointed to by the indices for the buffer given.
  351. Note that view->ndim is the assumed size of indices. */
  352. PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
  353. /* Return the implied itemsize of the data-format area from a
  354. struct-style description. */
  355. PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
  356. /* Implementation in memoryobject.c */
  357. PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
  358. Py_ssize_t len, char order);
  359. PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
  360. Py_ssize_t len, char order);
  361. /* Copy len bytes of data from the contiguous chunk of memory
  362. pointed to by buf into the buffer exported by obj. Return
  363. 0 on success and return -1 and raise a PyBuffer_Error on
  364. error (i.e. the object does not have a buffer interface or
  365. it is not working).
  366. If fort is 'F', then if the object is multi-dimensional,
  367. then the data will be copied into the array in
  368. Fortran-style (first dimension varies the fastest). If
  369. fort is 'C', then the data will be copied into the array
  370. in C-style (last dimension varies the fastest). If fort
  371. is 'A', then it does not matter and the copy will be made
  372. in whatever way is more efficient. */
  373. PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
  374. /* Copy the data from the src buffer to the buffer of destination. */
  375. PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
  376. /*Fill the strides array with byte-strides of a contiguous
  377. (Fortran-style if fort is 'F' or C-style otherwise)
  378. array of the given shape with the given number of bytes
  379. per element. */
  380. PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
  381. Py_ssize_t *shape,
  382. Py_ssize_t *strides,
  383. int itemsize,
  384. char fort);
  385. /* Fills in a buffer-info structure correctly for an exporter
  386. that can only share a contiguous chunk of memory of
  387. "unsigned bytes" of the given length.
  388. Returns 0 on success and -1 (with raising an error) on error. */
  389. PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
  390. Py_ssize_t len, int readonly,
  391. int flags);
  392. /* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
  393. PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
  394. #endif /* Py_LIMITED_API */
  395. /* Takes an arbitrary object and returns the result of calling
  396. obj.__format__(format_spec). */
  397. PyAPI_FUNC(PyObject *) PyObject_Format(PyObject *obj,
  398. PyObject *format_spec);
  399. /* ==== Iterators ================================================ */
  400. /* Takes an object and returns an iterator for it.
  401. This is typically a new iterator but if the argument is an iterator, this
  402. returns itself. */
  403. PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
  404. #define PyIter_Check(obj) \
  405. ((obj)->ob_type->tp_iternext != NULL && \
  406. (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
  407. /* Takes an iterator object and calls its tp_iternext slot,
  408. returning the next value.
  409. If the iterator is exhausted, this returns NULL without setting an
  410. exception.
  411. NULL with an exception means an error occurred. */
  412. PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
  413. /* === Number Protocol ================================================== */
  414. /* Returns 1 if the object 'o' provides numeric protocols, and 0 otherwise.
  415. This function always succeeds. */
  416. PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
  417. /* Returns the result of adding o1 and o2, or NULL on failure.
  418. This is the equivalent of the Python expression: o1 + o2. */
  419. PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
  420. /* Returns the result of subtracting o2 from o1, or NULL on failure.
  421. This is the equivalent of the Python expression: o1 - o2. */
  422. PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
  423. /* Returns the result of multiplying o1 and o2, or NULL on failure.
  424. This is the equivalent of the Python expression: o1 * o2. */
  425. PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
  426. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  427. /* This is the equivalent of the Python expression: o1 @ o2. */
  428. PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
  429. #endif
  430. /* Returns the result of dividing o1 by o2 giving an integral result,
  431. or NULL on failure.
  432. This is the equivalent of the Python expression: o1 // o2. */
  433. PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
  434. /* Returns the result of dividing o1 by o2 giving a float result, or NULL on
  435. failure.
  436. This is the equivalent of the Python expression: o1 / o2. */
  437. PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
  438. /* Returns the remainder of dividing o1 by o2, or NULL on failure.
  439. This is the equivalent of the Python expression: o1 % o2. */
  440. PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
  441. /* See the built-in function divmod.
  442. Returns NULL on failure.
  443. This is the equivalent of the Python expression: divmod(o1, o2). */
  444. PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
  445. /* See the built-in function pow. Returns NULL on failure.
  446. This is the equivalent of the Python expression: pow(o1, o2, o3),
  447. where o3 is optional. */
  448. PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
  449. PyObject *o3);
  450. /* Returns the negation of o on success, or NULL on failure.
  451. This is the equivalent of the Python expression: -o. */
  452. PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
  453. /* Returns the positive of o on success, or NULL on failure.
  454. This is the equivalent of the Python expression: +o. */
  455. PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
  456. /* Returns the absolute value of 'o', or NULL on failure.
  457. This is the equivalent of the Python expression: abs(o). */
  458. PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
  459. /* Returns the bitwise negation of 'o' on success, or NULL on failure.
  460. This is the equivalent of the Python expression: ~o. */
  461. PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
  462. /* Returns the result of left shifting o1 by o2 on success, or NULL on failure.
  463. This is the equivalent of the Python expression: o1 << o2. */
  464. PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
  465. /* Returns the result of right shifting o1 by o2 on success, or NULL on
  466. failure.
  467. This is the equivalent of the Python expression: o1 >> o2. */
  468. PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
  469. /* Returns the result of bitwise and of o1 and o2 on success, or NULL on
  470. failure.
  471. This is the equivalent of the Python expression: o1 & o2. */
  472. PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
  473. /* Returns the bitwise exclusive or of o1 by o2 on success, or NULL on failure.
  474. This is the equivalent of the Python expression: o1 ^ o2. */
  475. PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
  476. /* Returns the result of bitwise or on o1 and o2 on success, or NULL on
  477. failure.
  478. This is the equivalent of the Python expression: o1 | o2. */
  479. PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
  480. #define PyIndex_Check(obj) \
  481. ((obj)->ob_type->tp_as_number != NULL && \
  482. (obj)->ob_type->tp_as_number->nb_index != NULL)
  483. /* Returns the object 'o' converted to a Python int, or NULL with an exception
  484. raised on failure. */
  485. PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
  486. /* Returns the object 'o' converted to Py_ssize_t by going through
  487. PyNumber_Index() first.
  488. If an overflow error occurs while converting the int to Py_ssize_t, then the
  489. second argument 'exc' is the error-type to return. If it is NULL, then the
  490. overflow error is cleared and the value is clipped. */
  491. PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
  492. /* Returns the object 'o' converted to an integer object on success, or NULL
  493. on failure.
  494. This is the equivalent of the Python expression: int(o). */
  495. PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
  496. /* Returns the object 'o' converted to a float object on success, or NULL
  497. on failure.
  498. This is the equivalent of the Python expression: float(o). */
  499. PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
  500. /* --- In-place variants of (some of) the above number protocol functions -- */
  501. /* Returns the result of adding o2 to o1, possibly in-place, or NULL
  502. on failure.
  503. This is the equivalent of the Python expression: o1 += o2. */
  504. PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
  505. /* Returns the result of subtracting o2 from o1, possibly in-place or
  506. NULL on failure.
  507. This is the equivalent of the Python expression: o1 -= o2. */
  508. PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
  509. /* Returns the result of multiplying o1 by o2, possibly in-place, or NULL on
  510. failure.
  511. This is the equivalent of the Python expression: o1 *= o2. */
  512. PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
  513. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  514. /* This is the equivalent of the Python expression: o1 @= o2. */
  515. PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
  516. #endif
  517. /* Returns the result of dividing o1 by o2 giving an integral result, possibly
  518. in-place, or NULL on failure.
  519. This is the equivalent of the Python expression: o1 /= o2. */
  520. PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
  521. PyObject *o2);
  522. /* Returns the result of dividing o1 by o2 giving a float result, possibly
  523. in-place, or null on failure.
  524. This is the equivalent of the Python expression: o1 /= o2. */
  525. PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
  526. PyObject *o2);
  527. /* Returns the remainder of dividing o1 by o2, possibly in-place, or NULL on
  528. failure.
  529. This is the equivalent of the Python expression: o1 %= o2. */
  530. PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
  531. /* Returns the result of raising o1 to the power of o2, possibly in-place,
  532. or NULL on failure.
  533. This is the equivalent of the Python expression: o1 **= o2,
  534. or o1 = pow(o1, o2, o3) if o3 is present. */
  535. PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
  536. PyObject *o3);
  537. /* Returns the result of left shifting o1 by o2, possibly in-place, or NULL
  538. on failure.
  539. This is the equivalent of the Python expression: o1 <<= o2. */
  540. PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
  541. /* Returns the result of right shifting o1 by o2, possibly in-place or NULL
  542. on failure.
  543. This is the equivalent of the Python expression: o1 >>= o2. */
  544. PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
  545. /* Returns the result of bitwise and of o1 and o2, possibly in-place, or NULL
  546. on failure.
  547. This is the equivalent of the Python expression: o1 &= o2. */
  548. PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
  549. /* Returns the bitwise exclusive or of o1 by o2, possibly in-place, or NULL
  550. on failure.
  551. This is the equivalent of the Python expression: o1 ^= o2. */
  552. PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
  553. /* Returns the result of bitwise or of o1 and o2, possibly in-place,
  554. or NULL on failure.
  555. This is the equivalent of the Python expression: o1 |= o2. */
  556. PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
  557. /* Returns the integer n converted to a string with a base, with a base
  558. marker of 0b, 0o or 0x prefixed if applicable.
  559. If n is not an int object, it is converted with PyNumber_Index first. */
  560. PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
  561. /* === Sequence protocol ================================================ */
  562. /* Return 1 if the object provides sequence protocol, and zero
  563. otherwise.
  564. This function always succeeds. */
  565. PyAPI_FUNC(int) PySequence_Check(PyObject *o);
  566. /* Return the size of sequence object o, or -1 on failure. */
  567. PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
  568. /* For DLL compatibility */
  569. #undef PySequence_Length
  570. PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o);
  571. #define PySequence_Length PySequence_Size
  572. /* Return the concatenation of o1 and o2 on success, and NULL on failure.
  573. This is the equivalent of the Python expression: o1 + o2. */
  574. PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
  575. /* Return the result of repeating sequence object 'o' 'count' times,
  576. or NULL on failure.
  577. This is the equivalent of the Python expression: o * count. */
  578. PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
  579. /* Return the ith element of o, or NULL on failure.
  580. This is the equivalent of the Python expression: o[i]. */
  581. PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
  582. /* Return the slice of sequence object o between i1 and i2, or NULL on failure.
  583. This is the equivalent of the Python expression: o[i1:i2]. */
  584. PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
  585. /* Assign object 'v' to the ith element of the sequence 'o'. Raise an exception
  586. and return -1 on failure; return 0 on success.
  587. This is the equivalent of the Python statement o[i] = v. */
  588. PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
  589. /* Delete the 'i'-th element of the sequence 'v'. Returns -1 on failure.
  590. This is the equivalent of the Python statement: del o[i]. */
  591. PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
  592. /* Assign the sequence object 'v' to the slice in sequence object 'o',
  593. from 'i1' to 'i2'. Returns -1 on failure.
  594. This is the equivalent of the Python statement: o[i1:i2] = v. */
  595. PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
  596. PyObject *v);
  597. /* Delete the slice in sequence object 'o' from 'i1' to 'i2'.
  598. Returns -1 on failure.
  599. This is the equivalent of the Python statement: del o[i1:i2]. */
  600. PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
  601. /* Returns the sequence 'o' as a tuple on success, and NULL on failure.
  602. This is equivalent to the Python expression: tuple(o). */
  603. PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
  604. /* Returns the sequence 'o' as a list on success, and NULL on failure.
  605. This is equivalent to the Python expression: list(o) */
  606. PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
  607. /* Return the sequence 'o' as a list, unless it's already a tuple or list.
  608. Use PySequence_Fast_GET_ITEM to access the members of this list, and
  609. PySequence_Fast_GET_SIZE to get its length.
  610. Returns NULL on failure. If the object does not support iteration, raises a
  611. TypeError exception with 'm' as the message text. */
  612. PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
  613. /* Return the size of the sequence 'o', assuming that 'o' was returned by
  614. PySequence_Fast and is not NULL. */
  615. #define PySequence_Fast_GET_SIZE(o) \
  616. (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
  617. /* Return the 'i'-th element of the sequence 'o', assuming that o was returned
  618. by PySequence_Fast, and that i is within bounds. */
  619. #define PySequence_Fast_GET_ITEM(o, i)\
  620. (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
  621. /* Assume tp_as_sequence and sq_item exist and that 'i' does not
  622. need to be corrected for a negative index. */
  623. #define PySequence_ITEM(o, i)\
  624. ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
  625. /* Return a pointer to the underlying item array for
  626. an object retured by PySequence_Fast */
  627. #define PySequence_Fast_ITEMS(sf) \
  628. (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
  629. : ((PyTupleObject *)(sf))->ob_item)
  630. /* Return the number of occurrences on value on 'o', that is, return
  631. the number of keys for which o[key] == value.
  632. On failure, return -1. This is equivalent to the Python expression:
  633. o.count(value). */
  634. PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
  635. /* Return 1 if 'ob' is in the sequence 'seq'; 0 if 'ob' is not in the sequence
  636. 'seq'; -1 on error.
  637. Use __contains__ if possible, else _PySequence_IterSearch(). */
  638. PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
  639. #ifndef Py_LIMITED_API
  640. #define PY_ITERSEARCH_COUNT 1
  641. #define PY_ITERSEARCH_INDEX 2
  642. #define PY_ITERSEARCH_CONTAINS 3
  643. /* Iterate over seq.
  644. Result depends on the operation:
  645. PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
  646. error.
  647. PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
  648. obj in seq; set ValueError and return -1 if none found;
  649. also return -1 on error.
  650. PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
  651. error. */
  652. PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
  653. PyObject *obj, int operation);
  654. #endif
  655. /* For DLL-level backwards compatibility */
  656. #undef PySequence_In
  657. /* Determine if the sequence 'o' contains 'value'. If an item in 'o' is equal
  658. to 'value', return 1, otherwise return 0. On error, return -1.
  659. This is equivalent to the Python expression: value in o. */
  660. PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value);
  661. /* For source-level backwards compatibility */
  662. #define PySequence_In PySequence_Contains
  663. /* Return the first index for which o[i] == value.
  664. On error, return -1.
  665. This is equivalent to the Python expression: o.index(value). */
  666. PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
  667. /* --- In-place versions of some of the above Sequence functions --- */
  668. /* Append sequence 'o2' to sequence 'o1', in-place when possible. Return the
  669. resulting object, which could be 'o1', or NULL on failure.
  670. This is the equivalent of the Python expression: o1 += o2. */
  671. PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
  672. /* Repeat sequence 'o' by 'count', in-place when possible. Return the resulting
  673. object, which could be 'o', or NULL on failure.
  674. This is the equivalent of the Python expression: o1 *= count. */
  675. PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
  676. /* === Mapping protocol ================================================= */
  677. /* Return 1 if the object provides mapping protocol, and 0 otherwise.
  678. This function always succeeds. */
  679. PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
  680. /* Returns the number of keys in mapping object 'o' on success, and -1 on
  681. failure. This is equivalent to the Python expression: len(o). */
  682. PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
  683. /* For DLL compatibility */
  684. #undef PyMapping_Length
  685. PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
  686. #define PyMapping_Length PyMapping_Size
  687. /* Implemented as a macro:
  688. int PyMapping_DelItemString(PyObject *o, const char *key);
  689. Remove the mapping for the string 'key' from the mapping 'o'. Returns -1 on
  690. failure.
  691. This is equivalent to the Python statement: del o[key]. */
  692. #define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
  693. /* Implemented as a macro:
  694. int PyMapping_DelItem(PyObject *o, PyObject *key);
  695. Remove the mapping for the object 'key' from the mapping object 'o'.
  696. Returns -1 on failure.
  697. This is equivalent to the Python statement: del o[key]. */
  698. #define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
  699. /* On success, return 1 if the mapping object 'o' has the key 'key',
  700. and 0 otherwise.
  701. This is equivalent to the Python expression: key in o.
  702. This function always succeeds. */
  703. PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key);
  704. /* Return 1 if the mapping object has the key 'key', and 0 otherwise.
  705. This is equivalent to the Python expression: key in o.
  706. This function always succeeds. */
  707. PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
  708. /* On success, return a list or tuple of the keys in mapping object 'o'.
  709. On failure, return NULL. */
  710. PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
  711. /* On success, return a list or tuple of the values in mapping object 'o'.
  712. On failure, return NULL. */
  713. PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
  714. /* On success, return a list or tuple of the items in mapping object 'o',
  715. where each item is a tuple containing a key-value pair. On failure, return
  716. NULL. */
  717. PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
  718. /* Return element of 'o' corresponding to the string 'key' or NULL on failure.
  719. This is the equivalent of the Python expression: o[key]. */
  720. PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
  721. const char *key);
  722. /* Map the string 'key' to the value 'v' in the mapping 'o'.
  723. Returns -1 on failure.
  724. This is the equivalent of the Python statement: o[key]=v. */
  725. PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
  726. PyObject *value);
  727. /* isinstance(object, typeorclass) */
  728. PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
  729. /* issubclass(object, typeorclass) */
  730. PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
  731. #ifndef Py_LIMITED_API
  732. PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
  733. PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
  734. PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
  735. PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
  736. /* For internal use by buffer API functions */
  737. PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
  738. const Py_ssize_t *shape);
  739. PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
  740. const Py_ssize_t *shape);
  741. /* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
  742. PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
  743. #endif /* !Py_LIMITED_API */
  744. #ifdef __cplusplus
  745. }
  746. #endif
  747. #endif /* Py_ABSTRACTOBJECT_H */