|
|
|
Python/C API Reference Manual |
|
|
|
7.3.2 Unicode Objects
These are the basic Unicode object types used for the Unicode
implementation in Python:
- Py_UNICODE
-
This type represents a 16-bit unsigned storage type which is used by
Python internally as basis for holding Unicode ordinals. On
platforms where wchar_t is available and also has 16-bits,
Py_UNICODE is a typedef alias for wchar_t to enhance
native platform compatibility. On all other platforms,
Py_UNICODE is a typedef alias for unsigned short.
- PyUnicodeObject
-
This subtype of PyObject represents a Python Unicode object.
- PyTypeObject PyUnicode_Type
-
This instance of PyTypeObject represents the Python Unicode
type.
The following APIs are really C macros and can be used to do fast
checks and to access internal read-only data of Unicode objects:
-
Returns true if the object o is a Unicode object or an
instance of a Unicode subtype.
Changed in version 2.2:
Allowed subtypes to be accepted.
-
Returns true if the object o is a Unicode object, but not an
instance of a subtype.
New in version 2.2.
-
Returns the size of the object. o has to be a
PyUnicodeObject (not checked).
-
Returns the size of the object's internal buffer in bytes. o
has to be a PyUnicodeObject (not checked).
-
Returns a pointer to the internal Py_UNICODE buffer of the
object. o has to be a PyUnicodeObject (not checked).
-
Returns a pointer to the internal buffer of the object.
o has to be a PyUnicodeObject (not checked).
Unicode provides many different character properties. The most often
needed ones are available through these macros which are mapped to C
functions depending on the Python configuration.
-
Returns 1/0 depending on whether ch is a whitespace
character.
-
Returns 1/0 depending on whether ch is a lowercase character.
-
Returns 1/0 depending on whether ch is an uppercase
character.
-
Returns 1/0 depending on whether ch is a titlecase character.
-
Returns 1/0 depending on whether ch is a linebreak character.
-
Returns 1/0 depending on whether ch is a decimal character.
-
Returns 1/0 depending on whether ch is a digit character.
-
Returns 1/0 depending on whether ch is a numeric character.
-
Returns 1/0 depending on whether ch is an alphabetic
character.
-
Returns 1/0 depending on whether ch is an alphanumeric
character.
These APIs can be used for fast direct character conversions:
-
Returns the character ch converted to lower case.
-
Returns the character ch converted to upper case.
-
Returns the character ch converted to title case.
-
Returns the character ch converted to a decimal positive
integer. Returns
-1
if this is not possible. Does not raise
exceptions.
-
Returns the character ch converted to a single digit integer.
Returns
-1
if this is not possible. Does not raise
exceptions.
-
Returns the character ch converted to a (positive) double.
Returns
-1.0
if this is not possible. Does not raise
exceptions.
To create Unicode objects and access their basic sequence properties,
use these APIs:
-
Return value:
New reference.
Create a Unicode Object from the Py_UNICODE buffer u of the
given size. u may be NULL which causes the contents to be
undefined. It is the user's responsibility to fill in the needed
data. The buffer is copied into the new object. If the buffer is
not NULL, the return value might be a shared object. Therefore,
modification of the resulting Unicode object is only allowed when
u is NULL.
-
Return a read-only pointer to the Unicode object's internal
Py_UNICODE buffer, NULL if unicode is not a Unicode
object.
-
Return the length of the Unicode object.
-
Return value:
New reference.
Coerce an encoded object obj to an Unicode object and return a
reference with incremented refcount.
Coercion is done in the following way:
- Unicode objects are passed back as-is with incremented
refcount. Note:
These cannot be decoded; passing a non-NULL
value for encoding will result in a TypeError.
- String and other char buffer compatible objects are decoded
according to the given encoding and using the error handling
defined by errors. Both can be NULL to have the interface
use the default values (see the next section for details).
- All other objects cause an exception.
The API returns NULL if there was an error. The caller is
responsible for decref'ing the returned objects.
-
Return value:
New reference.
Shortcut for PyUnicode_FromEncodedObject(obj, NULL, "strict")
which is used throughout the interpreter whenever coercion to
Unicode is needed.
If the platform supports wchar_t and provides a header file
wchar.h, Python can interface directly to this type using the
following functions. Support is optimized if Python's own
Py_UNICODE type is identical to the system's wchar_t.
-
Return value:
New reference.
Create a Unicode object from the wchar_t buffer w of
the given size. Returns NULL on failure.
-
Copies the Unicode object contents into the wchar_t buffer
w. At most size wchar_t characters are copied.
Returns the number of wchar_t characters copied or -1 in
case of an error.
|
|
|
Python/C API Reference Manual |
|
|
|
Release 2.4a0, documentation updated on 23 September 2003.
See About this document... for information on suggesting changes.