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