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