Cython has moved to github.

cython-devel

view Cython/Includes/python_object.pxd @ 1169:30b7a6c2c7c1

isinstance uses PyObject_TypeCheck when second argument is a type
author Robert Bradshaw <robertwb@math.washington.edu>
date Mon Sep 22 23:45:08 2008 -0700 (3 years ago)
parents c7b40af87362
children fd38efcd7236
line source
1 cdef extern from "Python.h":
2 ctypedef void PyObject
3 ctypedef void PyTypeObject
4 ctypedef struct FILE
6 #####################################################################
7 # 6.1 Object Protocol
8 #####################################################################
9 int PyObject_Print(object o, FILE *fp, int flags)
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)
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)
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)
48 # Delete attribute named attr_name, for object o. Returns -1 on
49 # failure. This is the equivalent of the Python statement: "del
50 # o.attr_name".
52 int PyObject_DelAttr(object o, object attr_name)
53 # Delete attribute named attr_name, for object o. Returns -1 on
54 # failure. This is the equivalent of the Python statement "del
55 # o.attr_name".
57 object PyObject_RichCompare(object o1, object o2, int opid)
58 # Return value: New reference.
59 # Compare the values of o1 and o2 using the operation specified by
60 # opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
61 # Py_GE, corresponding to <, <=, ==, !=, >, or >=
62 # respectively. This is the equivalent of the Python expression
63 # "o1 op o2", where op is the operator corresponding to
64 # opid. Returns the value of the comparison on success, or NULL on
65 # failure.
67 int PyObject_RichCompareBool(object o1, object o2, int opid)
68 # Compare the values of o1 and o2 using the operation specified by
69 # opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or
70 # Py_GE, corresponding to <, <=, ==, !=, >, or >=
71 # respectively. Returns -1 on error, 0 if the result is false, 1
72 # otherwise. This is the equivalent of the Python expression "o1
73 # op o2", where op is the operator corresponding to opid.
75 int PyObject_Cmp(object o1, object o2, int *result)
76 # Compare the values of o1 and o2 using a routine provided by o1,
77 # if one exists, otherwise with a routine provided by o2. The
78 # result of the comparison is returned in result. Returns -1 on
79 # failure. This is the equivalent of the Python statement "result
80 # = cmp(o1, o2)".
82 int PyObject_Compare(object o1, object o2)
83 # Compare the values of o1 and o2 using a routine provided by o1,
84 # if one exists, otherwise with a routine provided by o2. Returns
85 # the result of the comparison on success. On error, the value
86 # returned is undefined; use PyErr_Occurred() to detect an
87 # error. This is equivalent to the Python expression "cmp(o1,
88 # o2)".
90 object PyObject_Repr(object o)
91 # Return value: New reference.
92 # Compute a string representation of object o. Returns the string
93 # representation on success, NULL on failure. This is the
94 # equivalent of the Python expression "repr(o)". Called by the
95 # repr() built-in function and by reverse quotes.
97 object PyObject_Str(object o)
98 # Return value: New reference.
99 # Compute a string representation of object o. Returns the string
100 # representation on success, NULL on failure. This is the
101 # equivalent of the Python expression "str(o)". Called by the
102 # str() built-in function and by the print statement.
104 object PyObject_Unicode(object o)
105 # Return value: New reference.
106 # Compute a Unicode string representation of object o. Returns the
107 # Unicode string representation on success, NULL on failure. This
108 # is the equivalent of the Python expression "unicode(o)". Called
109 # by the unicode() built-in function.
111 bint PyObject_IsInstance(object inst, object cls)
112 # Returns 1 if inst is an instance of the class cls or a subclass
113 # of cls, or 0 if not. On error, returns -1 and sets an
114 # exception. If cls is a type object rather than a class object,
115 # PyObject_IsInstance() returns 1 if inst is of type cls. If cls
116 # is a tuple, the check will be done against every entry in
117 # cls. The result will be 1 when at least one of the checks
118 # returns 1, otherwise it will be 0. If inst is not a class
119 # instance and cls is neither a type object, nor a class object,
120 # nor a tuple, inst must have a __class__ attribute -- the class
121 # relationship of the value of that attribute with cls will be
122 # used to determine the result of this function.
124 # Subclass determination is done in a fairly straightforward way,
125 # but includes a wrinkle that implementors of extensions to the
126 # class system may want to be aware of. If A and B are class
127 # objects, B is a subclass of A if it inherits from A either
128 # directly or indirectly. If either is not a class object, a more
129 # general mechanism is used to determine the class relationship of
130 # the two objects. When testing if B is a subclass of A, if A is
131 # B, PyObject_IsSubclass() returns true. If A and B are different
132 # objects, B's __bases__ attribute is searched in a depth-first
133 # fashion for A -- the presence of the __bases__ attribute is
134 # considered sufficient for this determination.
136 bint PyObject_IsSubclass(object derived, object cls)
137 # Returns 1 if the class derived is identical to or derived from
138 # the class cls, otherwise returns 0. In case of an error, returns
139 # -1. If cls is a tuple, the check will be done against every
140 # entry in cls. The result will be 1 when at least one of the
141 # checks returns 1, otherwise it will be 0. If either derived or
142 # cls is not an actual class object (or tuple), this function uses
143 # the generic algorithm described above. New in version
144 # 2.1. Changed in version 2.3: Older versions of Python did not
145 # support a tuple as the second argument.
147 bint PyCallable_Check(object o)
148 # Determine if the object o is callable. Return 1 if the object is
149 # callable and 0 otherwise. This function always succeeds.
151 object PyObject_Call(object callable_object, object args, object kw)
152 # Return value: New reference.
153 # Call a callable Python object callable_object, with arguments
154 # given by the tuple args, and named arguments given by the
155 # dictionary kw. If no named arguments are needed, kw may be
156 # NULL. args must not be NULL, use an empty tuple if no arguments
157 # are needed. Returns the result of the call on success, or NULL
158 # on failure. This is the equivalent of the Python expression
159 # "apply(callable_object, args, kw)" or "callable_object(*args,
160 # **kw)".
162 object PyObject_CallObject(object callable_object, object args)
163 # Return value: New reference.
164 # Call a callable Python object callable_object, with arguments
165 # given by the tuple args. If no arguments are needed, then args
166 # may be NULL. Returns the result of the call on success, or NULL
167 # on failure. This is the equivalent of the Python expression
168 # "apply(callable_object, args)" or "callable_object(*args)".
170 object PyObject_CallFunction(object callable, char *format, ...)
171 # Return value: New reference.
172 # Call a callable Python object callable, with a variable number
173 # of C arguments. The C arguments are described using a
174 # Py_BuildValue() style format string. The format may be NULL,
175 # indicating that no arguments are provided. Returns the result of
176 # the call on success, or NULL on failure. This is the equivalent
177 # of the Python expression "apply(callable, args)" or
178 # "callable(*args)". Note that if you only pass object args,
179 # PyObject_CallFunctionObjArgs is a faster alternative.
181 object PyObject_CallMethod(object o, char *method, char *format, ...)
182 # Return value: New reference.
183 # Call the method named method of object o with a variable number
184 # of C arguments. The C arguments are described by a
185 # Py_BuildValue() format string that should produce a tuple. The
186 # format may be NULL, indicating that no arguments are
187 # provided. Returns the result of the call on success, or NULL on
188 # failure. This is the equivalent of the Python expression
189 # "o.method(args)". Note that if you only pass object args,
190 # PyObject_CallMethodObjArgs is a faster alternative.
192 #object PyObject_CallFunctionObjArgs(object callable, ..., NULL)
193 object PyObject_CallFunctionObjArgs(object callable, ...)
194 # Return value: New reference.
195 # Call a callable Python object callable, with a variable number
196 # of PyObject* arguments. The arguments are provided as a variable
197 # number of parameters followed by NULL. Returns the result of the
198 # call on success, or NULL on failure.
200 #PyObject* PyObject_CallMethodObjArgs(object o, object name, ..., NULL)
201 object PyObject_CallMethodObjArgs(object o, object name, ...)
202 # Return value: New reference.
203 # Calls a method of the object o, where the name of the method is
204 # given as a Python string object in name. It is called with a
205 # variable number of PyObject* arguments. The arguments are
206 # provided as a variable number of parameters followed by
207 # NULL. Returns the result of the call on success, or NULL on
208 # failure.
210 long PyObject_Hash(object o)
211 # Compute and return the hash value of an object o. On failure,
212 # return -1. This is the equivalent of the Python expression
213 # "hash(o)".
215 bint PyObject_IsTrue(object o)
216 # Returns 1 if the object o is considered to be true, and 0
217 # otherwise. This is equivalent to the Python expression "not not
218 # o". On failure, return -1.
220 bint PyObject_Not(object o)
221 # Returns 0 if the object o is considered to be true, and 1
222 # otherwise. This is equivalent to the Python expression "not
223 # o". On failure, return -1.
225 object PyObject_Type(object o)
226 # Return value: New reference.
227 # When o is non-NULL, returns a type object corresponding to the
228 # object type of object o. On failure, raises SystemError and
229 # returns NULL. This is equivalent to the Python expression
230 # type(o). This function increments the reference count of the
231 # return value. There's really no reason to use this function
232 # instead of the common expression o->ob_type, which returns a
233 # pointer of type PyTypeObject*, except when the incremented
234 # reference count is needed.
236 bint PyObject_TypeCheck(object o, PyTypeObject *type)
237 # Return true if the object o is of type type or a subtype of
238 # type. Both parameters must be non-NULL.
240 Py_ssize_t PyObject_Length(object o)
241 Py_ssize_t PyObject_Size(object o)
242 # Return the length of object o. If the object o provides either
243 # the sequence and mapping protocols, the sequence length is
244 # returned. On error, -1 is returned. This is the equivalent to
245 # the Python expression "len(o)".
247 object PyObject_GetItem(object o, object key)
248 # Return value: New reference.
249 # Return element of o corresponding to the object key or NULL on
250 # failure. This is the equivalent of the Python expression
251 # "o[key]".
253 int PyObject_SetItem(object o, object key, object v)
254 # Map the object key to the value v. Returns -1 on failure. This
255 # is the equivalent of the Python statement "o[key] = v".
257 int PyObject_DelItem(object o, object key)
258 # Delete the mapping for key from o. Returns -1 on failure. This
259 # is the equivalent of the Python statement "del o[key]".
261 int PyObject_AsFileDescriptor(object o)
262 # Derives a file-descriptor from a Python object. If the object is
263 # an integer or long integer, its value is returned. If not, the
264 # object's fileno() method is called if it exists; the method must
265 # return an integer or long integer, which is returned as the file
266 # descriptor value. Returns -1 on failure.
268 object PyObject_Dir(object o)
269 # Return value: New reference.
270 # This is equivalent to the Python expression "dir(o)", returning
271 # a (possibly empty) list of strings appropriate for the object
272 # argument, or NULL if there was an error. If the argument is
273 # NULL, this is like the Python "dir()", returning the names of
274 # the current locals; in this case, if no execution frame is
275 # active then NULL is returned but PyErr_Occurred() will return
276 # false.
278 object PyObject_GetIter(object o)
279 # Return value: New reference.
280 # This is equivalent to the Python expression "iter(o)". It
281 # returns a new iterator for the object argument, or the object
282 # itself if the object is already an iterator. Raises TypeError
283 # and returns NULL if the object cannot be iterated.