1 from cpython.ref cimport PyObject, PyTypeObject
2 from libc.stdio cimport FILE
4 cdef extern from "Python.h":
6 #####################################################################
8 #####################################################################
9 int PyObject_Print(object o, FILE *fp, int flags) except -1
10 # Print an object o, on file fp. Returns -1 on error. The flags
11 # argument is used to enable certain printing options. The only
12 # option currently supported is Py_PRINT_RAW; if given, the str()
13 # of the object is written instead of the repr().
15 bint PyObject_HasAttrString(object o, char *attr_name)
16 # Returns 1 if o has the attribute attr_name, and 0
17 # otherwise. This is equivalent to the Python expression
18 # "hasattr(o, attr_name)". This function always succeeds.
20 object PyObject_GetAttrString(object o, char *attr_name)
21 # Return value: New reference. Retrieve an attribute named
22 # attr_name from object o. Returns the attribute value on success,
23 # or NULL on failure. This is the equivalent of the Python
24 # expression "o.attr_name".
26 bint PyObject_HasAttr(object o, object attr_name)
27 # Returns 1 if o has the attribute attr_name, and 0
28 # otherwise. This is equivalent to the Python expression
29 # "hasattr(o, attr_name)". This function always succeeds.
31 object PyObject_GetAttr(object o, object attr_name)
32 # Return value: New reference. Retrieve an attribute named
33 # attr_name from object o. Returns the attribute value on success,
34 # or NULL on failure. This is the equivalent of the Python
35 # expression "o.attr_name".
37 int PyObject_SetAttrString(object o, char *attr_name, object v) except -1
38 # Set the value of the attribute named attr_name, for object o, to
39 # the value v. Returns -1 on failure. This is the equivalent of
40 # the Python statement "o.attr_name = v".
42 int PyObject_SetAttr(object o, object attr_name, object v) except -1
43 # Set the value of the attribute named attr_name, for object o, to
44 # the value v. Returns -1 on failure. This is the equivalent of
45 # the Python statement "o.attr_name = v".
47 int PyObject_DelAttrString(object o, char *attr_name) except -1
48 # Delete attribute named attr_name, for object o. Returns -1 on
49 # failure. This is the equivalent of the Python statement: "del
52 int PyObject_DelAttr(object o, object attr_name) except -1
53 # Delete attribute named attr_name, for object o. Returns -1 on
54 # failure. This is the equivalent of the Python statement "del
57 int Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
59 object PyObject_RichCompare(object o1, object o2, int opid)
60 # Return value: New reference.
61 # Compare the values of o1 and o2 using the operation specified by
62 # opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
63 # Py_GE, corresponding to <, <=, ==, !=, >, or >=
64 # respectively. This is the equivalent of the Python expression
65 # "o1 op o2", where op is the operator corresponding to
66 # opid. Returns the value of the comparison on success, or NULL on
69 bint PyObject_RichCompareBool(object o1, object o2, int opid) except -1
70 # Compare the values of o1 and o2 using the operation specified by
71 # opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
72 # Py_GE, corresponding to <, <=, ==, !=, >, or >=
73 # respectively. Returns -1 on error, 0 if the result is false, 1
74 # otherwise. This is the equivalent of the Python expression "o1
75 # op o2", where op is the operator corresponding to opid.
77 int PyObject_Cmp(object o1, object o2, int *result) except -1
78 # Compare the values of o1 and o2 using a routine provided by o1,
79 # if one exists, otherwise with a routine provided by o2. The
80 # result of the comparison is returned in result. Returns -1 on
81 # failure. This is the equivalent of the Python statement "result
84 int PyObject_Compare(object o1, object o2) except *
85 # Compare the values of o1 and o2 using a routine provided by o1,
86 # if one exists, otherwise with a routine provided by o2. Returns
87 # the result of the comparison on success. On error, the value
88 # returned is undefined; use PyErr_Occurred() to detect an
89 # error. This is equivalent to the Python expression "cmp(o1,
92 object PyObject_Repr(object o)
93 # Return value: New reference.
94 # Compute a string representation of object o. Returns the string
95 # representation on success, NULL on failure. This is the
96 # equivalent of the Python expression "repr(o)". Called by the
97 # repr() built-in function and by reverse quotes.
99 object PyObject_Str(object o)
100 # Return value: New reference.
101 # Compute a string representation of object o. Returns the string
102 # representation on success, NULL on failure. This is the
103 # equivalent of the Python expression "str(o)". Called by the
104 # str() built-in function and by the print statement.
106 object PyObject_Unicode(object o)
107 # Return value: New reference.
108 # Compute a Unicode string representation of object o. Returns the
109 # Unicode string representation on success, NULL on failure. This
110 # is the equivalent of the Python expression "unicode(o)". Called
111 # by the unicode() built-in function.
113 bint PyObject_IsInstance(object inst, object cls) except -1
114 # Returns 1 if inst is an instance of the class cls or a subclass
115 # of cls, or 0 if not. On error, returns -1 and sets an
116 # exception. If cls is a type object rather than a class object,
117 # PyObject_IsInstance() returns 1 if inst is of type cls. If cls
118 # is a tuple, the check will be done against every entry in
119 # cls. The result will be 1 when at least one of the checks
120 # returns 1, otherwise it will be 0. If inst is not a class
121 # instance and cls is neither a type object, nor a class object,
122 # nor a tuple, inst must have a __class__ attribute -- the class
123 # relationship of the value of that attribute with cls will be
124 # used to determine the result of this function.
126 # Subclass determination is done in a fairly straightforward way,
127 # but includes a wrinkle that implementors of extensions to the
128 # class system may want to be aware of. If A and B are class
129 # objects, B is a subclass of A if it inherits from A either
130 # directly or indirectly. If either is not a class object, a more
131 # general mechanism is used to determine the class relationship of
132 # the two objects. When testing if B is a subclass of A, if A is
133 # B, PyObject_IsSubclass() returns true. If A and B are different
134 # objects, B's __bases__ attribute is searched in a depth-first
135 # fashion for A -- the presence of the __bases__ attribute is
136 # considered sufficient for this determination.
138 bint PyObject_IsSubclass(object derived, object cls) except -1
139 # Returns 1 if the class derived is identical to or derived from
140 # the class cls, otherwise returns 0. In case of an error, returns
141 # -1. If cls is a tuple, the check will be done against every
142 # entry in cls. The result will be 1 when at least one of the
143 # checks returns 1, otherwise it will be 0. If either derived or
144 # cls is not an actual class object (or tuple), this function uses
145 # the generic algorithm described above. New in version
146 # 2.1. Changed in version 2.3: Older versions of Python did not
147 # support a tuple as the second argument.
149 bint PyCallable_Check(object o)
150 # Determine if the object o is callable. Return 1 if the object is
151 # callable and 0 otherwise. This function always succeeds.
153 object PyObject_Call(object callable_object, object args, object kw)
154 # Return value: New reference.
155 # Call a callable Python object callable_object, with arguments
156 # given by the tuple args, and named arguments given by the
157 # dictionary kw. If no named arguments are needed, kw may be
158 # NULL. args must not be NULL, use an empty tuple if no arguments
159 # are needed. Returns the result of the call on success, or NULL
160 # on failure. This is the equivalent of the Python expression
161 # "apply(callable_object, args, kw)" or "callable_object(*args,
164 object PyObject_CallObject(object callable_object, object args)
165 # Return value: New reference.
166 # Call a callable Python object callable_object, with arguments
167 # given by the tuple args. If no arguments are needed, then args
168 # may be NULL. Returns the result of the call on success, or NULL
169 # on failure. This is the equivalent of the Python expression
170 # "apply(callable_object, args)" or "callable_object(*args)".
172 object PyObject_CallFunction(object callable, char *format, ...)
173 # Return value: New reference.
174 # Call a callable Python object callable, with a variable number
175 # of C arguments. The C arguments are described using a
176 # Py_BuildValue() style format string. The format may be NULL,
177 # indicating that no arguments are provided. Returns the result of
178 # the call on success, or NULL on failure. This is the equivalent
179 # of the Python expression "apply(callable, args)" or
180 # "callable(*args)". Note that if you only pass object args,
181 # PyObject_CallFunctionObjArgs is a faster alternative.
183 object PyObject_CallMethod(object o, char *method, char *format, ...)
184 # Return value: New reference.
185 # Call the method named method of object o with a variable number
186 # of C arguments. The C arguments are described by a
187 # Py_BuildValue() format string that should produce a tuple. The
188 # format may be NULL, indicating that no arguments are
189 # provided. Returns the result of the call on success, or NULL on
190 # failure. This is the equivalent of the Python expression
191 # "o.method(args)". Note that if you only pass object args,
192 # PyObject_CallMethodObjArgs is a faster alternative.
194 #object PyObject_CallFunctionObjArgs(object callable, ..., NULL)
195 object PyObject_CallFunctionObjArgs(object callable, ...)
196 # Return value: New reference.
197 # Call a callable Python object callable, with a variable number
198 # of PyObject* arguments. The arguments are provided as a variable
199 # number of parameters followed by NULL. Returns the result of the
200 # call on success, or NULL on failure.
202 #PyObject* PyObject_CallMethodObjArgs(object o, object name, ..., NULL)
203 object PyObject_CallMethodObjArgs(object o, object name, ...)
204 # Return value: New reference.
205 # Calls a method of the object o, where the name of the method is
206 # given as a Python string object in name. It is called with a
207 # variable number of PyObject* arguments. The arguments are
208 # provided as a variable number of parameters followed by
209 # NULL. Returns the result of the call on success, or NULL on
212 long PyObject_Hash(object o) except? -1
213 # Compute and return the hash value of an object o. On failure,
214 # return -1. This is the equivalent of the Python expression
217 bint PyObject_IsTrue(object o) except -1
218 # Returns 1 if the object o is considered to be true, and 0
219 # otherwise. This is equivalent to the Python expression "not not
220 # o". On failure, return -1.
222 bint PyObject_Not(object o) except -1
223 # Returns 0 if the object o is considered to be true, and 1
224 # otherwise. This is equivalent to the Python expression "not
225 # o". On failure, return -1.
227 object PyObject_Type(object o)
228 # Return value: New reference.
229 # When o is non-NULL, returns a type object corresponding to the
230 # object type of object o. On failure, raises SystemError and
231 # returns NULL. This is equivalent to the Python expression
232 # type(o). This function increments the reference count of the
233 # return value. There's really no reason to use this function
234 # instead of the common expression o->ob_type, which returns a
235 # pointer of type PyTypeObject*, except when the incremented
236 # reference count is needed.
238 bint PyObject_TypeCheck(object o, PyTypeObject *type)
239 # Return true if the object o is of type type or a subtype of
240 # type. Both parameters must be non-NULL.
242 Py_ssize_t PyObject_Length(object o) except -1
243 Py_ssize_t PyObject_Size(object o) except -1
244 # Return the length of object o. If the object o provides either
245 # the sequence and mapping protocols, the sequence length is
246 # returned. On error, -1 is returned. This is the equivalent to
247 # the Python expression "len(o)".
249 object PyObject_GetItem(object o, object key)
250 # Return value: New reference.
251 # Return element of o corresponding to the object key or NULL on
252 # failure. This is the equivalent of the Python expression
255 int PyObject_SetItem(object o, object key, object v) except -1
256 # Map the object key to the value v. Returns -1 on failure. This
257 # is the equivalent of the Python statement "o[key] = v".
259 int PyObject_DelItem(object o, object key) except -1
260 # Delete the mapping for key from o. Returns -1 on failure. This
261 # is the equivalent of the Python statement "del o[key]".
263 int PyObject_AsFileDescriptor(object o) except -1
264 # Derives a file-descriptor from a Python object. If the object is
265 # an integer or long integer, its value is returned. If not, the
266 # object's fileno() method is called if it exists; the method must
267 # return an integer or long integer, which is returned as the file
268 # descriptor value. Returns -1 on failure.
270 object PyObject_Dir(object o)
271 # Return value: New reference.
272 # This is equivalent to the Python expression "dir(o)", returning
273 # a (possibly empty) list of strings appropriate for the object
274 # argument, or NULL if there was an error. If the argument is
275 # NULL, this is like the Python "dir()", returning the names of
276 # the current locals; in this case, if no execution frame is
277 # active then NULL is returned but PyErr_Occurred() will return
280 object PyObject_GetIter(object o)
281 # Return value: New reference.
282 # This is equivalent to the Python expression "iter(o)". It
283 # returns a new iterator for the object argument, or the object
284 # itself if the object is already an iterator. Raises TypeError
285 # and returns NULL if the object cannot be iterated.
287 Py_ssize_t Py_SIZE(object o)
289 object PyObject_Format(object obj, object format_spec)
290 # Takes an arbitrary object and returns the result of calling
291 # obj.__format__(format_spec).