Académique Documents
Professionnel Documents
Culture Documents
#define Py_ABSTRACTOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef PY_SSIZE_T_CLEAN
#define PyObject_CallFunction _PyObject_CallFunction_SizeT
#define PyObject_CallMethod _PyObject_CallMethod_SizeT
#ifndef Py_LIMITED_API
#define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
#endif /* !Py_LIMITED_API */
#endif
/*
PROPOSAL: A Generic Python Object Interface for Python C Modules
Problem
if(is_tupleobject(o))
e=gettupleitem(o,i)
else if(is_listitem(o))
e=getlistitem(o,i)
The persistent programmer may peruse object.h and find that the
_typeobject structure provides a means of invoking up to (currently
about) 41 special operators. So, for example, a routine can get an
item from any object that provides sequence behavior. However, to
use this mechanism, the programmer must make their code dependent on
the current Python implementation.
Proposal
- "Very high level layer": two or three functions that let you exec or
eval arbitrary Python code given as a string in a module whose name is
given, passing C values in and getting C values out using
mkvalue/getargs style format strings. This does not require the user
to declare any variables of type "PyObject *". This should be enough
to write a simple application that gets Python code from the user,
execs it, and returns the output or errors. (Error handling must also
be part of this API.)
Memory Management
Note that the behavior mentioned here is different from the current
behavior for some objects (e.g. lists and tuples) when certain
type-specific routines are called directly (e.g. setlistitem). The
proposed abstraction layer will provide a consistent memory
management interface, correcting for inconsistent behavior for some
built-in types.
Protocols
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/* Object Protocol: */
/* Implemented elsewhere:
*/
/* Implemented elsewhere:
*/
/* Implemented elsewhere:
PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
*/
/* Implemented elsewhere:
*/
/* Implemented elsewhere:
*/
/* Implemented elsewhere:
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
*/
/* Implemented elsewhere:
*/
/* implemented as a macro:
*/
#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
/* implemented as a macro:
int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
*/
#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
/* Implemented elsewhere:
*/
/* Implemented elsewhere:
*/
/* Declared elsewhere
/*
Call a callable Python object, callable_object, with
arguments and keywords arguments. The 'args' argument can not be
NULL.
*/
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
PyObject **stack,
Py_ssize_t nargs);
kwnames must only contains str strings, no subclass, and all keys must
be unique. kwnames is not checked, usually these checks are done before or later
calling _PyStack_AsDict(). For example, _PyArg_ParseStack() raises an
error if a key is not a string. */
PyAPI_FUNC(PyObject *) _PyStack_AsDict(
PyObject **values,
PyObject *kwnames);
/* Convert (args, nargs, kwargs: dict) into (stack, nargs, kwnames: tuple).
Write the new stack into *p_stack. If *p_stack is differen than args, it
must be released by PyMem_Free().
The type of keyword keys is not checked, these checks should be done
later (ex: _PyArg_ParseStackAndKeywords). */
PyAPI_FUNC(int) _PyStack_UnpackDict(
PyObject **args,
Py_ssize_t nargs,
PyObject *kwargs,
PyObject ***p_stack,
PyObject **p_kwnames);
/* Call the callable object func with the "fast call" calling convention:
args is a C array for positional arguments (nargs is the number of
positional arguments), kwargs is a dictionary for keyword arguments.
/* Call the callable object func with the "fast call" calling convention:
args is a C array for positional arguments followed by values of
keyword arguments. Keys of keyword arguments are stored as a tuple
of strings in kwnames. nargs is the number of positional parameters at
the beginning of stack. The size of kwnames gives the number of keyword
values in the stack after positional arguments.
kwnames must only contains str strings, no subclass, and all keys must
be unique.
#define _PyObject_CallNoArg(func) \
_PyObject_FastCall((func), NULL, 0)
/*
Call a callable Python object, callable_object, with
arguments given by the tuple, args. If no arguments are
needed, then args may be NULL. Returns the result of the
call on success, or NULL on failure. This is the equivalent
of the Python expression: o(*args).
*/
/*
Call a callable Python object, callable_object, with a
variable number of C arguments. The C arguments are described
using a mkvalue-style format string. The format may be NULL,
indicating that no arguments are provided. Returns the
result of the call on success, or NULL on failure. This is
the equivalent of the Python expression: o(*args).
*/
PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *o,
const char *method,
const char *format, ...);
/*
Call the method named m of object o with a variable number of
C arguments. The C arguments are described by a mkvalue
format string. The format may be NULL, indicating that no
arguments are provided. Returns the result of the call on
success, or NULL on failure. This is the equivalent of the
Python expression: o.method(args).
*/
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *o,
_Py_Identifier *method,
const char *format, ...);
/*
Like PyObject_CallMethod, but expect a _Py_Identifier* as the
method name.
*/
#endif /* !Py_LIMITED_API */
/*
Call a callable Python object, callable_object, with a
variable number of C arguments. The C arguments are provided
as PyObject * values, terminated by a NULL. Returns the
result of the call on success, or NULL on failure. This is
the equivalent of the Python expression: o(*args).
*/
/*
Call the method named m of object o with a variable number of
C arguments. The C arguments are provided as PyObject *
values, terminated by NULL. Returns the result of the call
on success, or NULL on failure. This is the equivalent of
the Python expression: o.method(args).
*/
/* Implemented elsewhere:
/* Implemented elsewhere:
/* Implemented elsewhere:
/*
On success, returns a type object corresponding to the object
type of object o. On failure, returns NULL. This is
equivalent to the Python expression: type(o).
*/
/*
Return the size of object o. If the object, o, provides
both sequence and mapping protocols, the sequence size is
returned. On error, -1 is returned. This is the equivalent
to the Python expression: len(o).
*/
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
#endif
/*
Guess the size of object o using len(o) or o.__length_hint__().
If neither of those return a non-negative value, then return the
default value. If one of the calls fails, this function returns -1.
*/
/*
Return element of o corresponding to the object, key, or NULL
on failure. This is the equivalent of the Python expression:
o[key].
*/
/*
Map the object key to the value v. Raise an exception and return -1
on failure; return 0 on success. This is the equivalent of the Python
statement o[key]=v.
*/
/*
Remove the mapping for object, key, from the object *o.
Returns -1 on failure. This is equivalent to
the Python statement: del o[key].
*/
/*
Delete the mapping for key from *o. Returns -1 on failure.
This is the equivalent of the Python statement: del o[key].
*/
/* old buffer API
FIXME: usage of these should all be replaced in Python itself
but for backwards compatibility we will implement them.
Their usage without a corresponding "unlock" mechanism
may create issues (but they would already be there). */
/*
Takes an arbitrary object which must support the (character,
single segment) buffer interface and returns a pointer to a
read-only memory location useable as character based input
for subsequent processing.
/*
Checks whether an arbitrary object supports the (character,
single segment) buffer interface. Returns 1 on success, 0
on failure.
*/
/*
Same as PyObject_AsCharBuffer() except that this API expects
(readable, single segment) buffer interface and returns a
pointer to a read-only memory location which can contain
arbitrary data.
/*
Takes an arbitrary object which must support the (writable,
single segment) buffer interface and returns a pointer to a
writable memory location in buffer of size buffer_len.
#ifndef Py_LIMITED_API
#define PyObject_CheckBuffer(obj) \
(((obj)->ob_type->tp_as_buffer != NULL) && \
((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
/* Get the memory area pointed to by the indices for the buffer given.
Note that view->ndim is the assumed size of indices
*/
/* Implementation in memoryobject.c */
PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
Py_ssize_t len, char order);
PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
Py_ssize_t len, char order);
*/
/* Copy the data from the src buffer to the buffer of destination
*/
/* Iterators */
PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
/* Takes an object and returns an iterator for it.
This is typically a new iterator but if the argument
is an iterator, this returns itself. */
#define PyIter_Check(obj) \
((obj)->ob_type->tp_iternext != NULL && \
(obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
/* Number Protocol:*/
/*
Returns 1 if the object, o, provides numeric protocols, and
false otherwise.
/*
Returns the result of adding o1 and o2, or null on failure.
This is the equivalent of the Python expression: o1+o2.
*/
/*
Returns the result of subtracting o2 from o1, or null on
failure. This is the equivalent of the Python expression:
o1-o2.
*/
/*
Returns the result of multiplying o1 and o2, or null on
failure. This is the equivalent of the Python expression:
o1*o2.
*/
/*
This is the equivalent of the Python expression: o1 @ o2.
*/
#endif
/*
Returns the result of dividing o1 by o2 giving an integral result,
or null on failure.
This is the equivalent of the Python expression: o1//o2.
*/
/*
Returns the result of dividing o1 by o2 giving a float result,
or null on failure.
This is the equivalent of the Python expression: o1/o2.
*/
/*
Returns the remainder of dividing o1 by o2, or null on
failure. This is the equivalent of the Python expression:
o1%o2.
*/
/*
See the built-in function divmod. Returns NULL on failure.
This is the equivalent of the Python expression:
divmod(o1,o2).
*/
/*
See the built-in function pow. Returns NULL on failure.
This is the equivalent of the Python expression:
pow(o1,o2,o3), where o3 is optional.
*/
/*
Returns the negation of o on success, or null on failure.
This is the equivalent of the Python expression: -o.
*/
/*
Returns the (what?) of o on success, or NULL on failure.
This is the equivalent of the Python expression: +o.
*/
/*
Returns the absolute value of o, or null on failure. This is
the equivalent of the Python expression: abs(o).
*/
/*
Returns the bitwise negation of o on success, or NULL on
failure. This is the equivalent of the Python expression:
~o.
*/
PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
/*
Returns the result of left shifting o1 by o2 on success, or
NULL on failure. This is the equivalent of the Python
expression: o1 << o2.
*/
/*
Returns the result of right shifting o1 by o2 on success, or
NULL on failure. This is the equivalent of the Python
expression: o1 >> o2.
*/
/*
Returns the result of bitwise and of o1 and o2 on success, or
NULL on failure. This is the equivalent of the Python
expression: o1&o2.
*/
/*
Returns the bitwise exclusive or of o1 by o2 on success, or
NULL on failure. This is the equivalent of the Python
expression: o1^o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
/*
Returns the result of bitwise or on o1 and o2 on success, or
NULL on failure. This is the equivalent of the Python
expression: o1|o2.
*/
#define PyIndex_Check(obj) \
((obj)->ob_type->tp_as_number != NULL && \
(obj)->ob_type->tp_as_number->nb_index != NULL)
/*
Returns the object converted to a Python int
or NULL with an error raised on failure.
*/
/*
Returns the object converted to Py_ssize_t by going through
PyNumber_Index first. If an overflow error occurs while
converting the int to Py_ssize_t, then the second argument
is the error-type to return. If it is NULL, then the overflow error
is cleared and the value is clipped.
*/
/*
Returns the o converted to a float object on success, or NULL
on failure. This is the equivalent of the Python expression:
float(o).
*/
/*
Returns the result of adding o2 to o1, possibly in-place, or null
on failure. This is the equivalent of the Python expression:
o1 += o2.
*/
/*
Returns the result of subtracting o2 from o1, possibly in-place or
null on failure. This is the equivalent of the Python expression:
o1 -= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
/*
Returns the result of multiplying o1 by o2, possibly in-place, or
null on failure. This is the equivalent of the Python expression:
o1 *= o2.
*/
/*
This is the equivalent of the Python expression: o1 @= o2.
*/
#endif
/*
Returns the result of dividing o1 by o2 giving an integral result,
possibly in-place, or null on failure.
This is the equivalent of the Python expression:
o1 /= o2.
*/
/*
Returns the result of dividing o1 by o2 giving a float result,
possibly in-place, or null on failure.
This is the equivalent of the Python expression:
o1 /= o2.
*/
/*
Returns the remainder of dividing o1 by o2, possibly in-place, or
null on failure. This is the equivalent of the Python expression:
o1 %= o2.
*/
/*
Returns the result of raising o1 to the power of o2, possibly
in-place, or null on failure. This is the equivalent of the Python
expression: o1 **= o2, or pow(o1, o2, o3) if o3 is present.
*/
/*
Returns the result of left shifting o1 by o2, possibly in-place, or
null on failure. This is the equivalent of the Python expression:
o1 <<= o2.
*/
/*
Returns the result of right shifting o1 by o2, possibly in-place or
null on failure. This is the equivalent of the Python expression:
o1 >>= o2.
*/
/*
Returns the result of bitwise and of o1 and o2, possibly in-place,
or null on failure. This is the equivalent of the Python
expression: o1 &= o2.
*/
/*
Returns the bitwise exclusive or of o1 by o2, possibly in-place, or
null on failure. This is the equivalent of the Python expression:
o1 ^= o2.
*/
/*
Returns the result of bitwise or of o1 and o2, possibly in-place,
or null on failure. This is the equivalent of the Python
expression: o1 |= o2.
*/
/*
Returns the integer n converted to a string with a base, with a base
marker of 0b, 0o or 0x prefixed if applicable.
If n is not an int object, it is converted with PyNumber_Index first.
*/
/* Sequence protocol:*/
/*
Return 1 if the object provides sequence protocol, and zero
otherwise.
/*
Return the size of sequence object o, or -1 on failure.
*/
/*
Return the concatenation of o1 and o2 on success, and NULL on
failure. This is the equivalent of the Python
expression: o1+o2.
*/
/*
Return the result of repeating sequence object o count times,
or NULL on failure. This is the equivalent of the Python
expression: o1*count.
*/
/*
Return the ith element of o, or NULL on failure. This is the
equivalent of the Python expression: o[i].
*/
/*
Return the slice of sequence object o between i1 and i2, or
NULL on failure. This is the equivalent of the Python
expression: o[i1:i2].
*/
/*
Assign object v to the ith element of o. Raise an exception and return
-1 on failure; return 0 on success. This is the equivalent of the
Python statement o[i]=v.
*/
/*
Delete the ith element of object v. Returns
-1 on failure. This is the equivalent of the Python
statement: del o[i].
*/
/*
Assign the sequence object, v, to the slice in sequence
object, o, from i1 to i2. Returns -1 on failure. This is the
equivalent of the Python statement: o[i1:i2]=v.
*/
/*
Delete the slice in sequence object, o, from i1 to i2.
Returns -1 on failure. This is the equivalent of the Python
statement: del o[i1:i2].
*/
/*
Returns the sequence, o, as a tuple on success, and NULL on failure.
This is equivalent to the Python expression: tuple(o)
*/
#define PySequence_Fast_GET_SIZE(o) \
(PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
/*
Return the size of o, assuming that o was returned by
PySequence_Fast and is not NULL.
*/
#define PySequence_Fast_ITEMS(sf) \
(PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
: ((PyTupleObject *)(sf))->ob_item)
/* Return a pointer to the underlying item array for
an object retured by PySequence_Fast */
/*
Return the number of occurrences on value on o, that is,
return the number of keys for which o[key]==value. On
failure, return -1. This is equivalent to the Python
expression: o.count(value).
*/
#ifndef Py_LIMITED_API
#define PY_ITERSEARCH_COUNT 1
#define PY_ITERSEARCH_INDEX 2
#define PY_ITERSEARCH_CONTAINS 3
PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
PyObject *obj, int operation);
#endif
/*
Iterate over seq. Result depends on the operation:
PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
error.
PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
obj in seq; set ValueError and return -1 if none found;
also return -1 on error.
PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
error.
*/
/*
Determine if o contains value. If an item in o is equal to
X, return 1, otherwise return 0. On error, return -1. This
is equivalent to the Python expression: value in o.
*/
/*
Return the first index for which o[i]=value. On error,
return -1. This is equivalent to the Python
expression: o.index(value).
*/
/*
Append o2 to o1, in-place when possible. Return the resulting
object, which could be o1, or NULL on failure. This is the
equivalent of the Python expression: o1 += o2.
*/
/*
Repeat o1 by count, in-place when possible. Return the resulting
object, which could be o1, or NULL on failure. This is the
equivalent of the Python expression: o1 *= count.
*/
/* Mapping protocol:*/
/*
Return 1 if the object provides mapping protocol, and zero
otherwise.
This function always succeeds.
*/
/*
Returns the number of keys in object o on success, and -1 on
failure. For objects that do not provide sequence protocol,
this is equivalent to the Python expression: len(o).
*/
/* implemented as a macro:
Remove the mapping for object, key, from the object *o.
Returns -1 on failure. This is equivalent to
the Python statement: del o[key].
*/
#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
/* implemented as a macro:
/*
On success, return 1 if the mapping object has the key, key,
and 0 otherwise. This is equivalent to the Python expression:
key in o.
/*
Return 1 if the mapping object has the key, key,
and 0 otherwise. This is equivalent to the Python expression:
key in o.
*/
/*
On success, return a list or tuple of the keys in object o.
On failure, return NULL.
*/
/*
On success, return a list or tuple of the values in object o.
On failure, return NULL.
*/
/*
On success, return a list or tuple of the items in object o,
where each item is a tuple containing a key-value pair.
On failure, return NULL.
*/
/*
Return element of o corresponding to the object, key, or NULL
on failure. This is the equivalent of the Python expression:
o[key].
*/
/*
Map the object, key, to the value, v. Returns
-1 on failure. This is the equivalent of the Python
statement: o[key]=v.
*/
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
#ifdef __cplusplus
}
#endif
#endif /* Py_ABSTRACTOBJECT_H */