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