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