blob: eda56a5cbd0514f5b890bf1c8a9a2bb991eca6cf [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
1003 is exposed to Python code as ``unicode`` and ``types.UnicodeType``.
1004
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
1437 .. versionadded:: 3.0
1438
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
1448 .. versionadded:: 3.0
1449
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
1469
1470.. cfunction:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
1471
1472 Return a Python string using the UTF-32 encoding in native byte order. The
1473 string always starts with a BOM mark. Error handling is "strict". Return
1474 *NULL* if an exception was raised by the codec.
1475
Georg Brandl116aa622007-08-15 14:28:22 +00001476These are the UTF-16 codec APIs:
1477
1478.. % --- UTF-16 Codecs ------------------------------------------------------ */
1479
1480
1481.. cfunction:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
1482
1483 Decode *length* bytes from a UTF-16 encoded buffer string and return the
1484 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
1485 handling. It defaults to "strict".
1486
1487 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
1488 order::
1489
1490 *byteorder == -1: little endian
1491 *byteorder == 0: native order
1492 *byteorder == 1: big endian
1493
1494 and then switches if the first two bytes of the input data are a byte order mark
1495 (BOM) and the specified byte order is native order. This BOM is not copied into
1496 the resulting Unicode string. After completion, *\*byteorder* is set to the
Walter Dörwaldd9a15792007-08-16 16:55:51 +00001497 current byte order at the end of input data.
Georg Brandl116aa622007-08-15 14:28:22 +00001498
1499 If *byteorder* is *NULL*, the codec starts in native order mode.
1500
1501 Return *NULL* if an exception was raised by the codec.
1502
1503
1504.. cfunction:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
1505
1506 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF16`. If
1507 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF16Stateful` will not treat
1508 trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
1509 split surrogate pair) as an error. Those bytes will not be decoded and the
1510 number of bytes that have been decoded will be stored in *consumed*.
1511
1512 .. versionadded:: 2.4
1513
1514
1515.. cfunction:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
1516
1517 Return a Python string object holding the UTF-16 encoded value of the Unicode
1518 data in *s*. If *byteorder* is not ``0``, output is written according to the
1519 following byte order::
1520
1521 byteorder == -1: little endian
1522 byteorder == 0: native byte order (writes a BOM mark)
1523 byteorder == 1: big endian
1524
1525 If byteorder is ``0``, the output string will always start with the Unicode BOM
1526 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
1527
1528 If *Py_UNICODE_WIDE* is defined, a single :ctype:`Py_UNICODE` value may get
1529 represented as a surrogate pair. If it is not defined, each :ctype:`Py_UNICODE`
1530 values is interpreted as an UCS-2 character.
1531
1532 Return *NULL* if an exception was raised by the codec.
1533
1534
1535.. cfunction:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
1536
1537 Return a Python string using the UTF-16 encoding in native byte order. The
1538 string always starts with a BOM mark. Error handling is "strict". Return
1539 *NULL* if an exception was raised by the codec.
1540
1541These are the "Unicode Escape" codec APIs:
1542
1543.. % --- Unicode-Escape Codecs ----------------------------------------------
1544
1545
1546.. cfunction:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
1547
1548 Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded
1549 string *s*. Return *NULL* if an exception was raised by the codec.
1550
1551
1552.. cfunction:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
1553
1554 Encode the :ctype:`Py_UNICODE` buffer of the given size using Unicode-Escape and
1555 return a Python string object. Return *NULL* if an exception was raised by the
1556 codec.
1557
1558
1559.. cfunction:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
1560
1561 Encode a Unicode objects using Unicode-Escape and return the result as Python
1562 string object. Error handling is "strict". Return *NULL* if an exception was
1563 raised by the codec.
1564
1565These are the "Raw Unicode Escape" codec APIs:
1566
1567.. % --- Raw-Unicode-Escape Codecs ------------------------------------------
1568
1569
1570.. cfunction:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
1571
1572 Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape
1573 encoded string *s*. Return *NULL* if an exception was raised by the codec.
1574
1575
1576.. cfunction:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
1577
1578 Encode the :ctype:`Py_UNICODE` buffer of the given size using Raw-Unicode-Escape
1579 and return a Python string object. Return *NULL* if an exception was raised by
1580 the codec.
1581
1582
1583.. cfunction:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
1584
1585 Encode a Unicode objects using Raw-Unicode-Escape and return the result as
1586 Python string object. Error handling is "strict". Return *NULL* if an exception
1587 was raised by the codec.
1588
1589These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
1590ordinals and only these are accepted by the codecs during encoding.
1591
1592.. % --- Latin-1 Codecs -----------------------------------------------------
1593
1594
1595.. cfunction:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
1596
1597 Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string
1598 *s*. Return *NULL* if an exception was raised by the codec.
1599
1600
1601.. cfunction:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
1602
1603 Encode the :ctype:`Py_UNICODE` buffer of the given size using Latin-1 and return
1604 a Python string object. Return *NULL* if an exception was raised by the codec.
1605
1606
1607.. cfunction:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
1608
1609 Encode a Unicode objects using Latin-1 and return the result as Python string
1610 object. Error handling is "strict". Return *NULL* if an exception was raised
1611 by the codec.
1612
1613These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other
1614codes generate errors.
1615
1616.. % --- ASCII Codecs -------------------------------------------------------
1617
1618
1619.. cfunction:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
1620
1621 Create a Unicode object by decoding *size* bytes of the ASCII encoded string
1622 *s*. Return *NULL* if an exception was raised by the codec.
1623
1624
1625.. cfunction:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
1626
1627 Encode the :ctype:`Py_UNICODE` buffer of the given size using ASCII and return a
1628 Python string object. Return *NULL* if an exception was raised by the codec.
1629
1630
1631.. cfunction:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
1632
1633 Encode a Unicode objects using ASCII and return the result as Python string
1634 object. Error handling is "strict". Return *NULL* if an exception was raised
1635 by the codec.
1636
1637These are the mapping codec APIs:
1638
1639.. % --- Character Map Codecs -----------------------------------------------
1640
1641This codec is special in that it can be used to implement many different codecs
1642(and this is in fact what was done to obtain most of the standard codecs
1643included in the :mod:`encodings` package). The codec uses mapping to encode and
1644decode characters.
1645
1646Decoding mappings must map single string characters to single Unicode
1647characters, integers (which are then interpreted as Unicode ordinals) or None
1648(meaning "undefined mapping" and causing an error).
1649
1650Encoding mappings must map single Unicode characters to single string
1651characters, integers (which are then interpreted as Latin-1 ordinals) or None
1652(meaning "undefined mapping" and causing an error).
1653
1654The mapping objects provided must only support the __getitem__ mapping
1655interface.
1656
1657If a character lookup fails with a LookupError, the character is copied as-is
1658meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
1659resp. Because of this, mappings only need to contain those mappings which map
1660characters to different code points.
1661
1662
1663.. cfunction:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors)
1664
1665 Create a Unicode object by decoding *size* bytes of the encoded string *s* using
1666 the given *mapping* object. Return *NULL* if an exception was raised by the
1667 codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a
1668 dictionary mapping byte or a unicode string, which is treated as a lookup table.
1669 Byte values greater that the length of the string and U+FFFE "characters" are
1670 treated as "undefined mapping".
1671
1672 .. versionchanged:: 2.4
1673 Allowed unicode string as mapping argument.
1674
1675
1676.. cfunction:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)
1677
1678 Encode the :ctype:`Py_UNICODE` buffer of the given size using the given
1679 *mapping* object and return a Python string object. Return *NULL* if an
1680 exception was raised by the codec.
1681
1682
1683.. cfunction:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
1684
1685 Encode a Unicode objects using the given *mapping* object and return the result
1686 as Python string object. Error handling is "strict". Return *NULL* if an
1687 exception was raised by the codec.
1688
1689The following codec API is special in that maps Unicode to Unicode.
1690
1691
1692.. cfunction:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors)
1693
1694 Translate a :ctype:`Py_UNICODE` buffer of the given length by applying a
1695 character mapping *table* to it and return the resulting Unicode object. Return
1696 *NULL* when an exception was raised by the codec.
1697
1698 The *mapping* table must map Unicode ordinal integers to Unicode ordinal
1699 integers or None (causing deletion of the character).
1700
1701 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
1702 and sequences work well. Unmapped character ordinals (ones which cause a
1703 :exc:`LookupError`) are left untouched and are copied as-is.
1704
1705These are the MBCS codec APIs. They are currently only available on Windows and
1706use the Win32 MBCS converters to implement the conversions. Note that MBCS (or
1707DBCS) is a class of encodings, not just one. The target encoding is defined by
1708the user settings on the machine running the codec.
1709
1710.. % --- MBCS codecs for Windows --------------------------------------------
1711
1712
1713.. cfunction:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
1714
1715 Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*.
1716 Return *NULL* if an exception was raised by the codec.
1717
1718
1719.. cfunction:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed)
1720
1721 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeMBCS`. If
1722 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeMBCSStateful` will not decode
1723 trailing lead byte and the number of bytes that have been decoded will be stored
1724 in *consumed*.
1725
1726 .. versionadded:: 2.5
1727
1728
1729.. cfunction:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
1730
1731 Encode the :ctype:`Py_UNICODE` buffer of the given size using MBCS and return a
1732 Python string object. Return *NULL* if an exception was raised by the codec.
1733
1734
1735.. cfunction:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
1736
1737 Encode a Unicode objects using MBCS and return the result as Python string
1738 object. Error handling is "strict". Return *NULL* if an exception was raised
1739 by the codec.
1740
1741.. % --- Methods & Slots ----------------------------------------------------
1742
1743
1744.. _unicodemethodsandslots:
1745
1746Methods and Slot Functions
1747^^^^^^^^^^^^^^^^^^^^^^^^^^
1748
1749The following APIs are capable of handling Unicode objects and strings on input
1750(we refer to them as strings in the descriptions) and return Unicode objects or
1751integers as appropriate.
1752
1753They all return *NULL* or ``-1`` if an exception occurs.
1754
1755
1756.. cfunction:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
1757
1758 Concat two strings giving a new Unicode string.
1759
1760
1761.. cfunction:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
1762
1763 Split a string giving a list of Unicode strings. If sep is *NULL*, splitting
1764 will be done at all whitespace substrings. Otherwise, splits occur at the given
1765 separator. At most *maxsplit* splits will be done. If negative, no limit is
1766 set. Separators are not included in the resulting list.
1767
1768
1769.. cfunction:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
1770
1771 Split a Unicode string at line breaks, returning a list of Unicode strings.
1772 CRLF is considered to be one line break. If *keepend* is 0, the Line break
1773 characters are not included in the resulting strings.
1774
1775
1776.. cfunction:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
1777
1778 Translate a string by applying a character mapping table to it and return the
1779 resulting Unicode object.
1780
1781 The mapping table must map Unicode ordinal integers to Unicode ordinal integers
1782 or None (causing deletion of the character).
1783
1784 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
1785 and sequences work well. Unmapped character ordinals (ones which cause a
1786 :exc:`LookupError`) are left untouched and are copied as-is.
1787
1788 *errors* has the usual meaning for codecs. It may be *NULL* which indicates to
1789 use the default error handling.
1790
1791
1792.. cfunction:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)
1793
1794 Join a sequence of strings using the given separator and return the resulting
1795 Unicode string.
1796
1797
1798.. cfunction:: int PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
1799
1800 Return 1 if *substr* matches *str*[*start*:*end*] at the given tail end
1801 (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match),
1802 0 otherwise. Return ``-1`` if an error occurred.
1803
1804
1805.. cfunction:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
1806
1807 Return the first position of *substr* in *str*[*start*:*end*] using the given
1808 *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a
1809 backward search). The return value is the index of the first match; a value of
1810 ``-1`` indicates that no match was found, and ``-2`` indicates that an error
1811 occurred and an exception has been set.
1812
1813
1814.. cfunction:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
1815
1816 Return the number of non-overlapping occurrences of *substr* in
1817 ``str[start:end]``. Return ``-1`` if an error occurred.
1818
1819
1820.. cfunction:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
1821
1822 Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and
1823 return the resulting Unicode object. *maxcount* == -1 means replace all
1824 occurrences.
1825
1826
1827.. cfunction:: int PyUnicode_Compare(PyObject *left, PyObject *right)
1828
1829 Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
1830 respectively.
1831
1832
1833.. cfunction:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
1834
1835 Rich compare two unicode strings and return one of the following:
1836
1837 * ``NULL`` in case an exception was raised
1838 * :const:`Py_True` or :const:`Py_False` for successful comparisons
1839 * :const:`Py_NotImplemented` in case the type combination is unknown
1840
1841 Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
1842 :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
1843 with a :exc:`UnicodeDecodeError`.
1844
1845 Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
1846 :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
1847
1848
1849.. cfunction:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args)
1850
1851 Return a new string object from *format* and *args*; this is analogous to
1852 ``format % args``. The *args* argument must be a tuple.
1853
1854
1855.. cfunction:: int PyUnicode_Contains(PyObject *container, PyObject *element)
1856
1857 Check whether *element* is contained in *container* and return true or false
1858 accordingly.
1859
1860 *element* has to coerce to a one element Unicode string. ``-1`` is returned if
1861 there was an error.
1862
1863
1864.. cfunction:: void PyUnicode_InternInPlace(PyObject **string)
1865
1866 Intern the argument *\*string* in place. The argument must be the address of a
1867 pointer variable pointing to a Python unicode string object. If there is an
1868 existing interned string that is the same as *\*string*, it sets *\*string* to
1869 it (decrementing the reference count of the old string object and incrementing
1870 the reference count of the interned string object), otherwise it leaves
1871 *\*string* alone and interns it (incrementing its reference count).
1872 (Clarification: even though there is a lot of talk about reference counts, think
1873 of this function as reference-count-neutral; you own the object after the call
1874 if and only if you owned it before the call.)
1875
1876
1877.. cfunction:: PyObject* PyUnicode_InternFromString(const char *v)
1878
1879 A combination of :cfunc:`PyUnicode_FromString` and
1880 :cfunc:`PyUnicode_InternInPlace`, returning either a new unicode string object
1881 that has been interned, or a new ("owned") reference to an earlier interned
1882 string object with the same value.
1883
1884
1885.. _bufferobjects:
1886
1887Buffer Objects
1888--------------
1889
1890.. sectionauthor:: Greg Stein <gstein@lyra.org>
1891
1892
1893.. index::
1894 object: buffer
1895 single: buffer interface
1896
1897Python objects implemented in C can export a group of functions called the
1898"buffer interface." These functions can be used by an object to expose its data
1899in a raw, byte-oriented format. Clients of the object can use the buffer
1900interface to access the object data directly, without needing to copy it first.
1901
1902Two examples of objects that support the buffer interface are strings and
1903arrays. The string object exposes the character contents in the buffer
1904interface's byte-oriented form. An array can also expose its contents, but it
1905should be noted that array elements may be multi-byte values.
1906
1907An example user of the buffer interface is the file object's :meth:`write`
1908method. Any object that can export a series of bytes through the buffer
1909interface can be written to a file. There are a number of format codes to
1910:cfunc:`PyArg_ParseTuple` that operate against an object's buffer interface,
1911returning data from the target object.
1912
1913.. index:: single: PyBufferProcs
1914
1915More information on the buffer interface is provided in the section
1916:ref:`buffer-structs`, under the description for :ctype:`PyBufferProcs`.
1917
1918A "buffer object" is defined in the :file:`bufferobject.h` header (included by
1919:file:`Python.h`). These objects look very similar to string objects at the
1920Python programming level: they support slicing, indexing, concatenation, and
1921some other standard string operations. However, their data can come from one of
1922two sources: from a block of memory, or from another object which exports the
1923buffer interface.
1924
1925Buffer objects are useful as a way to expose the data from another object's
1926buffer interface to the Python programmer. They can also be used as a zero-copy
1927slicing mechanism. Using their ability to reference a block of memory, it is
1928possible to expose any data to the Python programmer quite easily. The memory
1929could be a large, constant array in a C extension, it could be a raw block of
1930memory for manipulation before passing to an operating system library, or it
1931could be used to pass around structured data in its native, in-memory format.
1932
1933
1934.. ctype:: PyBufferObject
1935
1936 This subtype of :ctype:`PyObject` represents a buffer object.
1937
1938
1939.. cvar:: PyTypeObject PyBuffer_Type
1940
1941 .. index:: single: BufferType (in module types)
1942
1943 The instance of :ctype:`PyTypeObject` which represents the Python buffer type;
1944 it is the same object as ``buffer`` and ``types.BufferType`` in the Python
1945 layer. .
1946
1947
1948.. cvar:: int Py_END_OF_BUFFER
1949
1950 This constant may be passed as the *size* parameter to
1951 :cfunc:`PyBuffer_FromObject` or :cfunc:`PyBuffer_FromReadWriteObject`. It
1952 indicates that the new :ctype:`PyBufferObject` should refer to *base* object
1953 from the specified *offset* to the end of its exported buffer. Using this
1954 enables the caller to avoid querying the *base* object for its length.
1955
1956
1957.. cfunction:: int PyBuffer_Check(PyObject *p)
1958
1959 Return true if the argument has type :cdata:`PyBuffer_Type`.
1960
1961
1962.. cfunction:: PyObject* PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
1963
1964 Return a new read-only buffer object. This raises :exc:`TypeError` if *base*
1965 doesn't support the read-only buffer protocol or doesn't provide exactly one
1966 buffer segment, or it raises :exc:`ValueError` if *offset* is less than zero.
1967 The buffer will hold a reference to the *base* object, and the buffer's contents
1968 will refer to the *base* object's buffer interface, starting as position
1969 *offset* and extending for *size* bytes. If *size* is :const:`Py_END_OF_BUFFER`,
1970 then the new buffer's contents extend to the length of the *base* object's
1971 exported buffer data.
1972
1973
1974.. cfunction:: PyObject* PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
1975
1976 Return a new writable buffer object. Parameters and exceptions are similar to
1977 those for :cfunc:`PyBuffer_FromObject`. If the *base* object does not export
1978 the writeable buffer protocol, then :exc:`TypeError` is raised.
1979
1980
1981.. cfunction:: PyObject* PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
1982
1983 Return a new read-only buffer object that reads from a specified location in
1984 memory, with a specified size. The caller is responsible for ensuring that the
1985 memory buffer, passed in as *ptr*, is not deallocated while the returned buffer
1986 object exists. Raises :exc:`ValueError` if *size* is less than zero. Note that
1987 :const:`Py_END_OF_BUFFER` may *not* be passed for the *size* parameter;
1988 :exc:`ValueError` will be raised in that case.
1989
1990
1991.. cfunction:: PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
1992
1993 Similar to :cfunc:`PyBuffer_FromMemory`, but the returned buffer is writable.
1994
1995
1996.. cfunction:: PyObject* PyBuffer_New(Py_ssize_t size)
1997
1998 Return a new writable buffer object that maintains its own memory buffer of
1999 *size* bytes. :exc:`ValueError` is returned if *size* is not zero or positive.
2000 Note that the memory buffer (as returned by :cfunc:`PyObject_AsWriteBuffer`) is
2001 not specifically aligned.
2002
2003
2004.. _tupleobjects:
2005
2006Tuple Objects
2007-------------
2008
2009.. index:: object: tuple
2010
2011
2012.. ctype:: PyTupleObject
2013
2014 This subtype of :ctype:`PyObject` represents a Python tuple object.
2015
2016
2017.. cvar:: PyTypeObject PyTuple_Type
2018
2019 .. index:: single: TupleType (in module types)
2020
2021 This instance of :ctype:`PyTypeObject` represents the Python tuple type; it is
2022 the same object as ``tuple`` and ``types.TupleType`` in the Python layer..
2023
2024
2025.. cfunction:: int PyTuple_Check(PyObject *p)
2026
2027 Return true if *p* is a tuple object or an instance of a subtype of the tuple
2028 type.
2029
2030 .. versionchanged:: 2.2
2031 Allowed subtypes to be accepted.
2032
2033
2034.. cfunction:: int PyTuple_CheckExact(PyObject *p)
2035
2036 Return true if *p* is a tuple object, but not an instance of a subtype of the
2037 tuple type.
2038
2039 .. versionadded:: 2.2
2040
2041
2042.. cfunction:: PyObject* PyTuple_New(Py_ssize_t len)
2043
2044 Return a new tuple object of size *len*, or *NULL* on failure.
2045
2046
2047.. cfunction:: PyObject* PyTuple_Pack(Py_ssize_t n, ...)
2048
2049 Return a new tuple object of size *n*, or *NULL* on failure. The tuple values
2050 are initialized to the subsequent *n* C arguments pointing to Python objects.
2051 ``PyTuple_Pack(2, a, b)`` is equivalent to ``Py_BuildValue("(OO)", a, b)``.
2052
2053 .. versionadded:: 2.4
2054
2055
2056.. cfunction:: int PyTuple_Size(PyObject *p)
2057
2058 Take a pointer to a tuple object, and return the size of that tuple.
2059
2060
2061.. cfunction:: int PyTuple_GET_SIZE(PyObject *p)
2062
2063 Return the size of the tuple *p*, which must be non-*NULL* and point to a tuple;
2064 no error checking is performed.
2065
2066
2067.. cfunction:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
2068
2069 Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
2070 out of bounds, return *NULL* and sets an :exc:`IndexError` exception.
2071
2072
2073.. cfunction:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
2074
2075 Like :cfunc:`PyTuple_GetItem`, but does no checking of its arguments.
2076
2077
2078.. cfunction:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
2079
2080 Take a slice of the tuple pointed to by *p* from *low* to *high* and return it
2081 as a new tuple.
2082
2083
2084.. cfunction:: int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
2085
2086 Insert a reference to object *o* at position *pos* of the tuple pointed to by
2087 *p*. Return ``0`` on success.
2088
2089 .. note::
2090
2091 This function "steals" a reference to *o*.
2092
2093
2094.. cfunction:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)
2095
2096 Like :cfunc:`PyTuple_SetItem`, but does no error checking, and should *only* be
2097 used to fill in brand new tuples.
2098
2099 .. note::
2100
2101 This function "steals" a reference to *o*.
2102
2103
2104.. cfunction:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)
2105
2106 Can be used to resize a tuple. *newsize* will be the new length of the tuple.
2107 Because tuples are *supposed* to be immutable, this should only be used if there
2108 is only one reference to the object. Do *not* use this if the tuple may already
2109 be known to some other part of the code. The tuple will always grow or shrink
2110 at the end. Think of this as destroying the old tuple and creating a new one,
2111 only more efficiently. Returns ``0`` on success. Client code should never
2112 assume that the resulting value of ``*p`` will be the same as before calling
2113 this function. If the object referenced by ``*p`` is replaced, the original
2114 ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and
2115 raises :exc:`MemoryError` or :exc:`SystemError`.
2116
2117 .. versionchanged:: 2.2
2118 Removed unused third parameter, *last_is_sticky*.
2119
2120
2121.. _listobjects:
2122
2123List Objects
2124------------
2125
2126.. index:: object: list
2127
2128
2129.. ctype:: PyListObject
2130
2131 This subtype of :ctype:`PyObject` represents a Python list object.
2132
2133
2134.. cvar:: PyTypeObject PyList_Type
2135
2136 .. index:: single: ListType (in module types)
2137
2138 This instance of :ctype:`PyTypeObject` represents the Python list type. This is
2139 the same object as ``list`` and ``types.ListType`` in the Python layer.
2140
2141
2142.. cfunction:: int PyList_Check(PyObject *p)
2143
2144 Return true if *p* is a list object or an instance of a subtype of the list
2145 type.
2146
2147 .. versionchanged:: 2.2
2148 Allowed subtypes to be accepted.
2149
2150
2151.. cfunction:: int PyList_CheckExact(PyObject *p)
2152
2153 Return true if *p* is a list object, but not an instance of a subtype of the
2154 list type.
2155
2156 .. versionadded:: 2.2
2157
2158
2159.. cfunction:: PyObject* PyList_New(Py_ssize_t len)
2160
2161 Return a new list of length *len* on success, or *NULL* on failure.
2162
2163 .. note::
2164
2165 If *length* is greater than zero, the returned list object's items are set to
2166 ``NULL``. Thus you cannot use abstract API functions such as
2167 :cfunc:`PySequence_SetItem` or expose the object to Python code before setting
2168 all items to a real object with :cfunc:`PyList_SetItem`.
2169
2170
2171.. cfunction:: Py_ssize_t PyList_Size(PyObject *list)
2172
2173 .. index:: builtin: len
2174
2175 Return the length of the list object in *list*; this is equivalent to
2176 ``len(list)`` on a list object.
2177
2178
2179.. cfunction:: Py_ssize_t PyList_GET_SIZE(PyObject *list)
2180
2181 Macro form of :cfunc:`PyList_Size` without error checking.
2182
2183
2184.. cfunction:: PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)
2185
2186 Return the object at position *pos* in the list pointed to by *p*. The position
2187 must be positive, indexing from the end of the list is not supported. If *pos*
2188 is out of bounds, return *NULL* and set an :exc:`IndexError` exception.
2189
2190
2191.. cfunction:: PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)
2192
2193 Macro form of :cfunc:`PyList_GetItem` without error checking.
2194
2195
2196.. cfunction:: int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)
2197
2198 Set the item at index *index* in list to *item*. Return ``0`` on success or
2199 ``-1`` on failure.
2200
2201 .. note::
2202
2203 This function "steals" a reference to *item* and discards a reference to an item
2204 already in the list at the affected position.
2205
2206
2207.. cfunction:: void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)
2208
2209 Macro form of :cfunc:`PyList_SetItem` without error checking. This is normally
2210 only used to fill in new lists where there is no previous content.
2211
2212 .. note::
2213
2214 This function "steals" a reference to *item*, and, unlike
2215 :cfunc:`PyList_SetItem`, does *not* discard a reference to any item that it
2216 being replaced; any reference in *list* at position *i* will be leaked.
2217
2218
2219.. cfunction:: int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
2220
2221 Insert the item *item* into list *list* in front of index *index*. Return ``0``
2222 if successful; return ``-1`` and set an exception if unsuccessful. Analogous to
2223 ``list.insert(index, item)``.
2224
2225
2226.. cfunction:: int PyList_Append(PyObject *list, PyObject *item)
2227
2228 Append the object *item* at the end of list *list*. Return ``0`` if successful;
2229 return ``-1`` and set an exception if unsuccessful. Analogous to
2230 ``list.append(item)``.
2231
2232
2233.. cfunction:: PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)
2234
2235 Return a list of the objects in *list* containing the objects *between* *low*
2236 and *high*. Return *NULL* and set an exception if unsuccessful. Analogous to
2237 ``list[low:high]``.
2238
2239
2240.. cfunction:: int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)
2241
2242 Set the slice of *list* between *low* and *high* to the contents of *itemlist*.
2243 Analogous to ``list[low:high] = itemlist``. The *itemlist* may be *NULL*,
2244 indicating the assignment of an empty list (slice deletion). Return ``0`` on
2245 success, ``-1`` on failure.
2246
2247
2248.. cfunction:: int PyList_Sort(PyObject *list)
2249
2250 Sort the items of *list* in place. Return ``0`` on success, ``-1`` on failure.
2251 This is equivalent to ``list.sort()``.
2252
2253
2254.. cfunction:: int PyList_Reverse(PyObject *list)
2255
2256 Reverse the items of *list* in place. Return ``0`` on success, ``-1`` on
2257 failure. This is the equivalent of ``list.reverse()``.
2258
2259
2260.. cfunction:: PyObject* PyList_AsTuple(PyObject *list)
2261
2262 .. index:: builtin: tuple
2263
2264 Return a new tuple object containing the contents of *list*; equivalent to
2265 ``tuple(list)``.
2266
2267
2268.. _mapobjects:
2269
2270Mapping Objects
2271===============
2272
2273.. index:: object: mapping
2274
2275
2276.. _dictobjects:
2277
2278Dictionary Objects
2279------------------
2280
2281.. index:: object: dictionary
2282
2283
2284.. ctype:: PyDictObject
2285
2286 This subtype of :ctype:`PyObject` represents a Python dictionary object.
2287
2288
2289.. cvar:: PyTypeObject PyDict_Type
2290
2291 .. index::
2292 single: DictType (in module types)
2293 single: DictionaryType (in module types)
2294
2295 This instance of :ctype:`PyTypeObject` represents the Python dictionary type.
2296 This is exposed to Python programs as ``dict`` and ``types.DictType``.
2297
2298
2299.. cfunction:: int PyDict_Check(PyObject *p)
2300
2301 Return true if *p* is a dict object or an instance of a subtype of the dict
2302 type.
2303
2304 .. versionchanged:: 2.2
2305 Allowed subtypes to be accepted.
2306
2307
2308.. cfunction:: int PyDict_CheckExact(PyObject *p)
2309
2310 Return true if *p* is a dict object, but not an instance of a subtype of the
2311 dict type.
2312
2313 .. versionadded:: 2.4
2314
2315
2316.. cfunction:: PyObject* PyDict_New()
2317
2318 Return a new empty dictionary, or *NULL* on failure.
2319
2320
2321.. cfunction:: PyObject* PyDictProxy_New(PyObject *dict)
2322
2323 Return a proxy object for a mapping which enforces read-only behavior. This is
2324 normally used to create a proxy to prevent modification of the dictionary for
2325 non-dynamic class types.
2326
2327 .. versionadded:: 2.2
2328
2329
2330.. cfunction:: void PyDict_Clear(PyObject *p)
2331
2332 Empty an existing dictionary of all key-value pairs.
2333
2334
2335.. cfunction:: int PyDict_Contains(PyObject *p, PyObject *key)
2336
2337 Determine if dictionary *p* contains *key*. If an item in *p* is matches *key*,
2338 return ``1``, otherwise return ``0``. On error, return ``-1``. This is
2339 equivalent to the Python expression ``key in p``.
2340
2341 .. versionadded:: 2.4
2342
2343
2344.. cfunction:: PyObject* PyDict_Copy(PyObject *p)
2345
2346 Return a new dictionary that contains the same key-value pairs as *p*.
2347
2348 .. versionadded:: 1.6
2349
2350
2351.. cfunction:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
2352
2353 Insert *value* into the dictionary *p* with a key of *key*. *key* must be
2354 hashable; if it isn't, :exc:`TypeError` will be raised. Return ``0`` on success
2355 or ``-1`` on failure.
2356
2357
2358.. cfunction:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
2359
2360 .. index:: single: PyString_FromString()
2361
2362 Insert *value* into the dictionary *p* using *key* as a key. *key* should be a
2363 :ctype:`char\*`. The key object is created using ``PyString_FromString(key)``.
2364 Return ``0`` on success or ``-1`` on failure.
2365
2366
2367.. cfunction:: int PyDict_DelItem(PyObject *p, PyObject *key)
2368
2369 Remove the entry in dictionary *p* with key *key*. *key* must be hashable; if it
2370 isn't, :exc:`TypeError` is raised. Return ``0`` on success or ``-1`` on
2371 failure.
2372
2373
2374.. cfunction:: int PyDict_DelItemString(PyObject *p, char *key)
2375
2376 Remove the entry in dictionary *p* which has a key specified by the string
2377 *key*. Return ``0`` on success or ``-1`` on failure.
2378
2379
2380.. cfunction:: PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
2381
2382 Return the object from dictionary *p* which has a key *key*. Return *NULL* if
2383 the key *key* is not present, but *without* setting an exception.
2384
2385
2386.. cfunction:: PyObject* PyDict_GetItemString(PyObject *p, const char *key)
2387
2388 This is the same as :cfunc:`PyDict_GetItem`, but *key* is specified as a
2389 :ctype:`char\*`, rather than a :ctype:`PyObject\*`.
2390
2391
2392.. cfunction:: PyObject* PyDict_Items(PyObject *p)
2393
2394 Return a :ctype:`PyListObject` containing all the items from the dictionary, as
2395 in the dictionary method :meth:`dict.items`.
2396
2397
2398.. cfunction:: PyObject* PyDict_Keys(PyObject *p)
2399
2400 Return a :ctype:`PyListObject` containing all the keys from the dictionary, as
2401 in the dictionary method :meth:`dict.keys`.
2402
2403
2404.. cfunction:: PyObject* PyDict_Values(PyObject *p)
2405
2406 Return a :ctype:`PyListObject` containing all the values from the dictionary
2407 *p*, as in the dictionary method :meth:`dict.values`.
2408
2409
2410.. cfunction:: Py_ssize_t PyDict_Size(PyObject *p)
2411
2412 .. index:: builtin: len
2413
2414 Return the number of items in the dictionary. This is equivalent to ``len(p)``
2415 on a dictionary.
2416
2417
2418.. cfunction:: int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
2419
2420 Iterate over all key-value pairs in the dictionary *p*. The :ctype:`int`
2421 referred to by *ppos* must be initialized to ``0`` prior to the first call to
2422 this function to start the iteration; the function returns true for each pair in
2423 the dictionary, and false once all pairs have been reported. The parameters
2424 *pkey* and *pvalue* should either point to :ctype:`PyObject\*` variables that
2425 will be filled in with each key and value, respectively, or may be *NULL*. Any
2426 references returned through them are borrowed. *ppos* should not be altered
2427 during iteration. Its value represents offsets within the internal dictionary
2428 structure, and since the structure is sparse, the offsets are not consecutive.
2429
2430 For example::
2431
2432 PyObject *key, *value;
2433 Py_ssize_t pos = 0;
2434
2435 while (PyDict_Next(self->dict, &pos, &key, &value)) {
2436 /* do something interesting with the values... */
2437 ...
2438 }
2439
2440 The dictionary *p* should not be mutated during iteration. It is safe (since
2441 Python 2.1) to modify the values of the keys as you iterate over the dictionary,
2442 but only so long as the set of keys does not change. For example::
2443
2444 PyObject *key, *value;
2445 Py_ssize_t pos = 0;
2446
2447 while (PyDict_Next(self->dict, &pos, &key, &value)) {
2448 int i = PyInt_AS_LONG(value) + 1;
2449 PyObject *o = PyInt_FromLong(i);
2450 if (o == NULL)
2451 return -1;
2452 if (PyDict_SetItem(self->dict, key, o) < 0) {
2453 Py_DECREF(o);
2454 return -1;
2455 }
2456 Py_DECREF(o);
2457 }
2458
2459
2460.. cfunction:: int PyDict_Merge(PyObject *a, PyObject *b, int override)
2461
2462 Iterate over mapping object *b* adding key-value pairs to dictionary *a*. *b*
2463 may be a dictionary, or any object supporting :func:`PyMapping_Keys` and
2464 :func:`PyObject_GetItem`. If *override* is true, existing pairs in *a* will be
2465 replaced if a matching key is found in *b*, otherwise pairs will only be added
2466 if there is not a matching key in *a*. Return ``0`` on success or ``-1`` if an
2467 exception was raised.
2468
2469 .. versionadded:: 2.2
2470
2471
2472.. cfunction:: int PyDict_Update(PyObject *a, PyObject *b)
2473
2474 This is the same as ``PyDict_Merge(a, b, 1)`` in C, or ``a.update(b)`` in
2475 Python. Return ``0`` on success or ``-1`` if an exception was raised.
2476
2477 .. versionadded:: 2.2
2478
2479
2480.. cfunction:: int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
2481
2482 Update or merge into dictionary *a*, from the key-value pairs in *seq2*. *seq2*
2483 must be an iterable object producing iterable objects of length 2, viewed as
2484 key-value pairs. In case of duplicate keys, the last wins if *override* is
2485 true, else the first wins. Return ``0`` on success or ``-1`` if an exception was
2486 raised. Equivalent Python (except for the return value)::
2487
2488 def PyDict_MergeFromSeq2(a, seq2, override):
2489 for key, value in seq2:
2490 if override or key not in a:
2491 a[key] = value
2492
2493 .. versionadded:: 2.2
2494
2495
2496.. _otherobjects:
2497
2498Other Objects
2499=============
2500
2501
2502.. _classobjects:
2503
2504Class Objects
2505-------------
2506
2507.. index:: object: class
2508
2509Note that the class objects described here represent old-style classes, which
2510will go away in Python 3. When creating new types for extension modules, you
2511will want to work with type objects (section :ref:`typeobjects`).
2512
2513
2514.. ctype:: PyClassObject
2515
2516 The C structure of the objects used to describe built-in classes.
2517
2518
2519.. cvar:: PyObject* PyClass_Type
2520
2521 .. index:: single: ClassType (in module types)
2522
2523 This is the type object for class objects; it is the same object as
2524 ``types.ClassType`` in the Python layer.
2525
2526
2527.. cfunction:: int PyClass_Check(PyObject *o)
2528
2529 Return true if the object *o* is a class object, including instances of types
2530 derived from the standard class object. Return false in all other cases.
2531
2532
2533.. cfunction:: int PyClass_IsSubclass(PyObject *klass, PyObject *base)
2534
2535 Return true if *klass* is a subclass of *base*. Return false in all other cases.
2536
2537
2538.. _fileobjects:
2539
2540File Objects
2541------------
2542
2543.. index:: object: file
2544
2545Python's built-in file objects are implemented entirely on the :ctype:`FILE\*`
2546support from the C standard library. This is an implementation detail and may
2547change in future releases of Python.
2548
2549
2550.. ctype:: PyFileObject
2551
2552 This subtype of :ctype:`PyObject` represents a Python file object.
2553
2554
2555.. cvar:: PyTypeObject PyFile_Type
2556
2557 .. index:: single: FileType (in module types)
2558
2559 This instance of :ctype:`PyTypeObject` represents the Python file type. This is
2560 exposed to Python programs as ``file`` and ``types.FileType``.
2561
2562
2563.. cfunction:: int PyFile_Check(PyObject *p)
2564
2565 Return true if its argument is a :ctype:`PyFileObject` or a subtype of
2566 :ctype:`PyFileObject`.
2567
2568 .. versionchanged:: 2.2
2569 Allowed subtypes to be accepted.
2570
2571
2572.. cfunction:: int PyFile_CheckExact(PyObject *p)
2573
2574 Return true if its argument is a :ctype:`PyFileObject`, but not a subtype of
2575 :ctype:`PyFileObject`.
2576
2577 .. versionadded:: 2.2
2578
2579
2580.. cfunction:: PyObject* PyFile_FromString(char *filename, char *mode)
2581
2582 .. index:: single: fopen()
2583
2584 On success, return a new file object that is opened on the file given by
2585 *filename*, with a file mode given by *mode*, where *mode* has the same
2586 semantics as the standard C routine :cfunc:`fopen`. On failure, return *NULL*.
2587
2588
2589.. cfunction:: PyObject* PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE*))
2590
2591 Create a new :ctype:`PyFileObject` from the already-open standard C file
2592 pointer, *fp*. The function *close* will be called when the file should be
2593 closed. Return *NULL* on failure.
2594
2595
2596.. cfunction:: FILE* PyFile_AsFile(PyObject *p)
2597
2598 Return the file object associated with *p* as a :ctype:`FILE\*`.
2599
2600
2601.. cfunction:: PyObject* PyFile_GetLine(PyObject *p, int n)
2602
2603 .. index:: single: EOFError (built-in exception)
2604
2605 Equivalent to ``p.readline([n])``, this function reads one line from the
2606 object *p*. *p* may be a file object or any object with a :meth:`readline`
2607 method. If *n* is ``0``, exactly one line is read, regardless of the length of
2608 the line. If *n* is greater than ``0``, no more than *n* bytes will be read
2609 from the file; a partial line can be returned. In both cases, an empty string
2610 is returned if the end of the file is reached immediately. If *n* is less than
2611 ``0``, however, one line is read regardless of length, but :exc:`EOFError` is
2612 raised if the end of the file is reached immediately.
2613
2614
2615.. cfunction:: PyObject* PyFile_Name(PyObject *p)
2616
2617 Return the name of the file specified by *p* as a string object.
2618
2619
2620.. cfunction:: void PyFile_SetBufSize(PyFileObject *p, int n)
2621
2622 .. index:: single: setvbuf()
2623
2624 Available on systems with :cfunc:`setvbuf` only. This should only be called
2625 immediately after file object creation.
2626
2627
2628.. cfunction:: int PyFile_Encoding(PyFileObject *p, char *enc)
2629
2630 Set the file's encoding for Unicode output to *enc*. Return 1 on success and 0
2631 on failure.
2632
2633 .. versionadded:: 2.3
2634
2635
2636.. cfunction:: int PyFile_SoftSpace(PyObject *p, int newflag)
2637
2638 .. index:: single: softspace (file attribute)
2639
2640 This function exists for internal use by the interpreter. Set the
2641 :attr:`softspace` attribute of *p* to *newflag* and return the previous value.
2642 *p* does not have to be a file object for this function to work properly; any
2643 object is supported (thought its only interesting if the :attr:`softspace`
2644 attribute can be set). This function clears any errors, and will return ``0``
2645 as the previous value if the attribute either does not exist or if there were
2646 errors in retrieving it. There is no way to detect errors from this function,
2647 but doing so should not be needed.
2648
2649
2650.. cfunction:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)
2651
2652 .. index:: single: Py_PRINT_RAW
2653
2654 Write object *obj* to file object *p*. The only supported flag for *flags* is
2655 :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
2656 instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the
2657 appropriate exception will be set.
2658
2659
2660.. cfunction:: int PyFile_WriteString(const char *s, PyObject *p)
2661
2662 Write string *s* to file object *p*. Return ``0`` on success or ``-1`` on
2663 failure; the appropriate exception will be set.
2664
2665
2666.. _instanceobjects:
2667
2668Instance Objects
2669----------------
2670
2671.. index:: object: instance
2672
2673There are very few functions specific to instance objects.
2674
2675
2676.. cvar:: PyTypeObject PyInstance_Type
2677
2678 Type object for class instances.
2679
2680
2681.. cfunction:: int PyInstance_Check(PyObject *obj)
2682
2683 Return true if *obj* is an instance.
2684
2685
2686.. cfunction:: PyObject* PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw)
2687
2688 Create a new instance of a specific class. The parameters *arg* and *kw* are
2689 used as the positional and keyword parameters to the object's constructor.
2690
2691
2692.. cfunction:: PyObject* PyInstance_NewRaw(PyObject *class, PyObject *dict)
2693
2694 Create a new instance of a specific class without calling its constructor.
2695 *class* is the class of new object. The *dict* parameter will be used as the
2696 object's :attr:`__dict__`; if *NULL*, a new dictionary will be created for the
2697 instance.
2698
2699
2700.. _function-objects:
2701
2702Function Objects
2703----------------
2704
2705.. index:: object: function
2706
2707There are a few functions specific to Python functions.
2708
2709
2710.. ctype:: PyFunctionObject
2711
2712 The C structure used for functions.
2713
2714
2715.. cvar:: PyTypeObject PyFunction_Type
2716
2717 .. index:: single: MethodType (in module types)
2718
2719 This is an instance of :ctype:`PyTypeObject` and represents the Python function
2720 type. It is exposed to Python programmers as ``types.FunctionType``.
2721
2722
2723.. cfunction:: int PyFunction_Check(PyObject *o)
2724
2725 Return true if *o* is a function object (has type :cdata:`PyFunction_Type`).
2726 The parameter must not be *NULL*.
2727
2728
2729.. cfunction:: PyObject* PyFunction_New(PyObject *code, PyObject *globals)
2730
2731 Return a new function object associated with the code object *code*. *globals*
2732 must be a dictionary with the global variables accessible to the function.
2733
2734 The function's docstring, name and *__module__* are retrieved from the code
2735 object, the argument defaults and closure are set to *NULL*.
2736
2737
2738.. cfunction:: PyObject* PyFunction_GetCode(PyObject *op)
2739
2740 Return the code object associated with the function object *op*.
2741
2742
2743.. cfunction:: PyObject* PyFunction_GetGlobals(PyObject *op)
2744
2745 Return the globals dictionary associated with the function object *op*.
2746
2747
2748.. cfunction:: PyObject* PyFunction_GetModule(PyObject *op)
2749
2750 Return the *__module__* attribute of the function object *op*. This is normally
2751 a string containing the module name, but can be set to any other object by
2752 Python code.
2753
2754
2755.. cfunction:: PyObject* PyFunction_GetDefaults(PyObject *op)
2756
2757 Return the argument default values of the function object *op*. This can be a
2758 tuple of arguments or *NULL*.
2759
2760
2761.. cfunction:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
2762
2763 Set the argument default values for the function object *op*. *defaults* must be
2764 *Py_None* or a tuple.
2765
2766 Raises :exc:`SystemError` and returns ``-1`` on failure.
2767
2768
2769.. cfunction:: PyObject* PyFunction_GetClosure(PyObject *op)
2770
2771 Return the closure associated with the function object *op*. This can be *NULL*
2772 or a tuple of cell objects.
2773
2774
2775.. cfunction:: int PyFunction_SetClosure(PyObject *op, PyObject *closure)
2776
2777 Set the closure associated with the function object *op*. *closure* must be
2778 *Py_None* or a tuple of cell objects.
2779
2780 Raises :exc:`SystemError` and returns ``-1`` on failure.
2781
2782
2783.. _method-objects:
2784
2785Method Objects
2786--------------
2787
2788.. index:: object: method
2789
2790There are some useful functions that are useful for working with method objects.
2791
2792
2793.. cvar:: PyTypeObject PyMethod_Type
2794
2795 .. index:: single: MethodType (in module types)
2796
2797 This instance of :ctype:`PyTypeObject` represents the Python method type. This
2798 is exposed to Python programs as ``types.MethodType``.
2799
2800
2801.. cfunction:: int PyMethod_Check(PyObject *o)
2802
2803 Return true if *o* is a method object (has type :cdata:`PyMethod_Type`). The
2804 parameter must not be *NULL*.
2805
2806
2807.. cfunction:: PyObject* PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
2808
2809 Return a new method object, with *func* being any callable object; this is the
2810 function that will be called when the method is called. If this method should
2811 be bound to an instance, *self* should be the instance and *class* should be the
2812 class of *self*, otherwise *self* should be *NULL* and *class* should be the
2813 class which provides the unbound method..
2814
2815
2816.. cfunction:: PyObject* PyMethod_Class(PyObject *meth)
2817
2818 Return the class object from which the method *meth* was created; if this was
2819 created from an instance, it will be the class of the instance.
2820
2821
2822.. cfunction:: PyObject* PyMethod_GET_CLASS(PyObject *meth)
2823
2824 Macro version of :cfunc:`PyMethod_Class` which avoids error checking.
2825
2826
2827.. cfunction:: PyObject* PyMethod_Function(PyObject *meth)
2828
2829 Return the function object associated with the method *meth*.
2830
2831
2832.. cfunction:: PyObject* PyMethod_GET_FUNCTION(PyObject *meth)
2833
2834 Macro version of :cfunc:`PyMethod_Function` which avoids error checking.
2835
2836
2837.. cfunction:: PyObject* PyMethod_Self(PyObject *meth)
2838
2839 Return the instance associated with the method *meth* if it is bound, otherwise
2840 return *NULL*.
2841
2842
2843.. cfunction:: PyObject* PyMethod_GET_SELF(PyObject *meth)
2844
2845 Macro version of :cfunc:`PyMethod_Self` which avoids error checking.
2846
2847
2848.. _moduleobjects:
2849
2850Module Objects
2851--------------
2852
2853.. index:: object: module
2854
2855There are only a few functions special to module objects.
2856
2857
2858.. cvar:: PyTypeObject PyModule_Type
2859
2860 .. index:: single: ModuleType (in module types)
2861
2862 This instance of :ctype:`PyTypeObject` represents the Python module type. This
2863 is exposed to Python programs as ``types.ModuleType``.
2864
2865
2866.. cfunction:: int PyModule_Check(PyObject *p)
2867
2868 Return true if *p* is a module object, or a subtype of a module object.
2869
2870 .. versionchanged:: 2.2
2871 Allowed subtypes to be accepted.
2872
2873
2874.. cfunction:: int PyModule_CheckExact(PyObject *p)
2875
2876 Return true if *p* is a module object, but not a subtype of
2877 :cdata:`PyModule_Type`.
2878
2879 .. versionadded:: 2.2
2880
2881
2882.. cfunction:: PyObject* PyModule_New(const char *name)
2883
2884 .. index::
2885 single: __name__ (module attribute)
2886 single: __doc__ (module attribute)
2887 single: __file__ (module attribute)
2888
2889 Return a new module object with the :attr:`__name__` attribute set to *name*.
2890 Only the module's :attr:`__doc__` and :attr:`__name__` attributes are filled in;
2891 the caller is responsible for providing a :attr:`__file__` attribute.
2892
2893
2894.. cfunction:: PyObject* PyModule_GetDict(PyObject *module)
2895
2896 .. index:: single: __dict__ (module attribute)
2897
2898 Return the dictionary object that implements *module*'s namespace; this object
2899 is the same as the :attr:`__dict__` attribute of the module object. This
2900 function never fails. It is recommended extensions use other
2901 :cfunc:`PyModule_\*` and :cfunc:`PyObject_\*` functions rather than directly
2902 manipulate a module's :attr:`__dict__`.
2903
2904
2905.. cfunction:: char* PyModule_GetName(PyObject *module)
2906
2907 .. index::
2908 single: __name__ (module attribute)
2909 single: SystemError (built-in exception)
2910
2911 Return *module*'s :attr:`__name__` value. If the module does not provide one,
2912 or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
2913
2914
2915.. cfunction:: char* PyModule_GetFilename(PyObject *module)
2916
2917 .. index::
2918 single: __file__ (module attribute)
2919 single: SystemError (built-in exception)
2920
2921 Return the name of the file from which *module* was loaded using *module*'s
2922 :attr:`__file__` attribute. If this is not defined, or if it is not a string,
2923 raise :exc:`SystemError` and return *NULL*.
2924
2925
2926.. cfunction:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
2927
2928 Add an object to *module* as *name*. This is a convenience function which can
2929 be used from the module's initialization function. This steals a reference to
2930 *value*. Return ``-1`` on error, ``0`` on success.
2931
2932 .. versionadded:: 2.0
2933
2934
2935.. cfunction:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
2936
2937 Add an integer constant to *module* as *name*. This convenience function can be
2938 used from the module's initialization function. Return ``-1`` on error, ``0`` on
2939 success.
2940
2941 .. versionadded:: 2.0
2942
2943
2944.. cfunction:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
2945
2946 Add a string constant to *module* as *name*. This convenience function can be
2947 used from the module's initialization function. The string *value* must be
2948 null-terminated. Return ``-1`` on error, ``0`` on success.
2949
2950 .. versionadded:: 2.0
2951
2952
2953.. _iterator-objects:
2954
2955Iterator Objects
2956----------------
2957
2958Python provides two general-purpose iterator objects. The first, a sequence
2959iterator, works with an arbitrary sequence supporting the :meth:`__getitem__`
2960method. The second works with a callable object and a sentinel value, calling
2961the callable for each item in the sequence, and ending the iteration when the
2962sentinel value is returned.
2963
2964
2965.. cvar:: PyTypeObject PySeqIter_Type
2966
2967 Type object for iterator objects returned by :cfunc:`PySeqIter_New` and the
2968 one-argument form of the :func:`iter` built-in function for built-in sequence
2969 types.
2970
2971 .. versionadded:: 2.2
2972
2973
2974.. cfunction:: int PySeqIter_Check(op)
2975
2976 Return true if the type of *op* is :cdata:`PySeqIter_Type`.
2977
2978 .. versionadded:: 2.2
2979
2980
2981.. cfunction:: PyObject* PySeqIter_New(PyObject *seq)
2982
2983 Return an iterator that works with a general sequence object, *seq*. The
2984 iteration ends when the sequence raises :exc:`IndexError` for the subscripting
2985 operation.
2986
2987 .. versionadded:: 2.2
2988
2989
2990.. cvar:: PyTypeObject PyCallIter_Type
2991
2992 Type object for iterator objects returned by :cfunc:`PyCallIter_New` and the
2993 two-argument form of the :func:`iter` built-in function.
2994
2995 .. versionadded:: 2.2
2996
2997
2998.. cfunction:: int PyCallIter_Check(op)
2999
3000 Return true if the type of *op* is :cdata:`PyCallIter_Type`.
3001
3002 .. versionadded:: 2.2
3003
3004
3005.. cfunction:: PyObject* PyCallIter_New(PyObject *callable, PyObject *sentinel)
3006
3007 Return a new iterator. The first parameter, *callable*, can be any Python
3008 callable object that can be called with no parameters; each call to it should
3009 return the next item in the iteration. When *callable* returns a value equal to
3010 *sentinel*, the iteration will be terminated.
3011
3012 .. versionadded:: 2.2
3013
3014
3015.. _descriptor-objects:
3016
3017Descriptor Objects
3018------------------
3019
3020"Descriptors" are objects that describe some attribute of an object. They are
3021found in the dictionary of type objects.
3022
3023
3024.. cvar:: PyTypeObject PyProperty_Type
3025
3026 The type object for the built-in descriptor types.
3027
3028 .. versionadded:: 2.2
3029
3030
3031.. cfunction:: PyObject* PyDescr_NewGetSet(PyTypeObject *type, struct PyGetSetDef *getset)
3032
3033 .. versionadded:: 2.2
3034
3035
3036.. cfunction:: PyObject* PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *meth)
3037
3038 .. versionadded:: 2.2
3039
3040
3041.. cfunction:: PyObject* PyDescr_NewMethod(PyTypeObject *type, struct PyMethodDef *meth)
3042
3043 .. versionadded:: 2.2
3044
3045
3046.. cfunction:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *wrapper, void *wrapped)
3047
3048 .. versionadded:: 2.2
3049
3050
3051.. cfunction:: PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
3052
3053 .. versionadded:: 2.3
3054
3055
3056.. cfunction:: int PyDescr_IsData(PyObject *descr)
3057
3058 Return true if the descriptor objects *descr* describes a data attribute, or
3059 false if it describes a method. *descr* must be a descriptor object; there is
3060 no error checking.
3061
3062 .. versionadded:: 2.2
3063
3064
3065.. cfunction:: PyObject* PyWrapper_New(PyObject *, PyObject *)
3066
3067 .. versionadded:: 2.2
3068
3069
3070.. _slice-objects:
3071
3072Slice Objects
3073-------------
3074
3075
3076.. cvar:: PyTypeObject PySlice_Type
3077
3078 .. index:: single: SliceType (in module types)
3079
3080 The type object for slice objects. This is the same as ``slice`` and
3081 ``types.SliceType``.
3082
3083
3084.. cfunction:: int PySlice_Check(PyObject *ob)
3085
3086 Return true if *ob* is a slice object; *ob* must not be *NULL*.
3087
3088
3089.. cfunction:: PyObject* PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
3090
3091 Return a new slice object with the given values. The *start*, *stop*, and
3092 *step* parameters are used as the values of the slice object attributes of the
3093 same names. Any of the values may be *NULL*, in which case the ``None`` will be
3094 used for the corresponding attribute. Return *NULL* if the new object could not
3095 be allocated.
3096
3097
3098.. cfunction:: int PySlice_GetIndices(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
3099
3100 Retrieve the start, stop and step indices from the slice object *slice*,
3101 assuming a sequence of length *length*. Treats indices greater than *length* as
3102 errors.
3103
3104 Returns 0 on success and -1 on error with no exception set (unless one of the
3105 indices was not :const:`None` and failed to be converted to an integer, in which
3106 case -1 is returned with an exception set).
3107
3108 You probably do not want to use this function. If you want to use slice objects
3109 in versions of Python prior to 2.3, you would probably do well to incorporate
3110 the source of :cfunc:`PySlice_GetIndicesEx`, suitably renamed, in the source of
3111 your extension.
3112
3113
3114.. 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)
3115
3116 Usable replacement for :cfunc:`PySlice_GetIndices`. Retrieve the start, stop,
3117 and step indices from the slice object *slice* assuming a sequence of length
3118 *length*, and store the length of the slice in *slicelength*. Out of bounds
3119 indices are clipped in a manner consistent with the handling of normal slices.
3120
3121 Returns 0 on success and -1 on error with exception set.
3122
3123 .. versionadded:: 2.3
3124
3125
3126.. _weakrefobjects:
3127
3128Weak Reference Objects
3129----------------------
3130
3131Python supports *weak references* as first-class objects. There are two
3132specific object types which directly implement weak references. The first is a
3133simple reference object, and the second acts as a proxy for the original object
3134as much as it can.
3135
3136
3137.. cfunction:: int PyWeakref_Check(ob)
3138
3139 Return true if *ob* is either a reference or proxy object.
3140
3141 .. versionadded:: 2.2
3142
3143
3144.. cfunction:: int PyWeakref_CheckRef(ob)
3145
3146 Return true if *ob* is a reference object.
3147
3148 .. versionadded:: 2.2
3149
3150
3151.. cfunction:: int PyWeakref_CheckProxy(ob)
3152
3153 Return true if *ob* is a proxy object.
3154
3155 .. versionadded:: 2.2
3156
3157
3158.. cfunction:: PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback)
3159
3160 Return a weak reference object for the object *ob*. This will always return
3161 a new reference, but is not guaranteed to create a new object; an existing
3162 reference object may be returned. The second parameter, *callback*, can be a
3163 callable object that receives notification when *ob* is garbage collected; it
3164 should accept a single parameter, which will be the weak reference object
3165 itself. *callback* may also be ``None`` or *NULL*. If *ob* is not a
3166 weakly-referencable object, or if *callback* is not callable, ``None``, or
3167 *NULL*, this will return *NULL* and raise :exc:`TypeError`.
3168
3169 .. versionadded:: 2.2
3170
3171
3172.. cfunction:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
3173
3174 Return a weak reference proxy object for the object *ob*. This will always
3175 return a new reference, but is not guaranteed to create a new object; an
3176 existing proxy object may be returned. The second parameter, *callback*, can
3177 be a callable object that receives notification when *ob* is garbage
3178 collected; it should accept a single parameter, which will be the weak
3179 reference object itself. *callback* may also be ``None`` or *NULL*. If *ob*
3180 is not a weakly-referencable object, or if *callback* is not callable,
3181 ``None``, or *NULL*, this will return *NULL* and raise :exc:`TypeError`.
3182
3183 .. versionadded:: 2.2
3184
3185
3186.. cfunction:: PyObject* PyWeakref_GetObject(PyObject *ref)
3187
3188 Return the referenced object from a weak reference, *ref*. If the referent is
3189 no longer live, returns ``None``.
3190
3191 .. versionadded:: 2.2
3192
3193
3194.. cfunction:: PyObject* PyWeakref_GET_OBJECT(PyObject *ref)
3195
3196 Similar to :cfunc:`PyWeakref_GetObject`, but implemented as a macro that does no
3197 error checking.
3198
3199 .. versionadded:: 2.2
3200
3201
3202.. _cobjects:
3203
3204CObjects
3205--------
3206
3207.. index:: object: CObject
3208
3209Refer to *Extending and Embedding the Python Interpreter*, section 1.12,
3210"Providing a C API for an Extension Module," for more information on using these
3211objects.
3212
3213
3214.. ctype:: PyCObject
3215
3216 This subtype of :ctype:`PyObject` represents an opaque value, useful for C
3217 extension modules who need to pass an opaque value (as a :ctype:`void\*`
3218 pointer) through Python code to other C code. It is often used to make a C
3219 function pointer defined in one module available to other modules, so the
3220 regular import mechanism can be used to access C APIs defined in dynamically
3221 loaded modules.
3222
3223
3224.. cfunction:: int PyCObject_Check(PyObject *p)
3225
3226 Return true if its argument is a :ctype:`PyCObject`.
3227
3228
3229.. cfunction:: PyObject* PyCObject_FromVoidPtr(void* cobj, void (*destr)(void *))
3230
3231 Create a :ctype:`PyCObject` from the ``void *`` *cobj*. The *destr* function
3232 will be called when the object is reclaimed, unless it is *NULL*.
3233
3234
3235.. cfunction:: PyObject* PyCObject_FromVoidPtrAndDesc(void* cobj, void* desc, void (*destr)(void *, void *))
3236
3237 Create a :ctype:`PyCObject` from the :ctype:`void \*` *cobj*. The *destr*
3238 function will be called when the object is reclaimed. The *desc* argument can
3239 be used to pass extra callback data for the destructor function.
3240
3241
3242.. cfunction:: void* PyCObject_AsVoidPtr(PyObject* self)
3243
3244 Return the object :ctype:`void \*` that the :ctype:`PyCObject` *self* was
3245 created with.
3246
3247
3248.. cfunction:: void* PyCObject_GetDesc(PyObject* self)
3249
3250 Return the description :ctype:`void \*` that the :ctype:`PyCObject` *self* was
3251 created with.
3252
3253
3254.. cfunction:: int PyCObject_SetVoidPtr(PyObject* self, void* cobj)
3255
3256 Set the void pointer inside *self* to *cobj*. The :ctype:`PyCObject` must not
3257 have an associated destructor. Return true on success, false on failure.
3258
3259
3260.. _cell-objects:
3261
3262Cell Objects
3263------------
3264
3265"Cell" objects are used to implement variables referenced by multiple scopes.
3266For each such variable, a cell object is created to store the value; the local
3267variables of each stack frame that references the value contains a reference to
3268the cells from outer scopes which also use that variable. When the value is
3269accessed, the value contained in the cell is used instead of the cell object
3270itself. This de-referencing of the cell object requires support from the
3271generated byte-code; these are not automatically de-referenced when accessed.
3272Cell objects are not likely to be useful elsewhere.
3273
3274
3275.. ctype:: PyCellObject
3276
3277 The C structure used for cell objects.
3278
3279
3280.. cvar:: PyTypeObject PyCell_Type
3281
3282 The type object corresponding to cell objects.
3283
3284
3285.. cfunction:: int PyCell_Check(ob)
3286
3287 Return true if *ob* is a cell object; *ob* must not be *NULL*.
3288
3289
3290.. cfunction:: PyObject* PyCell_New(PyObject *ob)
3291
3292 Create and return a new cell object containing the value *ob*. The parameter may
3293 be *NULL*.
3294
3295
3296.. cfunction:: PyObject* PyCell_Get(PyObject *cell)
3297
3298 Return the contents of the cell *cell*.
3299
3300
3301.. cfunction:: PyObject* PyCell_GET(PyObject *cell)
3302
3303 Return the contents of the cell *cell*, but without checking that *cell* is
3304 non-*NULL* and a cell object.
3305
3306
3307.. cfunction:: int PyCell_Set(PyObject *cell, PyObject *value)
3308
3309 Set the contents of the cell object *cell* to *value*. This releases the
3310 reference to any current content of the cell. *value* may be *NULL*. *cell*
3311 must be non-*NULL*; if it is not a cell object, ``-1`` will be returned. On
3312 success, ``0`` will be returned.
3313
3314
3315.. cfunction:: void PyCell_SET(PyObject *cell, PyObject *value)
3316
3317 Sets the value of the cell object *cell* to *value*. No reference counts are
3318 adjusted, and no checks are made for safety; *cell* must be non-*NULL* and must
3319 be a cell object.
3320
3321
3322.. _gen-objects:
3323
3324Generator Objects
3325-----------------
3326
3327Generator objects are what Python uses to implement generator iterators. They
3328are normally created by iterating over a function that yields values, rather
3329than explicitly calling :cfunc:`PyGen_New`.
3330
3331
3332.. ctype:: PyGenObject
3333
3334 The C structure used for generator objects.
3335
3336
3337.. cvar:: PyTypeObject PyGen_Type
3338
3339 The type object corresponding to generator objects
3340
3341
3342.. cfunction:: int PyGen_Check(ob)
3343
3344 Return true if *ob* is a generator object; *ob* must not be *NULL*.
3345
3346
3347.. cfunction:: int PyGen_CheckExact(ob)
3348
3349 Return true if *ob*'s type is *PyGen_Type* is a generator object; *ob* must not
3350 be *NULL*.
3351
3352
3353.. cfunction:: PyObject* PyGen_New(PyFrameObject *frame)
3354
3355 Create and return a new generator object based on the *frame* object. A
3356 reference to *frame* is stolen by this function. The parameter must not be
3357 *NULL*.
3358
3359
3360.. _datetimeobjects:
3361
3362DateTime Objects
3363----------------
3364
3365Various date and time objects are supplied by the :mod:`datetime` module.
3366Before using any of these functions, the header file :file:`datetime.h` must be
3367included in your source (note that this is not included by :file:`Python.h`),
3368and the macro :cfunc:`PyDateTime_IMPORT` must be invoked. The macro puts a
3369pointer to a C structure into a static variable, ``PyDateTimeAPI``, that is
3370used by the following macros.
3371
3372Type-check macros:
3373
3374
3375.. cfunction:: int PyDate_Check(PyObject *ob)
3376
3377 Return true if *ob* is of type :cdata:`PyDateTime_DateType` or a subtype of
3378 :cdata:`PyDateTime_DateType`. *ob* must not be *NULL*.
3379
3380 .. versionadded:: 2.4
3381
3382
3383.. cfunction:: int PyDate_CheckExact(PyObject *ob)
3384
3385 Return true if *ob* is of type :cdata:`PyDateTime_DateType`. *ob* must not be
3386 *NULL*.
3387
3388 .. versionadded:: 2.4
3389
3390
3391.. cfunction:: int PyDateTime_Check(PyObject *ob)
3392
3393 Return true if *ob* is of type :cdata:`PyDateTime_DateTimeType` or a subtype of
3394 :cdata:`PyDateTime_DateTimeType`. *ob* must not be *NULL*.
3395
3396 .. versionadded:: 2.4
3397
3398
3399.. cfunction:: int PyDateTime_CheckExact(PyObject *ob)
3400
3401 Return true if *ob* is of type :cdata:`PyDateTime_DateTimeType`. *ob* must not
3402 be *NULL*.
3403
3404 .. versionadded:: 2.4
3405
3406
3407.. cfunction:: int PyTime_Check(PyObject *ob)
3408
3409 Return true if *ob* is of type :cdata:`PyDateTime_TimeType` or a subtype of
3410 :cdata:`PyDateTime_TimeType`. *ob* must not be *NULL*.
3411
3412 .. versionadded:: 2.4
3413
3414
3415.. cfunction:: int PyTime_CheckExact(PyObject *ob)
3416
3417 Return true if *ob* is of type :cdata:`PyDateTime_TimeType`. *ob* must not be
3418 *NULL*.
3419
3420 .. versionadded:: 2.4
3421
3422
3423.. cfunction:: int PyDelta_Check(PyObject *ob)
3424
3425 Return true if *ob* is of type :cdata:`PyDateTime_DeltaType` or a subtype of
3426 :cdata:`PyDateTime_DeltaType`. *ob* must not be *NULL*.
3427
3428 .. versionadded:: 2.4
3429
3430
3431.. cfunction:: int PyDelta_CheckExact(PyObject *ob)
3432
3433 Return true if *ob* is of type :cdata:`PyDateTime_DeltaType`. *ob* must not be
3434 *NULL*.
3435
3436 .. versionadded:: 2.4
3437
3438
3439.. cfunction:: int PyTZInfo_Check(PyObject *ob)
3440
3441 Return true if *ob* is of type :cdata:`PyDateTime_TZInfoType` or a subtype of
3442 :cdata:`PyDateTime_TZInfoType`. *ob* must not be *NULL*.
3443
3444 .. versionadded:: 2.4
3445
3446
3447.. cfunction:: int PyTZInfo_CheckExact(PyObject *ob)
3448
3449 Return true if *ob* is of type :cdata:`PyDateTime_TZInfoType`. *ob* must not be
3450 *NULL*.
3451
3452 .. versionadded:: 2.4
3453
3454Macros to create objects:
3455
3456
3457.. cfunction:: PyObject* PyDate_FromDate(int year, int month, int day)
3458
3459 Return a ``datetime.date`` object with the specified year, month and day.
3460
3461 .. versionadded:: 2.4
3462
3463
3464.. cfunction:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
3465
3466 Return a ``datetime.datetime`` object with the specified year, month, day, hour,
3467 minute, second and microsecond.
3468
3469 .. versionadded:: 2.4
3470
3471
3472.. cfunction:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
3473
3474 Return a ``datetime.time`` object with the specified hour, minute, second and
3475 microsecond.
3476
3477 .. versionadded:: 2.4
3478
3479
3480.. cfunction:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
3481
3482 Return a ``datetime.timedelta`` object representing the given number of days,
3483 seconds and microseconds. Normalization is performed so that the resulting
3484 number of microseconds and seconds lie in the ranges documented for
3485 ``datetime.timedelta`` objects.
3486
3487 .. versionadded:: 2.4
3488
3489Macros to extract fields from date objects. The argument must be an instance of
3490:cdata:`PyDateTime_Date`, including subclasses (such as
3491:cdata:`PyDateTime_DateTime`). The argument must not be *NULL*, and the type is
3492not checked:
3493
3494
3495.. cfunction:: int PyDateTime_GET_YEAR(PyDateTime_Date *o)
3496
3497 Return the year, as a positive int.
3498
3499 .. versionadded:: 2.4
3500
3501
3502.. cfunction:: int PyDateTime_GET_MONTH(PyDateTime_Date *o)
3503
3504 Return the month, as an int from 1 through 12.
3505
3506 .. versionadded:: 2.4
3507
3508
3509.. cfunction:: int PyDateTime_GET_DAY(PyDateTime_Date *o)
3510
3511 Return the day, as an int from 1 through 31.
3512
3513 .. versionadded:: 2.4
3514
3515Macros to extract fields from datetime objects. The argument must be an
3516instance of :cdata:`PyDateTime_DateTime`, including subclasses. The argument
3517must not be *NULL*, and the type is not checked:
3518
3519
3520.. cfunction:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
3521
3522 Return the hour, as an int from 0 through 23.
3523
3524 .. versionadded:: 2.4
3525
3526
3527.. cfunction:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
3528
3529 Return the minute, as an int from 0 through 59.
3530
3531 .. versionadded:: 2.4
3532
3533
3534.. cfunction:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
3535
3536 Return the second, as an int from 0 through 59.
3537
3538 .. versionadded:: 2.4
3539
3540
3541.. cfunction:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
3542
3543 Return the microsecond, as an int from 0 through 999999.
3544
3545 .. versionadded:: 2.4
3546
3547Macros to extract fields from time objects. The argument must be an instance of
3548:cdata:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*,
3549and the type is not checked:
3550
3551
3552.. cfunction:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
3553
3554 Return the hour, as an int from 0 through 23.
3555
3556 .. versionadded:: 2.4
3557
3558
3559.. cfunction:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
3560
3561 Return the minute, as an int from 0 through 59.
3562
3563 .. versionadded:: 2.4
3564
3565
3566.. cfunction:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
3567
3568 Return the second, as an int from 0 through 59.
3569
3570 .. versionadded:: 2.4
3571
3572
3573.. cfunction:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
3574
3575 Return the microsecond, as an int from 0 through 999999.
3576
3577 .. versionadded:: 2.4
3578
3579Macros for the convenience of modules implementing the DB API:
3580
3581
3582.. cfunction:: PyObject* PyDateTime_FromTimestamp(PyObject *args)
3583
3584 Create and return a new ``datetime.datetime`` object given an argument tuple
3585 suitable for passing to ``datetime.datetime.fromtimestamp()``.
3586
3587 .. versionadded:: 2.4
3588
3589
3590.. cfunction:: PyObject* PyDate_FromTimestamp(PyObject *args)
3591
3592 Create and return a new ``datetime.date`` object given an argument tuple
3593 suitable for passing to ``datetime.date.fromtimestamp()``.
3594
3595 .. versionadded:: 2.4
3596
3597
3598.. _setobjects:
3599
3600Set Objects
3601-----------
3602
3603.. sectionauthor:: Raymond D. Hettinger <python@rcn.com>
3604
3605
3606.. index::
3607 object: set
3608 object: frozenset
3609
3610.. versionadded:: 2.5
3611
3612This section details the public API for :class:`set` and :class:`frozenset`
3613objects. Any functionality not listed below is best accessed using the either
3614the abstract object protocol (including :cfunc:`PyObject_CallMethod`,
3615:cfunc:`PyObject_RichCompareBool`, :cfunc:`PyObject_Hash`,
3616:cfunc:`PyObject_Repr`, :cfunc:`PyObject_IsTrue`, :cfunc:`PyObject_Print`, and
3617:cfunc:`PyObject_GetIter`) or the abstract number protocol (including
3618:cfunc:`PyNumber_And`, :cfunc:`PyNumber_Subtract`, :cfunc:`PyNumber_Or`,
3619:cfunc:`PyNumber_Xor`, :cfunc:`PyNumber_InPlaceAnd`,
3620:cfunc:`PyNumber_InPlaceSubtract`, :cfunc:`PyNumber_InPlaceOr`, and
3621:cfunc:`PyNumber_InPlaceXor`).
3622
3623
3624.. ctype:: PySetObject
3625
3626 This subtype of :ctype:`PyObject` is used to hold the internal data for both
3627 :class:`set` and :class:`frozenset` objects. It is like a :ctype:`PyDictObject`
3628 in that it is a fixed size for small sets (much like tuple storage) and will
3629 point to a separate, variable sized block of memory for medium and large sized
3630 sets (much like list storage). None of the fields of this structure should be
3631 considered public and are subject to change. All access should be done through
3632 the documented API rather than by manipulating the values in the structure.
3633
3634
3635.. cvar:: PyTypeObject PySet_Type
3636
3637 This is an instance of :ctype:`PyTypeObject` representing the Python
3638 :class:`set` type.
3639
3640
3641.. cvar:: PyTypeObject PyFrozenSet_Type
3642
3643 This is an instance of :ctype:`PyTypeObject` representing the Python
3644 :class:`frozenset` type.
3645
3646The following type check macros work on pointers to any Python object. Likewise,
3647the constructor functions work with any iterable Python object.
3648
3649
3650.. cfunction:: int PyAnySet_Check(PyObject *p)
3651
3652 Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an
3653 instance of a subtype.
3654
3655
3656.. cfunction:: int PyAnySet_CheckExact(PyObject *p)
3657
3658 Return true if *p* is a :class:`set` object or a :class:`frozenset` object but
3659 not an instance of a subtype.
3660
3661
3662.. cfunction:: int PyFrozenSet_CheckExact(PyObject *p)
3663
3664 Return true if *p* is a :class:`frozenset` object but not an instance of a
3665 subtype.
3666
3667
3668.. cfunction:: PyObject* PySet_New(PyObject *iterable)
3669
3670 Return a new :class:`set` containing objects returned by the *iterable*. The
3671 *iterable* may be *NULL* to create a new empty set. Return the new set on
3672 success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is not
3673 actually iterable. The constructor is also useful for copying a set
3674 (``c=set(s)``).
3675
3676
3677.. cfunction:: PyObject* PyFrozenSet_New(PyObject *iterable)
3678
3679 Return a new :class:`frozenset` containing objects returned by the *iterable*.
3680 The *iterable* may be *NULL* to create a new empty frozenset. Return the new
3681 set on success or *NULL* on failure. Raise :exc:`TypeError` if *iterable* is
3682 not actually iterable.
3683
3684The following functions and macros are available for instances of :class:`set`
3685or :class:`frozenset` or instances of their subtypes.
3686
3687
3688.. cfunction:: int PySet_Size(PyObject *anyset)
3689
3690 .. index:: builtin: len
3691
3692 Return the length of a :class:`set` or :class:`frozenset` object. Equivalent to
3693 ``len(anyset)``. Raises a :exc:`PyExc_SystemError` if *anyset* is not a
3694 :class:`set`, :class:`frozenset`, or an instance of a subtype.
3695
3696
3697.. cfunction:: int PySet_GET_SIZE(PyObject *anyset)
3698
3699 Macro form of :cfunc:`PySet_Size` without error checking.
3700
3701
3702.. cfunction:: int PySet_Contains(PyObject *anyset, PyObject *key)
3703
3704 Return 1 if found, 0 if not found, and -1 if an error is encountered. Unlike
3705 the Python :meth:`__contains__` method, this function does not automatically
3706 convert unhashable sets into temporary frozensets. Raise a :exc:`TypeError` if
3707 the *key* is unhashable. Raise :exc:`PyExc_SystemError` if *anyset* is not a
3708 :class:`set`, :class:`frozenset`, or an instance of a subtype.
3709
3710The following functions are available for instances of :class:`set` or its
3711subtypes but not for instances of :class:`frozenset` or its subtypes.
3712
3713
3714.. cfunction:: int PySet_Add(PyObject *set, PyObject *key)
3715
3716 Add *key* to a :class:`set` instance. Does not apply to :class:`frozenset`
3717 instances. Return 0 on success or -1 on failure. Raise a :exc:`TypeError` if
3718 the *key* is unhashable. Raise a :exc:`MemoryError` if there is no room to grow.
3719 Raise a :exc:`SystemError` if *set* is an not an instance of :class:`set` or its
3720 subtype.
3721
3722
3723.. cfunction:: int PySet_Discard(PyObject *set, PyObject *key)
3724
3725 Return 1 if found and removed, 0 if not found (no action taken), and -1 if an
3726 error is encountered. Does not raise :exc:`KeyError` for missing keys. Raise a
3727 :exc:`TypeError` if the *key* is unhashable. Unlike the Python :meth:`discard`
3728 method, this function does not automatically convert unhashable sets into
3729 temporary frozensets. Raise :exc:`PyExc_SystemError` if *set* is an not an
3730 instance of :class:`set` or its subtype.
3731
3732
3733.. cfunction:: PyObject* PySet_Pop(PyObject *set)
3734
3735 Return a new reference to an arbitrary object in the *set*, and removes the
3736 object from the *set*. Return *NULL* on failure. Raise :exc:`KeyError` if the
3737 set is empty. Raise a :exc:`SystemError` if *set* is an not an instance of
3738 :class:`set` or its subtype.
3739
3740
3741.. cfunction:: int PySet_Clear(PyObject *set)
3742
3743 Empty an existing set of all elements.
3744