#ifndef Py_ABSTRACTOBJECT_H
#define Py_ABSTRACTOBJECT_H
#define PyObject_CallFunction _PyObject_CallFunction_SizeT
#define PyObject_CallMethod _PyObject_CallMethod_SizeT
#define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
#endif /* !Py_LIMITED_API */
/* Abstract Object Interface (many thanks to Jim Fulton) */
PROPOSAL: A Generic Python Object Interface for Python C Modules
Python modules written in C that must access Python objects must do
so through routines whose interfaces are described by a set of
include files. Unfortunately, these routines vary according to the
object accessed. To use these routines, the C programmer must check
the type of the object being used and must call a routine based on
the object type. For example, to access an element of a sequence,
the programmer must determine whether the sequence is a list or a
If the programmer wants to get an item from another type of object
that provides sequence behavior, there is no clear way to do it
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.
Also, certain semantics, especially memory management semantics, may
differ by the type of object being used. Unfortunately, these
semantics are not clearly described in the current include files.
An abstract interface providing more consistent semantics is needed.
I propose the creation of a standard interface (with an associated
library of routines and/or macros) for generically obtaining the
services of Python objects. This proposal can be viewed as one
components of a Python C interface consisting of several components.
From the viewpoint of C access to Python services, we have (as
suggested by Guido in off-line discussions):
- "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
- "Abstract objects layer": which is the subject of this proposal.
It has many functions operating on objects, and lest you do many
things from C that you can also write in Python, without going
through the Python parser.
- "Concrete objects layer": This is the public type-dependent
interface provided by the standard built-in types, such as floats,
strings, and lists. This interface exists and is currently
documented by the collection of include files provided with the
From the point of view of Python accessing services provided by C
- "Python module interface": this interface consist of the basic
routines used to define modules and their members. Most of the
current extensions-writing guide deals with this interface.
- "Built-in object interface": this is the interface that a new
built-in type must provide and the mechanisms and rules that a
developer of a new built-in type must use and follow.
This proposal is a "first-cut" that is intended to spur
discussion. See especially the lists of notes.
The Python C object interface will provide four protocols: object,
numeric, sequence, and mapping. Each protocol consists of a
collection of related operations. If an operation that is not
provided by a particular type is invoked, then a standard exception,
NotImplementedError is raised with an operation name as an argument.
In addition, for convenience this interface defines a set of
constructors for building objects of built-in types. This is needed
so new objects can be returned from C functions that otherwise treat
For all of the functions described in this proposal, if a function
retains a reference to a Python object passed as an argument, then the
function will increase the reference count of the object. It is
unnecessary for the caller to increase the reference count of an
argument in anticipation of the object's retention.
All Python objects returned from functions should be treated as new
objects. Functions that return objects assume that the caller will
retain a reference and the reference count of the object has already
been incremented to account for this fact. A caller that does not
retain a reference to an object that is returned from a function
must decrement the reference count of the object (using
DECREF(object)) to prevent memory leaks.
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
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/* Implemented elsewhere:
int PyObject_Print(PyObject *o, FILE *fp, int flags);
Print an object, o, on file, fp. Returns -1 on
error. The flags argument is used to enable certain printing
options. The only option currently supported is Py_Print_RAW.
(What should be said about Py_Print_RAW?)
/* Implemented elsewhere:
int PyObject_HasAttrString(PyObject *o, const char *attr_name);
Returns 1 if o has the attribute attr_name, and 0 otherwise.
This is equivalent to the Python expression:
This function always succeeds.
/* Implemented elsewhere:
PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
Retrieve an attributed named attr_name form object o.
Returns the attribute value on success, or NULL on failure.
This is the equivalent of the Python expression: o.attr_name.
/* Implemented elsewhere:
int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
Returns 1 if o has the attribute attr_name, and 0 otherwise.
This is equivalent to the Python expression:
This function always succeeds.
/* Implemented elsewhere:
PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
Retrieve an attributed named attr_name form object o.
Returns the attribute value on success, or NULL on failure.
This is the equivalent of the Python expression: o.attr_name.
/* Implemented elsewhere:
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
Set the value of the attribute named attr_name, for object o,
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.attr_name=v.
/* Implemented elsewhere:
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
Set the value of the attribute named attr_name, for object o,
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.attr_name=v.
/* implemented as a macro:
int PyObject_DelAttrString(PyObject *o, const char *attr_name);
Delete attribute named attr_name, for object o. Returns
-1 on failure. This is the equivalent of the Python
statement: del o.attr_name.
#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
/* implemented as a macro:
int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
Delete attribute named attr_name, for object o. Returns -1
on failure. This is the equivalent of the Python
statement: del o.attr_name.
#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
/* Implemented elsewhere:
PyObject *PyObject_Repr(PyObject *o);
Compute the string representation of object, o. Returns the
string representation on success, NULL on failure. This is
the equivalent of the Python expression: repr(o).
Called by the repr() built-in function.
/* Implemented elsewhere:
PyObject *PyObject_Str(PyObject *o);
Compute the string representation of object, o. Returns the
string representation on success, NULL on failure. This is
the equivalent of the Python expression: str(o).)
Called by the str() and print() built-in functions.
PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
Determine if the object, o, is callable. Return 1 if the
object is callable and 0 otherwise.
This function always succeeds.
PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object,
PyObject *args, PyObject *kwargs);
Call a callable Python object, callable_object, with
arguments and keywords arguments. The 'args' argument can not be
PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
/* Convert keyword arguments from the (stack, kwnames) format to a Python
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(
/* Convert (args, nargs, kwargs: dict) into (stack, nargs, kwnames: tuple).
Return 0 on success, raise an exception and return -1 on error.
Write the new stack into *p_stack. If *p_stack is differen than args, it
must be released by PyMem_Free().
The stack uses borrowed references.
The type of keyword keys is not checked, these checks should be done
later (ex: _PyArg_ParseStackAndKeywords). */
PyAPI_FUNC(int) _PyStack_UnpackDict(
/* 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.
If nargs is equal to zero, args can be NULL. kwargs can be NULL.
nargs must be greater or equal to zero.
Return the result on success. Raise an exception on return NULL on
PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(PyObject *func,
PyObject **args, Py_ssize_t nargs,
/* 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
If nargs is equal to zero and there is no keyword argument (kwnames is
NULL or its size is zero), args can be NULL.
Return the result on success. Raise an exception and return NULL on
PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords
#define _PyObject_FastCall(func, args, nargs) \
_PyObject_FastCallDict((func), (args), (nargs), NULL)
#define _PyObject_CallNoArg(func) \
_PyObject_FastCall((func), NULL, 0)
#define _PyObject_CallArg1(func, arg) \
_PyObject_FastCall((func), &(arg), 1)
PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(PyObject *func,
PyObject *obj, PyObject *args,
PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *func,
#endif /* Py_LIMITED_API */
PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable_object,
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).
PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable_object,
const char *format, ...);
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 *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).
PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *o,
const char *format, ...);
Like PyObject_CallMethod, but expect a _Py_Identifier* as the
#endif /* !Py_LIMITED_API */
PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *o,
PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *o,
#endif /* !Py_LIMITED_API */
PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
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).
PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(PyObject *o,
PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(PyObject *o,
struct _Py_Identifier *method,
#endif /* !Py_LIMITED_API */
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:
long PyObject_Hash(PyObject *o);
Compute and return the hash, hash_value, of an object, o. On
failure, return -1. This is the equivalent of the Python
/* Implemented elsewhere:
int PyObject_IsTrue(PyObject *o);
Returns 1 if the object, o, is considered to be true, 0 if o is
considered to be false and -1 on failure. This is equivalent to the
Python expression: not not o
/* Implemented elsewhere:
int PyObject_Not(PyObject *o);
Returns 0 if the object, o, is considered to be true, 1 if o is
considered to be false and -1 on failure. This is equivalent to the
PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
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).
PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *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).
/* For DLL compatibility */