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