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