Chris@87: #ifndef Py_ABSTRACTOBJECT_H Chris@87: #define Py_ABSTRACTOBJECT_H Chris@87: #ifdef __cplusplus Chris@87: extern "C" { Chris@87: #endif Chris@87: Chris@87: #ifdef PY_SSIZE_T_CLEAN Chris@87: #define PyObject_CallFunction _PyObject_CallFunction_SizeT Chris@87: #define PyObject_CallMethod _PyObject_CallMethod_SizeT Chris@87: #endif Chris@87: Chris@87: /* Abstract Object Interface (many thanks to Jim Fulton) */ Chris@87: Chris@87: /* Chris@87: PROPOSAL: A Generic Python Object Interface for Python C Modules Chris@87: Chris@87: Problem Chris@87: Chris@87: Python modules written in C that must access Python objects must do Chris@87: so through routines whose interfaces are described by a set of Chris@87: include files. Unfortunately, these routines vary according to the Chris@87: object accessed. To use these routines, the C programmer must check Chris@87: the type of the object being used and must call a routine based on Chris@87: the object type. For example, to access an element of a sequence, Chris@87: the programmer must determine whether the sequence is a list or a Chris@87: tuple: Chris@87: Chris@87: if(is_tupleobject(o)) Chris@87: e=gettupleitem(o,i) Chris@87: else if(is_listitem(o)) Chris@87: e=getlistitem(o,i) Chris@87: Chris@87: If the programmer wants to get an item from another type of object Chris@87: that provides sequence behavior, there is no clear way to do it Chris@87: correctly. Chris@87: Chris@87: The persistent programmer may peruse object.h and find that the Chris@87: _typeobject structure provides a means of invoking up to (currently Chris@87: about) 41 special operators. So, for example, a routine can get an Chris@87: item from any object that provides sequence behavior. However, to Chris@87: use this mechanism, the programmer must make their code dependent on Chris@87: the current Python implementation. Chris@87: Chris@87: Also, certain semantics, especially memory management semantics, may Chris@87: differ by the type of object being used. Unfortunately, these Chris@87: semantics are not clearly described in the current include files. Chris@87: An abstract interface providing more consistent semantics is needed. Chris@87: Chris@87: Proposal Chris@87: Chris@87: I propose the creation of a standard interface (with an associated Chris@87: library of routines and/or macros) for generically obtaining the Chris@87: services of Python objects. This proposal can be viewed as one Chris@87: components of a Python C interface consisting of several components. Chris@87: Chris@87: From the viewpoint of C access to Python services, we have (as Chris@87: suggested by Guido in off-line discussions): Chris@87: Chris@87: - "Very high level layer": two or three functions that let you exec or Chris@87: eval arbitrary Python code given as a string in a module whose name is Chris@87: given, passing C values in and getting C values out using Chris@87: mkvalue/getargs style format strings. This does not require the user Chris@87: to declare any variables of type "PyObject *". This should be enough Chris@87: to write a simple application that gets Python code from the user, Chris@87: execs it, and returns the output or errors. (Error handling must also Chris@87: be part of this API.) Chris@87: Chris@87: - "Abstract objects layer": which is the subject of this proposal. Chris@87: It has many functions operating on objects, and lest you do many Chris@87: things from C that you can also write in Python, without going Chris@87: through the Python parser. Chris@87: Chris@87: - "Concrete objects layer": This is the public type-dependent Chris@87: interface provided by the standard built-in types, such as floats, Chris@87: strings, and lists. This interface exists and is currently Chris@87: documented by the collection of include files provided with the Chris@87: Python distributions. Chris@87: Chris@87: From the point of view of Python accessing services provided by C Chris@87: modules: Chris@87: Chris@87: - "Python module interface": this interface consist of the basic Chris@87: routines used to define modules and their members. Most of the Chris@87: current extensions-writing guide deals with this interface. Chris@87: Chris@87: - "Built-in object interface": this is the interface that a new Chris@87: built-in type must provide and the mechanisms and rules that a Chris@87: developer of a new built-in type must use and follow. Chris@87: Chris@87: This proposal is a "first-cut" that is intended to spur Chris@87: discussion. See especially the lists of notes. Chris@87: Chris@87: The Python C object interface will provide four protocols: object, Chris@87: numeric, sequence, and mapping. Each protocol consists of a Chris@87: collection of related operations. If an operation that is not Chris@87: provided by a particular type is invoked, then a standard exception, Chris@87: NotImplementedError is raised with a operation name as an argument. Chris@87: In addition, for convenience this interface defines a set of Chris@87: constructors for building objects of built-in types. This is needed Chris@87: so new objects can be returned from C functions that otherwise treat Chris@87: objects generically. Chris@87: Chris@87: Memory Management Chris@87: Chris@87: For all of the functions described in this proposal, if a function Chris@87: retains a reference to a Python object passed as an argument, then the Chris@87: function will increase the reference count of the object. It is Chris@87: unnecessary for the caller to increase the reference count of an Chris@87: argument in anticipation of the object's retention. Chris@87: Chris@87: All Python objects returned from functions should be treated as new Chris@87: objects. Functions that return objects assume that the caller will Chris@87: retain a reference and the reference count of the object has already Chris@87: been incremented to account for this fact. A caller that does not Chris@87: retain a reference to an object that is returned from a function Chris@87: must decrement the reference count of the object (using Chris@87: DECREF(object)) to prevent memory leaks. Chris@87: Chris@87: Note that the behavior mentioned here is different from the current Chris@87: behavior for some objects (e.g. lists and tuples) when certain Chris@87: type-specific routines are called directly (e.g. setlistitem). The Chris@87: proposed abstraction layer will provide a consistent memory Chris@87: management interface, correcting for inconsistent behavior for some Chris@87: built-in types. Chris@87: Chris@87: Protocols Chris@87: Chris@87: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ Chris@87: Chris@87: /* Object Protocol: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_Print(PyObject *o, FILE *fp, int flags); Chris@87: Chris@87: Print an object, o, on file, fp. Returns -1 on Chris@87: error. The flags argument is used to enable certain printing Chris@87: options. The only option currently supported is Py_Print_RAW. Chris@87: Chris@87: (What should be said about Py_Print_RAW?) Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_HasAttrString(PyObject *o, char *attr_name); Chris@87: Chris@87: Returns 1 if o has the attribute attr_name, and 0 otherwise. Chris@87: This is equivalent to the Python expression: Chris@87: hasattr(o,attr_name). Chris@87: Chris@87: This function always succeeds. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: PyObject* PyObject_GetAttrString(PyObject *o, char *attr_name); Chris@87: Chris@87: Retrieve an attributed named attr_name form object o. Chris@87: Returns the attribute value on success, or NULL on failure. Chris@87: This is the equivalent of the Python expression: o.attr_name. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_HasAttr(PyObject *o, PyObject *attr_name); Chris@87: Chris@87: Returns 1 if o has the attribute attr_name, and 0 otherwise. Chris@87: This is equivalent to the Python expression: Chris@87: hasattr(o,attr_name). Chris@87: Chris@87: This function always succeeds. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name); Chris@87: Chris@87: Retrieve an attributed named attr_name form object o. Chris@87: Returns the attribute value on success, or NULL on failure. Chris@87: This is the equivalent of the Python expression: o.attr_name. Chris@87: Chris@87: */ Chris@87: Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_SetAttrString(PyObject *o, char *attr_name, PyObject *v); Chris@87: Chris@87: Set the value of the attribute named attr_name, for object o, Chris@87: to the value, v. Returns -1 on failure. This is Chris@87: the equivalent of the Python statement: o.attr_name=v. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v); Chris@87: Chris@87: Set the value of the attribute named attr_name, for object o, Chris@87: to the value, v. Returns -1 on failure. This is Chris@87: the equivalent of the Python statement: o.attr_name=v. Chris@87: Chris@87: */ Chris@87: Chris@87: /* implemented as a macro: Chris@87: Chris@87: int PyObject_DelAttrString(PyObject *o, char *attr_name); Chris@87: Chris@87: Delete attribute named attr_name, for object o. Returns Chris@87: -1 on failure. This is the equivalent of the Python Chris@87: statement: del o.attr_name. Chris@87: Chris@87: */ Chris@87: #define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL) Chris@87: Chris@87: /* implemented as a macro: Chris@87: Chris@87: int PyObject_DelAttr(PyObject *o, PyObject *attr_name); Chris@87: Chris@87: Delete attribute named attr_name, for object o. Returns -1 Chris@87: on failure. This is the equivalent of the Python Chris@87: statement: del o.attr_name. Chris@87: Chris@87: */ Chris@87: #define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL) Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_Cmp(PyObject *o1, PyObject *o2, int *result); Chris@87: Chris@87: /* Chris@87: Compare the values of o1 and o2 using a routine provided by Chris@87: o1, if one exists, otherwise with a routine provided by o2. Chris@87: The result of the comparison is returned in result. Returns Chris@87: -1 on failure. This is the equivalent of the Python Chris@87: statement: result=cmp(o1,o2). Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_Compare(PyObject *o1, PyObject *o2); Chris@87: Chris@87: Compare the values of o1 and o2 using a routine provided by Chris@87: o1, if one exists, otherwise with a routine provided by o2. Chris@87: Returns the result of the comparison on success. On error, Chris@87: the value returned is undefined. This is equivalent to the Chris@87: Python expression: cmp(o1,o2). Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: PyObject *PyObject_Repr(PyObject *o); Chris@87: Chris@87: Compute the string representation of object, o. Returns the Chris@87: string representation on success, NULL on failure. This is Chris@87: the equivalent of the Python expression: repr(o). Chris@87: Chris@87: Called by the repr() built-in function and by reverse quotes. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: PyObject *PyObject_Str(PyObject *o); Chris@87: Chris@87: Compute the string representation of object, o. Returns the Chris@87: string representation on success, NULL on failure. This is Chris@87: the equivalent of the Python expression: str(o).) Chris@87: Chris@87: Called by the str() built-in function and by the print Chris@87: statement. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: PyObject *PyObject_Unicode(PyObject *o); Chris@87: Chris@87: Compute the unicode representation of object, o. Returns the Chris@87: unicode representation on success, NULL on failure. This is Chris@87: the equivalent of the Python expression: unistr(o).) Chris@87: Chris@87: Called by the unistr() built-in function. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Declared elsewhere Chris@87: Chris@87: PyAPI_FUNC(int) PyCallable_Check(PyObject *o); Chris@87: Chris@87: Determine if the object, o, is callable. Return 1 if the Chris@87: object is callable and 0 otherwise. Chris@87: Chris@87: This function always succeeds. Chris@87: Chris@87: */ Chris@87: Chris@87: Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object, Chris@87: PyObject *args, PyObject *kw); Chris@87: Chris@87: /* Chris@87: Call a callable Python object, callable_object, with Chris@87: arguments and keywords arguments. The 'args' argument can not be Chris@87: NULL, but the 'kw' argument can be NULL. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable_object, Chris@87: PyObject *args); Chris@87: Chris@87: /* Chris@87: Call a callable Python object, callable_object, with Chris@87: arguments given by the tuple, args. If no arguments are Chris@87: needed, then args may be NULL. Returns the result of the Chris@87: call on success, or NULL on failure. This is the equivalent Chris@87: of the Python expression: apply(o,args). Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable_object, Chris@87: char *format, ...); Chris@87: Chris@87: /* Chris@87: Call a callable Python object, callable_object, with a Chris@87: variable number of C arguments. The C arguments are described Chris@87: using a mkvalue-style format string. The format may be NULL, Chris@87: indicating that no arguments are provided. Returns the Chris@87: result of the call on success, or NULL on failure. This is Chris@87: the equivalent of the Python expression: apply(o,args). Chris@87: Chris@87: */ Chris@87: Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *o, char *m, Chris@87: char *format, ...); Chris@87: Chris@87: /* Chris@87: Call the method named m of object o with a variable number of Chris@87: C arguments. The C arguments are described by a mkvalue Chris@87: format string. The format may be NULL, indicating that no Chris@87: arguments are provided. Returns the result of the call on Chris@87: success, or NULL on failure. This is the equivalent of the Chris@87: Python expression: o.method(args). Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable, Chris@87: char *format, ...); Chris@87: PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *o, Chris@87: char *name, Chris@87: char *format, ...); Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable, Chris@87: ...); Chris@87: Chris@87: /* Chris@87: Call a callable Python object, callable_object, with a Chris@87: variable number of C arguments. The C arguments are provided Chris@87: as PyObject * values, terminated by a NULL. Returns the Chris@87: result of the call on success, or NULL on failure. This is Chris@87: the equivalent of the Python expression: apply(o,args). Chris@87: */ Chris@87: Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(PyObject *o, Chris@87: PyObject *m, ...); Chris@87: Chris@87: /* Chris@87: Call the method named m of object o with a variable number of Chris@87: C arguments. The C arguments are provided as PyObject * Chris@87: values, terminated by NULL. Returns the result of the call Chris@87: on success, or NULL on failure. This is the equivalent of Chris@87: the Python expression: o.method(args). Chris@87: */ Chris@87: Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: long PyObject_Hash(PyObject *o); Chris@87: Chris@87: Compute and return the hash, hash_value, of an object, o. On Chris@87: failure, return -1. This is the equivalent of the Python Chris@87: expression: hash(o). Chris@87: Chris@87: */ Chris@87: Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_IsTrue(PyObject *o); Chris@87: Chris@87: Returns 1 if the object, o, is considered to be true, 0 if o is Chris@87: considered to be false and -1 on failure. This is equivalent to the Chris@87: Python expression: not not o Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyObject_Not(PyObject *o); Chris@87: Chris@87: Returns 0 if the object, o, is considered to be true, 1 if o is Chris@87: considered to be false and -1 on failure. This is equivalent to the Chris@87: Python expression: not o Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o); Chris@87: Chris@87: /* Chris@87: On success, returns a type object corresponding to the object Chris@87: type of object o. On failure, returns NULL. This is Chris@87: equivalent to the Python expression: type(o). Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o); Chris@87: Chris@87: /* Chris@87: Return the size of object o. If the object, o, provides Chris@87: both sequence and mapping protocols, the sequence size is Chris@87: returned. On error, -1 is returned. This is the equivalent Chris@87: to the Python expression: len(o). Chris@87: Chris@87: */ Chris@87: Chris@87: /* For DLL compatibility */ Chris@87: #undef PyObject_Length Chris@87: PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o); Chris@87: #define PyObject_Length PyObject_Size Chris@87: Chris@87: PyAPI_FUNC(Py_ssize_t) _PyObject_LengthHint(PyObject *o, Py_ssize_t); Chris@87: Chris@87: /* Chris@87: Guess the size of object o using len(o) or o.__length_hint__(). Chris@87: If neither of those return a non-negative value, then return the Chris@87: default value. If one of the calls fails, this function returns -1. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key); Chris@87: Chris@87: /* Chris@87: Return element of o corresponding to the object, key, or NULL Chris@87: on failure. This is the equivalent of the Python expression: Chris@87: o[key]. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v); Chris@87: Chris@87: /* Chris@87: Map the object, key, to the value, v. Returns Chris@87: -1 on failure. This is the equivalent of the Python Chris@87: statement: o[key]=v. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, char *key); Chris@87: Chris@87: /* Chris@87: Remove the mapping for object, key, from the object *o. Chris@87: Returns -1 on failure. This is equivalent to Chris@87: the Python statement: del o[key]. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key); Chris@87: Chris@87: /* Chris@87: Delete the mapping for key from *o. Returns -1 on failure. Chris@87: This is the equivalent of the Python statement: del o[key]. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj, Chris@87: const char **buffer, Chris@87: Py_ssize_t *buffer_len); Chris@87: Chris@87: /* Chris@87: Takes an arbitrary object which must support the (character, Chris@87: single segment) buffer interface and returns a pointer to a Chris@87: read-only memory location useable as character based input Chris@87: for subsequent processing. Chris@87: Chris@87: 0 is returned on success. buffer and buffer_len are only Chris@87: set in case no error occurs. Otherwise, -1 is returned and Chris@87: an exception set. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj); Chris@87: Chris@87: /* Chris@87: Checks whether an arbitrary object supports the (character, Chris@87: single segment) buffer interface. Returns 1 on success, 0 Chris@87: on failure. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj, Chris@87: const void **buffer, Chris@87: Py_ssize_t *buffer_len); Chris@87: Chris@87: /* Chris@87: Same as PyObject_AsCharBuffer() except that this API expects Chris@87: (readable, single segment) buffer interface and returns a Chris@87: pointer to a read-only memory location which can contain Chris@87: arbitrary data. Chris@87: Chris@87: 0 is returned on success. buffer and buffer_len are only Chris@87: set in case no error occurs. Otherwise, -1 is returned and Chris@87: an exception set. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj, Chris@87: void **buffer, Chris@87: Py_ssize_t *buffer_len); Chris@87: Chris@87: /* Chris@87: Takes an arbitrary object which must support the (writeable, Chris@87: single segment) buffer interface and returns a pointer to a Chris@87: writeable memory location in buffer of size buffer_len. Chris@87: Chris@87: 0 is returned on success. buffer and buffer_len are only Chris@87: set in case no error occurs. Otherwise, -1 is returned and Chris@87: an exception set. Chris@87: Chris@87: */ Chris@87: Chris@87: /* new buffer API */ Chris@87: Chris@87: #define PyObject_CheckBuffer(obj) \ Chris@87: (((obj)->ob_type->tp_as_buffer != NULL) && \ Chris@87: (PyType_HasFeature((obj)->ob_type, Py_TPFLAGS_HAVE_NEWBUFFER)) && \ Chris@87: ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL)) Chris@87: Chris@87: /* Return 1 if the getbuffer function is available, otherwise Chris@87: return 0 */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view, Chris@87: int flags); Chris@87: Chris@87: /* This is a C-API version of the getbuffer function call. It checks Chris@87: to make sure object has the required function pointer and issues the Chris@87: call. Returns -1 and raises an error on failure and returns 0 on Chris@87: success Chris@87: */ Chris@87: Chris@87: Chris@87: PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices); Chris@87: Chris@87: /* Get the memory area pointed to by the indices for the buffer given. Chris@87: Note that view->ndim is the assumed size of indices Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *); Chris@87: Chris@87: /* Return the implied itemsize of the data-format area from a Chris@87: struct-style description */ Chris@87: Chris@87: Chris@87: Chris@87: PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view, Chris@87: Py_ssize_t len, char fort); Chris@87: Chris@87: PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf, Chris@87: Py_ssize_t len, char fort); Chris@87: Chris@87: Chris@87: /* Copy len bytes of data from the contiguous chunk of memory Chris@87: pointed to by buf into the buffer exported by obj. Return Chris@87: 0 on success and return -1 and raise a PyBuffer_Error on Chris@87: error (i.e. the object does not have a buffer interface or Chris@87: it is not working). Chris@87: Chris@87: If fort is 'F' and the object is multi-dimensional, Chris@87: then the data will be copied into the array in Chris@87: Fortran-style (first dimension varies the fastest). If Chris@87: fort is 'C', then the data will be copied into the array Chris@87: in C-style (last dimension varies the fastest). If fort Chris@87: is 'A', then it does not matter and the copy will be made Chris@87: in whatever way is more efficient. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src); Chris@87: Chris@87: /* Copy the data from the src buffer to the buffer of destination Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyBuffer_IsContiguous(Py_buffer *view, char fort); Chris@87: Chris@87: Chris@87: PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims, Chris@87: Py_ssize_t *shape, Chris@87: Py_ssize_t *strides, Chris@87: int itemsize, Chris@87: char fort); Chris@87: Chris@87: /* Fill the strides array with byte-strides of a contiguous Chris@87: (Fortran-style if fort is 'F' or C-style otherwise) Chris@87: array of the given shape with the given number of bytes Chris@87: per element. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf, Chris@87: Py_ssize_t len, int readonly, Chris@87: int flags); Chris@87: Chris@87: /* Fills in a buffer-info structure correctly for an exporter Chris@87: that can only share a contiguous chunk of memory of Chris@87: "unsigned bytes" of the given length. Returns 0 on success Chris@87: and -1 (with raising an error) on error. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view); Chris@87: Chris@87: /* Releases a Py_buffer obtained from getbuffer ParseTuple's s*. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_Format(PyObject* obj, Chris@87: PyObject *format_spec); Chris@87: /* Chris@87: Takes an arbitrary object and returns the result of Chris@87: calling obj.__format__(format_spec). Chris@87: */ Chris@87: Chris@87: /* Iterators */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *); Chris@87: /* Takes an object and returns an iterator for it. Chris@87: This is typically a new iterator but if the argument Chris@87: is an iterator, this returns itself. */ Chris@87: Chris@87: #define PyIter_Check(obj) \ Chris@87: (PyType_HasFeature((obj)->ob_type, Py_TPFLAGS_HAVE_ITER) && \ Chris@87: (obj)->ob_type->tp_iternext != NULL && \ Chris@87: (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented) Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *); Chris@87: /* Takes an iterator object and calls its tp_iternext slot, Chris@87: returning the next value. If the iterator is exhausted, Chris@87: this returns NULL without setting an exception. Chris@87: NULL with an exception means an error occurred. */ Chris@87: Chris@87: /* Number Protocol:*/ Chris@87: Chris@87: PyAPI_FUNC(int) PyNumber_Check(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns 1 if the object, o, provides numeric protocols, and Chris@87: false otherwise. Chris@87: Chris@87: This function always succeeds. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of adding o1 and o2, or null on failure. Chris@87: This is the equivalent of the Python expression: o1+o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of subtracting o2 from o1, or null on Chris@87: failure. This is the equivalent of the Python expression: Chris@87: o1-o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of multiplying o1 and o2, or null on Chris@87: failure. This is the equivalent of the Python expression: Chris@87: o1*o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Divide(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of dividing o1 by o2, or null on failure. Chris@87: This is the equivalent of the Python expression: o1/o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of dividing o1 by o2 giving an integral result, Chris@87: or null on failure. Chris@87: This is the equivalent of the Python expression: o1//o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of dividing o1 by o2 giving a float result, Chris@87: or null on failure. Chris@87: This is the equivalent of the Python expression: o1/o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the remainder of dividing o1 by o2, or null on Chris@87: failure. This is the equivalent of the Python expression: Chris@87: o1%o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: See the built-in function divmod. Returns NULL on failure. Chris@87: This is the equivalent of the Python expression: Chris@87: divmod(o1,o2). Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2, Chris@87: PyObject *o3); Chris@87: Chris@87: /* Chris@87: See the built-in function pow. Returns NULL on failure. Chris@87: This is the equivalent of the Python expression: Chris@87: pow(o1,o2,o3), where o3 is optional. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the negation of o on success, or null on failure. Chris@87: This is the equivalent of the Python expression: -o. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the (what?) of o on success, or NULL on failure. Chris@87: This is the equivalent of the Python expression: +o. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the absolute value of o, or null on failure. This is Chris@87: the equivalent of the Python expression: abs(o). Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the bitwise negation of o on success, or NULL on Chris@87: failure. This is the equivalent of the Python expression: Chris@87: ~o. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of left shifting o1 by o2 on success, or Chris@87: NULL on failure. This is the equivalent of the Python Chris@87: expression: o1 << o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of right shifting o1 by o2 on success, or Chris@87: NULL on failure. This is the equivalent of the Python Chris@87: expression: o1 >> o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of bitwise and of o1 and o2 on success, or Chris@87: NULL on failure. This is the equivalent of the Python Chris@87: expression: o1&o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the bitwise exclusive or of o1 by o2 on success, or Chris@87: NULL on failure. This is the equivalent of the Python Chris@87: expression: o1^o2. Chris@87: Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of bitwise or on o1 and o2 on success, or Chris@87: NULL on failure. This is the equivalent of the Python Chris@87: expression: o1|o2. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented elsewhere: Chris@87: Chris@87: int PyNumber_Coerce(PyObject **p1, PyObject **p2); Chris@87: Chris@87: This function takes the addresses of two variables of type Chris@87: PyObject*. Chris@87: Chris@87: If the objects pointed to by *p1 and *p2 have the same type, Chris@87: increment their reference count and return 0 (success). Chris@87: If the objects can be converted to a common numeric type, Chris@87: replace *p1 and *p2 by their converted value (with 'new' Chris@87: reference counts), and return 0. Chris@87: If no conversion is possible, or if some other error occurs, Chris@87: return -1 (failure) and don't increment the reference counts. Chris@87: The call PyNumber_Coerce(&o1, &o2) is equivalent to the Python Chris@87: statement o1, o2 = coerce(o1, o2). Chris@87: Chris@87: */ Chris@87: Chris@87: #define PyIndex_Check(obj) \ Chris@87: ((obj)->ob_type->tp_as_number != NULL && \ Chris@87: PyType_HasFeature((obj)->ob_type, Py_TPFLAGS_HAVE_INDEX) && \ Chris@87: (obj)->ob_type->tp_as_number->nb_index != NULL) Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the object converted to a Python long or int Chris@87: or NULL with an error raised on failure. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc); Chris@87: Chris@87: /* Chris@87: Returns the Integral instance converted to an int. The Chris@87: instance is expected to be int or long or have an __int__ Chris@87: method. Steals integral's reference. error_format will be Chris@87: used to create the TypeError if integral isn't actually an Chris@87: Integral instance. error_format should be a format string Chris@87: that can accept a char* naming integral's type. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) _PyNumber_ConvertIntegralToInt( Chris@87: PyObject *integral, Chris@87: const char* error_format); Chris@87: Chris@87: /* Chris@87: Returns the object converted to Py_ssize_t by going through Chris@87: PyNumber_Index first. If an overflow error occurs while Chris@87: converting the int-or-long to Py_ssize_t, then the second argument Chris@87: is the error-type to return. If it is NULL, then the overflow error Chris@87: is cleared and the value is clipped. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Int(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the o converted to an integer object on success, or Chris@87: NULL on failure. This is the equivalent of the Python Chris@87: expression: int(o). Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the o converted to a long integer object on success, Chris@87: or NULL on failure. This is the equivalent of the Python Chris@87: expression: long(o). Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the o converted to a float object on success, or NULL Chris@87: on failure. This is the equivalent of the Python expression: Chris@87: float(o). Chris@87: */ Chris@87: Chris@87: /* In-place variants of (some of) the above number protocol functions */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of adding o2 to o1, possibly in-place, or null Chris@87: on failure. This is the equivalent of the Python expression: Chris@87: o1 += o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of subtracting o2 from o1, possibly in-place or Chris@87: null on failure. This is the equivalent of the Python expression: Chris@87: o1 -= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of multiplying o1 by o2, possibly in-place, or Chris@87: null on failure. This is the equivalent of the Python expression: Chris@87: o1 *= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of dividing o1 by o2, possibly in-place, or null Chris@87: on failure. This is the equivalent of the Python expression: Chris@87: o1 /= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1, Chris@87: PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of dividing o1 by o2 giving an integral result, Chris@87: possibly in-place, or null on failure. Chris@87: This is the equivalent of the Python expression: Chris@87: o1 /= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1, Chris@87: PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of dividing o1 by o2 giving a float result, Chris@87: possibly in-place, or null on failure. Chris@87: This is the equivalent of the Python expression: Chris@87: o1 /= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the remainder of dividing o1 by o2, possibly in-place, or Chris@87: null on failure. This is the equivalent of the Python expression: Chris@87: o1 %= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2, Chris@87: PyObject *o3); Chris@87: Chris@87: /* Chris@87: Returns the result of raising o1 to the power of o2, possibly Chris@87: in-place, or null on failure. This is the equivalent of the Python Chris@87: expression: o1 **= o2, or pow(o1, o2, o3) if o3 is present. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of left shifting o1 by o2, possibly in-place, or Chris@87: null on failure. This is the equivalent of the Python expression: Chris@87: o1 <<= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of right shifting o1 by o2, possibly in-place or Chris@87: null on failure. This is the equivalent of the Python expression: Chris@87: o1 >>= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of bitwise and of o1 and o2, possibly in-place, Chris@87: or null on failure. This is the equivalent of the Python Chris@87: expression: o1 &= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the bitwise exclusive or of o1 by o2, possibly in-place, or Chris@87: null on failure. This is the equivalent of the Python expression: Chris@87: o1 ^= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Returns the result of bitwise or of o1 and o2, possibly in-place, Chris@87: or null on failure. This is the equivalent of the Python Chris@87: expression: o1 |= o2. Chris@87: Chris@87: */ Chris@87: Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base); Chris@87: Chris@87: /* Chris@87: Returns the integer n converted to a string with a base, with a base Chris@87: marker of 0b, 0o or 0x prefixed if applicable. Chris@87: If n is not an int object, it is converted with PyNumber_Index first. Chris@87: */ Chris@87: Chris@87: Chris@87: /* Sequence protocol:*/ Chris@87: Chris@87: PyAPI_FUNC(int) PySequence_Check(PyObject *o); Chris@87: Chris@87: /* Chris@87: Return 1 if the object provides sequence protocol, and zero Chris@87: otherwise. Chris@87: Chris@87: This function always succeeds. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o); Chris@87: Chris@87: /* Chris@87: Return the size of sequence object o, or -1 on failure. Chris@87: Chris@87: */ Chris@87: Chris@87: /* For DLL compatibility */ Chris@87: #undef PySequence_Length Chris@87: PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o); Chris@87: #define PySequence_Length PySequence_Size Chris@87: Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Return the concatenation of o1 and o2 on success, and NULL on Chris@87: failure. This is the equivalent of the Python Chris@87: expression: o1+o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count); Chris@87: Chris@87: /* Chris@87: Return the result of repeating sequence object o count times, Chris@87: or NULL on failure. This is the equivalent of the Python Chris@87: expression: o1*count. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i); Chris@87: Chris@87: /* Chris@87: Return the ith element of o, or NULL on failure. This is the Chris@87: equivalent of the Python expression: o[i]. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2); Chris@87: Chris@87: /* Chris@87: Return the slice of sequence object o between i1 and i2, or Chris@87: NULL on failure. This is the equivalent of the Python Chris@87: expression: o[i1:i2]. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v); Chris@87: Chris@87: /* Chris@87: Assign object v to the ith element of o. Returns Chris@87: -1 on failure. This is the equivalent of the Python Chris@87: statement: o[i]=v. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i); Chris@87: Chris@87: /* Chris@87: Delete the ith element of object v. Returns Chris@87: -1 on failure. This is the equivalent of the Python Chris@87: statement: del o[i]. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, Chris@87: PyObject *v); Chris@87: Chris@87: /* Chris@87: Assign the sequence object, v, to the slice in sequence Chris@87: object, o, from i1 to i2. Returns -1 on failure. This is the Chris@87: equivalent of the Python statement: o[i1:i2]=v. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2); Chris@87: Chris@87: /* Chris@87: Delete the slice in sequence object, o, from i1 to i2. Chris@87: Returns -1 on failure. This is the equivalent of the Python Chris@87: statement: del o[i1:i2]. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the sequence, o, as a tuple on success, and NULL on failure. Chris@87: This is equivalent to the Python expression: tuple(o) Chris@87: */ Chris@87: Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o); Chris@87: /* Chris@87: Returns the sequence, o, as a list on success, and NULL on failure. Chris@87: This is equivalent to the Python expression: list(o) Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m); Chris@87: /* Chris@87: Return the sequence, o, as a list, unless it's already a Chris@87: tuple or list. Use PySequence_Fast_GET_ITEM to access the Chris@87: members of this list, and PySequence_Fast_GET_SIZE to get its length. Chris@87: Chris@87: Returns NULL on failure. If the object does not support iteration, Chris@87: raises a TypeError exception with m as the message text. Chris@87: */ Chris@87: Chris@87: #define PySequence_Fast_GET_SIZE(o) \ Chris@87: (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o)) Chris@87: /* Chris@87: Return the size of o, assuming that o was returned by Chris@87: PySequence_Fast and is not NULL. Chris@87: */ Chris@87: Chris@87: #define PySequence_Fast_GET_ITEM(o, i)\ Chris@87: (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i)) Chris@87: /* Chris@87: Return the ith element of o, assuming that o was returned by Chris@87: PySequence_Fast, and that i is within bounds. Chris@87: */ Chris@87: Chris@87: #define PySequence_ITEM(o, i)\ Chris@87: ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) ) Chris@87: /* Assume tp_as_sequence and sq_item exist and that i does not Chris@87: need to be corrected for a negative index Chris@87: */ Chris@87: Chris@87: #define PySequence_Fast_ITEMS(sf) \ Chris@87: (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \ Chris@87: : ((PyTupleObject *)(sf))->ob_item) Chris@87: /* Return a pointer to the underlying item array for Chris@87: an object retured by PySequence_Fast */ Chris@87: Chris@87: PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value); Chris@87: Chris@87: /* Chris@87: Return the number of occurrences on value on o, that is, Chris@87: return the number of keys for which o[key]==value. On Chris@87: failure, return -1. This is equivalent to the Python Chris@87: expression: o.count(value). Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob); Chris@87: /* Chris@87: Return -1 if error; 1 if ob in seq; 0 if ob not in seq. Chris@87: Use __contains__ if possible, else _PySequence_IterSearch(). Chris@87: */ Chris@87: Chris@87: #define PY_ITERSEARCH_COUNT 1 Chris@87: #define PY_ITERSEARCH_INDEX 2 Chris@87: #define PY_ITERSEARCH_CONTAINS 3 Chris@87: PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq, Chris@87: PyObject *obj, int operation); Chris@87: /* Chris@87: Iterate over seq. Result depends on the operation: Chris@87: PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if Chris@87: error. Chris@87: PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of Chris@87: obj in seq; set ValueError and return -1 if none found; Chris@87: also return -1 on error. Chris@87: PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on Chris@87: error. Chris@87: */ Chris@87: Chris@87: /* For DLL-level backwards compatibility */ Chris@87: #undef PySequence_In Chris@87: PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value); Chris@87: Chris@87: /* For source-level backwards compatibility */ Chris@87: #define PySequence_In PySequence_Contains Chris@87: Chris@87: /* Chris@87: Determine if o contains value. If an item in o is equal to Chris@87: X, return 1, otherwise return 0. On error, return -1. This Chris@87: is equivalent to the Python expression: value in o. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value); Chris@87: Chris@87: /* Chris@87: Return the first index for which o[i]=value. On error, Chris@87: return -1. This is equivalent to the Python Chris@87: expression: o.index(value). Chris@87: */ Chris@87: Chris@87: /* In-place versions of some of the above Sequence functions. */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2); Chris@87: Chris@87: /* Chris@87: Append o2 to o1, in-place when possible. Return the resulting Chris@87: object, which could be o1, or NULL on failure. This is the Chris@87: equivalent of the Python expression: o1 += o2. Chris@87: Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count); Chris@87: Chris@87: /* Chris@87: Repeat o1 by count, in-place when possible. Return the resulting Chris@87: object, which could be o1, or NULL on failure. This is the Chris@87: equivalent of the Python expression: o1 *= count. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Mapping protocol:*/ Chris@87: Chris@87: PyAPI_FUNC(int) PyMapping_Check(PyObject *o); Chris@87: Chris@87: /* Chris@87: Return 1 if the object provides mapping protocol, and zero Chris@87: otherwise. Chris@87: Chris@87: This function always succeeds. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o); Chris@87: Chris@87: /* Chris@87: Returns the number of keys in object o on success, and -1 on Chris@87: failure. For objects that do not provide sequence protocol, Chris@87: this is equivalent to the Python expression: len(o). Chris@87: */ Chris@87: Chris@87: /* For DLL compatibility */ Chris@87: #undef PyMapping_Length Chris@87: PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o); Chris@87: #define PyMapping_Length PyMapping_Size Chris@87: Chris@87: Chris@87: /* implemented as a macro: Chris@87: Chris@87: int PyMapping_DelItemString(PyObject *o, char *key); Chris@87: Chris@87: Remove the mapping for object, key, from the object *o. Chris@87: Returns -1 on failure. This is equivalent to Chris@87: the Python statement: del o[key]. Chris@87: */ Chris@87: #define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K)) Chris@87: Chris@87: /* implemented as a macro: Chris@87: Chris@87: int PyMapping_DelItem(PyObject *o, PyObject *key); Chris@87: Chris@87: Remove the mapping for object, key, from the object *o. Chris@87: Returns -1 on failure. This is equivalent to Chris@87: the Python statement: del o[key]. Chris@87: */ Chris@87: #define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K)) Chris@87: Chris@87: PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, char *key); Chris@87: Chris@87: /* Chris@87: On success, return 1 if the mapping object has the key, key, Chris@87: and 0 otherwise. This is equivalent to the Python expression: Chris@87: o.has_key(key). Chris@87: Chris@87: This function always succeeds. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key); Chris@87: Chris@87: /* Chris@87: Return 1 if the mapping object has the key, key, Chris@87: and 0 otherwise. This is equivalent to the Python expression: Chris@87: o.has_key(key). Chris@87: Chris@87: This function always succeeds. Chris@87: Chris@87: */ Chris@87: Chris@87: /* Implemented as macro: Chris@87: Chris@87: PyObject *PyMapping_Keys(PyObject *o); Chris@87: Chris@87: On success, return a list of the keys in object o. On Chris@87: failure, return NULL. This is equivalent to the Python Chris@87: expression: o.keys(). Chris@87: */ Chris@87: #define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL) Chris@87: Chris@87: /* Implemented as macro: Chris@87: Chris@87: PyObject *PyMapping_Values(PyObject *o); Chris@87: Chris@87: On success, return a list of the values in object o. On Chris@87: failure, return NULL. This is equivalent to the Python Chris@87: expression: o.values(). Chris@87: */ Chris@87: #define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL) Chris@87: Chris@87: /* Implemented as macro: Chris@87: Chris@87: PyObject *PyMapping_Items(PyObject *o); Chris@87: Chris@87: On success, return a list of the items in object o, where Chris@87: each item is a tuple containing a key-value pair. On Chris@87: failure, return NULL. This is equivalent to the Python Chris@87: expression: o.items(). Chris@87: Chris@87: */ Chris@87: #define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL) Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o, char *key); Chris@87: Chris@87: /* Chris@87: Return element of o corresponding to the object, key, or NULL Chris@87: on failure. This is the equivalent of the Python expression: Chris@87: o[key]. Chris@87: */ Chris@87: Chris@87: PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, char *key, Chris@87: PyObject *value); Chris@87: Chris@87: /* Chris@87: Map the object, key, to the value, v. Returns Chris@87: -1 on failure. This is the equivalent of the Python Chris@87: statement: o[key]=v. Chris@87: */ Chris@87: Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass); Chris@87: /* isinstance(object, typeorclass) */ Chris@87: Chris@87: PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass); Chris@87: /* issubclass(object, typeorclass) */ Chris@87: Chris@87: Chris@87: PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls); Chris@87: Chris@87: PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls); Chris@87: Chris@87: Chris@87: /* For internal use by buffer API functions */ Chris@87: PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index, Chris@87: const Py_ssize_t *shape); Chris@87: PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index, Chris@87: const Py_ssize_t *shape); Chris@87: Chris@87: Chris@87: #ifdef __cplusplus Chris@87: } Chris@87: #endif Chris@87: #endif /* Py_ABSTRACTOBJECT_H */