| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1 | \chapter{Concrete Objects Layer \label{concrete}} | 
|  | 2 |  | 
|  | 3 |  | 
|  | 4 | The functions in this chapter are specific to certain Python object | 
|  | 5 | types.  Passing them an object of the wrong type is not a good idea; | 
|  | 6 | if you receive an object from a Python program and you are not sure | 
|  | 7 | that it has the right type, you must perform a type check first; | 
|  | 8 | for example, to check that an object is a dictionary, use | 
|  | 9 | \cfunction{PyDict_Check()}.  The chapter is structured like the | 
|  | 10 | ``family tree'' of Python object types. | 
|  | 11 |  | 
|  | 12 | \warning{While the functions described in this chapter carefully check | 
|  | 13 | the type of the objects which are passed in, many of them do not check | 
|  | 14 | for \NULL{} being passed instead of a valid object.  Allowing \NULL{} | 
|  | 15 | to be passed in can cause memory access violations and immediate | 
|  | 16 | termination of the interpreter.} | 
|  | 17 |  | 
|  | 18 |  | 
|  | 19 | \section{Fundamental Objects \label{fundamental}} | 
|  | 20 |  | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 21 | This section describes Python type objects and the singleton object | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 22 | \code{None}. | 
|  | 23 |  | 
|  | 24 |  | 
|  | 25 | \subsection{Type Objects \label{typeObjects}} | 
|  | 26 |  | 
|  | 27 | \obindex{type} | 
|  | 28 | \begin{ctypedesc}{PyTypeObject} | 
|  | 29 | The C structure of the objects used to describe built-in types. | 
|  | 30 | \end{ctypedesc} | 
|  | 31 |  | 
|  | 32 | \begin{cvardesc}{PyObject*}{PyType_Type} | 
|  | 33 | This is the type object for type objects; it is the same object as | 
|  | 34 | \code{types.TypeType} in the Python layer. | 
|  | 35 | \withsubitem{(in module types)}{\ttindex{TypeType}} | 
|  | 36 | \end{cvardesc} | 
|  | 37 |  | 
|  | 38 | \begin{cfuncdesc}{int}{PyType_Check}{PyObject *o} | 
| Fred Drake | e3c764b | 2002-04-10 17:52:52 +0000 | [diff] [blame] | 39 | Returns true if the object \var{o} is a type object, including | 
|  | 40 | instances of types derived from the standard type object.  Returns | 
|  | 41 | false in all other cases. | 
|  | 42 | \end{cfuncdesc} | 
|  | 43 |  | 
|  | 44 | \begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o} | 
|  | 45 | Returns true if the object \var{o} is a type object, but not a | 
|  | 46 | subtype of the standard type object.  Returns false in all other | 
|  | 47 | cases. | 
|  | 48 | \versionadded{2.2} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 49 | \end{cfuncdesc} | 
|  | 50 |  | 
|  | 51 | \begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature} | 
|  | 52 | Returns true if the type object \var{o} sets the feature | 
|  | 53 | \var{feature}.  Type features are denoted by single bit flags. | 
|  | 54 | \end{cfuncdesc} | 
|  | 55 |  | 
| Fred Drake | e3c764b | 2002-04-10 17:52:52 +0000 | [diff] [blame] | 56 | \begin{cfuncdesc}{int}{PyType_IS_GC}{PyObject *o} | 
|  | 57 | Return true if the type object includes support for the cycle | 
|  | 58 | detector; this tests the type flag \constant{Py_TPFLAGS_HAVE_GC}. | 
|  | 59 | \versionadded{2.0} | 
|  | 60 | \end{cfuncdesc} | 
|  | 61 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 62 | \begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b} | 
|  | 63 | Returns true if \var{a} is a subtype of \var{b}. | 
|  | 64 | \versionadded{2.2} | 
|  | 65 | \end{cfuncdesc} | 
|  | 66 |  | 
|  | 67 | \begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type, | 
|  | 68 | int nitems} | 
|  | 69 | \versionadded{2.2} | 
|  | 70 | \end{cfuncdesc} | 
|  | 71 |  | 
|  | 72 | \begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type, | 
|  | 73 | PyObject *args, PyObject *kwds} | 
|  | 74 | \versionadded{2.2} | 
|  | 75 | \end{cfuncdesc} | 
|  | 76 |  | 
|  | 77 | \begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type} | 
| Fred Drake | 28de8d4 | 2002-04-12 16:15:10 +0000 | [diff] [blame] | 78 | Finalize a type object.  This should be called on all type objects | 
|  | 79 | to finish their initialization.  This function is responsible for | 
|  | 80 | adding inherited slots from a type's base class.  Returns \code{0} | 
|  | 81 | on success, or returns \code{-1} and sets an exception on error. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 82 | \versionadded{2.2} | 
|  | 83 | \end{cfuncdesc} | 
|  | 84 |  | 
|  | 85 |  | 
|  | 86 | \subsection{The None Object \label{noneObject}} | 
|  | 87 |  | 
| Fred Drake | 7a700b8 | 2004-01-01 05:43:53 +0000 | [diff] [blame] | 88 | \obindex{None} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 89 | Note that the \ctype{PyTypeObject} for \code{None} is not directly | 
|  | 90 | exposed in the Python/C API.  Since \code{None} is a singleton, | 
|  | 91 | testing for object identity (using \samp{==} in C) is sufficient. | 
|  | 92 | There is no \cfunction{PyNone_Check()} function for the same reason. | 
|  | 93 |  | 
|  | 94 | \begin{cvardesc}{PyObject*}{Py_None} | 
|  | 95 | The Python \code{None} object, denoting lack of value.  This object | 
| Fred Drake | 6ccdccd | 2002-03-12 20:12:54 +0000 | [diff] [blame] | 96 | has no methods.  It needs to be treated just like any other object | 
|  | 97 | with respect to reference counts. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 98 | \end{cvardesc} | 
|  | 99 |  | 
| Brett Cannon | 35d8360 | 2003-11-09 04:15:30 +0000 | [diff] [blame] | 100 | \begin{csimplemacrodesc}{Py_RETURN_NONE} | 
| Fred Drake | 28eae08 | 2003-11-10 14:48:48 +0000 | [diff] [blame] | 101 | Properly handles returning \cdata{Py_None} from within a C function. | 
| Brett Cannon | 35d8360 | 2003-11-09 04:15:30 +0000 | [diff] [blame] | 102 | \end{csimplemacrodesc} | 
|  | 103 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 104 |  | 
|  | 105 | \section{Numeric Objects \label{numericObjects}} | 
|  | 106 |  | 
|  | 107 | \obindex{numeric} | 
|  | 108 |  | 
|  | 109 |  | 
|  | 110 | \subsection{Plain Integer Objects \label{intObjects}} | 
|  | 111 |  | 
|  | 112 | \obindex{integer} | 
|  | 113 | \begin{ctypedesc}{PyIntObject} | 
|  | 114 | This subtype of \ctype{PyObject} represents a Python integer | 
|  | 115 | object. | 
|  | 116 | \end{ctypedesc} | 
|  | 117 |  | 
|  | 118 | \begin{cvardesc}{PyTypeObject}{PyInt_Type} | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 119 | This instance of \ctype{PyTypeObject} represents the Python plain | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 120 | integer type.  This is the same object as \code{types.IntType}. | 
|  | 121 | \withsubitem{(in modules types)}{\ttindex{IntType}} | 
|  | 122 | \end{cvardesc} | 
|  | 123 |  | 
|  | 124 | \begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o} | 
|  | 125 | Returns true if \var{o} is of type \cdata{PyInt_Type} or a subtype | 
|  | 126 | of \cdata{PyInt_Type}. | 
|  | 127 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 128 | \end{cfuncdesc} | 
|  | 129 |  | 
|  | 130 | \begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject* o} | 
|  | 131 | Returns true if \var{o} is of type \cdata{PyInt_Type}, but not a | 
|  | 132 | subtype of \cdata{PyInt_Type}. | 
|  | 133 | \versionadded{2.2} | 
|  | 134 | \end{cfuncdesc} | 
|  | 135 |  | 
| Skip Montanaro | 1ff49a7 | 2003-02-03 05:13:24 +0000 | [diff] [blame] | 136 | \begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend, | 
|  | 137 | int base} | 
|  | 138 | Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the | 
|  | 139 | string value in \var{str}, which is interpreted according to the radix in | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 140 | \var{base}.  If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to | 
| Skip Montanaro | 1ff49a7 | 2003-02-03 05:13:24 +0000 | [diff] [blame] | 141 | the first character in \var{str} which follows the representation of the | 
|  | 142 | number.  If \var{base} is \code{0}, the radix will be determined based on | 
|  | 143 | the leading characters of \var{str}: if \var{str} starts with \code{'0x'} | 
|  | 144 | or \code{'0X'}, radix 16 will be used; if \var{str} starts with | 
|  | 145 | \code{'0'}, radix 8 will be used; otherwise radix 10 will be used.  If | 
|  | 146 | \var{base} is not \code{0}, it must be between \code{2} and \code{36}, | 
|  | 147 | inclusive.  Leading spaces are ignored.  If there are no digits, | 
|  | 148 | \exception{ValueError} will be raised.  If the string represents a number | 
|  | 149 | too large to be contained within the machine's \ctype{long int} type and | 
|  | 150 | overflow warnings are being suppressed, a \ctype{PyLongObject} will be | 
|  | 151 | returned.  If overflow warnings are not being suppressed, \NULL{} will be | 
|  | 152 | returned in this case. | 
|  | 153 | \end{cfuncdesc} | 
|  | 154 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 155 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival} | 
|  | 156 | Creates a new integer object with a value of \var{ival}. | 
|  | 157 |  | 
|  | 158 | The current implementation keeps an array of integer objects for all | 
|  | 159 | integers between \code{-1} and \code{100}, when you create an int in | 
|  | 160 | that range you actually just get back a reference to the existing | 
|  | 161 | object. So it should be possible to change the value of \code{1}.  I | 
|  | 162 | suspect the behaviour of Python in this case is undefined. :-) | 
|  | 163 | \end{cfuncdesc} | 
|  | 164 |  | 
|  | 165 | \begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io} | 
|  | 166 | Will first attempt to cast the object to a \ctype{PyIntObject}, if | 
|  | 167 | it is not already one, and then return its value. | 
|  | 168 | \end{cfuncdesc} | 
|  | 169 |  | 
|  | 170 | \begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io} | 
|  | 171 | Returns the value of the object \var{io}.  No error checking is | 
|  | 172 | performed. | 
|  | 173 | \end{cfuncdesc} | 
|  | 174 |  | 
| Thomas Heller | 34d7f09 | 2003-04-23 19:51:05 +0000 | [diff] [blame] | 175 | \begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io} | 
|  | 176 | Will first attempt to cast the object to a \ctype{PyIntObject} or | 
| Fred Drake | c22b299 | 2003-04-23 20:38:41 +0000 | [diff] [blame] | 177 | \ctype{PyLongObject}, if it is not already one, and then return its | 
| Thomas Heller | 34d7f09 | 2003-04-23 19:51:05 +0000 | [diff] [blame] | 178 | value as unsigned long.  This function does not check for overflow. | 
|  | 179 | \versionadded{2.3} | 
|  | 180 | \end{cfuncdesc} | 
|  | 181 |  | 
| Thomas Heller | fe08083 | 2004-07-23 14:49:52 +0000 | [diff] [blame] | 182 | \begin{cfuncdesc}{unsigned long long}{PyInt_AsUnsignedLongLongMask}{PyObject *io} | 
| Thomas Heller | 34d7f09 | 2003-04-23 19:51:05 +0000 | [diff] [blame] | 183 | Will first attempt to cast the object to a \ctype{PyIntObject} or | 
| Fred Drake | c22b299 | 2003-04-23 20:38:41 +0000 | [diff] [blame] | 184 | \ctype{PyLongObject}, if it is not already one, and then return its | 
| Thomas Heller | 34d7f09 | 2003-04-23 19:51:05 +0000 | [diff] [blame] | 185 | value as unsigned long long, without checking for overflow. | 
|  | 186 | \versionadded{2.3} | 
|  | 187 | \end{cfuncdesc} | 
|  | 188 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 189 | \begin{cfuncdesc}{long}{PyInt_GetMax}{} | 
|  | 190 | Returns the system's idea of the largest integer it can handle | 
|  | 191 | (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system | 
|  | 192 | header files). | 
|  | 193 | \end{cfuncdesc} | 
|  | 194 |  | 
| Fred Drake | 2be406b | 2004-08-03 16:02:35 +0000 | [diff] [blame] | 195 | \subsection{Boolean Objects \label{boolObjects}} | 
| Skip Montanaro | 33ee76a | 2004-07-28 14:17:04 +0000 | [diff] [blame] | 196 |  | 
|  | 197 | Booleans in Python are implemented as a subclass of integers.  There | 
|  | 198 | are only two booleans, \constant{Py_False} and \constant{Py_True}.  As | 
|  | 199 | such, the normal creation and deletion functions don't apply to | 
|  | 200 | booleans.  The following macros are available, however. | 
|  | 201 |  | 
|  | 202 | \begin{cfuncdesc}{int}{PyBool_Check}{PyObject* o} | 
|  | 203 | Returns true if \var{o} is of type \cdata{PyBool_Type}. | 
|  | 204 | \versionadded{2.3} | 
|  | 205 | \end{cfuncdesc} | 
|  | 206 |  | 
| Skip Montanaro | 6d3db70 | 2004-07-29 02:16:04 +0000 | [diff] [blame] | 207 | \begin{cvardesc}{PyObject*}{Py_False} | 
|  | 208 | The Python \code{False} object.  This object has no methods.  It needs to | 
|  | 209 | be treated just like any other object with respect to reference counts. | 
|  | 210 | \end{cvardesc} | 
| Skip Montanaro | 33ee76a | 2004-07-28 14:17:04 +0000 | [diff] [blame] | 211 |  | 
| Skip Montanaro | 6d3db70 | 2004-07-29 02:16:04 +0000 | [diff] [blame] | 212 | \begin{cvardesc}{PyObject*}{Py_True} | 
|  | 213 | The Python \code{True} object.  This object has no methods.  It needs to | 
|  | 214 | be treated just like any other object with respect to reference counts. | 
|  | 215 | \end{cvardesc} | 
|  | 216 |  | 
|  | 217 | \begin{csimplemacrodesc}{Py_RETURN_FALSE} | 
|  | 218 | Return \constant{Py_False} from a function, properly incrementing its | 
|  | 219 | reference count. | 
|  | 220 | \versionadded{2.4} | 
|  | 221 | \end{csimplemacrodesc} | 
|  | 222 |  | 
|  | 223 | \begin{csimplemacrodesc}{Py_RETURN_TRUE} | 
| Skip Montanaro | 33ee76a | 2004-07-28 14:17:04 +0000 | [diff] [blame] | 224 | Return Py_True from a function, properly incrementing its reference | 
|  | 225 | count. | 
|  | 226 | \versionadded{2.4} | 
| Skip Montanaro | 6d3db70 | 2004-07-29 02:16:04 +0000 | [diff] [blame] | 227 | \end{csimplemacrodesc} | 
| Skip Montanaro | 33ee76a | 2004-07-28 14:17:04 +0000 | [diff] [blame] | 228 |  | 
|  | 229 | \begin{cfuncdesc}{int}{PyBool_Check}{PyObject* o} | 
|  | 230 | Returns true if \var{o} is of type \cdata{PyBool_Type}. | 
|  | 231 | \versionadded{2.3} | 
|  | 232 | \end{cfuncdesc} | 
|  | 233 |  | 
|  | 234 | \begin{cfuncdesc}{int}{PyBool_FromLong}{long v} | 
|  | 235 | Returns \constant{Py_True} or \constant{Py_False} depending on the | 
|  | 236 | truth value of \var{v}. | 
|  | 237 | \versionadded{2.3} | 
|  | 238 | \end{cfuncdesc} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 239 |  | 
|  | 240 | \subsection{Long Integer Objects \label{longObjects}} | 
|  | 241 |  | 
|  | 242 | \obindex{long integer} | 
|  | 243 | \begin{ctypedesc}{PyLongObject} | 
|  | 244 | This subtype of \ctype{PyObject} represents a Python long integer | 
|  | 245 | object. | 
|  | 246 | \end{ctypedesc} | 
|  | 247 |  | 
|  | 248 | \begin{cvardesc}{PyTypeObject}{PyLong_Type} | 
|  | 249 | This instance of \ctype{PyTypeObject} represents the Python long | 
|  | 250 | integer type.  This is the same object as \code{types.LongType}. | 
|  | 251 | \withsubitem{(in modules types)}{\ttindex{LongType}} | 
|  | 252 | \end{cvardesc} | 
|  | 253 |  | 
|  | 254 | \begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p} | 
|  | 255 | Returns true if its argument is a \ctype{PyLongObject} or a subtype | 
|  | 256 | of \ctype{PyLongObject}. | 
|  | 257 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 258 | \end{cfuncdesc} | 
|  | 259 |  | 
|  | 260 | \begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p} | 
|  | 261 | Returns true if its argument is a \ctype{PyLongObject}, but not a | 
|  | 262 | subtype of \ctype{PyLongObject}. | 
|  | 263 | \versionadded{2.2} | 
|  | 264 | \end{cfuncdesc} | 
|  | 265 |  | 
|  | 266 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} | 
|  | 267 | Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{} | 
|  | 268 | on failure. | 
|  | 269 | \end{cfuncdesc} | 
|  | 270 |  | 
|  | 271 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v} | 
|  | 272 | Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned | 
|  | 273 | long}, or \NULL{} on failure. | 
|  | 274 | \end{cfuncdesc} | 
|  | 275 |  | 
|  | 276 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v} | 
|  | 277 | Returns a new \ctype{PyLongObject} object from a C \ctype{long long}, | 
|  | 278 | or \NULL{} on failure. | 
|  | 279 | \end{cfuncdesc} | 
|  | 280 |  | 
|  | 281 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v} | 
|  | 282 | Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned | 
|  | 283 | long long}, or \NULL{} on failure. | 
|  | 284 | \end{cfuncdesc} | 
|  | 285 |  | 
|  | 286 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} | 
|  | 287 | Returns a new \ctype{PyLongObject} object from the integer part of | 
|  | 288 | \var{v}, or \NULL{} on failure. | 
|  | 289 | \end{cfuncdesc} | 
|  | 290 |  | 
|  | 291 | \begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend, | 
|  | 292 | int base} | 
|  | 293 | Return a new \ctype{PyLongObject} based on the string value in | 
|  | 294 | \var{str}, which is interpreted according to the radix in | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 295 | \var{base}.  If \var{pend} is non-\NULL{}, \code{*\var{pend}} will | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 296 | point to the first character in \var{str} which follows the | 
|  | 297 | representation of the number.  If \var{base} is \code{0}, the radix | 
| Skip Montanaro | 1ff49a7 | 2003-02-03 05:13:24 +0000 | [diff] [blame] | 298 | will be determined based on the leading characters of \var{str}: if | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 299 | \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be | 
|  | 300 | used; if \var{str} starts with \code{'0'}, radix 8 will be used; | 
|  | 301 | otherwise radix 10 will be used.  If \var{base} is not \code{0}, it | 
|  | 302 | must be between \code{2} and \code{36}, inclusive.  Leading spaces | 
|  | 303 | are ignored.  If there are no digits, \exception{ValueError} will be | 
|  | 304 | raised. | 
|  | 305 | \end{cfuncdesc} | 
|  | 306 |  | 
|  | 307 | \begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u, | 
|  | 308 | int length, int base} | 
|  | 309 | Convert a sequence of Unicode digits to a Python long integer | 
|  | 310 | value.  The first parameter, \var{u}, points to the first character | 
|  | 311 | of the Unicode string, \var{length} gives the number of characters, | 
|  | 312 | and \var{base} is the radix for the conversion.  The radix must be | 
|  | 313 | in the range [2, 36]; if it is out of range, \exception{ValueError} | 
|  | 314 | will be raised. | 
|  | 315 | \versionadded{1.6} | 
|  | 316 | \end{cfuncdesc} | 
|  | 317 |  | 
|  | 318 | \begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p} | 
|  | 319 | Create a Python integer or long integer from the pointer \var{p}. | 
|  | 320 | The pointer value can be retrieved from the resulting value using | 
|  | 321 | \cfunction{PyLong_AsVoidPtr()}. | 
|  | 322 | \versionadded{1.5.2} | 
|  | 323 | \end{cfuncdesc} | 
|  | 324 |  | 
|  | 325 | \begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong} | 
|  | 326 | Returns a C \ctype{long} representation of the contents of | 
|  | 327 | \var{pylong}.  If \var{pylong} is greater than | 
|  | 328 | \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError} | 
|  | 329 | is raised. | 
|  | 330 | \withsubitem{(built-in exception)}{\ttindex{OverflowError}} | 
|  | 331 | \end{cfuncdesc} | 
|  | 332 |  | 
|  | 333 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong} | 
|  | 334 | Returns a C \ctype{unsigned long} representation of the contents of | 
|  | 335 | \var{pylong}.  If \var{pylong} is greater than | 
|  | 336 | \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an | 
|  | 337 | \exception{OverflowError} is raised. | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 338 | \withsubitem{(built-in exception)}{\ttindex{OverflowError}} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 339 | \end{cfuncdesc} | 
|  | 340 |  | 
|  | 341 | \begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong} | 
|  | 342 | Return a C \ctype{long long} from a Python long integer.  If | 
|  | 343 | \var{pylong} cannot be represented as a \ctype{long long}, an | 
|  | 344 | \exception{OverflowError} will be raised. | 
|  | 345 | \versionadded{2.2} | 
|  | 346 | \end{cfuncdesc} | 
|  | 347 |  | 
|  | 348 | \begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject | 
|  | 349 | *pylong} | 
|  | 350 | Return a C \ctype{unsigned long long} from a Python long integer. | 
|  | 351 | If \var{pylong} cannot be represented as an \ctype{unsigned long | 
|  | 352 | long}, an \exception{OverflowError} will be raised if the value is | 
|  | 353 | positive, or a \exception{TypeError} will be raised if the value is | 
|  | 354 | negative. | 
|  | 355 | \versionadded{2.2} | 
|  | 356 | \end{cfuncdesc} | 
|  | 357 |  | 
| Thomas Heller | 34d7f09 | 2003-04-23 19:51:05 +0000 | [diff] [blame] | 358 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io} | 
|  | 359 | Return a C \ctype{unsigned long} from a Python long integer, without | 
|  | 360 | checking for overflow. | 
|  | 361 | \versionadded{2.3} | 
|  | 362 | \end{cfuncdesc} | 
|  | 363 |  | 
|  | 364 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongLongMask}{PyObject *io} | 
|  | 365 | Return a C \ctype{unsigned long long} from a Python long integer, without | 
|  | 366 | checking for overflow. | 
|  | 367 | \versionadded{2.3} | 
|  | 368 | \end{cfuncdesc} | 
|  | 369 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 370 | \begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong} | 
|  | 371 | Returns a C \ctype{double} representation of the contents of | 
|  | 372 | \var{pylong}.  If \var{pylong} cannot be approximately represented | 
|  | 373 | as a \ctype{double}, an \exception{OverflowError} exception is | 
|  | 374 | raised and \code{-1.0} will be returned. | 
|  | 375 | \end{cfuncdesc} | 
|  | 376 |  | 
|  | 377 | \begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong} | 
|  | 378 | Convert a Python integer or long integer \var{pylong} to a C | 
|  | 379 | \ctype{void} pointer.  If \var{pylong} cannot be converted, an | 
|  | 380 | \exception{OverflowError} will be raised.  This is only assured to | 
|  | 381 | produce a usable \ctype{void} pointer for values created with | 
|  | 382 | \cfunction{PyLong_FromVoidPtr()}. | 
|  | 383 | \versionadded{1.5.2} | 
|  | 384 | \end{cfuncdesc} | 
|  | 385 |  | 
|  | 386 |  | 
|  | 387 | \subsection{Floating Point Objects \label{floatObjects}} | 
|  | 388 |  | 
|  | 389 | \obindex{floating point} | 
|  | 390 | \begin{ctypedesc}{PyFloatObject} | 
|  | 391 | This subtype of \ctype{PyObject} represents a Python floating point | 
|  | 392 | object. | 
|  | 393 | \end{ctypedesc} | 
|  | 394 |  | 
|  | 395 | \begin{cvardesc}{PyTypeObject}{PyFloat_Type} | 
|  | 396 | This instance of \ctype{PyTypeObject} represents the Python floating | 
|  | 397 | point type.  This is the same object as \code{types.FloatType}. | 
|  | 398 | \withsubitem{(in modules types)}{\ttindex{FloatType}} | 
|  | 399 | \end{cvardesc} | 
|  | 400 |  | 
|  | 401 | \begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p} | 
|  | 402 | Returns true if its argument is a \ctype{PyFloatObject} or a subtype | 
|  | 403 | of \ctype{PyFloatObject}. | 
|  | 404 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 405 | \end{cfuncdesc} | 
|  | 406 |  | 
|  | 407 | \begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p} | 
|  | 408 | Returns true if its argument is a \ctype{PyFloatObject}, but not a | 
|  | 409 | subtype of \ctype{PyFloatObject}. | 
|  | 410 | \versionadded{2.2} | 
|  | 411 | \end{cfuncdesc} | 
|  | 412 |  | 
| Skip Montanaro | 1ff49a7 | 2003-02-03 05:13:24 +0000 | [diff] [blame] | 413 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend} | 
| Skip Montanaro | ae31e9b | 2003-02-03 03:56:36 +0000 | [diff] [blame] | 414 | Creates a \ctype{PyFloatObject} object based on the string value in | 
| Skip Montanaro | 1ff49a7 | 2003-02-03 05:13:24 +0000 | [diff] [blame] | 415 | \var{str}, or \NULL{} on failure.  The \var{pend} argument is ignored.  It | 
|  | 416 | remains only for backward compatibility. | 
| Skip Montanaro | ae31e9b | 2003-02-03 03:56:36 +0000 | [diff] [blame] | 417 | \end{cfuncdesc} | 
|  | 418 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 419 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} | 
|  | 420 | Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on | 
|  | 421 | failure. | 
|  | 422 | \end{cfuncdesc} | 
|  | 423 |  | 
|  | 424 | \begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat} | 
|  | 425 | Returns a C \ctype{double} representation of the contents of | 
|  | 426 | \var{pyfloat}. | 
|  | 427 | \end{cfuncdesc} | 
|  | 428 |  | 
|  | 429 | \begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat} | 
|  | 430 | Returns a C \ctype{double} representation of the contents of | 
|  | 431 | \var{pyfloat}, but without error checking. | 
|  | 432 | \end{cfuncdesc} | 
|  | 433 |  | 
|  | 434 |  | 
|  | 435 | \subsection{Complex Number Objects \label{complexObjects}} | 
|  | 436 |  | 
|  | 437 | \obindex{complex number} | 
|  | 438 | Python's complex number objects are implemented as two distinct types | 
|  | 439 | when viewed from the C API:  one is the Python object exposed to | 
|  | 440 | Python programs, and the other is a C structure which represents the | 
|  | 441 | actual complex number value.  The API provides functions for working | 
|  | 442 | with both. | 
|  | 443 |  | 
|  | 444 | \subsubsection{Complex Numbers as C Structures} | 
|  | 445 |  | 
|  | 446 | Note that the functions which accept these structures as parameters | 
|  | 447 | and return them as results do so \emph{by value} rather than | 
|  | 448 | dereferencing them through pointers.  This is consistent throughout | 
|  | 449 | the API. | 
|  | 450 |  | 
|  | 451 | \begin{ctypedesc}{Py_complex} | 
|  | 452 | The C structure which corresponds to the value portion of a Python | 
|  | 453 | complex number object.  Most of the functions for dealing with | 
|  | 454 | complex number objects use structures of this type as input or | 
|  | 455 | output values, as appropriate.  It is defined as: | 
|  | 456 |  | 
|  | 457 | \begin{verbatim} | 
|  | 458 | typedef struct { | 
|  | 459 | double real; | 
|  | 460 | double imag; | 
|  | 461 | } Py_complex; | 
|  | 462 | \end{verbatim} | 
|  | 463 | \end{ctypedesc} | 
|  | 464 |  | 
|  | 465 | \begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right} | 
|  | 466 | Return the sum of two complex numbers, using the C | 
|  | 467 | \ctype{Py_complex} representation. | 
|  | 468 | \end{cfuncdesc} | 
|  | 469 |  | 
|  | 470 | \begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right} | 
|  | 471 | Return the difference between two complex numbers, using the C | 
|  | 472 | \ctype{Py_complex} representation. | 
|  | 473 | \end{cfuncdesc} | 
|  | 474 |  | 
|  | 475 | \begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex} | 
|  | 476 | Return the negation of the complex number \var{complex}, using the C | 
|  | 477 | \ctype{Py_complex} representation. | 
|  | 478 | \end{cfuncdesc} | 
|  | 479 |  | 
|  | 480 | \begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right} | 
|  | 481 | Return the product of two complex numbers, using the C | 
|  | 482 | \ctype{Py_complex} representation. | 
|  | 483 | \end{cfuncdesc} | 
|  | 484 |  | 
|  | 485 | \begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend, | 
|  | 486 | Py_complex divisor} | 
|  | 487 | Return the quotient of two complex numbers, using the C | 
|  | 488 | \ctype{Py_complex} representation. | 
|  | 489 | \end{cfuncdesc} | 
|  | 490 |  | 
|  | 491 | \begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp} | 
|  | 492 | Return the exponentiation of \var{num} by \var{exp}, using the C | 
|  | 493 | \ctype{Py_complex} representation. | 
|  | 494 | \end{cfuncdesc} | 
|  | 495 |  | 
|  | 496 |  | 
|  | 497 | \subsubsection{Complex Numbers as Python Objects} | 
|  | 498 |  | 
|  | 499 | \begin{ctypedesc}{PyComplexObject} | 
|  | 500 | This subtype of \ctype{PyObject} represents a Python complex number | 
|  | 501 | object. | 
|  | 502 | \end{ctypedesc} | 
|  | 503 |  | 
|  | 504 | \begin{cvardesc}{PyTypeObject}{PyComplex_Type} | 
|  | 505 | This instance of \ctype{PyTypeObject} represents the Python complex | 
|  | 506 | number type. | 
|  | 507 | \end{cvardesc} | 
|  | 508 |  | 
|  | 509 | \begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p} | 
|  | 510 | Returns true if its argument is a \ctype{PyComplexObject} or a | 
|  | 511 | subtype of \ctype{PyComplexObject}. | 
|  | 512 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 513 | \end{cfuncdesc} | 
|  | 514 |  | 
|  | 515 | \begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p} | 
|  | 516 | Returns true if its argument is a \ctype{PyComplexObject}, but not a | 
|  | 517 | subtype of \ctype{PyComplexObject}. | 
|  | 518 | \versionadded{2.2} | 
|  | 519 | \end{cfuncdesc} | 
|  | 520 |  | 
|  | 521 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v} | 
|  | 522 | Create a new Python complex number object from a C | 
|  | 523 | \ctype{Py_complex} value. | 
|  | 524 | \end{cfuncdesc} | 
|  | 525 |  | 
|  | 526 | \begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag} | 
|  | 527 | Returns a new \ctype{PyComplexObject} object from \var{real} and | 
|  | 528 | \var{imag}. | 
|  | 529 | \end{cfuncdesc} | 
|  | 530 |  | 
|  | 531 | \begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op} | 
|  | 532 | Returns the real part of \var{op} as a C \ctype{double}. | 
|  | 533 | \end{cfuncdesc} | 
|  | 534 |  | 
|  | 535 | \begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op} | 
|  | 536 | Returns the imaginary part of \var{op} as a C \ctype{double}. | 
|  | 537 | \end{cfuncdesc} | 
|  | 538 |  | 
|  | 539 | \begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op} | 
|  | 540 | Returns the \ctype{Py_complex} value of the complex number | 
|  | 541 | \var{op}. | 
|  | 542 | \end{cfuncdesc} | 
|  | 543 |  | 
|  | 544 |  | 
|  | 545 |  | 
|  | 546 | \section{Sequence Objects \label{sequenceObjects}} | 
|  | 547 |  | 
|  | 548 | \obindex{sequence} | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 549 | Generic operations on sequence objects were discussed in the previous | 
|  | 550 | chapter; this section deals with the specific kinds of sequence | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 551 | objects that are intrinsic to the Python language. | 
|  | 552 |  | 
|  | 553 |  | 
|  | 554 | \subsection{String Objects \label{stringObjects}} | 
|  | 555 |  | 
|  | 556 | These functions raise \exception{TypeError} when expecting a string | 
|  | 557 | parameter and are called with a non-string parameter. | 
|  | 558 |  | 
|  | 559 | \obindex{string} | 
|  | 560 | \begin{ctypedesc}{PyStringObject} | 
|  | 561 | This subtype of \ctype{PyObject} represents a Python string object. | 
|  | 562 | \end{ctypedesc} | 
|  | 563 |  | 
|  | 564 | \begin{cvardesc}{PyTypeObject}{PyString_Type} | 
|  | 565 | This instance of \ctype{PyTypeObject} represents the Python string | 
|  | 566 | type; it is the same object as \code{types.TypeType} in the Python | 
|  | 567 | layer. | 
|  | 568 | \withsubitem{(in module types)}{\ttindex{StringType}}. | 
|  | 569 | \end{cvardesc} | 
|  | 570 |  | 
|  | 571 | \begin{cfuncdesc}{int}{PyString_Check}{PyObject *o} | 
|  | 572 | Returns true if the object \var{o} is a string object or an instance | 
|  | 573 | of a subtype of the string type. | 
|  | 574 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 575 | \end{cfuncdesc} | 
|  | 576 |  | 
|  | 577 | \begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o} | 
|  | 578 | Returns true if the object \var{o} is a string object, but not an | 
|  | 579 | instance of a subtype of the string type. | 
|  | 580 | \versionadded{2.2} | 
|  | 581 | \end{cfuncdesc} | 
|  | 582 |  | 
|  | 583 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v} | 
|  | 584 | Returns a new string object with the value \var{v} on success, and | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 585 | \NULL{} on failure.  The parameter \var{v} must not be \NULL{}; it | 
| Fred Drake | 32a3587 | 2001-12-06 20:38:15 +0000 | [diff] [blame] | 586 | will not be checked. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 587 | \end{cfuncdesc} | 
|  | 588 |  | 
|  | 589 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v, | 
|  | 590 | int len} | 
|  | 591 | Returns a new string object with the value \var{v} and length | 
|  | 592 | \var{len} on success, and \NULL{} on failure.  If \var{v} is | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 593 | \NULL{}, the contents of the string are uninitialized. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 594 | \end{cfuncdesc} | 
|  | 595 |  | 
|  | 596 | \begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...} | 
|  | 597 | Takes a C \cfunction{printf()}-style \var{format} string and a | 
|  | 598 | variable number of arguments, calculates the size of the resulting | 
|  | 599 | Python string and returns a string with the values formatted into | 
|  | 600 | it.  The variable arguments must be C types and must correspond | 
|  | 601 | exactly to the format characters in the \var{format} string.  The | 
|  | 602 | following format characters are allowed: | 
|  | 603 |  | 
|  | 604 | \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment} | 
|  | 605 | \lineiii{\%\%}{\emph{n/a}}{The literal \% character.} | 
|  | 606 | \lineiii{\%c}{int}{A single character, represented as an C int.} | 
|  | 607 | \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.} | 
|  | 608 | \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.} | 
|  | 609 | \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.} | 
|  | 610 | \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.} | 
|  | 611 | \lineiii{\%s}{char*}{A null-terminated C character array.} | 
|  | 612 | \lineiii{\%p}{void*}{The hex representation of a C pointer. | 
|  | 613 | Mostly equivalent to \code{printf("\%p")} except that it is | 
|  | 614 | guaranteed to start with the literal \code{0x} regardless of | 
|  | 615 | what the platform's \code{printf} yields.} | 
|  | 616 | \end{tableiii} | 
|  | 617 | \end{cfuncdesc} | 
|  | 618 |  | 
|  | 619 | \begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format, | 
|  | 620 | va_list vargs} | 
|  | 621 | Identical to \function{PyString_FromFormat()} except that it takes | 
|  | 622 | exactly two arguments. | 
|  | 623 | \end{cfuncdesc} | 
|  | 624 |  | 
|  | 625 | \begin{cfuncdesc}{int}{PyString_Size}{PyObject *string} | 
|  | 626 | Returns the length of the string in string object \var{string}. | 
|  | 627 | \end{cfuncdesc} | 
|  | 628 |  | 
|  | 629 | \begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string} | 
|  | 630 | Macro form of \cfunction{PyString_Size()} but without error | 
|  | 631 | checking. | 
|  | 632 | \end{cfuncdesc} | 
|  | 633 |  | 
|  | 634 | \begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string} | 
| Fred Drake | 4b24726 | 2002-10-22 20:20:20 +0000 | [diff] [blame] | 635 | Returns a NUL-terminated representation of the contents of | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 636 | \var{string}.  The pointer refers to the internal buffer of | 
|  | 637 | \var{string}, not a copy.  The data must not be modified in any way, | 
|  | 638 | unless the string was just created using | 
|  | 639 | \code{PyString_FromStringAndSize(NULL, \var{size})}. | 
| Fred Drake | 4b24726 | 2002-10-22 20:20:20 +0000 | [diff] [blame] | 640 | It must not be deallocated.  If \var{string} is a Unicode object, | 
|  | 641 | this function computes the default encoding of \var{string} and | 
|  | 642 | operates on that.  If \var{string} is not a string object at all, | 
|  | 643 | \cfunction{PyString_AsString()} returns \NULL{} and raises | 
|  | 644 | \exception{TypeError}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 645 | \end{cfuncdesc} | 
|  | 646 |  | 
|  | 647 | \begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string} | 
|  | 648 | Macro form of \cfunction{PyString_AsString()} but without error | 
| Fred Drake | 4b24726 | 2002-10-22 20:20:20 +0000 | [diff] [blame] | 649 | checking.  Only string objects are supported; no Unicode objects | 
|  | 650 | should be passed. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 651 | \end{cfuncdesc} | 
|  | 652 |  | 
|  | 653 | \begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj, | 
|  | 654 | char **buffer, | 
|  | 655 | int *length} | 
| Fred Drake | 4b24726 | 2002-10-22 20:20:20 +0000 | [diff] [blame] | 656 | Returns a NUL-terminated representation of the contents of the | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 657 | object \var{obj} through the output variables \var{buffer} and | 
|  | 658 | \var{length}. | 
|  | 659 |  | 
|  | 660 | The function accepts both string and Unicode objects as input. For | 
|  | 661 | Unicode objects it returns the default encoded version of the | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 662 | object.  If \var{length} is \NULL{}, the resulting buffer may not | 
| Fred Drake | 4b24726 | 2002-10-22 20:20:20 +0000 | [diff] [blame] | 663 | contain NUL characters; if it does, the function returns \code{-1} | 
|  | 664 | and a \exception{TypeError} is raised. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 665 |  | 
|  | 666 | The buffer refers to an internal string buffer of \var{obj}, not a | 
|  | 667 | copy. The data must not be modified in any way, unless the string | 
|  | 668 | was just created using \code{PyString_FromStringAndSize(NULL, | 
| Fred Drake | 4b24726 | 2002-10-22 20:20:20 +0000 | [diff] [blame] | 669 | \var{size})}.  It must not be deallocated.  If \var{string} is a | 
|  | 670 | Unicode object, this function computes the default encoding of | 
|  | 671 | \var{string} and operates on that.  If \var{string} is not a string | 
|  | 672 | object at all, \cfunction{PyString_AsString()} returns \NULL{} and | 
|  | 673 | raises \exception{TypeError}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 674 | \end{cfuncdesc} | 
|  | 675 |  | 
|  | 676 | \begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string, | 
|  | 677 | PyObject *newpart} | 
|  | 678 | Creates a new string object in \var{*string} containing the contents | 
|  | 679 | of \var{newpart} appended to \var{string}; the caller will own the | 
|  | 680 | new reference.  The reference to the old value of \var{string} will | 
|  | 681 | be stolen.  If the new string cannot be created, the old reference | 
|  | 682 | to \var{string} will still be discarded and the value of | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 683 | \var{*string} will be set to \NULL{}; the appropriate exception will | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 684 | be set. | 
|  | 685 | \end{cfuncdesc} | 
|  | 686 |  | 
|  | 687 | \begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string, | 
|  | 688 | PyObject *newpart} | 
|  | 689 | Creates a new string object in \var{*string} containing the contents | 
|  | 690 | of \var{newpart} appended to \var{string}.  This version decrements | 
|  | 691 | the reference count of \var{newpart}. | 
|  | 692 | \end{cfuncdesc} | 
|  | 693 |  | 
|  | 694 | \begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize} | 
|  | 695 | A way to resize a string object even though it is ``immutable''. | 
|  | 696 | Only use this to build up a brand new string object; don't use this | 
| Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 697 | if the string may already be known in other parts of the code.  It | 
|  | 698 | is an error to call this function if the refcount on the input string | 
|  | 699 | object is not one. | 
|  | 700 | Pass the address of an existing string object as an lvalue (it may | 
|  | 701 | be written into), and the new size desired.  On success, \var{*string} | 
| Fred Drake | 432425e | 2002-04-29 15:17:16 +0000 | [diff] [blame] | 702 | holds the resized string object and \code{0} is returned; the address in | 
| Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 703 | \var{*string} may differ from its input value.  If the | 
|  | 704 | reallocation fails, the original string object at \var{*string} is | 
|  | 705 | deallocated, \var{*string} is set to \NULL{}, a memory exception is set, | 
| Fred Drake | 432425e | 2002-04-29 15:17:16 +0000 | [diff] [blame] | 706 | and \code{-1} is returned. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 707 | \end{cfuncdesc} | 
|  | 708 |  | 
|  | 709 | \begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format, | 
|  | 710 | PyObject *args} | 
|  | 711 | Returns a new string object from \var{format} and \var{args}. | 
|  | 712 | Analogous to \code{\var{format} \%\ \var{args}}.  The \var{args} | 
|  | 713 | argument must be a tuple. | 
|  | 714 | \end{cfuncdesc} | 
|  | 715 |  | 
|  | 716 | \begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string} | 
|  | 717 | Intern the argument \var{*string} in place.  The argument must be | 
|  | 718 | the address of a pointer variable pointing to a Python string | 
|  | 719 | object.  If there is an existing interned string that is the same as | 
|  | 720 | \var{*string}, it sets \var{*string} to it (decrementing the | 
|  | 721 | reference count of the old string object and incrementing the | 
|  | 722 | reference count of the interned string object), otherwise it leaves | 
|  | 723 | \var{*string} alone and interns it (incrementing its reference | 
|  | 724 | count).  (Clarification: even though there is a lot of talk about | 
|  | 725 | reference counts, think of this function as reference-count-neutral; | 
|  | 726 | you own the object after the call if and only if you owned it before | 
|  | 727 | the call.) | 
|  | 728 | \end{cfuncdesc} | 
|  | 729 |  | 
|  | 730 | \begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v} | 
|  | 731 | A combination of \cfunction{PyString_FromString()} and | 
|  | 732 | \cfunction{PyString_InternInPlace()}, returning either a new string | 
|  | 733 | object that has been interned, or a new (``owned'') reference to an | 
|  | 734 | earlier interned string object with the same value. | 
|  | 735 | \end{cfuncdesc} | 
|  | 736 |  | 
|  | 737 | \begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s, | 
|  | 738 | int size, | 
|  | 739 | const char *encoding, | 
|  | 740 | const char *errors} | 
|  | 741 | Creates an object by decoding \var{size} bytes of the encoded | 
|  | 742 | buffer \var{s} using the codec registered for | 
|  | 743 | \var{encoding}.  \var{encoding} and \var{errors} have the same | 
|  | 744 | meaning as the parameters of the same name in the | 
|  | 745 | \function{unicode()} built-in function.  The codec to be used is | 
|  | 746 | looked up using the Python codec registry.  Returns \NULL{} if | 
|  | 747 | an exception was raised by the codec. | 
|  | 748 | \end{cfuncdesc} | 
|  | 749 |  | 
|  | 750 | \begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str, | 
|  | 751 | const char *encoding, | 
|  | 752 | const char *errors} | 
|  | 753 | Decodes a string object by passing it to the codec registered for | 
|  | 754 | \var{encoding} and returns the result as Python | 
|  | 755 | object. \var{encoding} and \var{errors} have the same meaning as the | 
|  | 756 | parameters of the same name in the string \method{encode()} method. | 
|  | 757 | The codec to be used is looked up using the Python codec registry. | 
|  | 758 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 759 | \end{cfuncdesc} | 
|  | 760 |  | 
|  | 761 | \begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s, | 
|  | 762 | int size, | 
|  | 763 | const char *encoding, | 
|  | 764 | const char *errors} | 
|  | 765 | Encodes the \ctype{char} buffer of the given size by passing it to | 
|  | 766 | the codec registered for \var{encoding} and returns a Python object. | 
|  | 767 | \var{encoding} and \var{errors} have the same meaning as the | 
|  | 768 | parameters of the same name in the string \method{encode()} method. | 
|  | 769 | The codec to be used is looked up using the Python codec | 
|  | 770 | registry.  Returns \NULL{} if an exception was raised by the | 
|  | 771 | codec. | 
|  | 772 | \end{cfuncdesc} | 
|  | 773 |  | 
|  | 774 | \begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str, | 
|  | 775 | const char *encoding, | 
|  | 776 | const char *errors} | 
|  | 777 | Encodes a string object using the codec registered for | 
|  | 778 | \var{encoding} and returns the result as Python object. | 
|  | 779 | \var{encoding} and \var{errors} have the same meaning as the | 
|  | 780 | parameters of the same name in the string \method{encode()} method. | 
|  | 781 | The codec to be used is looked up using the Python codec registry. | 
|  | 782 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 783 | \end{cfuncdesc} | 
|  | 784 |  | 
|  | 785 |  | 
|  | 786 | \subsection{Unicode Objects \label{unicodeObjects}} | 
|  | 787 | \sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com} | 
|  | 788 |  | 
|  | 789 | %--- Unicode Type ------------------------------------------------------- | 
|  | 790 |  | 
|  | 791 | These are the basic Unicode object types used for the Unicode | 
|  | 792 | implementation in Python: | 
|  | 793 |  | 
|  | 794 | \begin{ctypedesc}{Py_UNICODE} | 
|  | 795 | This type represents a 16-bit unsigned storage type which is used by | 
|  | 796 | Python internally as basis for holding Unicode ordinals.  On | 
|  | 797 | platforms where \ctype{wchar_t} is available and also has 16-bits, | 
|  | 798 | \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance | 
|  | 799 | native platform compatibility.  On all other platforms, | 
|  | 800 | \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}. | 
|  | 801 | \end{ctypedesc} | 
|  | 802 |  | 
|  | 803 | \begin{ctypedesc}{PyUnicodeObject} | 
|  | 804 | This subtype of \ctype{PyObject} represents a Python Unicode object. | 
|  | 805 | \end{ctypedesc} | 
|  | 806 |  | 
|  | 807 | \begin{cvardesc}{PyTypeObject}{PyUnicode_Type} | 
|  | 808 | This instance of \ctype{PyTypeObject} represents the Python Unicode | 
|  | 809 | type. | 
|  | 810 | \end{cvardesc} | 
|  | 811 |  | 
|  | 812 | The following APIs are really C macros and can be used to do fast | 
|  | 813 | checks and to access internal read-only data of Unicode objects: | 
|  | 814 |  | 
|  | 815 | \begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o} | 
|  | 816 | Returns true if the object \var{o} is a Unicode object or an | 
|  | 817 | instance of a Unicode subtype. | 
|  | 818 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 819 | \end{cfuncdesc} | 
|  | 820 |  | 
|  | 821 | \begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o} | 
|  | 822 | Returns true if the object \var{o} is a Unicode object, but not an | 
|  | 823 | instance of a subtype. | 
|  | 824 | \versionadded{2.2} | 
|  | 825 | \end{cfuncdesc} | 
|  | 826 |  | 
|  | 827 | \begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o} | 
|  | 828 | Returns the size of the object.  \var{o} has to be a | 
|  | 829 | \ctype{PyUnicodeObject} (not checked). | 
|  | 830 | \end{cfuncdesc} | 
|  | 831 |  | 
|  | 832 | \begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o} | 
|  | 833 | Returns the size of the object's internal buffer in bytes.  \var{o} | 
|  | 834 | has to be a \ctype{PyUnicodeObject} (not checked). | 
|  | 835 | \end{cfuncdesc} | 
|  | 836 |  | 
|  | 837 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o} | 
|  | 838 | Returns a pointer to the internal \ctype{Py_UNICODE} buffer of the | 
|  | 839 | object.  \var{o} has to be a \ctype{PyUnicodeObject} (not checked). | 
|  | 840 | \end{cfuncdesc} | 
|  | 841 |  | 
|  | 842 | \begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o} | 
|  | 843 | Returns a pointer to the internal buffer of the object. | 
|  | 844 | \var{o} has to be a \ctype{PyUnicodeObject} (not checked). | 
|  | 845 | \end{cfuncdesc} | 
|  | 846 |  | 
|  | 847 | % --- Unicode character properties --------------------------------------- | 
|  | 848 |  | 
|  | 849 | Unicode provides many different character properties. The most often | 
|  | 850 | needed ones are available through these macros which are mapped to C | 
|  | 851 | functions depending on the Python configuration. | 
|  | 852 |  | 
|  | 853 | \begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch} | 
|  | 854 | Returns 1/0 depending on whether \var{ch} is a whitespace | 
|  | 855 | character. | 
|  | 856 | \end{cfuncdesc} | 
|  | 857 |  | 
|  | 858 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch} | 
|  | 859 | Returns 1/0 depending on whether \var{ch} is a lowercase character. | 
|  | 860 | \end{cfuncdesc} | 
|  | 861 |  | 
|  | 862 | \begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch} | 
|  | 863 | Returns 1/0 depending on whether \var{ch} is an uppercase | 
|  | 864 | character. | 
|  | 865 | \end{cfuncdesc} | 
|  | 866 |  | 
|  | 867 | \begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch} | 
|  | 868 | Returns 1/0 depending on whether \var{ch} is a titlecase character. | 
|  | 869 | \end{cfuncdesc} | 
|  | 870 |  | 
|  | 871 | \begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch} | 
|  | 872 | Returns 1/0 depending on whether \var{ch} is a linebreak character. | 
|  | 873 | \end{cfuncdesc} | 
|  | 874 |  | 
|  | 875 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch} | 
|  | 876 | Returns 1/0 depending on whether \var{ch} is a decimal character. | 
|  | 877 | \end{cfuncdesc} | 
|  | 878 |  | 
|  | 879 | \begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch} | 
|  | 880 | Returns 1/0 depending on whether \var{ch} is a digit character. | 
|  | 881 | \end{cfuncdesc} | 
|  | 882 |  | 
|  | 883 | \begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch} | 
|  | 884 | Returns 1/0 depending on whether \var{ch} is a numeric character. | 
|  | 885 | \end{cfuncdesc} | 
|  | 886 |  | 
|  | 887 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch} | 
|  | 888 | Returns 1/0 depending on whether \var{ch} is an alphabetic | 
|  | 889 | character. | 
|  | 890 | \end{cfuncdesc} | 
|  | 891 |  | 
|  | 892 | \begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch} | 
|  | 893 | Returns 1/0 depending on whether \var{ch} is an alphanumeric | 
|  | 894 | character. | 
|  | 895 | \end{cfuncdesc} | 
|  | 896 |  | 
|  | 897 | These APIs can be used for fast direct character conversions: | 
|  | 898 |  | 
|  | 899 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch} | 
|  | 900 | Returns the character \var{ch} converted to lower case. | 
|  | 901 | \end{cfuncdesc} | 
|  | 902 |  | 
|  | 903 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch} | 
|  | 904 | Returns the character \var{ch} converted to upper case. | 
|  | 905 | \end{cfuncdesc} | 
|  | 906 |  | 
|  | 907 | \begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch} | 
|  | 908 | Returns the character \var{ch} converted to title case. | 
|  | 909 | \end{cfuncdesc} | 
|  | 910 |  | 
|  | 911 | \begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch} | 
|  | 912 | Returns the character \var{ch} converted to a decimal positive | 
|  | 913 | integer.  Returns \code{-1} if this is not possible.  Does not raise | 
|  | 914 | exceptions. | 
|  | 915 | \end{cfuncdesc} | 
|  | 916 |  | 
|  | 917 | \begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch} | 
|  | 918 | Returns the character \var{ch} converted to a single digit integer. | 
|  | 919 | Returns \code{-1} if this is not possible.  Does not raise | 
|  | 920 | exceptions. | 
|  | 921 | \end{cfuncdesc} | 
|  | 922 |  | 
|  | 923 | \begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch} | 
|  | 924 | Returns the character \var{ch} converted to a (positive) double. | 
|  | 925 | Returns \code{-1.0} if this is not possible.  Does not raise | 
|  | 926 | exceptions. | 
|  | 927 | \end{cfuncdesc} | 
|  | 928 |  | 
|  | 929 | % --- Plain Py_UNICODE --------------------------------------------------- | 
|  | 930 |  | 
|  | 931 | To create Unicode objects and access their basic sequence properties, | 
|  | 932 | use these APIs: | 
|  | 933 |  | 
|  | 934 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u, | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 935 | int size} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 936 | Create a Unicode Object from the Py_UNICODE buffer \var{u} of the | 
|  | 937 | given size. \var{u} may be \NULL{} which causes the contents to be | 
|  | 938 | undefined. It is the user's responsibility to fill in the needed | 
|  | 939 | data.  The buffer is copied into the new object. If the buffer is | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 940 | not \NULL{}, the return value might be a shared object. Therefore, | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 941 | modification of the resulting Unicode object is only allowed when | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 942 | \var{u} is \NULL{}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 943 | \end{cfuncdesc} | 
|  | 944 |  | 
|  | 945 | \begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode} | 
|  | 946 | Return a read-only pointer to the Unicode object's internal | 
|  | 947 | \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode | 
|  | 948 | object. | 
|  | 949 | \end{cfuncdesc} | 
|  | 950 |  | 
|  | 951 | \begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode} | 
|  | 952 | Return the length of the Unicode object. | 
|  | 953 | \end{cfuncdesc} | 
|  | 954 |  | 
|  | 955 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj, | 
|  | 956 | const char *encoding, | 
|  | 957 | const char *errors} | 
|  | 958 | Coerce an encoded object \var{obj} to an Unicode object and return a | 
|  | 959 | reference with incremented refcount. | 
|  | 960 |  | 
|  | 961 | Coercion is done in the following way: | 
|  | 962 |  | 
|  | 963 | \begin{enumerate} | 
|  | 964 | \item  Unicode objects are passed back as-is with incremented | 
|  | 965 | refcount. \note{These cannot be decoded; passing a non-\NULL{} | 
|  | 966 | value for encoding will result in a \exception{TypeError}.} | 
|  | 967 |  | 
|  | 968 | \item String and other char buffer compatible objects are decoded | 
|  | 969 | according to the given encoding and using the error handling | 
|  | 970 | defined by errors.  Both can be \NULL{} to have the interface | 
|  | 971 | use the default values (see the next section for details). | 
|  | 972 |  | 
|  | 973 | \item All other objects cause an exception. | 
|  | 974 | \end{enumerate} | 
|  | 975 |  | 
|  | 976 | The API returns \NULL{} if there was an error.  The caller is | 
|  | 977 | responsible for decref'ing the returned objects. | 
|  | 978 | \end{cfuncdesc} | 
|  | 979 |  | 
|  | 980 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj} | 
|  | 981 | Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")} | 
|  | 982 | which is used throughout the interpreter whenever coercion to | 
|  | 983 | Unicode is needed. | 
|  | 984 | \end{cfuncdesc} | 
|  | 985 |  | 
|  | 986 | % --- wchar_t support for platforms which support it --------------------- | 
|  | 987 |  | 
|  | 988 | If the platform supports \ctype{wchar_t} and provides a header file | 
|  | 989 | wchar.h, Python can interface directly to this type using the | 
|  | 990 | following functions. Support is optimized if Python's own | 
|  | 991 | \ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}. | 
|  | 992 |  | 
|  | 993 | \begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w, | 
|  | 994 | int size} | 
| Thomas Heller | 541703b | 2002-04-29 17:28:43 +0000 | [diff] [blame] | 995 | Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 996 | the given size.  Returns \NULL{} on failure. | 
|  | 997 | \end{cfuncdesc} | 
|  | 998 |  | 
|  | 999 | \begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode, | 
|  | 1000 | wchar_t *w, | 
|  | 1001 | int size} | 
| Thomas Heller | 541703b | 2002-04-29 17:28:43 +0000 | [diff] [blame] | 1002 | Copies the Unicode object contents into the \ctype{wchar_t} buffer | 
|  | 1003 | \var{w}.  At most \var{size} \ctype{wchar_t} characters are copied. | 
|  | 1004 | Returns the number of \ctype{wchar_t} characters copied or -1 in | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1005 | case of an error. | 
|  | 1006 | \end{cfuncdesc} | 
|  | 1007 |  | 
|  | 1008 |  | 
|  | 1009 | \subsubsection{Built-in Codecs \label{builtinCodecs}} | 
|  | 1010 |  | 
|  | 1011 | Python provides a set of builtin codecs which are written in C | 
|  | 1012 | for speed. All of these codecs are directly usable via the | 
|  | 1013 | following functions. | 
|  | 1014 |  | 
|  | 1015 | Many of the following APIs take two arguments encoding and | 
|  | 1016 | errors. These parameters encoding and errors have the same semantics | 
|  | 1017 | as the ones of the builtin unicode() Unicode object constructor. | 
|  | 1018 |  | 
|  | 1019 | Setting encoding to \NULL{} causes the default encoding to be used | 
|  | 1020 | which is \ASCII.  The file system calls should use | 
|  | 1021 | \cdata{Py_FileSystemDefaultEncoding} as the encoding for file | 
|  | 1022 | names. This variable should be treated as read-only: On some systems, | 
|  | 1023 | it will be a pointer to a static string, on others, it will change at | 
| Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 1024 | run-time (such as when the application invokes setlocale). | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1025 |  | 
|  | 1026 | Error handling is set by errors which may also be set to \NULL{} | 
|  | 1027 | meaning to use the default handling defined for the codec.  Default | 
|  | 1028 | error handling for all builtin codecs is ``strict'' | 
|  | 1029 | (\exception{ValueError} is raised). | 
|  | 1030 |  | 
|  | 1031 | The codecs all use a similar interface.  Only deviation from the | 
|  | 1032 | following generic ones are documented for simplicity. | 
|  | 1033 |  | 
|  | 1034 | % --- Generic Codecs ----------------------------------------------------- | 
|  | 1035 |  | 
|  | 1036 | These are the generic codec APIs: | 
|  | 1037 |  | 
|  | 1038 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s, | 
|  | 1039 | int size, | 
|  | 1040 | const char *encoding, | 
|  | 1041 | const char *errors} | 
|  | 1042 | Create a Unicode object by decoding \var{size} bytes of the encoded | 
|  | 1043 | string \var{s}.  \var{encoding} and \var{errors} have the same | 
|  | 1044 | meaning as the parameters of the same name in the | 
|  | 1045 | \function{unicode()} builtin function.  The codec to be used is | 
|  | 1046 | looked up using the Python codec registry.  Returns \NULL{} if an | 
|  | 1047 | exception was raised by the codec. | 
|  | 1048 | \end{cfuncdesc} | 
|  | 1049 |  | 
|  | 1050 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s, | 
|  | 1051 | int size, | 
|  | 1052 | const char *encoding, | 
|  | 1053 | const char *errors} | 
|  | 1054 | Encodes the \ctype{Py_UNICODE} buffer of the given size and returns | 
|  | 1055 | a Python string object.  \var{encoding} and \var{errors} have the | 
|  | 1056 | same meaning as the parameters of the same name in the Unicode | 
|  | 1057 | \method{encode()} method.  The codec to be used is looked up using | 
|  | 1058 | the Python codec registry.  Returns \NULL{} if an exception was | 
|  | 1059 | raised by the codec. | 
|  | 1060 | \end{cfuncdesc} | 
|  | 1061 |  | 
|  | 1062 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode, | 
|  | 1063 | const char *encoding, | 
|  | 1064 | const char *errors} | 
|  | 1065 | Encodes a Unicode object and returns the result as Python string | 
|  | 1066 | object. \var{encoding} and \var{errors} have the same meaning as the | 
|  | 1067 | parameters of the same name in the Unicode \method{encode()} method. | 
|  | 1068 | The codec to be used is looked up using the Python codec registry. | 
|  | 1069 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 1070 | \end{cfuncdesc} | 
|  | 1071 |  | 
|  | 1072 | % --- UTF-8 Codecs ------------------------------------------------------- | 
|  | 1073 |  | 
|  | 1074 | These are the UTF-8 codec APIs: | 
|  | 1075 |  | 
|  | 1076 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s, | 
|  | 1077 | int size, | 
|  | 1078 | const char *errors} | 
|  | 1079 | Creates a Unicode object by decoding \var{size} bytes of the UTF-8 | 
|  | 1080 | encoded string \var{s}. Returns \NULL{} if an exception was raised | 
|  | 1081 | by the codec. | 
|  | 1082 | \end{cfuncdesc} | 
|  | 1083 |  | 
|  | 1084 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s, | 
|  | 1085 | int size, | 
|  | 1086 | const char *errors} | 
|  | 1087 | Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8 | 
|  | 1088 | and returns a Python string object.  Returns \NULL{} if an exception | 
|  | 1089 | was raised by the codec. | 
|  | 1090 | \end{cfuncdesc} | 
|  | 1091 |  | 
|  | 1092 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode} | 
|  | 1093 | Encodes a Unicode objects using UTF-8 and returns the result as | 
|  | 1094 | Python string object.  Error handling is ``strict''.  Returns | 
|  | 1095 | \NULL{} if an exception was raised by the codec. | 
|  | 1096 | \end{cfuncdesc} | 
|  | 1097 |  | 
|  | 1098 | % --- UTF-16 Codecs ------------------------------------------------------ */ | 
|  | 1099 |  | 
|  | 1100 | These are the UTF-16 codec APIs: | 
|  | 1101 |  | 
|  | 1102 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s, | 
|  | 1103 | int size, | 
|  | 1104 | const char *errors, | 
|  | 1105 | int *byteorder} | 
|  | 1106 | Decodes \var{length} bytes from a UTF-16 encoded buffer string and | 
|  | 1107 | returns the corresponding Unicode object.  \var{errors} (if | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1108 | non-\NULL{}) defines the error handling. It defaults to ``strict''. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1109 |  | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1110 | If \var{byteorder} is non-\NULL{}, the decoder starts decoding using | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1111 | the given byte order: | 
|  | 1112 |  | 
|  | 1113 | \begin{verbatim} | 
|  | 1114 | *byteorder == -1: little endian | 
|  | 1115 | *byteorder == 0:  native order | 
|  | 1116 | *byteorder == 1:  big endian | 
|  | 1117 | \end{verbatim} | 
|  | 1118 |  | 
|  | 1119 | and then switches according to all byte order marks (BOM) it finds | 
|  | 1120 | in the input data.  BOMs are not copied into the resulting Unicode | 
|  | 1121 | string.  After completion, \var{*byteorder} is set to the current | 
|  | 1122 | byte order at the end of input data. | 
|  | 1123 |  | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1124 | If \var{byteorder} is \NULL{}, the codec starts in native order mode. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1125 |  | 
|  | 1126 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 1127 | \end{cfuncdesc} | 
|  | 1128 |  | 
|  | 1129 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s, | 
|  | 1130 | int size, | 
|  | 1131 | const char *errors, | 
|  | 1132 | int byteorder} | 
|  | 1133 | Returns a Python string object holding the UTF-16 encoded value of | 
|  | 1134 | the Unicode data in \var{s}.  If \var{byteorder} is not \code{0}, | 
|  | 1135 | output is written according to the following byte order: | 
|  | 1136 |  | 
|  | 1137 | \begin{verbatim} | 
|  | 1138 | byteorder == -1: little endian | 
|  | 1139 | byteorder == 0:  native byte order (writes a BOM mark) | 
|  | 1140 | byteorder == 1:  big endian | 
|  | 1141 | \end{verbatim} | 
|  | 1142 |  | 
|  | 1143 | If byteorder is \code{0}, the output string will always start with | 
|  | 1144 | the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark | 
|  | 1145 | is prepended. | 
|  | 1146 |  | 
| Martin v. Löwis | 9bc4f2d | 2004-06-03 09:55:28 +0000 | [diff] [blame] | 1147 | If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE} | 
|  | 1148 | value may get represented as a surrogate pair. If it is not | 
|  | 1149 | defined, each \ctype{Py_UNICODE} values is interpreted as an | 
|  | 1150 | UCS-2 character. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1151 |  | 
|  | 1152 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 1153 | \end{cfuncdesc} | 
|  | 1154 |  | 
|  | 1155 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode} | 
|  | 1156 | Returns a Python string using the UTF-16 encoding in native byte | 
|  | 1157 | order. The string always starts with a BOM mark.  Error handling is | 
|  | 1158 | ``strict''.  Returns \NULL{} if an exception was raised by the | 
|  | 1159 | codec. | 
|  | 1160 | \end{cfuncdesc} | 
|  | 1161 |  | 
|  | 1162 | % --- Unicode-Escape Codecs ---------------------------------------------- | 
|  | 1163 |  | 
| Martin v. Löwis | 95cf84a | 2003-10-19 07:32:24 +0000 | [diff] [blame] | 1164 | These are the ``Unicode Escape'' codec APIs: | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1165 |  | 
|  | 1166 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s, | 
|  | 1167 | int size, | 
|  | 1168 | const char *errors} | 
|  | 1169 | Creates a Unicode object by decoding \var{size} bytes of the | 
|  | 1170 | Unicode-Escape encoded string \var{s}.  Returns \NULL{} if an | 
|  | 1171 | exception was raised by the codec. | 
|  | 1172 | \end{cfuncdesc} | 
|  | 1173 |  | 
|  | 1174 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s, | 
|  | 1175 | int size, | 
|  | 1176 | const char *errors} | 
|  | 1177 | Encodes the \ctype{Py_UNICODE} buffer of the given size using | 
|  | 1178 | Unicode-Escape and returns a Python string object.  Returns \NULL{} | 
|  | 1179 | if an exception was raised by the codec. | 
|  | 1180 | \end{cfuncdesc} | 
|  | 1181 |  | 
|  | 1182 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode} | 
|  | 1183 | Encodes a Unicode objects using Unicode-Escape and returns the | 
|  | 1184 | result as Python string object.  Error handling is ``strict''. | 
|  | 1185 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 1186 | \end{cfuncdesc} | 
|  | 1187 |  | 
|  | 1188 | % --- Raw-Unicode-Escape Codecs ------------------------------------------ | 
|  | 1189 |  | 
| Martin v. Löwis | 95cf84a | 2003-10-19 07:32:24 +0000 | [diff] [blame] | 1190 | These are the ``Raw Unicode Escape'' codec APIs: | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1191 |  | 
|  | 1192 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s, | 
|  | 1193 | int size, | 
|  | 1194 | const char *errors} | 
|  | 1195 | Creates a Unicode object by decoding \var{size} bytes of the | 
| Martin v. Löwis | 95cf84a | 2003-10-19 07:32:24 +0000 | [diff] [blame] | 1196 | Raw-Unicode-Escape encoded string \var{s}.  Returns \NULL{} if an | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1197 | exception was raised by the codec. | 
|  | 1198 | \end{cfuncdesc} | 
|  | 1199 |  | 
|  | 1200 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s, | 
|  | 1201 | int size, | 
|  | 1202 | const char *errors} | 
|  | 1203 | Encodes the \ctype{Py_UNICODE} buffer of the given size using | 
|  | 1204 | Raw-Unicode-Escape and returns a Python string object.  Returns | 
|  | 1205 | \NULL{} if an exception was raised by the codec. | 
|  | 1206 | \end{cfuncdesc} | 
|  | 1207 |  | 
|  | 1208 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode} | 
|  | 1209 | Encodes a Unicode objects using Raw-Unicode-Escape and returns the | 
|  | 1210 | result as Python string object. Error handling is ``strict''. | 
|  | 1211 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 1212 | \end{cfuncdesc} | 
|  | 1213 |  | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1214 | % --- Latin-1 Codecs ----------------------------------------------------- | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1215 |  | 
|  | 1216 | These are the Latin-1 codec APIs: | 
|  | 1217 | Latin-1 corresponds to the first 256 Unicode ordinals and only these | 
|  | 1218 | are accepted by the codecs during encoding. | 
|  | 1219 |  | 
|  | 1220 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s, | 
|  | 1221 | int size, | 
|  | 1222 | const char *errors} | 
|  | 1223 | Creates a Unicode object by decoding \var{size} bytes of the Latin-1 | 
|  | 1224 | encoded string \var{s}.  Returns \NULL{} if an exception was raised | 
|  | 1225 | by the codec. | 
|  | 1226 | \end{cfuncdesc} | 
|  | 1227 |  | 
|  | 1228 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s, | 
|  | 1229 | int size, | 
|  | 1230 | const char *errors} | 
|  | 1231 | Encodes the \ctype{Py_UNICODE} buffer of the given size using | 
|  | 1232 | Latin-1 and returns a Python string object.  Returns \NULL{} if an | 
|  | 1233 | exception was raised by the codec. | 
|  | 1234 | \end{cfuncdesc} | 
|  | 1235 |  | 
|  | 1236 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode} | 
|  | 1237 | Encodes a Unicode objects using Latin-1 and returns the result as | 
|  | 1238 | Python string object.  Error handling is ``strict''.  Returns | 
|  | 1239 | \NULL{} if an exception was raised by the codec. | 
|  | 1240 | \end{cfuncdesc} | 
|  | 1241 |  | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1242 | % --- ASCII Codecs ------------------------------------------------------- | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1243 |  | 
|  | 1244 | These are the \ASCII{} codec APIs.  Only 7-bit \ASCII{} data is | 
|  | 1245 | accepted. All other codes generate errors. | 
|  | 1246 |  | 
|  | 1247 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s, | 
|  | 1248 | int size, | 
|  | 1249 | const char *errors} | 
|  | 1250 | Creates a Unicode object by decoding \var{size} bytes of the | 
|  | 1251 | \ASCII{} encoded string \var{s}.  Returns \NULL{} if an exception | 
|  | 1252 | was raised by the codec. | 
|  | 1253 | \end{cfuncdesc} | 
|  | 1254 |  | 
|  | 1255 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s, | 
|  | 1256 | int size, | 
|  | 1257 | const char *errors} | 
|  | 1258 | Encodes the \ctype{Py_UNICODE} buffer of the given size using | 
|  | 1259 | \ASCII{} and returns a Python string object.  Returns \NULL{} if an | 
|  | 1260 | exception was raised by the codec. | 
|  | 1261 | \end{cfuncdesc} | 
|  | 1262 |  | 
|  | 1263 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode} | 
|  | 1264 | Encodes a Unicode objects using \ASCII{} and returns the result as | 
|  | 1265 | Python string object.  Error handling is ``strict''.  Returns | 
|  | 1266 | \NULL{} if an exception was raised by the codec. | 
|  | 1267 | \end{cfuncdesc} | 
|  | 1268 |  | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1269 | % --- Character Map Codecs ----------------------------------------------- | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1270 |  | 
|  | 1271 | These are the mapping codec APIs: | 
|  | 1272 |  | 
|  | 1273 | This codec is special in that it can be used to implement many | 
|  | 1274 | different codecs (and this is in fact what was done to obtain most of | 
|  | 1275 | the standard codecs included in the \module{encodings} package). The | 
|  | 1276 | codec uses mapping to encode and decode characters. | 
|  | 1277 |  | 
|  | 1278 | Decoding mappings must map single string characters to single Unicode | 
|  | 1279 | characters, integers (which are then interpreted as Unicode ordinals) | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1280 | or None (meaning "undefined mapping" and causing an error). | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1281 |  | 
|  | 1282 | Encoding mappings must map single Unicode characters to single string | 
|  | 1283 | characters, integers (which are then interpreted as Latin-1 ordinals) | 
|  | 1284 | or None (meaning "undefined mapping" and causing an error). | 
|  | 1285 |  | 
|  | 1286 | The mapping objects provided must only support the __getitem__ mapping | 
|  | 1287 | interface. | 
|  | 1288 |  | 
|  | 1289 | If a character lookup fails with a LookupError, the character is | 
|  | 1290 | copied as-is meaning that its ordinal value will be interpreted as | 
|  | 1291 | Unicode or Latin-1 ordinal resp. Because of this, mappings only need | 
|  | 1292 | to contain those mappings which map characters to different code | 
|  | 1293 | points. | 
|  | 1294 |  | 
|  | 1295 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s, | 
|  | 1296 | int size, | 
|  | 1297 | PyObject *mapping, | 
|  | 1298 | const char *errors} | 
|  | 1299 | Creates a Unicode object by decoding \var{size} bytes of the encoded | 
|  | 1300 | string \var{s} using the given \var{mapping} object.  Returns | 
|  | 1301 | \NULL{} if an exception was raised by the codec. | 
|  | 1302 | \end{cfuncdesc} | 
|  | 1303 |  | 
|  | 1304 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s, | 
|  | 1305 | int size, | 
|  | 1306 | PyObject *mapping, | 
|  | 1307 | const char *errors} | 
|  | 1308 | Encodes the \ctype{Py_UNICODE} buffer of the given size using the | 
|  | 1309 | given \var{mapping} object and returns a Python string object. | 
|  | 1310 | Returns \NULL{} if an exception was raised by the codec. | 
|  | 1311 | \end{cfuncdesc} | 
|  | 1312 |  | 
|  | 1313 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode, | 
|  | 1314 | PyObject *mapping} | 
|  | 1315 | Encodes a Unicode objects using the given \var{mapping} object and | 
|  | 1316 | returns the result as Python string object.  Error handling is | 
|  | 1317 | ``strict''.  Returns \NULL{} if an exception was raised by the | 
|  | 1318 | codec. | 
|  | 1319 | \end{cfuncdesc} | 
|  | 1320 |  | 
|  | 1321 | The following codec API is special in that maps Unicode to Unicode. | 
|  | 1322 |  | 
|  | 1323 | \begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s, | 
|  | 1324 | int size, | 
|  | 1325 | PyObject *table, | 
|  | 1326 | const char *errors} | 
|  | 1327 | Translates a \ctype{Py_UNICODE} buffer of the given length by | 
|  | 1328 | applying a character mapping \var{table} to it and returns the | 
|  | 1329 | resulting Unicode object.  Returns \NULL{} when an exception was | 
|  | 1330 | raised by the codec. | 
|  | 1331 |  | 
|  | 1332 | The \var{mapping} table must map Unicode ordinal integers to Unicode | 
|  | 1333 | ordinal integers or None (causing deletion of the character). | 
|  | 1334 |  | 
|  | 1335 | Mapping tables need only provide the method{__getitem__()} | 
|  | 1336 | interface; dictionaries and sequences work well.  Unmapped character | 
|  | 1337 | ordinals (ones which cause a \exception{LookupError}) are left | 
|  | 1338 | untouched and are copied as-is. | 
|  | 1339 | \end{cfuncdesc} | 
|  | 1340 |  | 
|  | 1341 | % --- MBCS codecs for Windows -------------------------------------------- | 
|  | 1342 |  | 
|  | 1343 | These are the MBCS codec APIs. They are currently only available on | 
|  | 1344 | Windows and use the Win32 MBCS converters to implement the | 
|  | 1345 | conversions.  Note that MBCS (or DBCS) is a class of encodings, not | 
|  | 1346 | just one.  The target encoding is defined by the user settings on the | 
|  | 1347 | machine running the codec. | 
|  | 1348 |  | 
|  | 1349 | \begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s, | 
|  | 1350 | int size, | 
|  | 1351 | const char *errors} | 
|  | 1352 | Creates a Unicode object by decoding \var{size} bytes of the MBCS | 
|  | 1353 | encoded string \var{s}.  Returns \NULL{} if an exception was | 
|  | 1354 | raised by the codec. | 
|  | 1355 | \end{cfuncdesc} | 
|  | 1356 |  | 
|  | 1357 | \begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s, | 
|  | 1358 | int size, | 
|  | 1359 | const char *errors} | 
|  | 1360 | Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS | 
|  | 1361 | and returns a Python string object.  Returns \NULL{} if an exception | 
|  | 1362 | was raised by the codec. | 
|  | 1363 | \end{cfuncdesc} | 
|  | 1364 |  | 
|  | 1365 | \begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode} | 
|  | 1366 | Encodes a Unicode objects using MBCS and returns the result as | 
|  | 1367 | Python string object.  Error handling is ``strict''.  Returns | 
|  | 1368 | \NULL{} if an exception was raised by the codec. | 
|  | 1369 | \end{cfuncdesc} | 
|  | 1370 |  | 
|  | 1371 | % --- Methods & Slots ---------------------------------------------------- | 
|  | 1372 |  | 
|  | 1373 | \subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}} | 
|  | 1374 |  | 
|  | 1375 | The following APIs are capable of handling Unicode objects and strings | 
|  | 1376 | on input (we refer to them as strings in the descriptions) and return | 
| Martin v. Löwis | 95cf84a | 2003-10-19 07:32:24 +0000 | [diff] [blame] | 1377 | Unicode objects or integers as appropriate. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1378 |  | 
|  | 1379 | They all return \NULL{} or \code{-1} if an exception occurs. | 
|  | 1380 |  | 
|  | 1381 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left, | 
|  | 1382 | PyObject *right} | 
|  | 1383 | Concat two strings giving a new Unicode string. | 
|  | 1384 | \end{cfuncdesc} | 
|  | 1385 |  | 
|  | 1386 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s, | 
|  | 1387 | PyObject *sep, | 
|  | 1388 | int maxsplit} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1389 | Split a string giving a list of Unicode strings.  If sep is \NULL{}, | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1390 | splitting will be done at all whitespace substrings.  Otherwise, | 
|  | 1391 | splits occur at the given separator.  At most \var{maxsplit} splits | 
|  | 1392 | will be done.  If negative, no limit is set.  Separators are not | 
|  | 1393 | included in the resulting list. | 
|  | 1394 | \end{cfuncdesc} | 
|  | 1395 |  | 
|  | 1396 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s, | 
| Martin v. Löwis | 24b8881 | 2003-03-30 16:40:42 +0000 | [diff] [blame] | 1397 | int keepend} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1398 | Split a Unicode string at line breaks, returning a list of Unicode | 
| Martin v. Löwis | 24b8881 | 2003-03-30 16:40:42 +0000 | [diff] [blame] | 1399 | strings.  CRLF is considered to be one line break.  If \var{keepend} | 
|  | 1400 | is 0, the Line break characters are not included in the resulting | 
|  | 1401 | strings. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1402 | \end{cfuncdesc} | 
|  | 1403 |  | 
|  | 1404 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str, | 
|  | 1405 | PyObject *table, | 
|  | 1406 | const char *errors} | 
|  | 1407 | Translate a string by applying a character mapping table to it and | 
|  | 1408 | return the resulting Unicode object. | 
|  | 1409 |  | 
|  | 1410 | The mapping table must map Unicode ordinal integers to Unicode | 
|  | 1411 | ordinal integers or None (causing deletion of the character). | 
|  | 1412 |  | 
|  | 1413 | Mapping tables need only provide the \method{__getitem__()} | 
|  | 1414 | interface; dictionaries and sequences work well.  Unmapped character | 
|  | 1415 | ordinals (ones which cause a \exception{LookupError}) are left | 
|  | 1416 | untouched and are copied as-is. | 
|  | 1417 |  | 
|  | 1418 | \var{errors} has the usual meaning for codecs. It may be \NULL{} | 
|  | 1419 | which indicates to use the default error handling. | 
|  | 1420 | \end{cfuncdesc} | 
|  | 1421 |  | 
|  | 1422 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator, | 
|  | 1423 | PyObject *seq} | 
|  | 1424 | Join a sequence of strings using the given separator and return the | 
|  | 1425 | resulting Unicode string. | 
|  | 1426 | \end{cfuncdesc} | 
|  | 1427 |  | 
|  | 1428 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str, | 
|  | 1429 | PyObject *substr, | 
|  | 1430 | int start, | 
|  | 1431 | int end, | 
|  | 1432 | int direction} | 
|  | 1433 | Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at | 
|  | 1434 | the given tail end (\var{direction} == -1 means to do a prefix | 
|  | 1435 | match, \var{direction} == 1 a suffix match), 0 otherwise. | 
|  | 1436 | \end{cfuncdesc} | 
|  | 1437 |  | 
| Fred Drake | 1d1e1db | 2002-06-20 22:07:04 +0000 | [diff] [blame] | 1438 | \begin{cfuncdesc}{int}{PyUnicode_Find}{PyObject *str, | 
|  | 1439 | PyObject *substr, | 
|  | 1440 | int start, | 
|  | 1441 | int end, | 
|  | 1442 | int direction} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1443 | Return the first position of \var{substr} in | 
|  | 1444 | \var{str}[\var{start}:\var{end}] using the given \var{direction} | 
|  | 1445 | (\var{direction} == 1 means to do a forward search, | 
| Fred Drake | 1d1e1db | 2002-06-20 22:07:04 +0000 | [diff] [blame] | 1446 | \var{direction} == -1 a backward search).  The return value is the | 
|  | 1447 | index of the first match; a value of \code{-1} indicates that no | 
|  | 1448 | match was found, and \code{-2} indicates that an error occurred and | 
|  | 1449 | an exception has been set. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1450 | \end{cfuncdesc} | 
|  | 1451 |  | 
| Fred Drake | 1d1e1db | 2002-06-20 22:07:04 +0000 | [diff] [blame] | 1452 | \begin{cfuncdesc}{int}{PyUnicode_Count}{PyObject *str, | 
|  | 1453 | PyObject *substr, | 
|  | 1454 | int start, | 
|  | 1455 | int end} | 
|  | 1456 | Return the number of non-overlapping occurrences of \var{substr} in | 
|  | 1457 | \code{\var{str}[\var{start}:\var{end}]}.  Returns \code{-1} if an | 
|  | 1458 | error occurred. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1459 | \end{cfuncdesc} | 
|  | 1460 |  | 
|  | 1461 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str, | 
|  | 1462 | PyObject *substr, | 
|  | 1463 | PyObject *replstr, | 
|  | 1464 | int maxcount} | 
|  | 1465 | Replace at most \var{maxcount} occurrences of \var{substr} in | 
|  | 1466 | \var{str} with \var{replstr} and return the resulting Unicode object. | 
|  | 1467 | \var{maxcount} == -1 means replace all occurrences. | 
|  | 1468 | \end{cfuncdesc} | 
|  | 1469 |  | 
|  | 1470 | \begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right} | 
|  | 1471 | Compare two strings and return -1, 0, 1 for less than, equal, and | 
|  | 1472 | greater than, respectively. | 
|  | 1473 | \end{cfuncdesc} | 
|  | 1474 |  | 
|  | 1475 | \begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format, | 
|  | 1476 | PyObject *args} | 
|  | 1477 | Returns a new string object from \var{format} and \var{args}; this | 
|  | 1478 | is analogous to \code{\var{format} \%\ \var{args}}.  The | 
|  | 1479 | \var{args} argument must be a tuple. | 
|  | 1480 | \end{cfuncdesc} | 
|  | 1481 |  | 
|  | 1482 | \begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container, | 
|  | 1483 | PyObject *element} | 
|  | 1484 | Checks whether \var{element} is contained in \var{container} and | 
|  | 1485 | returns true or false accordingly. | 
|  | 1486 |  | 
|  | 1487 | \var{element} has to coerce to a one element Unicode | 
|  | 1488 | string. \code{-1} is returned if there was an error. | 
|  | 1489 | \end{cfuncdesc} | 
|  | 1490 |  | 
|  | 1491 |  | 
|  | 1492 | \subsection{Buffer Objects \label{bufferObjects}} | 
|  | 1493 | \sectionauthor{Greg Stein}{gstein@lyra.org} | 
|  | 1494 |  | 
|  | 1495 | \obindex{buffer} | 
|  | 1496 | Python objects implemented in C can export a group of functions called | 
|  | 1497 | the ``buffer\index{buffer interface} interface.''  These functions can | 
|  | 1498 | be used by an object to expose its data in a raw, byte-oriented | 
|  | 1499 | format. Clients of the object can use the buffer interface to access | 
|  | 1500 | the object data directly, without needing to copy it first. | 
|  | 1501 |  | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1502 | Two examples of objects that support | 
|  | 1503 | the buffer interface are strings and arrays. The string object exposes | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1504 | the character contents in the buffer interface's byte-oriented | 
|  | 1505 | form. An array can also expose its contents, but it should be noted | 
|  | 1506 | that array elements may be multi-byte values. | 
|  | 1507 |  | 
|  | 1508 | An example user of the buffer interface is the file object's | 
|  | 1509 | \method{write()} method. Any object that can export a series of bytes | 
|  | 1510 | through the buffer interface can be written to a file. There are a | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1511 | number of format codes to \cfunction{PyArg_ParseTuple()} that operate | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1512 | against an object's buffer interface, returning data from the target | 
|  | 1513 | object. | 
|  | 1514 |  | 
|  | 1515 | More information on the buffer interface is provided in the section | 
| Fred Drake | 54e6294 | 2001-12-11 19:40:16 +0000 | [diff] [blame] | 1516 | ``Buffer Object Structures'' (section~\ref{buffer-structs}), under | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1517 | the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}. | 
|  | 1518 |  | 
|  | 1519 | A ``buffer object'' is defined in the \file{bufferobject.h} header | 
|  | 1520 | (included by \file{Python.h}). These objects look very similar to | 
|  | 1521 | string objects at the Python programming level: they support slicing, | 
|  | 1522 | indexing, concatenation, and some other standard string | 
|  | 1523 | operations. However, their data can come from one of two sources: from | 
|  | 1524 | a block of memory, or from another object which exports the buffer | 
|  | 1525 | interface. | 
|  | 1526 |  | 
|  | 1527 | Buffer objects are useful as a way to expose the data from another | 
|  | 1528 | object's buffer interface to the Python programmer. They can also be | 
|  | 1529 | used as a zero-copy slicing mechanism. Using their ability to | 
|  | 1530 | reference a block of memory, it is possible to expose any data to the | 
|  | 1531 | Python programmer quite easily. The memory could be a large, constant | 
|  | 1532 | array in a C extension, it could be a raw block of memory for | 
|  | 1533 | manipulation before passing to an operating system library, or it | 
|  | 1534 | could be used to pass around structured data in its native, in-memory | 
|  | 1535 | format. | 
|  | 1536 |  | 
|  | 1537 | \begin{ctypedesc}{PyBufferObject} | 
|  | 1538 | This subtype of \ctype{PyObject} represents a buffer object. | 
|  | 1539 | \end{ctypedesc} | 
|  | 1540 |  | 
|  | 1541 | \begin{cvardesc}{PyTypeObject}{PyBuffer_Type} | 
|  | 1542 | The instance of \ctype{PyTypeObject} which represents the Python | 
|  | 1543 | buffer type; it is the same object as \code{types.BufferType} in the | 
|  | 1544 | Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}. | 
|  | 1545 | \end{cvardesc} | 
|  | 1546 |  | 
|  | 1547 | \begin{cvardesc}{int}{Py_END_OF_BUFFER} | 
|  | 1548 | This constant may be passed as the \var{size} parameter to | 
|  | 1549 | \cfunction{PyBuffer_FromObject()} or | 
|  | 1550 | \cfunction{PyBuffer_FromReadWriteObject()}.  It indicates that the | 
|  | 1551 | new \ctype{PyBufferObject} should refer to \var{base} object from | 
|  | 1552 | the specified \var{offset} to the end of its exported buffer.  Using | 
|  | 1553 | this enables the caller to avoid querying the \var{base} object for | 
|  | 1554 | its length. | 
|  | 1555 | \end{cvardesc} | 
|  | 1556 |  | 
|  | 1557 | \begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p} | 
|  | 1558 | Return true if the argument has type \cdata{PyBuffer_Type}. | 
|  | 1559 | \end{cfuncdesc} | 
|  | 1560 |  | 
|  | 1561 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base, | 
|  | 1562 | int offset, int size} | 
|  | 1563 | Return a new read-only buffer object.  This raises | 
|  | 1564 | \exception{TypeError} if \var{base} doesn't support the read-only | 
|  | 1565 | buffer protocol or doesn't provide exactly one buffer segment, or it | 
|  | 1566 | raises \exception{ValueError} if \var{offset} is less than zero. The | 
|  | 1567 | buffer will hold a reference to the \var{base} object, and the | 
|  | 1568 | buffer's contents will refer to the \var{base} object's buffer | 
|  | 1569 | interface, starting as position \var{offset} and extending for | 
|  | 1570 | \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then | 
|  | 1571 | the new buffer's contents extend to the length of the \var{base} | 
|  | 1572 | object's exported buffer data. | 
|  | 1573 | \end{cfuncdesc} | 
|  | 1574 |  | 
|  | 1575 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base, | 
|  | 1576 | int offset, | 
|  | 1577 | int size} | 
|  | 1578 | Return a new writable buffer object.  Parameters and exceptions are | 
|  | 1579 | similar to those for \cfunction{PyBuffer_FromObject()}.  If the | 
|  | 1580 | \var{base} object does not export the writeable buffer protocol, | 
|  | 1581 | then \exception{TypeError} is raised. | 
|  | 1582 | \end{cfuncdesc} | 
|  | 1583 |  | 
|  | 1584 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size} | 
|  | 1585 | Return a new read-only buffer object that reads from a specified | 
|  | 1586 | location in memory, with a specified size.  The caller is | 
|  | 1587 | responsible for ensuring that the memory buffer, passed in as | 
|  | 1588 | \var{ptr}, is not deallocated while the returned buffer object | 
|  | 1589 | exists.  Raises \exception{ValueError} if \var{size} is less than | 
|  | 1590 | zero.  Note that \constant{Py_END_OF_BUFFER} may \emph{not} be | 
|  | 1591 | passed for the \var{size} parameter; \exception{ValueError} will be | 
|  | 1592 | raised in that case. | 
|  | 1593 | \end{cfuncdesc} | 
|  | 1594 |  | 
|  | 1595 | \begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size} | 
|  | 1596 | Similar to \cfunction{PyBuffer_FromMemory()}, but the returned | 
|  | 1597 | buffer is writable. | 
|  | 1598 | \end{cfuncdesc} | 
|  | 1599 |  | 
|  | 1600 | \begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size} | 
|  | 1601 | Returns a new writable buffer object that maintains its own memory | 
|  | 1602 | buffer of \var{size} bytes.  \exception{ValueError} is returned if | 
| Neil Schemenauer | d68d3ee | 2004-06-08 02:58:50 +0000 | [diff] [blame] | 1603 | \var{size} is not zero or positive.  Note that the memory buffer (as | 
|  | 1604 | returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically | 
|  | 1605 | aligned. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1606 | \end{cfuncdesc} | 
|  | 1607 |  | 
|  | 1608 |  | 
|  | 1609 | \subsection{Tuple Objects \label{tupleObjects}} | 
|  | 1610 |  | 
|  | 1611 | \obindex{tuple} | 
|  | 1612 | \begin{ctypedesc}{PyTupleObject} | 
|  | 1613 | This subtype of \ctype{PyObject} represents a Python tuple object. | 
|  | 1614 | \end{ctypedesc} | 
|  | 1615 |  | 
|  | 1616 | \begin{cvardesc}{PyTypeObject}{PyTuple_Type} | 
|  | 1617 | This instance of \ctype{PyTypeObject} represents the Python tuple | 
|  | 1618 | type; it is the same object as \code{types.TupleType} in the Python | 
|  | 1619 | layer.\withsubitem{(in module types)}{\ttindex{TupleType}}. | 
|  | 1620 | \end{cvardesc} | 
|  | 1621 |  | 
|  | 1622 | \begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p} | 
|  | 1623 | Return true if \var{p} is a tuple object or an instance of a subtype | 
|  | 1624 | of the tuple type. | 
|  | 1625 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 1626 | \end{cfuncdesc} | 
|  | 1627 |  | 
|  | 1628 | \begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p} | 
|  | 1629 | Return true if \var{p} is a tuple object, but not an instance of a | 
|  | 1630 | subtype of the tuple type. | 
|  | 1631 | \versionadded{2.2} | 
|  | 1632 | \end{cfuncdesc} | 
|  | 1633 |  | 
|  | 1634 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len} | 
|  | 1635 | Return a new tuple object of size \var{len}, or \NULL{} on failure. | 
|  | 1636 | \end{cfuncdesc} | 
|  | 1637 |  | 
| Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 1638 | \begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs} | 
|  | 1639 | Return a new tuple object of size \var{n}, or \NULL{} on failure. | 
|  | 1640 | The tuple values are initialized to the subsequent \var{n} C arguments | 
|  | 1641 | pointing to Python objects.  \samp{PyTuple_Pack(2, \var{a}, \var{b})} | 
|  | 1642 | is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1643 | \versionadded{2.4} | 
| Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 1644 | \end{cfuncdesc} | 
|  | 1645 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1646 | \begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p} | 
|  | 1647 | Takes a pointer to a tuple object, and returns the size of that | 
|  | 1648 | tuple. | 
|  | 1649 | \end{cfuncdesc} | 
|  | 1650 |  | 
|  | 1651 | \begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p} | 
|  | 1652 | Return the size of the tuple \var{p}, which must be non-\NULL{} and | 
|  | 1653 | point to a tuple; no error checking is performed. | 
|  | 1654 | \end{cfuncdesc} | 
|  | 1655 |  | 
|  | 1656 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos} | 
|  | 1657 | Returns the object at position \var{pos} in the tuple pointed to by | 
|  | 1658 | \var{p}.  If \var{pos} is out of bounds, returns \NULL{} and sets an | 
|  | 1659 | \exception{IndexError} exception. | 
|  | 1660 | \end{cfuncdesc} | 
|  | 1661 |  | 
|  | 1662 | \begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos} | 
|  | 1663 | Like \cfunction{PyTuple_GetItem()}, but does no checking of its | 
|  | 1664 | arguments. | 
|  | 1665 | \end{cfuncdesc} | 
|  | 1666 |  | 
|  | 1667 | \begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p, | 
|  | 1668 | int low, int high} | 
|  | 1669 | Takes a slice of the tuple pointed to by \var{p} from \var{low} to | 
|  | 1670 | \var{high} and returns it as a new tuple. | 
|  | 1671 | \end{cfuncdesc} | 
|  | 1672 |  | 
|  | 1673 | \begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p, | 
|  | 1674 | int pos, PyObject *o} | 
|  | 1675 | Inserts a reference to object \var{o} at position \var{pos} of the | 
|  | 1676 | tuple pointed to by \var{p}. It returns \code{0} on success. | 
|  | 1677 | \note{This function ``steals'' a reference to \var{o}.} | 
|  | 1678 | \end{cfuncdesc} | 
|  | 1679 |  | 
|  | 1680 | \begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p, | 
|  | 1681 | int pos, PyObject *o} | 
|  | 1682 | Like \cfunction{PyTuple_SetItem()}, but does no error checking, and | 
|  | 1683 | should \emph{only} be used to fill in brand new tuples.  \note{This | 
|  | 1684 | function ``steals'' a reference to \var{o}.} | 
|  | 1685 | \end{cfuncdesc} | 
|  | 1686 |  | 
|  | 1687 | \begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize} | 
|  | 1688 | Can be used to resize a tuple.  \var{newsize} will be the new length | 
|  | 1689 | of the tuple.  Because tuples are \emph{supposed} to be immutable, | 
|  | 1690 | this should only be used if there is only one reference to the | 
|  | 1691 | object.  Do \emph{not} use this if the tuple may already be known to | 
|  | 1692 | some other part of the code.  The tuple will always grow or shrink | 
|  | 1693 | at the end.  Think of this as destroying the old tuple and creating | 
|  | 1694 | a new one, only more efficiently.  Returns \code{0} on success. | 
|  | 1695 | Client code should never assume that the resulting value of | 
|  | 1696 | \code{*\var{p}} will be the same as before calling this function. | 
|  | 1697 | If the object referenced by \code{*\var{p}} is replaced, the | 
|  | 1698 | original \code{*\var{p}} is destroyed.  On failure, returns | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1699 | \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1700 | \exception{MemoryError} or | 
|  | 1701 | \exception{SystemError}. | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1702 | \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1703 | \end{cfuncdesc} | 
|  | 1704 |  | 
|  | 1705 |  | 
|  | 1706 | \subsection{List Objects \label{listObjects}} | 
|  | 1707 |  | 
|  | 1708 | \obindex{list} | 
|  | 1709 | \begin{ctypedesc}{PyListObject} | 
|  | 1710 | This subtype of \ctype{PyObject} represents a Python list object. | 
|  | 1711 | \end{ctypedesc} | 
|  | 1712 |  | 
|  | 1713 | \begin{cvardesc}{PyTypeObject}{PyList_Type} | 
|  | 1714 | This instance of \ctype{PyTypeObject} represents the Python list | 
|  | 1715 | type.  This is the same object as \code{types.ListType}. | 
|  | 1716 | \withsubitem{(in module types)}{\ttindex{ListType}} | 
|  | 1717 | \end{cvardesc} | 
|  | 1718 |  | 
|  | 1719 | \begin{cfuncdesc}{int}{PyList_Check}{PyObject *p} | 
| Andrew MacIntyre | 13cd889 | 2003-12-25 23:57:52 +0000 | [diff] [blame] | 1720 | Returns true if \var{p} is a list object or an instance of a | 
|  | 1721 | subtype of the list type. | 
|  | 1722 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 1723 | \end{cfuncdesc} | 
|  | 1724 |  | 
|  | 1725 | \begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p} | 
|  | 1726 | Return true if \var{p} is a list object, but not an instance of a | 
|  | 1727 | subtype of the list type. | 
|  | 1728 | \versionadded{2.2} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1729 | \end{cfuncdesc} | 
|  | 1730 |  | 
|  | 1731 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int len} | 
|  | 1732 | Returns a new list of length \var{len} on success, or \NULL{} on | 
|  | 1733 | failure. | 
|  | 1734 | \end{cfuncdesc} | 
|  | 1735 |  | 
|  | 1736 | \begin{cfuncdesc}{int}{PyList_Size}{PyObject *list} | 
|  | 1737 | Returns the length of the list object in \var{list}; this is | 
|  | 1738 | equivalent to \samp{len(\var{list})} on a list object. | 
|  | 1739 | \bifuncindex{len} | 
|  | 1740 | \end{cfuncdesc} | 
|  | 1741 |  | 
|  | 1742 | \begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list} | 
|  | 1743 | Macro form of \cfunction{PyList_Size()} without error checking. | 
|  | 1744 | \end{cfuncdesc} | 
|  | 1745 |  | 
|  | 1746 | \begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index} | 
|  | 1747 | Returns the object at position \var{pos} in the list pointed to by | 
|  | 1748 | \var{p}.  If \var{pos} is out of bounds, returns \NULL{} and sets an | 
|  | 1749 | \exception{IndexError} exception. | 
|  | 1750 | \end{cfuncdesc} | 
|  | 1751 |  | 
|  | 1752 | \begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i} | 
|  | 1753 | Macro form of \cfunction{PyList_GetItem()} without error checking. | 
|  | 1754 | \end{cfuncdesc} | 
|  | 1755 |  | 
|  | 1756 | \begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index, | 
|  | 1757 | PyObject *item} | 
|  | 1758 | Sets the item at index \var{index} in list to \var{item}.  Returns | 
|  | 1759 | \code{0} on success or \code{-1} on failure.  \note{This function | 
|  | 1760 | ``steals'' a reference to \var{item} and discards a reference to an | 
|  | 1761 | item already in the list at the affected position.} | 
|  | 1762 | \end{cfuncdesc} | 
|  | 1763 |  | 
|  | 1764 | \begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i, | 
|  | 1765 | PyObject *o} | 
|  | 1766 | Macro form of \cfunction{PyList_SetItem()} without error checking. | 
|  | 1767 | This is normally only used to fill in new lists where there is no | 
|  | 1768 | previous content. | 
|  | 1769 | \note{This function ``steals'' a reference to \var{item}, and, | 
|  | 1770 | unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a | 
|  | 1771 | reference to any item that it being replaced; any reference in | 
|  | 1772 | \var{list} at position \var{i} will be leaked.} | 
|  | 1773 | \end{cfuncdesc} | 
|  | 1774 |  | 
|  | 1775 | \begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index, | 
|  | 1776 | PyObject *item} | 
|  | 1777 | Inserts the item \var{item} into list \var{list} in front of index | 
|  | 1778 | \var{index}.  Returns \code{0} if successful; returns \code{-1} and | 
|  | 1779 | raises an exception if unsuccessful.  Analogous to | 
|  | 1780 | \code{\var{list}.insert(\var{index}, \var{item})}. | 
|  | 1781 | \end{cfuncdesc} | 
|  | 1782 |  | 
|  | 1783 | \begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item} | 
|  | 1784 | Appends the object \var{item} at the end of list \var{list}. | 
|  | 1785 | Returns \code{0} if successful; returns \code{-1} and sets an | 
|  | 1786 | exception if unsuccessful.  Analogous to | 
|  | 1787 | \code{\var{list}.append(\var{item})}. | 
|  | 1788 | \end{cfuncdesc} | 
|  | 1789 |  | 
|  | 1790 | \begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list, | 
|  | 1791 | int low, int high} | 
|  | 1792 | Returns a list of the objects in \var{list} containing the objects | 
|  | 1793 | \emph{between} \var{low} and \var{high}.  Returns \NULL{} and sets | 
|  | 1794 | an exception if unsuccessful. | 
|  | 1795 | Analogous to \code{\var{list}[\var{low}:\var{high}]}. | 
|  | 1796 | \end{cfuncdesc} | 
|  | 1797 |  | 
|  | 1798 | \begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list, | 
|  | 1799 | int low, int high, | 
|  | 1800 | PyObject *itemlist} | 
|  | 1801 | Sets the slice of \var{list} between \var{low} and \var{high} to the | 
|  | 1802 | contents of \var{itemlist}.  Analogous to | 
| Raymond Hettinger | 9c7ed4c | 2003-10-26 17:20:07 +0000 | [diff] [blame] | 1803 | \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. | 
|  | 1804 | The \var{itemlist} may be \NULL{}, indicating the assignment | 
|  | 1805 | of an empty list (slice deletion). | 
|  | 1806 | Returns \code{0} on success, \code{-1} on failure. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1807 | \end{cfuncdesc} | 
|  | 1808 |  | 
|  | 1809 | \begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list} | 
|  | 1810 | Sorts the items of \var{list} in place.  Returns \code{0} on | 
|  | 1811 | success, \code{-1} on failure.  This is equivalent to | 
|  | 1812 | \samp{\var{list}.sort()}. | 
|  | 1813 | \end{cfuncdesc} | 
|  | 1814 |  | 
|  | 1815 | \begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list} | 
|  | 1816 | Reverses the items of \var{list} in place.  Returns \code{0} on | 
|  | 1817 | success, \code{-1} on failure.  This is the equivalent of | 
|  | 1818 | \samp{\var{list}.reverse()}. | 
|  | 1819 | \end{cfuncdesc} | 
|  | 1820 |  | 
|  | 1821 | \begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list} | 
|  | 1822 | Returns a new tuple object containing the contents of \var{list}; | 
|  | 1823 | equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple} | 
|  | 1824 | \end{cfuncdesc} | 
|  | 1825 |  | 
|  | 1826 |  | 
|  | 1827 | \section{Mapping Objects \label{mapObjects}} | 
|  | 1828 |  | 
|  | 1829 | \obindex{mapping} | 
|  | 1830 |  | 
|  | 1831 |  | 
|  | 1832 | \subsection{Dictionary Objects \label{dictObjects}} | 
|  | 1833 |  | 
|  | 1834 | \obindex{dictionary} | 
|  | 1835 | \begin{ctypedesc}{PyDictObject} | 
|  | 1836 | This subtype of \ctype{PyObject} represents a Python dictionary | 
|  | 1837 | object. | 
|  | 1838 | \end{ctypedesc} | 
|  | 1839 |  | 
|  | 1840 | \begin{cvardesc}{PyTypeObject}{PyDict_Type} | 
|  | 1841 | This instance of \ctype{PyTypeObject} represents the Python | 
|  | 1842 | dictionary type.  This is exposed to Python programs as | 
|  | 1843 | \code{types.DictType} and \code{types.DictionaryType}. | 
|  | 1844 | \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}} | 
|  | 1845 | \end{cvardesc} | 
|  | 1846 |  | 
|  | 1847 | \begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p} | 
| Andrew MacIntyre | 13cd889 | 2003-12-25 23:57:52 +0000 | [diff] [blame] | 1848 | Returns true if \var{p} is a dict object or an instance of a | 
|  | 1849 | subtype of the dict type. | 
|  | 1850 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1851 | \end{cfuncdesc} | 
|  | 1852 |  | 
| Andrew MacIntyre | f72af65 | 2003-12-26 00:07:51 +0000 | [diff] [blame] | 1853 | \begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p} | 
|  | 1854 | Return true if \var{p} is a dict object, but not an instance of a | 
|  | 1855 | subtype of the dict type. | 
|  | 1856 | \versionadded{2.4} | 
|  | 1857 | \end{cfuncdesc} | 
|  | 1858 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1859 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} | 
|  | 1860 | Returns a new empty dictionary, or \NULL{} on failure. | 
|  | 1861 | \end{cfuncdesc} | 
|  | 1862 |  | 
|  | 1863 | \begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict} | 
|  | 1864 | Return a proxy object for a mapping which enforces read-only | 
|  | 1865 | behavior.  This is normally used to create a proxy to prevent | 
|  | 1866 | modification of the dictionary for non-dynamic class types. | 
|  | 1867 | \versionadded{2.2} | 
|  | 1868 | \end{cfuncdesc} | 
|  | 1869 |  | 
|  | 1870 | \begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p} | 
|  | 1871 | Empties an existing dictionary of all key-value pairs. | 
|  | 1872 | \end{cfuncdesc} | 
|  | 1873 |  | 
| Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 1874 | \begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key} | 
|  | 1875 | Determine if dictionary \var{p} contains \var{key}.  If an item | 
|  | 1876 | in \var{p} is matches \var{key}, return \code{1}, otherwise return | 
|  | 1877 | \code{0}.  On error, return \code{-1}.  This is equivalent to the | 
|  | 1878 | Python expression \samp{\var{key} in \var{p}}. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1879 | \versionadded{2.4} | 
| Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 1880 | \end{cfuncdesc} | 
|  | 1881 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1882 | \begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p} | 
|  | 1883 | Returns a new dictionary that contains the same key-value pairs as | 
|  | 1884 | \var{p}. | 
|  | 1885 | \versionadded{1.6} | 
|  | 1886 | \end{cfuncdesc} | 
|  | 1887 |  | 
|  | 1888 | \begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key, | 
|  | 1889 | PyObject *val} | 
|  | 1890 | Inserts \var{value} into the dictionary \var{p} with a key of | 
|  | 1891 | \var{key}.  \var{key} must be hashable; if it isn't, | 
|  | 1892 | \exception{TypeError} will be raised. | 
|  | 1893 | Returns \code{0} on success or \code{-1} on failure. | 
|  | 1894 | \end{cfuncdesc} | 
|  | 1895 |  | 
|  | 1896 | \begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p, | 
|  | 1897 | char *key, | 
|  | 1898 | PyObject *val} | 
|  | 1899 | Inserts \var{value} into the dictionary \var{p} using \var{key} as a | 
|  | 1900 | key. \var{key} should be a \ctype{char*}.  The key object is created | 
|  | 1901 | using \code{PyString_FromString(\var{key})}. Returns \code{0} on | 
|  | 1902 | success or \code{-1} on failure. | 
|  | 1903 | \ttindex{PyString_FromString()} | 
|  | 1904 | \end{cfuncdesc} | 
|  | 1905 |  | 
|  | 1906 | \begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key} | 
|  | 1907 | Removes the entry in dictionary \var{p} with key \var{key}. | 
|  | 1908 | \var{key} must be hashable; if it isn't, \exception{TypeError} is | 
| Skip Montanaro | a23bc42 | 2002-01-23 08:18:30 +0000 | [diff] [blame] | 1909 | raised.  Returns \code{0} on success or \code{-1} on failure. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1910 | \end{cfuncdesc} | 
|  | 1911 |  | 
|  | 1912 | \begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key} | 
|  | 1913 | Removes the entry in dictionary \var{p} which has a key specified by | 
|  | 1914 | the string \var{key}.  Returns \code{0} on success or \code{-1} on | 
|  | 1915 | failure. | 
|  | 1916 | \end{cfuncdesc} | 
|  | 1917 |  | 
|  | 1918 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key} | 
|  | 1919 | Returns the object from dictionary \var{p} which has a key | 
|  | 1920 | \var{key}.  Returns \NULL{} if the key \var{key} is not present, but | 
|  | 1921 | \emph{without} setting an exception. | 
|  | 1922 | \end{cfuncdesc} | 
|  | 1923 |  | 
|  | 1924 | \begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key} | 
|  | 1925 | This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is | 
|  | 1926 | specified as a \ctype{char*}, rather than a \ctype{PyObject*}. | 
|  | 1927 | \end{cfuncdesc} | 
|  | 1928 |  | 
|  | 1929 | \begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p} | 
|  | 1930 | Returns a \ctype{PyListObject} containing all the items from the | 
|  | 1931 | dictionary, as in the dictinoary method \method{items()} (see the | 
|  | 1932 | \citetitle[../lib/lib.html]{Python Library Reference}). | 
|  | 1933 | \end{cfuncdesc} | 
|  | 1934 |  | 
|  | 1935 | \begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p} | 
|  | 1936 | Returns a \ctype{PyListObject} containing all the keys from the | 
|  | 1937 | dictionary, as in the dictionary method \method{keys()} (see the | 
|  | 1938 | \citetitle[../lib/lib.html]{Python Library Reference}). | 
|  | 1939 | \end{cfuncdesc} | 
|  | 1940 |  | 
|  | 1941 | \begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p} | 
|  | 1942 | Returns a \ctype{PyListObject} containing all the values from the | 
|  | 1943 | dictionary \var{p}, as in the dictionary method \method{values()} | 
|  | 1944 | (see the \citetitle[../lib/lib.html]{Python Library Reference}). | 
|  | 1945 | \end{cfuncdesc} | 
|  | 1946 |  | 
|  | 1947 | \begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p} | 
|  | 1948 | Returns the number of items in the dictionary.  This is equivalent | 
|  | 1949 | to \samp{len(\var{p})} on a dictionary.\bifuncindex{len} | 
|  | 1950 | \end{cfuncdesc} | 
|  | 1951 |  | 
|  | 1952 | \begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos, | 
|  | 1953 | PyObject **pkey, PyObject **pvalue} | 
|  | 1954 | Iterate over all key-value pairs in the dictionary \var{p}.  The | 
|  | 1955 | \ctype{int} referred to by \var{ppos} must be initialized to | 
|  | 1956 | \code{0} prior to the first call to this function to start the | 
|  | 1957 | iteration; the function returns true for each pair in the | 
|  | 1958 | dictionary, and false once all pairs have been reported.  The | 
|  | 1959 | parameters \var{pkey} and \var{pvalue} should either point to | 
|  | 1960 | \ctype{PyObject*} variables that will be filled in with each key and | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 1961 | value, respectively, or may be \NULL{}.  Any references returned through | 
| Raymond Hettinger | 5469324 | 2003-12-13 19:48:41 +0000 | [diff] [blame] | 1962 | them are borrowed.  \var{ppos} should not be altered during iteration. | 
|  | 1963 | Its value represents offsets within the internal dictionary structure, | 
|  | 1964 | and since the structure is sparse, the offsets are not consecutive. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1965 |  | 
|  | 1966 | For example: | 
|  | 1967 |  | 
|  | 1968 | \begin{verbatim} | 
|  | 1969 | PyObject *key, *value; | 
|  | 1970 | int pos = 0; | 
|  | 1971 |  | 
|  | 1972 | while (PyDict_Next(self->dict, &pos, &key, &value)) { | 
|  | 1973 | /* do something interesting with the values... */ | 
|  | 1974 | ... | 
|  | 1975 | } | 
|  | 1976 | \end{verbatim} | 
|  | 1977 |  | 
|  | 1978 | The dictionary \var{p} should not be mutated during iteration.  It | 
|  | 1979 | is safe (since Python 2.1) to modify the values of the keys as you | 
|  | 1980 | iterate over the dictionary, but only so long as the set of keys | 
|  | 1981 | does not change.  For example: | 
|  | 1982 |  | 
|  | 1983 | \begin{verbatim} | 
|  | 1984 | PyObject *key, *value; | 
|  | 1985 | int pos = 0; | 
|  | 1986 |  | 
|  | 1987 | while (PyDict_Next(self->dict, &pos, &key, &value)) { | 
|  | 1988 | int i = PyInt_AS_LONG(value) + 1; | 
|  | 1989 | PyObject *o = PyInt_FromLong(i); | 
|  | 1990 | if (o == NULL) | 
|  | 1991 | return -1; | 
|  | 1992 | if (PyDict_SetItem(self->dict, key, o) < 0) { | 
|  | 1993 | Py_DECREF(o); | 
|  | 1994 | return -1; | 
|  | 1995 | } | 
|  | 1996 | Py_DECREF(o); | 
|  | 1997 | } | 
|  | 1998 | \end{verbatim} | 
|  | 1999 | \end{cfuncdesc} | 
|  | 2000 |  | 
|  | 2001 | \begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override} | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 2002 | Iterate over mapping object \var{b} adding key-value pairs to dictionary | 
|  | 2003 | \var{a}. | 
|  | 2004 | \var{b} may be a dictionary, or any object supporting | 
|  | 2005 | \function{PyMapping_Keys()} and \function{PyObject_GetItem()}. | 
|  | 2006 | If \var{override} is true, existing pairs in \var{a} will | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2007 | be replaced if a matching key is found in \var{b}, otherwise pairs | 
|  | 2008 | will only be added if there is not a matching key in \var{a}. | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 2009 | Return \code{0} on success or \code{-1} if an exception was | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2010 | raised. | 
|  | 2011 | \versionadded{2.2} | 
|  | 2012 | \end{cfuncdesc} | 
|  | 2013 |  | 
|  | 2014 | \begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b} | 
|  | 2015 | This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C, | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 2016 | or \code{\var{a}.update(\var{b})} in Python.  Return \code{0} on | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2017 | success or \code{-1} if an exception was raised. | 
|  | 2018 | \versionadded{2.2} | 
|  | 2019 | \end{cfuncdesc} | 
|  | 2020 |  | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 2021 | \begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2, | 
|  | 2022 | int override} | 
|  | 2023 | Update or merge into dictionary \var{a}, from the key-value pairs in | 
|  | 2024 | \var{seq2}.  \var{seq2} must be an iterable object producing | 
|  | 2025 | iterable objects of length 2, viewed as key-value pairs.  In case of | 
|  | 2026 | duplicate keys, the last wins if \var{override} is true, else the | 
|  | 2027 | first wins. | 
|  | 2028 | Return \code{0} on success or \code{-1} if an exception | 
|  | 2029 | was raised. | 
|  | 2030 | Equivalent Python (except for the return value): | 
|  | 2031 |  | 
|  | 2032 | \begin{verbatim} | 
|  | 2033 | def PyDict_MergeFromSeq2(a, seq2, override): | 
|  | 2034 | for key, value in seq2: | 
|  | 2035 | if override or key not in a: | 
|  | 2036 | a[key] = value | 
|  | 2037 | \end{verbatim} | 
|  | 2038 |  | 
|  | 2039 | \versionadded{2.2} | 
|  | 2040 | \end{cfuncdesc} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2041 |  | 
| Fred Drake | 54e6294 | 2001-12-11 19:40:16 +0000 | [diff] [blame] | 2042 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2043 | \section{Other Objects \label{otherObjects}} | 
|  | 2044 |  | 
|  | 2045 | \subsection{File Objects \label{fileObjects}} | 
|  | 2046 |  | 
|  | 2047 | \obindex{file} | 
|  | 2048 | Python's built-in file objects are implemented entirely on the | 
|  | 2049 | \ctype{FILE*} support from the C standard library.  This is an | 
|  | 2050 | implementation detail and may change in future releases of Python. | 
|  | 2051 |  | 
|  | 2052 | \begin{ctypedesc}{PyFileObject} | 
|  | 2053 | This subtype of \ctype{PyObject} represents a Python file object. | 
|  | 2054 | \end{ctypedesc} | 
|  | 2055 |  | 
|  | 2056 | \begin{cvardesc}{PyTypeObject}{PyFile_Type} | 
|  | 2057 | This instance of \ctype{PyTypeObject} represents the Python file | 
|  | 2058 | type.  This is exposed to Python programs as \code{types.FileType}. | 
|  | 2059 | \withsubitem{(in module types)}{\ttindex{FileType}} | 
|  | 2060 | \end{cvardesc} | 
|  | 2061 |  | 
|  | 2062 | \begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p} | 
|  | 2063 | Returns true if its argument is a \ctype{PyFileObject} or a subtype | 
|  | 2064 | of \ctype{PyFileObject}. | 
|  | 2065 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 2066 | \end{cfuncdesc} | 
|  | 2067 |  | 
|  | 2068 | \begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p} | 
|  | 2069 | Returns true if its argument is a \ctype{PyFileObject}, but not a | 
|  | 2070 | subtype of \ctype{PyFileObject}. | 
|  | 2071 | \versionadded{2.2} | 
|  | 2072 | \end{cfuncdesc} | 
|  | 2073 |  | 
|  | 2074 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode} | 
|  | 2075 | On success, returns a new file object that is opened on the file | 
|  | 2076 | given by \var{filename}, with a file mode given by \var{mode}, where | 
|  | 2077 | \var{mode} has the same semantics as the standard C routine | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2078 | \cfunction{fopen()}\ttindex{fopen()}.  On failure, returns \NULL{}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2079 | \end{cfuncdesc} | 
|  | 2080 |  | 
|  | 2081 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, | 
|  | 2082 | char *name, char *mode, | 
|  | 2083 | int (*close)(FILE*)} | 
|  | 2084 | Creates a new \ctype{PyFileObject} from the already-open standard C | 
|  | 2085 | file pointer, \var{fp}.  The function \var{close} will be called | 
|  | 2086 | when the file should be closed.  Returns \NULL{} on failure. | 
|  | 2087 | \end{cfuncdesc} | 
|  | 2088 |  | 
|  | 2089 | \begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p} | 
|  | 2090 | Returns the file object associated with \var{p} as a \ctype{FILE*}. | 
|  | 2091 | \end{cfuncdesc} | 
|  | 2092 |  | 
|  | 2093 | \begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n} | 
|  | 2094 | Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this | 
|  | 2095 | function reads one line from the object \var{p}.  \var{p} may be a | 
|  | 2096 | file object or any object with a \method{readline()} method.  If | 
|  | 2097 | \var{n} is \code{0}, exactly one line is read, regardless of the | 
|  | 2098 | length of the line.  If \var{n} is greater than \code{0}, no more | 
|  | 2099 | than \var{n} bytes will be read from the file; a partial line can be | 
|  | 2100 | returned.  In both cases, an empty string is returned if the end of | 
|  | 2101 | the file is reached immediately.  If \var{n} is less than \code{0}, | 
|  | 2102 | however, one line is read regardless of length, but | 
|  | 2103 | \exception{EOFError} is raised if the end of the file is reached | 
|  | 2104 | immediately. | 
|  | 2105 | \withsubitem{(built-in exception)}{\ttindex{EOFError}} | 
|  | 2106 | \end{cfuncdesc} | 
|  | 2107 |  | 
|  | 2108 | \begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p} | 
|  | 2109 | Returns the name of the file specified by \var{p} as a string | 
|  | 2110 | object. | 
|  | 2111 | \end{cfuncdesc} | 
|  | 2112 |  | 
|  | 2113 | \begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n} | 
|  | 2114 | Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()} | 
|  | 2115 | only.  This should only be called immediately after file object | 
|  | 2116 | creation. | 
|  | 2117 | \end{cfuncdesc} | 
|  | 2118 |  | 
| Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 2119 | \begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc} | 
|  | 2120 | Set the file's encoding for Unicode output to \var{enc}. Return | 
|  | 2121 | 1 on success and 0 on failure. | 
|  | 2122 | \versionadded{2.3} | 
|  | 2123 | \end{cfuncdesc} | 
|  | 2124 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2125 | \begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag} | 
|  | 2126 | This function exists for internal use by the interpreter.  Sets the | 
|  | 2127 | \member{softspace} attribute of \var{p} to \var{newflag} and | 
|  | 2128 | \withsubitem{(file attribute)}{\ttindex{softspace}}returns the | 
|  | 2129 | previous value.  \var{p} does not have to be a file object for this | 
|  | 2130 | function to work properly; any object is supported (thought its only | 
|  | 2131 | interesting if the \member{softspace} attribute can be set).  This | 
|  | 2132 | function clears any errors, and will return \code{0} as the previous | 
|  | 2133 | value if the attribute either does not exist or if there were errors | 
|  | 2134 | in retrieving it.  There is no way to detect errors from this | 
|  | 2135 | function, but doing so should not be needed. | 
|  | 2136 | \end{cfuncdesc} | 
|  | 2137 |  | 
|  | 2138 | \begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p, | 
|  | 2139 | int flags} | 
|  | 2140 | Writes object \var{obj} to file object \var{p}.  The only supported | 
|  | 2141 | flag for \var{flags} is | 
|  | 2142 | \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the | 
|  | 2143 | \function{str()} of the object is written instead of the | 
|  | 2144 | \function{repr()}.  Returns \code{0} on success or \code{-1} on | 
|  | 2145 | failure; the appropriate exception will be set. | 
|  | 2146 | \end{cfuncdesc} | 
|  | 2147 |  | 
| Fred Drake | 454af89 | 2001-11-29 22:42:59 +0000 | [diff] [blame] | 2148 | \begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2149 | Writes string \var{s} to file object \var{p}.  Returns \code{0} on | 
|  | 2150 | success or \code{-1} on failure; the appropriate exception will be | 
|  | 2151 | set. | 
|  | 2152 | \end{cfuncdesc} | 
|  | 2153 |  | 
|  | 2154 |  | 
|  | 2155 | \subsection{Instance Objects \label{instanceObjects}} | 
|  | 2156 |  | 
|  | 2157 | \obindex{instance} | 
|  | 2158 | There are very few functions specific to instance objects. | 
|  | 2159 |  | 
|  | 2160 | \begin{cvardesc}{PyTypeObject}{PyInstance_Type} | 
|  | 2161 | Type object for class instances. | 
|  | 2162 | \end{cvardesc} | 
|  | 2163 |  | 
|  | 2164 | \begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj} | 
|  | 2165 | Returns true if \var{obj} is an instance. | 
|  | 2166 | \end{cfuncdesc} | 
|  | 2167 |  | 
|  | 2168 | \begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class, | 
|  | 2169 | PyObject *arg, | 
|  | 2170 | PyObject *kw} | 
|  | 2171 | Create a new instance of a specific class.  The parameters \var{arg} | 
|  | 2172 | and \var{kw} are used as the positional and keyword parameters to | 
|  | 2173 | the object's constructor. | 
|  | 2174 | \end{cfuncdesc} | 
|  | 2175 |  | 
|  | 2176 | \begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class, | 
|  | 2177 | PyObject *dict} | 
|  | 2178 | Create a new instance of a specific class without calling it's | 
|  | 2179 | constructor.  \var{class} is the class of new object.  The | 
|  | 2180 | \var{dict} parameter will be used as the object's \member{__dict__}; | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2181 | if \NULL{}, a new dictionary will be created for the instance. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2182 | \end{cfuncdesc} | 
|  | 2183 |  | 
|  | 2184 |  | 
|  | 2185 | \subsection{Method Objects \label{method-objects}} | 
|  | 2186 |  | 
|  | 2187 | \obindex{method} | 
|  | 2188 | There are some useful functions that are useful for working with | 
|  | 2189 | method objects. | 
|  | 2190 |  | 
|  | 2191 | \begin{cvardesc}{PyTypeObject}{PyMethod_Type} | 
|  | 2192 | This instance of \ctype{PyTypeObject} represents the Python method | 
|  | 2193 | type.  This is exposed to Python programs as \code{types.MethodType}. | 
|  | 2194 | \withsubitem{(in module types)}{\ttindex{MethodType}} | 
|  | 2195 | \end{cvardesc} | 
|  | 2196 |  | 
|  | 2197 | \begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o} | 
|  | 2198 | Return true if \var{o} is a method object (has type | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2199 | \cdata{PyMethod_Type}).  The parameter must not be \NULL{}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2200 | \end{cfuncdesc} | 
|  | 2201 |  | 
|  | 2202 | \begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func. | 
|  | 2203 | PyObject *self, PyObject *class} | 
|  | 2204 | Return a new method object, with \var{func} being any callable | 
|  | 2205 | object; this is the function that will be called when the method is | 
|  | 2206 | called.  If this method should be bound to an instance, \var{self} | 
|  | 2207 | should be the instance and \var{class} should be the class of | 
|  | 2208 | \var{self}, otherwise \var{self} should be \NULL{} and \var{class} | 
|  | 2209 | should be the class which provides the unbound method.. | 
|  | 2210 | \end{cfuncdesc} | 
|  | 2211 |  | 
|  | 2212 | \begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth} | 
|  | 2213 | Return the class object from which the method \var{meth} was | 
|  | 2214 | created; if this was created from an instance, it will be the class | 
|  | 2215 | of the instance. | 
|  | 2216 | \end{cfuncdesc} | 
|  | 2217 |  | 
|  | 2218 | \begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth} | 
|  | 2219 | Macro version of \cfunction{PyMethod_Class()} which avoids error | 
|  | 2220 | checking. | 
|  | 2221 | \end{cfuncdesc} | 
|  | 2222 |  | 
|  | 2223 | \begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth} | 
|  | 2224 | Return the function object associated with the method \var{meth}. | 
|  | 2225 | \end{cfuncdesc} | 
|  | 2226 |  | 
|  | 2227 | \begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth} | 
|  | 2228 | Macro version of \cfunction{PyMethod_Function()} which avoids error | 
|  | 2229 | checking. | 
|  | 2230 | \end{cfuncdesc} | 
|  | 2231 |  | 
|  | 2232 | \begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth} | 
|  | 2233 | Return the instance associated with the method \var{meth} if it is | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2234 | bound, otherwise return \NULL{}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2235 | \end{cfuncdesc} | 
|  | 2236 |  | 
|  | 2237 | \begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth} | 
|  | 2238 | Macro version of \cfunction{PyMethod_Self()} which avoids error | 
|  | 2239 | checking. | 
|  | 2240 | \end{cfuncdesc} | 
|  | 2241 |  | 
|  | 2242 |  | 
|  | 2243 | \subsection{Module Objects \label{moduleObjects}} | 
|  | 2244 |  | 
|  | 2245 | \obindex{module} | 
|  | 2246 | There are only a few functions special to module objects. | 
|  | 2247 |  | 
|  | 2248 | \begin{cvardesc}{PyTypeObject}{PyModule_Type} | 
|  | 2249 | This instance of \ctype{PyTypeObject} represents the Python module | 
|  | 2250 | type.  This is exposed to Python programs as | 
|  | 2251 | \code{types.ModuleType}. | 
|  | 2252 | \withsubitem{(in module types)}{\ttindex{ModuleType}} | 
|  | 2253 | \end{cvardesc} | 
|  | 2254 |  | 
|  | 2255 | \begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p} | 
|  | 2256 | Returns true if \var{p} is a module object, or a subtype of a module | 
|  | 2257 | object. | 
|  | 2258 | \versionchanged[Allowed subtypes to be accepted]{2.2} | 
|  | 2259 | \end{cfuncdesc} | 
|  | 2260 |  | 
|  | 2261 | \begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p} | 
|  | 2262 | Returns true if \var{p} is a module object, but not a subtype of | 
|  | 2263 | \cdata{PyModule_Type}. | 
|  | 2264 | \versionadded{2.2} | 
|  | 2265 | \end{cfuncdesc} | 
|  | 2266 |  | 
|  | 2267 | \begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name} | 
|  | 2268 | Return a new module object with the \member{__name__} attribute set | 
|  | 2269 | to \var{name}.  Only the module's \member{__doc__} and | 
|  | 2270 | \member{__name__} attributes are filled in; the caller is | 
|  | 2271 | responsible for providing a \member{__file__} attribute. | 
|  | 2272 | \withsubitem{(module attribute)}{ | 
|  | 2273 | \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}} | 
|  | 2274 | \end{cfuncdesc} | 
|  | 2275 |  | 
|  | 2276 | \begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module} | 
|  | 2277 | Return the dictionary object that implements \var{module}'s | 
|  | 2278 | namespace; this object is the same as the \member{__dict__} | 
|  | 2279 | attribute of the module object.  This function never fails. | 
|  | 2280 | \withsubitem{(module attribute)}{\ttindex{__dict__}} | 
| Fred Drake | f495ef7 | 2002-04-12 19:32:07 +0000 | [diff] [blame] | 2281 | It is recommended extensions use other \cfunction{PyModule_*()} | 
|  | 2282 | and \cfunction{PyObject_*()} functions rather than directly | 
|  | 2283 | manipulate a module's \member{__dict__}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2284 | \end{cfuncdesc} | 
|  | 2285 |  | 
|  | 2286 | \begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module} | 
|  | 2287 | Return \var{module}'s \member{__name__} value.  If the module does | 
|  | 2288 | not provide one, or if it is not a string, \exception{SystemError} | 
|  | 2289 | is raised and \NULL{} is returned. | 
|  | 2290 | \withsubitem{(module attribute)}{\ttindex{__name__}} | 
|  | 2291 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} | 
|  | 2292 | \end{cfuncdesc} | 
|  | 2293 |  | 
|  | 2294 | \begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module} | 
|  | 2295 | Return the name of the file from which \var{module} was loaded using | 
|  | 2296 | \var{module}'s \member{__file__} attribute.  If this is not defined, | 
|  | 2297 | or if it is not a string, raise \exception{SystemError} and return | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2298 | \NULL{}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2299 | \withsubitem{(module attribute)}{\ttindex{__file__}} | 
|  | 2300 | \withsubitem{(built-in exception)}{\ttindex{SystemError}} | 
|  | 2301 | \end{cfuncdesc} | 
|  | 2302 |  | 
|  | 2303 | \begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module, | 
|  | 2304 | char *name, PyObject *value} | 
|  | 2305 | Add an object to \var{module} as \var{name}.  This is a convenience | 
|  | 2306 | function which can be used from the module's initialization | 
|  | 2307 | function.  This steals a reference to \var{value}.  Returns | 
|  | 2308 | \code{-1} on error, \code{0} on success. | 
|  | 2309 | \versionadded{2.0} | 
|  | 2310 | \end{cfuncdesc} | 
|  | 2311 |  | 
|  | 2312 | \begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module, | 
| Martin v. Löwis | dd07e59 | 2004-06-02 12:45:27 +0000 | [diff] [blame] | 2313 | char *name, long value} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2314 | Add an integer constant to \var{module} as \var{name}.  This | 
|  | 2315 | convenience function can be used from the module's initialization | 
|  | 2316 | function. Returns \code{-1} on error, \code{0} on success. | 
|  | 2317 | \versionadded{2.0} | 
|  | 2318 | \end{cfuncdesc} | 
|  | 2319 |  | 
|  | 2320 | \begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module, | 
|  | 2321 | char *name, char *value} | 
|  | 2322 | Add a string constant to \var{module} as \var{name}.  This | 
|  | 2323 | convenience function can be used from the module's initialization | 
|  | 2324 | function.  The string \var{value} must be null-terminated.  Returns | 
|  | 2325 | \code{-1} on error, \code{0} on success. | 
|  | 2326 | \versionadded{2.0} | 
|  | 2327 | \end{cfuncdesc} | 
|  | 2328 |  | 
|  | 2329 |  | 
|  | 2330 | \subsection{Iterator Objects \label{iterator-objects}} | 
|  | 2331 |  | 
|  | 2332 | Python provides two general-purpose iterator objects.  The first, a | 
|  | 2333 | sequence iterator, works with an arbitrary sequence supporting the | 
|  | 2334 | \method{__getitem__()} method.  The second works with a callable | 
|  | 2335 | object and a sentinel value, calling the callable for each item in the | 
|  | 2336 | sequence, and ending the iteration when the sentinel value is | 
|  | 2337 | returned. | 
|  | 2338 |  | 
|  | 2339 | \begin{cvardesc}{PyTypeObject}{PySeqIter_Type} | 
|  | 2340 | Type object for iterator objects returned by | 
|  | 2341 | \cfunction{PySeqIter_New()} and the one-argument form of the | 
|  | 2342 | \function{iter()} built-in function for built-in sequence types. | 
|  | 2343 | \versionadded{2.2} | 
|  | 2344 | \end{cvardesc} | 
|  | 2345 |  | 
|  | 2346 | \begin{cfuncdesc}{int}{PySeqIter_Check}{op} | 
|  | 2347 | Return true if the type of \var{op} is \cdata{PySeqIter_Type}. | 
|  | 2348 | \versionadded{2.2} | 
|  | 2349 | \end{cfuncdesc} | 
|  | 2350 |  | 
|  | 2351 | \begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq} | 
|  | 2352 | Return an iterator that works with a general sequence object, | 
|  | 2353 | \var{seq}.  The iteration ends when the sequence raises | 
|  | 2354 | \exception{IndexError} for the subscripting operation. | 
|  | 2355 | \versionadded{2.2} | 
|  | 2356 | \end{cfuncdesc} | 
|  | 2357 |  | 
|  | 2358 | \begin{cvardesc}{PyTypeObject}{PyCallIter_Type} | 
|  | 2359 | Type object for iterator objects returned by | 
|  | 2360 | \cfunction{PyCallIter_New()} and the two-argument form of the | 
|  | 2361 | \function{iter()} built-in function. | 
|  | 2362 | \versionadded{2.2} | 
|  | 2363 | \end{cvardesc} | 
|  | 2364 |  | 
|  | 2365 | \begin{cfuncdesc}{int}{PyCallIter_Check}{op} | 
|  | 2366 | Return true if the type of \var{op} is \cdata{PyCallIter_Type}. | 
|  | 2367 | \versionadded{2.2} | 
|  | 2368 | \end{cfuncdesc} | 
|  | 2369 |  | 
|  | 2370 | \begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable, | 
|  | 2371 | PyObject *sentinel} | 
|  | 2372 | Return a new iterator.  The first parameter, \var{callable}, can be | 
|  | 2373 | any Python callable object that can be called with no parameters; | 
|  | 2374 | each call to it should return the next item in the iteration.  When | 
|  | 2375 | \var{callable} returns a value equal to \var{sentinel}, the | 
|  | 2376 | iteration will be terminated. | 
|  | 2377 | \versionadded{2.2} | 
|  | 2378 | \end{cfuncdesc} | 
|  | 2379 |  | 
|  | 2380 |  | 
|  | 2381 | \subsection{Descriptor Objects \label{descriptor-objects}} | 
|  | 2382 |  | 
| Fred Drake | 54e6294 | 2001-12-11 19:40:16 +0000 | [diff] [blame] | 2383 | ``Descriptors'' are objects that describe some attribute of an object. | 
|  | 2384 | They are found in the dictionary of type objects. | 
|  | 2385 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2386 | \begin{cvardesc}{PyTypeObject}{PyProperty_Type} | 
| Fred Drake | 54e6294 | 2001-12-11 19:40:16 +0000 | [diff] [blame] | 2387 | The type object for the built-in descriptor types. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2388 | \versionadded{2.2} | 
|  | 2389 | \end{cvardesc} | 
|  | 2390 |  | 
|  | 2391 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type, | 
|  | 2392 | PyGetSetDef *getset} | 
|  | 2393 | \versionadded{2.2} | 
|  | 2394 | \end{cfuncdesc} | 
|  | 2395 |  | 
|  | 2396 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type, | 
|  | 2397 | PyMemberDef *meth} | 
|  | 2398 | \versionadded{2.2} | 
|  | 2399 | \end{cfuncdesc} | 
|  | 2400 |  | 
|  | 2401 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type, | 
|  | 2402 | PyMethodDef *meth} | 
|  | 2403 | \versionadded{2.2} | 
|  | 2404 | \end{cfuncdesc} | 
|  | 2405 |  | 
|  | 2406 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type, | 
|  | 2407 | struct wrapperbase *wrapper, | 
|  | 2408 | void *wrapped} | 
|  | 2409 | \versionadded{2.2} | 
|  | 2410 | \end{cfuncdesc} | 
|  | 2411 |  | 
| Thomas Heller | 8178a22 | 2004-02-09 10:47:11 +0000 | [diff] [blame] | 2412 | \begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type, | 
|  | 2413 | PyMethodDef *method} | 
|  | 2414 | \versionadded{2.3} | 
|  | 2415 | \end{cfuncdesc} | 
|  | 2416 |  | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2417 | \begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr} | 
|  | 2418 | Returns true if the descriptor objects \var{descr} describes a data | 
|  | 2419 | attribute, or false if it describes a method.  \var{descr} must be a | 
|  | 2420 | descriptor object; there is no error checking. | 
|  | 2421 | \versionadded{2.2} | 
|  | 2422 | \end{cfuncdesc} | 
|  | 2423 |  | 
|  | 2424 | \begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *} | 
|  | 2425 | \versionadded{2.2} | 
|  | 2426 | \end{cfuncdesc} | 
|  | 2427 |  | 
|  | 2428 |  | 
|  | 2429 | \subsection{Slice Objects \label{slice-objects}} | 
|  | 2430 |  | 
|  | 2431 | \begin{cvardesc}{PyTypeObject}{PySlice_Type} | 
|  | 2432 | The type object for slice objects.  This is the same as | 
|  | 2433 | \code{types.SliceType}. | 
|  | 2434 | \withsubitem{(in module types)}{\ttindex{SliceType}} | 
|  | 2435 | \end{cvardesc} | 
|  | 2436 |  | 
|  | 2437 | \begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob} | 
|  | 2438 | Returns true if \var{ob} is a slice object; \var{ob} must not be | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2439 | \NULL{}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2440 | \end{cfuncdesc} | 
|  | 2441 |  | 
|  | 2442 | \begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop, | 
|  | 2443 | PyObject *step} | 
|  | 2444 | Return a new slice object with the given values.  The \var{start}, | 
|  | 2445 | \var{stop}, and \var{step} parameters are used as the values of the | 
|  | 2446 | slice object attributes of the same names.  Any of the values may be | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2447 | \NULL{}, in which case the \code{None} will be used for the | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2448 | corresponding attribute.  Returns \NULL{} if the new object could | 
|  | 2449 | not be allocated. | 
|  | 2450 | \end{cfuncdesc} | 
|  | 2451 |  | 
|  | 2452 | \begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length, | 
|  | 2453 | int *start, int *stop, int *step} | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2454 | Retrieve the start, stop and step indices from the slice object | 
|  | 2455 | \var{slice}, assuming a sequence of length \var{length}. Treats | 
|  | 2456 | indices greater than \var{length} as errors. | 
|  | 2457 |  | 
|  | 2458 | Returns 0 on success and -1 on error with no exception set (unless one | 
|  | 2459 | of the indices was not \constant{None} and failed to be converted to | 
|  | 2460 | an integer, in which case -1 is returned with an exception set). | 
|  | 2461 |  | 
|  | 2462 | You probably do not want to use this function.  If you want to use | 
|  | 2463 | slice objects in versions of Python prior to 2.3, you would probably | 
|  | 2464 | do well to incorporate the source of \cfunction{PySlice_GetIndicesEx}, | 
|  | 2465 | suitably renamed, in the source of your extension. | 
|  | 2466 | \end{cfuncdesc} | 
|  | 2467 |  | 
|  | 2468 | \begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length, | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2469 | int *start, int *stop, int *step, | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 2470 | int *slicelength} | 
|  | 2471 | Usable replacement for \cfunction{PySlice_GetIndices}.  Retrieve the | 
|  | 2472 | start, stop, and step indices from the slice object \var{slice} | 
|  | 2473 | assuming a sequence of length \var{length}, and store the length of | 
|  | 2474 | the slice in \var{slicelength}.  Out of bounds indices are clipped in | 
|  | 2475 | a manner consistent with the handling of normal slices. | 
|  | 2476 |  | 
|  | 2477 | Returns 0 on success and -1 on error with exception set. | 
|  | 2478 |  | 
|  | 2479 | \versionadded{2.3} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2480 | \end{cfuncdesc} | 
|  | 2481 |  | 
|  | 2482 |  | 
|  | 2483 | \subsection{Weak Reference Objects \label{weakref-objects}} | 
|  | 2484 |  | 
|  | 2485 | Python supports \emph{weak references} as first-class objects.  There | 
|  | 2486 | are two specific object types which directly implement weak | 
|  | 2487 | references.  The first is a simple reference object, and the second | 
|  | 2488 | acts as a proxy for the original object as much as it can. | 
|  | 2489 |  | 
|  | 2490 | \begin{cfuncdesc}{int}{PyWeakref_Check}{ob} | 
|  | 2491 | Return true if \var{ob} is either a reference or proxy object. | 
|  | 2492 | \versionadded{2.2} | 
|  | 2493 | \end{cfuncdesc} | 
|  | 2494 |  | 
|  | 2495 | \begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob} | 
|  | 2496 | Return true if \var{ob} is a reference object. | 
|  | 2497 | \versionadded{2.2} | 
|  | 2498 | \end{cfuncdesc} | 
|  | 2499 |  | 
|  | 2500 | \begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob} | 
|  | 2501 | Return true if \var{ob} is a proxy object. | 
|  | 2502 | \versionadded{2.2} | 
|  | 2503 | \end{cfuncdesc} | 
|  | 2504 |  | 
|  | 2505 | \begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob, | 
|  | 2506 | PyObject *callback} | 
|  | 2507 | Return a weak reference object for the object \var{ob}.  This will | 
|  | 2508 | always return a new reference, but is not guaranteed to create a new | 
|  | 2509 | object; an existing reference object may be returned.  The second | 
|  | 2510 | parameter, \var{callback}, can be a callable object that receives | 
|  | 2511 | notification when \var{ob} is garbage collected; it should accept a | 
| Raymond Hettinger | 5232f50 | 2004-03-25 08:51:36 +0000 | [diff] [blame] | 2512 | single parameter, which will be the weak reference object itself. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2513 | \var{callback} may also be \code{None} or \NULL{}.  If \var{ob} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2514 | is not a weakly-referencable object, or if \var{callback} is not | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2515 | callable, \code{None}, or \NULL{}, this will return \NULL{} and | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2516 | raise \exception{TypeError}. | 
|  | 2517 | \versionadded{2.2} | 
|  | 2518 | \end{cfuncdesc} | 
|  | 2519 |  | 
|  | 2520 | \begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob, | 
|  | 2521 | PyObject *callback} | 
|  | 2522 | Return a weak reference proxy object for the object \var{ob}.  This | 
|  | 2523 | will always return a new reference, but is not guaranteed to create | 
|  | 2524 | a new object; an existing proxy object may be returned.  The second | 
|  | 2525 | parameter, \var{callback}, can be a callable object that receives | 
|  | 2526 | notification when \var{ob} is garbage collected; it should accept a | 
| Raymond Hettinger | 5232f50 | 2004-03-25 08:51:36 +0000 | [diff] [blame] | 2527 | single parameter, which will be the weak reference object itself. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2528 | \var{callback} may also be \code{None} or \NULL{}.  If \var{ob} is not | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2529 | a weakly-referencable object, or if \var{callback} is not callable, | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2530 | \code{None}, or \NULL{}, this will return \NULL{} and raise | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2531 | \exception{TypeError}. | 
|  | 2532 | \versionadded{2.2} | 
|  | 2533 | \end{cfuncdesc} | 
|  | 2534 |  | 
|  | 2535 | \begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref} | 
|  | 2536 | Returns the referenced object from a weak reference, \var{ref}.  If | 
| Ka-Ping Yee | bd379e9 | 2003-03-28 18:07:16 +0000 | [diff] [blame] | 2537 | the referent is no longer live, returns \code{None}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2538 | \versionadded{2.2} | 
|  | 2539 | \end{cfuncdesc} | 
|  | 2540 |  | 
|  | 2541 | \begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref} | 
|  | 2542 | Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a | 
|  | 2543 | macro that does no error checking. | 
|  | 2544 | \versionadded{2.2} | 
|  | 2545 | \end{cfuncdesc} | 
|  | 2546 |  | 
|  | 2547 |  | 
|  | 2548 | \subsection{CObjects \label{cObjects}} | 
|  | 2549 |  | 
|  | 2550 | \obindex{CObject} | 
|  | 2551 | Refer to \emph{Extending and Embedding the Python Interpreter}, | 
| Fred Drake | 54e6294 | 2001-12-11 19:40:16 +0000 | [diff] [blame] | 2552 | section~1.12, ``Providing a C API for an Extension Module,'' for more | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2553 | information on using these objects. | 
|  | 2554 |  | 
|  | 2555 |  | 
|  | 2556 | \begin{ctypedesc}{PyCObject} | 
|  | 2557 | This subtype of \ctype{PyObject} represents an opaque value, useful | 
|  | 2558 | for C extension modules who need to pass an opaque value (as a | 
|  | 2559 | \ctype{void*} pointer) through Python code to other C code.  It is | 
|  | 2560 | often used to make a C function pointer defined in one module | 
|  | 2561 | available to other modules, so the regular import mechanism can be | 
|  | 2562 | used to access C APIs defined in dynamically loaded modules. | 
|  | 2563 | \end{ctypedesc} | 
|  | 2564 |  | 
|  | 2565 | \begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p} | 
| Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 2566 | Return true if its argument is a \ctype{PyCObject}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2567 | \end{cfuncdesc} | 
|  | 2568 |  | 
| Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 2569 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj, | 
| Fred Drake | 54e6294 | 2001-12-11 19:40:16 +0000 | [diff] [blame] | 2570 | void (*destr)(void *)} | 
| Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 2571 | Create a \ctype{PyCObject} from the \code{void *}\var{cobj}.  The | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2572 | \var{destr} function will be called when the object is reclaimed, | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2573 | unless it is \NULL{}. | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2574 | \end{cfuncdesc} | 
|  | 2575 |  | 
|  | 2576 | \begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj, | 
|  | 2577 | void* desc, void (*destr)(void *, void *)} | 
| Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 2578 | Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}.  The | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2579 | \var{destr} function will be called when the object is reclaimed. | 
|  | 2580 | The \var{desc} argument can be used to pass extra callback data for | 
|  | 2581 | the destructor function. | 
|  | 2582 | \end{cfuncdesc} | 
|  | 2583 |  | 
|  | 2584 | \begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self} | 
| Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 2585 | Return the object \ctype{void *} that the \ctype{PyCObject} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2586 | \var{self} was created with. | 
|  | 2587 | \end{cfuncdesc} | 
|  | 2588 |  | 
|  | 2589 | \begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self} | 
| Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 2590 | Return the description \ctype{void *} that the \ctype{PyCObject} | 
| Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 2591 | \var{self} was created with. | 
|  | 2592 | \end{cfuncdesc} | 
| Fred Drake | cd8474e | 2001-11-26 21:29:17 +0000 | [diff] [blame] | 2593 |  | 
| Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 2594 | \begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2595 | Set the void pointer inside \var{self} to \var{cobj}. | 
| Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 2596 | The \ctype{PyCObject} must not have an associated destructor. | 
|  | 2597 | Return true on success, false on failure. | 
|  | 2598 | \end{cfuncdesc} | 
|  | 2599 |  | 
| Fred Drake | cd8474e | 2001-11-26 21:29:17 +0000 | [diff] [blame] | 2600 |  | 
|  | 2601 | \subsection{Cell Objects \label{cell-objects}} | 
|  | 2602 |  | 
|  | 2603 | ``Cell'' objects are used to implement variables referenced by | 
|  | 2604 | multiple scopes.  For each such variable, a cell object is created to | 
|  | 2605 | store the value; the local variables of each stack frame that | 
|  | 2606 | references the value contains a reference to the cells from outer | 
|  | 2607 | scopes which also use that variable.  When the value is accessed, the | 
|  | 2608 | value contained in the cell is used instead of the cell object | 
|  | 2609 | itself.  This de-referencing of the cell object requires support from | 
|  | 2610 | the generated byte-code; these are not automatically de-referenced | 
|  | 2611 | when accessed.  Cell objects are not likely to be useful elsewhere. | 
|  | 2612 |  | 
| Fred Drake | 54e6294 | 2001-12-11 19:40:16 +0000 | [diff] [blame] | 2613 | \begin{ctypedesc}{PyCellObject} | 
|  | 2614 | The C structure used for cell objects. | 
|  | 2615 | \end{ctypedesc} | 
|  | 2616 |  | 
| Fred Drake | cd8474e | 2001-11-26 21:29:17 +0000 | [diff] [blame] | 2617 | \begin{cvardesc}{PyTypeObject}{PyCell_Type} | 
|  | 2618 | The type object corresponding to cell objects | 
|  | 2619 | \end{cvardesc} | 
|  | 2620 |  | 
|  | 2621 | \begin{cfuncdesc}{int}{PyCell_Check}{ob} | 
|  | 2622 | Return true if \var{ob} is a cell object; \var{ob} must not be | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2623 | \NULL{}. | 
| Fred Drake | cd8474e | 2001-11-26 21:29:17 +0000 | [diff] [blame] | 2624 | \end{cfuncdesc} | 
|  | 2625 |  | 
|  | 2626 | \begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob} | 
|  | 2627 | Create and return a new cell object containing the value \var{ob}. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2628 | The parameter may be \NULL{}. | 
| Fred Drake | cd8474e | 2001-11-26 21:29:17 +0000 | [diff] [blame] | 2629 | \end{cfuncdesc} | 
|  | 2630 |  | 
|  | 2631 | \begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell} | 
|  | 2632 | Return the contents of the cell \var{cell}. | 
|  | 2633 | \end{cfuncdesc} | 
|  | 2634 |  | 
|  | 2635 | \begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell} | 
|  | 2636 | Return the contents of the cell \var{cell}, but without checking | 
| Raymond Hettinger | f4bb1f9 | 2003-08-23 03:38:11 +0000 | [diff] [blame] | 2637 | that \var{cell} is non-\NULL{} and a cell object. | 
| Fred Drake | cd8474e | 2001-11-26 21:29:17 +0000 | [diff] [blame] | 2638 | \end{cfuncdesc} | 
|  | 2639 |  | 
|  | 2640 | \begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value} | 
|  | 2641 | Set the contents of the cell object \var{cell} to \var{value}.  This | 
|  | 2642 | releases the reference to any current content of the cell. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2643 | \var{value} may be \NULL{}.  \var{cell} must be non-\NULL{}; if it is | 
| Fred Drake | cd8474e | 2001-11-26 21:29:17 +0000 | [diff] [blame] | 2644 | not a cell object, \code{-1} will be returned.  On success, \code{0} | 
|  | 2645 | will be returned. | 
|  | 2646 | \end{cfuncdesc} | 
|  | 2647 |  | 
|  | 2648 | \begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value} | 
|  | 2649 | Sets the value of the cell object \var{cell} to \var{value}.  No | 
|  | 2650 | reference counts are adjusted, and no checks are made for safety; | 
|  | 2651 | \var{cell} must be non-\NULL{} and must be a cell object. | 
|  | 2652 | \end{cfuncdesc} | 
| Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 2653 |  | 
|  | 2654 |  | 
|  | 2655 | \subsection{Generator Objects \label{gen-objects}} | 
|  | 2656 |  | 
|  | 2657 | Generator objects are what Python uses to implement generator iterators. | 
|  | 2658 | They are normally created by iterating over a function that yields values, | 
|  | 2659 | rather than explicitly calling \cfunction{PyGen_New}. | 
|  | 2660 |  | 
|  | 2661 | \begin{ctypedesc}{PyGenObject} | 
|  | 2662 | The C structure used for generator objects. | 
|  | 2663 | \end{ctypedesc} | 
|  | 2664 |  | 
|  | 2665 | \begin{cvardesc}{PyTypeObject}{PyGen_Type} | 
|  | 2666 | The type object corresponding to generator objects | 
|  | 2667 | \end{cvardesc} | 
|  | 2668 |  | 
|  | 2669 | \begin{cfuncdesc}{int}{PyGen_Check}{ob} | 
|  | 2670 | Return true if \var{ob} is a generator object; \var{ob} must not be | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2671 | \NULL{}. | 
| Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 2672 | \end{cfuncdesc} | 
|  | 2673 |  | 
|  | 2674 | \begin{cfuncdesc}{int}{PyGen_CheckExact}{ob} | 
|  | 2675 | Return true if \var{ob}'s type is \var{PyGen_Type} | 
|  | 2676 | is a generator object; \var{ob} must not be | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2677 | \NULL{}. | 
| Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 2678 | \end{cfuncdesc} | 
|  | 2679 |  | 
|  | 2680 | \begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame} | 
|  | 2681 | Create and return a new generator object based on the \var{frame} object. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2682 | The parameter must not be \NULL{}. | 
|  | 2683 | \end{cfuncdesc} | 
|  | 2684 |  | 
|  | 2685 |  | 
|  | 2686 | \subsection{DateTime Objects \label{datetime-objects}} | 
|  | 2687 |  | 
|  | 2688 | Various date and time objects are supplied by the \module{datetime} | 
|  | 2689 | module.  Before using any of these functions, the header file | 
|  | 2690 | \file{datetime.h} must be included in your source (note that this is | 
|  | 2691 | not include by \file{Python.h}), and macro \cfunction{PyDateTime_IMPORT()} | 
|  | 2692 | must be invoked.  The macro arranges to put a pointer to a C structure | 
|  | 2693 | in a static variable \code{PyDateTimeAPI}, which is used by the following | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2694 | macros. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2695 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2696 | Type-check macros: | 
|  | 2697 |  | 
|  | 2698 | \begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2699 | Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or | 
|  | 2700 | a subtype of \cdata{PyDateTime_DateType}.  \var{ob} must not be | 
|  | 2701 | \NULL{}. | 
|  | 2702 | \versionadded{2.4} | 
|  | 2703 | \end{cfuncdesc} | 
|  | 2704 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2705 | \begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2706 | Return true if \var{ob} is of type \cdata{PyDateTime_DateType}. | 
|  | 2707 | \var{ob} must not be \NULL{}. | 
|  | 2708 | \versionadded{2.4} | 
|  | 2709 | \end{cfuncdesc} | 
|  | 2710 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2711 | \begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2712 | Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or | 
|  | 2713 | a subtype of \cdata{PyDateTime_DateTimeType}.  \var{ob} must not be | 
|  | 2714 | \NULL{}. | 
|  | 2715 | \versionadded{2.4} | 
|  | 2716 | \end{cfuncdesc} | 
|  | 2717 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2718 | \begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2719 | Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}. | 
|  | 2720 | \var{ob} must not be \NULL{}. | 
|  | 2721 | \versionadded{2.4} | 
|  | 2722 | \end{cfuncdesc} | 
|  | 2723 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2724 | \begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2725 | Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or | 
|  | 2726 | a subtype of \cdata{PyDateTime_TimeType}.  \var{ob} must not be | 
|  | 2727 | \NULL{}. | 
|  | 2728 | \versionadded{2.4} | 
|  | 2729 | \end{cfuncdesc} | 
|  | 2730 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2731 | \begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2732 | Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}. | 
|  | 2733 | \var{ob} must not be \NULL{}. | 
|  | 2734 | \versionadded{2.4} | 
|  | 2735 | \end{cfuncdesc} | 
|  | 2736 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2737 | \begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2738 | Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or | 
|  | 2739 | a subtype of \cdata{PyDateTime_DeltaType}.  \var{ob} must not be | 
|  | 2740 | \NULL{}. | 
|  | 2741 | \versionadded{2.4} | 
|  | 2742 | \end{cfuncdesc} | 
|  | 2743 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2744 | \begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2745 | Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}. | 
|  | 2746 | \var{ob} must not be \NULL{}. | 
|  | 2747 | \versionadded{2.4} | 
|  | 2748 | \end{cfuncdesc} | 
|  | 2749 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2750 | \begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2751 | Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or | 
|  | 2752 | a subtype of \cdata{PyDateTime_TZInfoType}.  \var{ob} must not be | 
|  | 2753 | \NULL{}. | 
|  | 2754 | \versionadded{2.4} | 
|  | 2755 | \end{cfuncdesc} | 
|  | 2756 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2757 | \begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob} | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2758 | Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}. | 
|  | 2759 | \var{ob} must not be \NULL{}. | 
|  | 2760 | \versionadded{2.4} | 
|  | 2761 | \end{cfuncdesc} | 
|  | 2762 |  | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2763 | Macros to create objects: | 
|  | 2764 |  | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2765 | \begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day} | 
|  | 2766 | Return a \code{datetime.date} object with the specified year, month | 
|  | 2767 | and day. | 
|  | 2768 | \versionadded{2.4} | 
|  | 2769 | \end{cfuncdesc} | 
|  | 2770 |  | 
|  | 2771 | \begin{cfuncdesc}{PyObject*}{PyDate_FromDateAndTime}{int year, int month, | 
|  | 2772 | int day, int hour, int minute, int second, int usecond} | 
|  | 2773 | Return a \code{datetime.datetime} object with the specified year, month, | 
|  | 2774 | day, hour, minute, second and microsecond. | 
|  | 2775 | \versionadded{2.4} | 
|  | 2776 | \end{cfuncdesc} | 
|  | 2777 |  | 
|  | 2778 | \begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute, | 
|  | 2779 | int second, int usecond} | 
|  | 2780 | Return a \code{datetime.time} object with the specified hour, minute, | 
|  | 2781 | second and microsecond. | 
|  | 2782 | \versionadded{2.4} | 
|  | 2783 | \end{cfuncdesc} | 
|  | 2784 |  | 
|  | 2785 | \begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds, | 
|  | 2786 | int useconds} | 
|  | 2787 | Return a \code{datetime.timedelta} object representing the given number | 
|  | 2788 | of days, seconds and microseconds.  Normalization is performed so that | 
|  | 2789 | the resulting number of microseconds and seconds lie in the ranges | 
|  | 2790 | documented for \code{datetime.timedelta} objects. | 
|  | 2791 | \versionadded{2.4} | 
|  | 2792 | \end{cfuncdesc} | 
|  | 2793 |  | 
| Tim Peters | 8ff9f9f | 2004-07-17 01:42:26 +0000 | [diff] [blame] | 2794 | Macros to extract fields from date objects.  The argument must be an | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2795 | instance of \cdata{PyDateTime_Date}, including subclasses (such as | 
|  | 2796 | \cdata{PyDateTime_DateTime}).  The argument must not be \NULL{}, and | 
|  | 2797 | the type is not checked: | 
|  | 2798 |  | 
|  | 2799 | \begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o} | 
|  | 2800 | Return the year, as a positive int. | 
|  | 2801 | \versionadded{2.4} | 
|  | 2802 | \end{cfuncdesc} | 
|  | 2803 |  | 
|  | 2804 | \begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o} | 
|  | 2805 | Return the month, as an int from 1 through 12. | 
|  | 2806 | \versionadded{2.4} | 
|  | 2807 | \end{cfuncdesc} | 
|  | 2808 |  | 
|  | 2809 | \begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o} | 
|  | 2810 | Return the day, as an int from 1 through 31. | 
|  | 2811 | \versionadded{2.4} | 
|  | 2812 | \end{cfuncdesc} | 
|  | 2813 |  | 
| Tim Peters | 8ff9f9f | 2004-07-17 01:42:26 +0000 | [diff] [blame] | 2814 | Macros to extract fields from datetime objects.  The argument must be an | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2815 | instance of \cdata{PyDateTime_DateTime}, including subclasses. | 
|  | 2816 | The argument must not be \NULL{}, and the type is not checked: | 
|  | 2817 |  | 
|  | 2818 | \begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o} | 
| Neal Norwitz | 7fdd92f | 2004-08-02 21:56:33 +0000 | [diff] [blame] | 2819 | Return the hour, as an int from 0 through 23. | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2820 | \versionadded{2.4} | 
|  | 2821 | \end{cfuncdesc} | 
|  | 2822 |  | 
|  | 2823 | \begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o} | 
|  | 2824 | Return the minute, as an int from 0 through 59. | 
|  | 2825 | \versionadded{2.4} | 
|  | 2826 | \end{cfuncdesc} | 
|  | 2827 |  | 
|  | 2828 | \begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o} | 
|  | 2829 | Return the second, as an int from 0 through 59. | 
|  | 2830 | \versionadded{2.4} | 
|  | 2831 | \end{cfuncdesc} | 
|  | 2832 |  | 
|  | 2833 | \begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o} | 
|  | 2834 | Return the microsecond, as an int from 0 through 999999. | 
|  | 2835 | \versionadded{2.4} | 
|  | 2836 | \end{cfuncdesc} | 
|  | 2837 |  | 
| Tim Peters | 8ff9f9f | 2004-07-17 01:42:26 +0000 | [diff] [blame] | 2838 | Macros to extract fields from time objects.  The argument must be an | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2839 | instance of \cdata{PyDateTime_Time}, including subclasses. | 
|  | 2840 | The argument must not be \NULL{}, and the type is not checked: | 
|  | 2841 |  | 
|  | 2842 | \begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o} | 
| Neal Norwitz | 7fdd92f | 2004-08-02 21:56:33 +0000 | [diff] [blame] | 2843 | Return the hour, as an int from 0 through 23. | 
| Tim Peters | 183dabc | 2004-07-11 19:26:19 +0000 | [diff] [blame] | 2844 | \versionadded{2.4} | 
|  | 2845 | \end{cfuncdesc} | 
|  | 2846 |  | 
|  | 2847 | \begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o} | 
|  | 2848 | Return the minute, as an int from 0 through 59. | 
|  | 2849 | \versionadded{2.4} | 
|  | 2850 | \end{cfuncdesc} | 
|  | 2851 |  | 
|  | 2852 | \begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o} | 
|  | 2853 | Return the second, as an int from 0 through 59. | 
|  | 2854 | \versionadded{2.4} | 
|  | 2855 | \end{cfuncdesc} | 
|  | 2856 |  | 
|  | 2857 | \begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o} | 
|  | 2858 | Return the microsecond, as an int from 0 through 999999. | 
|  | 2859 | \versionadded{2.4} | 
|  | 2860 | \end{cfuncdesc} | 
|  | 2861 |  | 
|  | 2862 | Macros for the convenience of modules implementing the DB API: | 
|  | 2863 |  | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2864 | \begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args} | 
|  | 2865 | Create and return a new \code{datetime.datetime} object given an argument | 
|  | 2866 | tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2867 | \versionadded{2.4} | 
|  | 2868 | \end{cfuncdesc} | 
|  | 2869 |  | 
|  | 2870 | \begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args} | 
|  | 2871 | Create and return a new \code{datetime.date} object given an argument | 
|  | 2872 | tuple suitable for passing to \code{datetime.date.fromtimestamp()}. | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 2873 | \versionadded{2.4} | 
| Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 2874 | \end{cfuncdesc} |