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