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