Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1276 lines
38 KiB

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