abstract.h 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. #ifndef Py_ABSTRACTOBJECT_H
  2. #define Py_ABSTRACTOBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifdef PY_SSIZE_T_CLEAN
  7. #define PyObject_CallFunction _PyObject_CallFunction_SizeT
  8. #define PyObject_CallMethod _PyObject_CallMethod_SizeT
  9. #define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
  10. #endif
  11. /* Abstract Object Interface (many thanks to Jim Fulton) */
  12. /*
  13. PROPOSAL: A Generic Python Object Interface for Python C Modules
  14. Problem
  15. Python modules written in C that must access Python objects must do
  16. so through routines whose interfaces are described by a set of
  17. include files. Unfortunately, these routines vary according to the
  18. object accessed. To use these routines, the C programmer must check
  19. the type of the object being used and must call a routine based on
  20. the object type. For example, to access an element of a sequence,
  21. the programmer must determine whether the sequence is a list or a
  22. tuple:
  23. if(is_tupleobject(o))
  24. e=gettupleitem(o,i)
  25. else if(is_listitem(o))
  26. e=getlistitem(o,i)
  27. If the programmer wants to get an item from another type of object
  28. that provides sequence behavior, there is no clear way to do it
  29. correctly.
  30. The persistent programmer may peruse object.h and find that the
  31. _typeobject structure provides a means of invoking up to (currently
  32. about) 41 special operators. So, for example, a routine can get an
  33. item from any object that provides sequence behavior. However, to
  34. use this mechanism, the programmer must make their code dependent on
  35. the current Python implementation.
  36. Also, certain semantics, especially memory management semantics, may
  37. differ by the type of object being used. Unfortunately, these
  38. semantics are not clearly described in the current include files.
  39. An abstract interface providing more consistent semantics is needed.
  40. Proposal
  41. I propose the creation of a standard interface (with an associated
  42. library of routines and/or macros) for generically obtaining the
  43. services of Python objects. This proposal can be viewed as one
  44. components of a Python C interface consisting of several components.
  45. From the viewpoint of C access to Python services, we have (as
  46. suggested by Guido in off-line discussions):
  47. - "Very high level layer": two or three functions that let you exec or
  48. eval arbitrary Python code given as a string in a module whose name is
  49. given, passing C values in and getting C values out using
  50. mkvalue/getargs style format strings. This does not require the user
  51. to declare any variables of type "PyObject *". This should be enough
  52. to write a simple application that gets Python code from the user,
  53. execs it, and returns the output or errors. (Error handling must also
  54. be part of this API.)
  55. - "Abstract objects layer": which is the subject of this proposal.
  56. It has many functions operating on objects, and lest you do many
  57. things from C that you can also write in Python, without going
  58. through the Python parser.
  59. - "Concrete objects layer": This is the public type-dependent
  60. interface provided by the standard built-in types, such as floats,
  61. strings, and lists. This interface exists and is currently
  62. documented by the collection of include files provided with the
  63. Python distributions.
  64. From the point of view of Python accessing services provided by C
  65. modules:
  66. - "Python module interface": this interface consist of the basic
  67. routines used to define modules and their members. Most of the
  68. current extensions-writing guide deals with this interface.
  69. - "Built-in object interface": this is the interface that a new
  70. built-in type must provide and the mechanisms and rules that a
  71. developer of a new built-in type must use and follow.
  72. This proposal is a "first-cut" that is intended to spur
  73. discussion. See especially the lists of notes.
  74. The Python C object interface will provide four protocols: object,
  75. numeric, sequence, and mapping. Each protocol consists of a
  76. collection of related operations. If an operation that is not
  77. provided by a particular type is invoked, then a standard exception,
  78. NotImplementedError is raised with an operation name as an argument.
  79. In addition, for convenience this interface defines a set of
  80. constructors for building objects of built-in types. This is needed
  81. so new objects can be returned from C functions that otherwise treat
  82. objects generically.
  83. Memory Management
  84. For all of the functions described in this proposal, if a function
  85. retains a reference to a Python object passed as an argument, then the
  86. function will increase the reference count of the object. It is
  87. unnecessary for the caller to increase the reference count of an
  88. argument in anticipation of the object's retention.
  89. All Python objects returned from functions should be treated as new
  90. objects. Functions that return objects assume that the caller will
  91. retain a reference and the reference count of the object has already
  92. been incremented to account for this fact. A caller that does not
  93. retain a reference to an object that is returned from a function
  94. must decrement the reference count of the object (using
  95. DECREF(object)) to prevent memory leaks.
  96. Note that the behavior mentioned here is different from the current
  97. behavior for some objects (e.g. lists and tuples) when certain
  98. type-specific routines are called directly (e.g. setlistitem). The
  99. proposed abstraction layer will provide a consistent memory
  100. management interface, correcting for inconsistent behavior for some
  101. built-in types.
  102. Protocols
  103. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
  104. /* Object Protocol: */
  105. /* Implemented elsewhere:
  106. int PyObject_Print(PyObject *o, FILE *fp, int flags);
  107. Print an object, o, on file, fp. Returns -1 on
  108. error. The flags argument is used to enable certain printing
  109. options. The only option currently supported is Py_Print_RAW.
  110. (What should be said about Py_Print_RAW?)
  111. */
  112. /* Implemented elsewhere:
  113. int PyObject_HasAttrString(PyObject *o, const char *attr_name);
  114. Returns 1 if o has the attribute attr_name, and 0 otherwise.
  115. This is equivalent to the Python expression:
  116. hasattr(o,attr_name).
  117. This function always succeeds.
  118. */
  119. /* Implemented elsewhere:
  120. PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
  121. Retrieve an attributed named attr_name form object o.
  122. Returns the attribute value on success, or NULL on failure.
  123. This is the equivalent of the Python expression: o.attr_name.
  124. */
  125. /* Implemented elsewhere:
  126. int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
  127. Returns 1 if o has the attribute attr_name, and 0 otherwise.
  128. This is equivalent to the Python expression:
  129. hasattr(o,attr_name).
  130. This function always succeeds.
  131. */
  132. /* Implemented elsewhere:
  133. PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
  134. Retrieve an attributed named attr_name form object o.
  135. Returns the attribute value on success, or NULL on failure.
  136. This is the equivalent of the Python expression: o.attr_name.
  137. */
  138. /* Implemented elsewhere:
  139. int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
  140. Set the value of the attribute named attr_name, for object o,
  141. to the value v. Raise an exception and return -1 on failure; return 0 on
  142. success. This is the equivalent of the Python statement o.attr_name=v.
  143. */
  144. /* Implemented elsewhere:
  145. int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
  146. Set the value of the attribute named attr_name, for object o,
  147. to the value v. Raise an exception and return -1 on failure; return 0 on
  148. success. This is the equivalent of the Python statement o.attr_name=v.
  149. */
  150. /* implemented as a macro:
  151. int PyObject_DelAttrString(PyObject *o, const char *attr_name);
  152. Delete attribute named attr_name, for object o. Returns
  153. -1 on failure. This is the equivalent of the Python
  154. statement: del o.attr_name.
  155. */
  156. #define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
  157. /* implemented as a macro:
  158. int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
  159. Delete attribute named attr_name, for object o. Returns -1
  160. on failure. This is the equivalent of the Python
  161. statement: del o.attr_name.
  162. */
  163. #define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
  164. /* Implemented elsewhere:
  165. PyObject *PyObject_Repr(PyObject *o);
  166. Compute the string representation of object, o. Returns the
  167. string representation on success, NULL on failure. This is
  168. the equivalent of the Python expression: repr(o).
  169. Called by the repr() built-in function.
  170. */
  171. /* Implemented elsewhere:
  172. PyObject *PyObject_Str(PyObject *o);
  173. Compute the string representation of object, o. Returns the
  174. string representation on success, NULL on failure. This is
  175. the equivalent of the Python expression: str(o).)
  176. Called by the str() and print() built-in functions.
  177. */
  178. /* Declared elsewhere
  179. PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
  180. Determine if the object, o, is callable. Return 1 if the
  181. object is callable and 0 otherwise.
  182. This function always succeeds.
  183. */
  184. PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object,
  185. PyObject *args, PyObject *kw);
  186. #ifndef Py_LIMITED_API
  187. PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *func,
  188. PyObject *result,
  189. const char *where);
  190. #endif
  191. /*
  192. Call a callable Python object, callable_object, with
  193. arguments and keywords arguments. The 'args' argument can not be
  194. NULL, but the 'kw' argument can be NULL.
  195. */
  196. PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable_object,
  197. PyObject *args);
  198. /*
  199. Call a callable Python object, callable_object, with
  200. arguments given by the tuple, args. If no arguments are
  201. needed, then args may be NULL. Returns the result of the
  202. call on success, or NULL on failure. This is the equivalent
  203. of the Python expression: o(*args).
  204. */
  205. PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable_object,
  206. const char *format, ...);
  207. /*
  208. Call a callable Python object, callable_object, with a
  209. variable number of C arguments. The C arguments are described
  210. using a mkvalue-style format string. The format may be NULL,
  211. indicating that no arguments are provided. Returns the
  212. result of the call on success, or NULL on failure. This is
  213. the equivalent of the Python expression: o(*args).
  214. */
  215. PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *o,
  216. const char *method,
  217. const char *format, ...);
  218. /*
  219. Call the method named m of object o with a variable number of
  220. C arguments. The C arguments are described by a mkvalue
  221. format string. The format may be NULL, indicating that no
  222. arguments are provided. Returns the result of the call on
  223. success, or NULL on failure. This is the equivalent of the
  224. Python expression: o.method(args).
  225. */
  226. PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *o,
  227. _Py_Identifier *method,
  228. const char *format, ...);
  229. /*
  230. Like PyObject_CallMethod, but expect a _Py_Identifier* as the
  231. method name.
  232. */
  233. PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
  234. const char *format,
  235. ...);
  236. PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *o,
  237. const char *name,
  238. const char *format,
  239. ...);
  240. PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *o,
  241. _Py_Identifier *name,
  242. const char *format,
  243. ...);
  244. PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
  245. ...);
  246. /*
  247. Call a callable Python object, callable_object, with a
  248. variable number of C arguments. The C arguments are provided
  249. as PyObject * values, terminated by a NULL. Returns the
  250. result of the call on success, or NULL on failure. This is
  251. the equivalent of the Python expression: o(*args).
  252. */
  253. PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(PyObject *o,
  254. PyObject *method, ...);
  255. PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(PyObject *o,
  256. struct _Py_Identifier *method,
  257. ...);
  258. /*
  259. Call the method named m of object o with a variable number of
  260. C arguments. The C arguments are provided as PyObject *
  261. values, terminated by NULL. Returns the result of the call
  262. on success, or NULL on failure. This is the equivalent of
  263. the Python expression: o.method(args).
  264. */
  265. /* Implemented elsewhere:
  266. long PyObject_Hash(PyObject *o);
  267. Compute and return the hash, hash_value, of an object, o. On
  268. failure, return -1. This is the equivalent of the Python
  269. expression: hash(o).
  270. */
  271. /* Implemented elsewhere:
  272. int PyObject_IsTrue(PyObject *o);
  273. Returns 1 if the object, o, is considered to be true, 0 if o is
  274. considered to be false and -1 on failure. This is equivalent to the
  275. Python expression: not not o
  276. */
  277. /* Implemented elsewhere:
  278. int PyObject_Not(PyObject *o);
  279. Returns 0 if the object, o, is considered to be true, 1 if o is
  280. considered to be false and -1 on failure. This is equivalent to the
  281. Python expression: not o
  282. */
  283. PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
  284. /*
  285. On success, returns a type object corresponding to the object
  286. type of object o. On failure, returns NULL. This is
  287. equivalent to the Python expression: type(o).
  288. */
  289. PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
  290. /*
  291. Return the size of object o. If the object, o, provides
  292. both sequence and mapping protocols, the sequence size is
  293. returned. On error, -1 is returned. This is the equivalent
  294. to the Python expression: len(o).
  295. */
  296. /* For DLL compatibility */
  297. #undef PyObject_Length
  298. PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
  299. #define PyObject_Length PyObject_Size
  300. #ifndef Py_LIMITED_API
  301. PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
  302. PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
  303. #endif
  304. /*
  305. Guess the size of object o using len(o) or o.__length_hint__().
  306. If neither of those return a non-negative value, then return the
  307. default value. If one of the calls fails, this function returns -1.
  308. */
  309. PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
  310. /*
  311. Return element of o corresponding to the object, key, or NULL
  312. on failure. This is the equivalent of the Python expression:
  313. o[key].
  314. */
  315. PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
  316. /*
  317. Map the object key to the value v. Raise an exception and return -1
  318. on failure; return 0 on success. This is the equivalent of the Python
  319. statement o[key]=v.
  320. */
  321. PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
  322. /*
  323. Remove the mapping for object, key, from the object *o.
  324. Returns -1 on failure. This is equivalent to
  325. the Python statement: del o[key].
  326. */
  327. PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
  328. /*
  329. Delete the mapping for key from *o. Returns -1 on failure.
  330. This is the equivalent of the Python statement: del o[key].
  331. */
  332. /* old buffer API
  333. FIXME: usage of these should all be replaced in Python itself
  334. but for backwards compatibility we will implement them.
  335. Their usage without a corresponding "unlock" mechansim
  336. may create issues (but they would already be there). */
  337. PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
  338. const char **buffer,
  339. Py_ssize_t *buffer_len);
  340. /*
  341. Takes an arbitrary object which must support the (character,
  342. single segment) buffer interface and returns a pointer to a
  343. read-only memory location useable as character based input
  344. for subsequent processing.
  345. 0 is returned on success. buffer and buffer_len are only
  346. set in case no error occurs. Otherwise, -1 is returned and
  347. an exception set.
  348. */
  349. PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj);
  350. /*
  351. Checks whether an arbitrary object supports the (character,
  352. single segment) buffer interface. Returns 1 on success, 0
  353. on failure.
  354. */
  355. PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj,
  356. const void **buffer,
  357. Py_ssize_t *buffer_len);
  358. /*
  359. Same as PyObject_AsCharBuffer() except that this API expects
  360. (readable, single segment) buffer interface and returns a
  361. pointer to a read-only memory location which can contain
  362. arbitrary data.
  363. 0 is returned on success. buffer and buffer_len are only
  364. set in case no error occurs. Otherwise, -1 is returned and
  365. an exception set.
  366. */
  367. PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj,
  368. void **buffer,
  369. Py_ssize_t *buffer_len);
  370. /*
  371. Takes an arbitrary object which must support the (writable,
  372. single segment) buffer interface and returns a pointer to a
  373. writable memory location in buffer of size buffer_len.
  374. 0 is returned on success. buffer and buffer_len are only
  375. set in case no error occurs. Otherwise, -1 is returned and
  376. an exception set.
  377. */
  378. /* new buffer API */
  379. #ifndef Py_LIMITED_API
  380. #define PyObject_CheckBuffer(obj) \
  381. (((obj)->ob_type->tp_as_buffer != NULL) && \
  382. ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
  383. /* Return 1 if the getbuffer function is available, otherwise
  384. return 0 */
  385. PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
  386. int flags);
  387. /* This is a C-API version of the getbuffer function call. It checks
  388. to make sure object has the required function pointer and issues the
  389. call. Returns -1 and raises an error on failure and returns 0 on
  390. success
  391. */
  392. PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
  393. /* Get the memory area pointed to by the indices for the buffer given.
  394. Note that view->ndim is the assumed size of indices
  395. */
  396. PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
  397. /* Return the implied itemsize of the data-format area from a
  398. struct-style description */
  399. /* Implementation in memoryobject.c */
  400. PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
  401. Py_ssize_t len, char order);
  402. PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
  403. Py_ssize_t len, char order);
  404. /* Copy len bytes of data from the contiguous chunk of memory
  405. pointed to by buf into the buffer exported by obj. Return
  406. 0 on success and return -1 and raise a PyBuffer_Error on
  407. error (i.e. the object does not have a buffer interface or
  408. it is not working).
  409. If fort is 'F', then if the object is multi-dimensional,
  410. then the data will be copied into the array in
  411. Fortran-style (first dimension varies the fastest). If
  412. fort is 'C', then the data will be copied into the array
  413. in C-style (last dimension varies the fastest). If fort
  414. is 'A', then it does not matter and the copy will be made
  415. in whatever way is more efficient.
  416. */
  417. PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
  418. /* Copy the data from the src buffer to the buffer of destination
  419. */
  420. PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
  421. PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
  422. Py_ssize_t *shape,
  423. Py_ssize_t *strides,
  424. int itemsize,
  425. char fort);
  426. /* Fill the strides array with byte-strides of a contiguous
  427. (Fortran-style if fort is 'F' or C-style otherwise)
  428. array of the given shape with the given number of bytes
  429. per element.
  430. */
  431. PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
  432. Py_ssize_t len, int readonly,
  433. int flags);
  434. /* Fills in a buffer-info structure correctly for an exporter
  435. that can only share a contiguous chunk of memory of
  436. "unsigned bytes" of the given length. Returns 0 on success
  437. and -1 (with raising an error) on error.
  438. */
  439. PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
  440. /* Releases a Py_buffer obtained from getbuffer ParseTuple's s*.
  441. */
  442. #endif /* Py_LIMITED_API */
  443. PyAPI_FUNC(PyObject *) PyObject_Format(PyObject* obj,
  444. PyObject *format_spec);
  445. /*
  446. Takes an arbitrary object and returns the result of
  447. calling obj.__format__(format_spec).
  448. */
  449. /* Iterators */
  450. PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
  451. /* Takes an object and returns an iterator for it.
  452. This is typically a new iterator but if the argument
  453. is an iterator, this returns itself. */
  454. #define PyIter_Check(obj) \
  455. ((obj)->ob_type->tp_iternext != NULL && \
  456. (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
  457. PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
  458. /* Takes an iterator object and calls its tp_iternext slot,
  459. returning the next value. If the iterator is exhausted,
  460. this returns NULL without setting an exception.
  461. NULL with an exception means an error occurred. */
  462. /* Number Protocol:*/
  463. PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
  464. /*
  465. Returns 1 if the object, o, provides numeric protocols, and
  466. false otherwise.
  467. This function always succeeds.
  468. */
  469. PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
  470. /*
  471. Returns the result of adding o1 and o2, or null on failure.
  472. This is the equivalent of the Python expression: o1+o2.
  473. */
  474. PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
  475. /*
  476. Returns the result of subtracting o2 from o1, or null on
  477. failure. This is the equivalent of the Python expression:
  478. o1-o2.
  479. */
  480. PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
  481. /*
  482. Returns the result of multiplying o1 and o2, or null on
  483. failure. This is the equivalent of the Python expression:
  484. o1*o2.
  485. */
  486. PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
  487. /*
  488. This is the equivalent of the Python expression: o1 @ o2.
  489. */
  490. PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
  491. /*
  492. Returns the result of dividing o1 by o2 giving an integral result,
  493. or null on failure.
  494. This is the equivalent of the Python expression: o1//o2.
  495. */
  496. PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
  497. /*
  498. Returns the result of dividing o1 by o2 giving a float result,
  499. or null on failure.
  500. This is the equivalent of the Python expression: o1/o2.
  501. */
  502. PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
  503. /*
  504. Returns the remainder of dividing o1 by o2, or null on
  505. failure. This is the equivalent of the Python expression:
  506. o1%o2.
  507. */
  508. PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
  509. /*
  510. See the built-in function divmod. Returns NULL on failure.
  511. This is the equivalent of the Python expression:
  512. divmod(o1,o2).
  513. */
  514. PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
  515. PyObject *o3);
  516. /*
  517. See the built-in function pow. Returns NULL on failure.
  518. This is the equivalent of the Python expression:
  519. pow(o1,o2,o3), where o3 is optional.
  520. */
  521. PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
  522. /*
  523. Returns the negation of o on success, or null on failure.
  524. This is the equivalent of the Python expression: -o.
  525. */
  526. PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
  527. /*
  528. Returns the (what?) of o on success, or NULL on failure.
  529. This is the equivalent of the Python expression: +o.
  530. */
  531. PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
  532. /*
  533. Returns the absolute value of o, or null on failure. This is
  534. the equivalent of the Python expression: abs(o).
  535. */
  536. PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
  537. /*
  538. Returns the bitwise negation of o on success, or NULL on
  539. failure. This is the equivalent of the Python expression:
  540. ~o.
  541. */
  542. PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
  543. /*
  544. Returns the result of left shifting o1 by o2 on success, or
  545. NULL on failure. This is the equivalent of the Python
  546. expression: o1 << o2.
  547. */
  548. PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
  549. /*
  550. Returns the result of right shifting o1 by o2 on success, or
  551. NULL on failure. This is the equivalent of the Python
  552. expression: o1 >> o2.
  553. */
  554. PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
  555. /*
  556. Returns the result of bitwise and of o1 and o2 on success, or
  557. NULL on failure. This is the equivalent of the Python
  558. expression: o1&o2.
  559. */
  560. PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
  561. /*
  562. Returns the bitwise exclusive or of o1 by o2 on success, or
  563. NULL on failure. This is the equivalent of the Python
  564. expression: o1^o2.
  565. */
  566. PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
  567. /*
  568. Returns the result of bitwise or on o1 and o2 on success, or
  569. NULL on failure. This is the equivalent of the Python
  570. expression: o1|o2.
  571. */
  572. #define PyIndex_Check(obj) \
  573. ((obj)->ob_type->tp_as_number != NULL && \
  574. (obj)->ob_type->tp_as_number->nb_index != NULL)
  575. PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
  576. /*
  577. Returns the object converted to a Python int
  578. or NULL with an error raised on failure.
  579. */
  580. PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
  581. /*
  582. Returns the object converted to Py_ssize_t by going through
  583. PyNumber_Index first. If an overflow error occurs while
  584. converting the int to Py_ssize_t, then the second argument
  585. is the error-type to return. If it is NULL, then the overflow error
  586. is cleared and the value is clipped.
  587. */
  588. PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
  589. /*
  590. Returns the o converted to an integer object on success, or
  591. NULL on failure. This is the equivalent of the Python
  592. expression: int(o).
  593. */
  594. PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
  595. /*
  596. Returns the o converted to a float object on success, or NULL
  597. on failure. This is the equivalent of the Python expression:
  598. float(o).
  599. */
  600. /* In-place variants of (some of) the above number protocol functions */
  601. PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
  602. /*
  603. Returns the result of adding o2 to o1, possibly in-place, or null
  604. on failure. This is the equivalent of the Python expression:
  605. o1 += o2.
  606. */
  607. PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
  608. /*
  609. Returns the result of subtracting o2 from o1, possibly in-place or
  610. null on failure. This is the equivalent of the Python expression:
  611. o1 -= o2.
  612. */
  613. PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
  614. /*
  615. Returns the result of multiplying o1 by o2, possibly in-place, or
  616. null on failure. This is the equivalent of the Python expression:
  617. o1 *= o2.
  618. */
  619. PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
  620. /*
  621. This is the equivalent of the Python expression: o1 @= o2.
  622. */
  623. PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
  624. PyObject *o2);
  625. /*
  626. Returns the result of dividing o1 by o2 giving an integral result,
  627. possibly in-place, or null on failure.
  628. This is the equivalent of the Python expression:
  629. o1 /= o2.
  630. */
  631. PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
  632. PyObject *o2);
  633. /*
  634. Returns the result of dividing o1 by o2 giving a float result,
  635. possibly in-place, or null on failure.
  636. This is the equivalent of the Python expression:
  637. o1 /= o2.
  638. */
  639. PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
  640. /*
  641. Returns the remainder of dividing o1 by o2, possibly in-place, or
  642. null on failure. This is the equivalent of the Python expression:
  643. o1 %= o2.
  644. */
  645. PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
  646. PyObject *o3);
  647. /*
  648. Returns the result of raising o1 to the power of o2, possibly
  649. in-place, or null on failure. This is the equivalent of the Python
  650. expression: o1 **= o2, or pow(o1, o2, o3) if o3 is present.
  651. */
  652. PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
  653. /*
  654. Returns the result of left shifting o1 by o2, possibly in-place, or
  655. null on failure. This is the equivalent of the Python expression:
  656. o1 <<= o2.
  657. */
  658. PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
  659. /*
  660. Returns the result of right shifting o1 by o2, possibly in-place or
  661. null on failure. This is the equivalent of the Python expression:
  662. o1 >>= o2.
  663. */
  664. PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
  665. /*
  666. Returns the result of bitwise and of o1 and o2, possibly in-place,
  667. or null on failure. This is the equivalent of the Python
  668. expression: o1 &= o2.
  669. */
  670. PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
  671. /*
  672. Returns the bitwise exclusive or of o1 by o2, possibly in-place, or
  673. null on failure. This is the equivalent of the Python expression:
  674. o1 ^= o2.
  675. */
  676. PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
  677. /*
  678. Returns the result of bitwise or of o1 and o2, possibly in-place,
  679. or null on failure. This is the equivalent of the Python
  680. expression: o1 |= o2.
  681. */
  682. PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
  683. /*
  684. Returns the integer n converted to a string with a base, with a base
  685. marker of 0b, 0o or 0x prefixed if applicable.
  686. If n is not an int object, it is converted with PyNumber_Index first.
  687. */
  688. /* Sequence protocol:*/
  689. PyAPI_FUNC(int) PySequence_Check(PyObject *o);
  690. /*
  691. Return 1 if the object provides sequence protocol, and zero
  692. otherwise.
  693. This function always succeeds.
  694. */
  695. PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
  696. /*
  697. Return the size of sequence object o, or -1 on failure.
  698. */
  699. /* For DLL compatibility */
  700. #undef PySequence_Length
  701. PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o);
  702. #define PySequence_Length PySequence_Size
  703. PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
  704. /*
  705. Return the concatenation of o1 and o2 on success, and NULL on
  706. failure. This is the equivalent of the Python
  707. expression: o1+o2.
  708. */
  709. PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
  710. /*
  711. Return the result of repeating sequence object o count times,
  712. or NULL on failure. This is the equivalent of the Python
  713. expression: o1*count.
  714. */
  715. PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
  716. /*
  717. Return the ith element of o, or NULL on failure. This is the
  718. equivalent of the Python expression: o[i].
  719. */
  720. PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
  721. /*
  722. Return the slice of sequence object o between i1 and i2, or
  723. NULL on failure. This is the equivalent of the Python
  724. expression: o[i1:i2].
  725. */
  726. PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
  727. /*
  728. Assign object v to the ith element of o. Raise an exception and return
  729. -1 on failure; return 0 on success. This is the equivalent of the
  730. Python statement o[i]=v.
  731. */
  732. PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
  733. /*
  734. Delete the ith element of object v. Returns
  735. -1 on failure. This is the equivalent of the Python
  736. statement: del o[i].
  737. */
  738. PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
  739. PyObject *v);
  740. /*
  741. Assign the sequence object, v, to the slice in sequence
  742. object, o, from i1 to i2. Returns -1 on failure. This is the
  743. equivalent of the Python statement: o[i1:i2]=v.
  744. */
  745. PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
  746. /*
  747. Delete the slice in sequence object, o, from i1 to i2.
  748. Returns -1 on failure. This is the equivalent of the Python
  749. statement: del o[i1:i2].
  750. */
  751. PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
  752. /*
  753. Returns the sequence, o, as a tuple on success, and NULL on failure.
  754. This is equivalent to the Python expression: tuple(o)
  755. */
  756. PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
  757. /*
  758. Returns the sequence, o, as a list on success, and NULL on failure.
  759. This is equivalent to the Python expression: list(o)
  760. */
  761. PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
  762. /*
  763. Return the sequence, o, as a list, unless it's already a
  764. tuple or list. Use PySequence_Fast_GET_ITEM to access the
  765. members of this list, and PySequence_Fast_GET_SIZE to get its length.
  766. Returns NULL on failure. If the object does not support iteration,
  767. raises a TypeError exception with m as the message text.
  768. */
  769. #define PySequence_Fast_GET_SIZE(o) \
  770. (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
  771. /*
  772. Return the size of o, assuming that o was returned by
  773. PySequence_Fast and is not NULL.
  774. */
  775. #define PySequence_Fast_GET_ITEM(o, i)\
  776. (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
  777. /*
  778. Return the ith element of o, assuming that o was returned by
  779. PySequence_Fast, and that i is within bounds.
  780. */
  781. #define PySequence_ITEM(o, i)\
  782. ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
  783. /* Assume tp_as_sequence and sq_item exist and that i does not
  784. need to be corrected for a negative index
  785. */
  786. #define PySequence_Fast_ITEMS(sf) \
  787. (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
  788. : ((PyTupleObject *)(sf))->ob_item)
  789. /* Return a pointer to the underlying item array for
  790. an object retured by PySequence_Fast */
  791. PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
  792. /*
  793. Return the number of occurrences on value on o, that is,
  794. return the number of keys for which o[key]==value. On
  795. failure, return -1. This is equivalent to the Python
  796. expression: o.count(value).
  797. */
  798. PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
  799. /*
  800. Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
  801. Use __contains__ if possible, else _PySequence_IterSearch().
  802. */
  803. #ifndef Py_LIMITED_API
  804. #define PY_ITERSEARCH_COUNT 1
  805. #define PY_ITERSEARCH_INDEX 2
  806. #define PY_ITERSEARCH_CONTAINS 3
  807. PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
  808. PyObject *obj, int operation);
  809. #endif
  810. /*
  811. Iterate over seq. Result depends on the operation:
  812. PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
  813. error.
  814. PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
  815. obj in seq; set ValueError and return -1 if none found;
  816. also return -1 on error.
  817. PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
  818. error.
  819. */
  820. /* For DLL-level backwards compatibility */
  821. #undef PySequence_In
  822. PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value);
  823. /* For source-level backwards compatibility */
  824. #define PySequence_In PySequence_Contains
  825. /*
  826. Determine if o contains value. If an item in o is equal to
  827. X, return 1, otherwise return 0. On error, return -1. This
  828. is equivalent to the Python expression: value in o.
  829. */
  830. PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
  831. /*
  832. Return the first index for which o[i]=value. On error,
  833. return -1. This is equivalent to the Python
  834. expression: o.index(value).
  835. */
  836. /* In-place versions of some of the above Sequence functions. */
  837. PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
  838. /*
  839. Append o2 to o1, in-place when possible. Return the resulting
  840. object, which could be o1, or NULL on failure. This is the
  841. equivalent of the Python expression: o1 += o2.
  842. */
  843. PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
  844. /*
  845. Repeat o1 by count, in-place when possible. Return the resulting
  846. object, which could be o1, or NULL on failure. This is the
  847. equivalent of the Python expression: o1 *= count.
  848. */
  849. /* Mapping protocol:*/
  850. PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
  851. /*
  852. Return 1 if the object provides mapping protocol, and zero
  853. otherwise.
  854. This function always succeeds.
  855. */
  856. PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
  857. /*
  858. Returns the number of keys in object o on success, and -1 on
  859. failure. For objects that do not provide sequence protocol,
  860. this is equivalent to the Python expression: len(o).
  861. */
  862. /* For DLL compatibility */
  863. #undef PyMapping_Length
  864. PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
  865. #define PyMapping_Length PyMapping_Size
  866. /* implemented as a macro:
  867. int PyMapping_DelItemString(PyObject *o, const char *key);
  868. Remove the mapping for object, key, from the object *o.
  869. Returns -1 on failure. This is equivalent to
  870. the Python statement: del o[key].
  871. */
  872. #define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
  873. /* implemented as a macro:
  874. int PyMapping_DelItem(PyObject *o, PyObject *key);
  875. Remove the mapping for object, key, from the object *o.
  876. Returns -1 on failure. This is equivalent to
  877. the Python statement: del o[key].
  878. */
  879. #define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
  880. PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key);
  881. /*
  882. On success, return 1 if the mapping object has the key, key,
  883. and 0 otherwise. This is equivalent to the Python expression:
  884. key in o.
  885. This function always succeeds.
  886. */
  887. PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
  888. /*
  889. Return 1 if the mapping object has the key, key,
  890. and 0 otherwise. This is equivalent to the Python expression:
  891. key in o.
  892. This function always succeeds.
  893. */
  894. PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
  895. /*
  896. On success, return a list, a tuple or a dictionary view in case of a dict,
  897. of the keys in object o. On failure, return NULL.
  898. */
  899. PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
  900. /*
  901. On success, return a list, a tuple or a dictionary view in case of a dict,
  902. of the values in object o. On failure, return NULL.
  903. */
  904. PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
  905. /*
  906. On success, return a list, a tuple or a dictionary view in case of a dict,
  907. of the items in object o, where each item is a tuple containing a key-value
  908. pair. On failure, return NULL.
  909. */
  910. PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
  911. const char *key);
  912. /*
  913. Return element of o corresponding to the object, key, or NULL
  914. on failure. This is the equivalent of the Python expression:
  915. o[key].
  916. */
  917. PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
  918. PyObject *value);
  919. /*
  920. Map the object, key, to the value, v. Returns
  921. -1 on failure. This is the equivalent of the Python
  922. statement: o[key]=v.
  923. */
  924. PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
  925. /* isinstance(object, typeorclass) */
  926. PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
  927. /* issubclass(object, typeorclass) */
  928. #ifndef Py_LIMITED_API
  929. PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
  930. PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
  931. PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
  932. PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
  933. #endif
  934. /* For internal use by buffer API functions */
  935. PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
  936. const Py_ssize_t *shape);
  937. PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
  938. const Py_ssize_t *shape);
  939. #ifdef __cplusplus
  940. }
  941. #endif
  942. #endif /* Py_ABSTRACTOBJECT_H */