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