String/Bytes Objects¶
These functions raiseTypeError when expecting a string parameter and arecalled with a non-string parameter.
Note
These functions have been renamed to PyBytes_* in Python 3.x. Unlessotherwise noted, the PyBytes functions available in 3.x are aliased to theirPyString_* equivalents to help porting.
- PyTypeObject
PyString_Type¶ This instance of
PyTypeObjectrepresents the Python string type; it isthe same object asstrandtypes.StringTypein the Python layer. .
- int
PyString_Check(PyObject *o)¶ Return true if the objecto is a string object or an instance of a subtype ofthe string type.
Changed in version 2.2:Allowed subtypes to be accepted.
- int
PyString_CheckExact(PyObject *o)¶ Return true if the objecto is a string object, but not an instance of asubtype of the string type.
New in version 2.2.
- PyObject*
PyString_FromString(const char *v)¶ - Return value: New reference.
Return a new string object with a copy of the stringv as value on success,andNULL on failure. The parameterv must not beNULL; it will not bechecked.
- PyObject*
PyString_FromStringAndSize(const char *v, Py_ssize_t len)¶ - Return value: New reference.
Return a new string object with a copy of the stringv as value and lengthlen on success, andNULL on failure. Ifv isNULL, the contents of thestring are uninitialized.
Changed in version 2.5:This function used an
inttype forlen. This might requirechanges in your code for properly supporting 64-bit systems.
- PyObject*
PyString_FromFormat(const char *format, ...)¶ - Return value: New reference.
Take a C
printf()-styleformat string and a variable number ofarguments, calculate the size of the resulting Python string and return a stringwith the values formatted into it. The variable arguments must be C types andmust correspond exactly to the format characters in theformat string. Thefollowing format characters are allowed:Format Characters
Type
Comment
%%n/a
The literal % character.
%cint
A single character,represented as a C int.
%dint
Exactly equivalent to
printf("%d").%uunsigned int
Exactly equivalent to
printf("%u").%ldlong
Exactly equivalent to
printf("%ld").%luunsigned long
Exactly equivalent to
printf("%lu").%lldlong long
Exactly equivalent to
printf("%lld").%lluunsignedlong long
Exactly equivalent to
printf("%llu").%zdPy_ssize_t
Exactly equivalent to
printf("%zd").%zusize_t
Exactly equivalent to
printf("%zu").%iint
Exactly equivalent to
printf("%i").%xint
Exactly equivalent to
printf("%x").%schar*
A null-terminated C characterarray.
%pvoid*
The hex representation of a Cpointer. Mostly equivalent to
printf("%p")except thatit is guaranteed to start withthe literal0xregardlessof what the platform’sprintfyields.An unrecognized format character causes all the rest of the format string to becopied as-is to the result string, and any extra arguments discarded.
Note
The“%lld” and“%llu” format specifiers are only availablewhen
HAVE_LONG_LONGis defined.Changed in version 2.7:Support for“%lld” and“%llu” added.
- PyObject*
PyString_FromFormatV(const char *format, va_list vargs)¶ - Return value: New reference.
Identical to
PyString_FromFormat()except that it takes exactly twoarguments.
- Py_ssize_t
PyString_Size(PyObject *string)¶ Return the length of the string in string objectstring.
Changed in version 2.5:This function returned an
inttype. This might require changesin your code for properly supporting 64-bit systems.
- Py_ssize_t
PyString_GET_SIZE(PyObject *string)¶ Macro form of
PyString_Size()but without error checking.Changed in version 2.5:This macro returned an
inttype. This might require changes inyour code for properly supporting 64-bit systems.
- char*
PyString_AsString(PyObject *string)¶ Return a NUL-terminated representation of the contents ofstring. The pointerrefers to the internal buffer ofstring, not a copy. The data must not bemodified in any way, unless the string was just created using
PyString_FromStringAndSize(NULL,size). It must not be deallocated. Ifstring is a Unicode object, this function computes the default encoding ofstring and operates on that. Ifstring is not a string object at all,PyString_AsString()returnsNULL and raisesTypeError.
- char*
PyString_AS_STRING(PyObject *string)¶ Macro form of
PyString_AsString()but without error checking. Onlystring objects are supported; no Unicode objects should be passed.
- int
PyString_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)¶ Return a NUL-terminated representation of the contents of the objectobjthrough the output variablesbuffer andlength.
The function accepts both string and Unicode objects as input. For Unicodeobjects it returns the default encoded version of the object. Iflength isNULL, the resulting buffer may not contain NUL characters; if it does, thefunction returns
-1and aTypeErroris raised.The buffer refers to an internal string buffer ofobj, not a copy. The datamust not be modified in any way, unless the string was just created using
PyString_FromStringAndSize(NULL,size). It must not be deallocated. Ifstring is a Unicode object, this function computes the default encoding ofstring and operates on that. Ifstring is not a string object at all,PyString_AsStringAndSize()returns-1and raisesTypeError.Changed in version 2.5:This function used an
int*type forlength. This mightrequire changes in your code for properly supporting 64-bit systems.
- void
PyString_Concat(PyObject **string,PyObject *newpart)¶ Create a new string object in*string containing the contents ofnewpartappended tostring; the caller will own the new reference. The reference tothe old value ofstring will be stolen. If the new string cannot be created,the old reference tostring will still be discarded and the value of*string will be set toNULL; the appropriate exception will be set.
- void
PyString_ConcatAndDel(PyObject **string,PyObject *newpart)¶ Create a new string object in*string containing the contents ofnewpartappended tostring. This version decrements the reference count ofnewpart.
- int
_PyString_Resize(PyObject **string, Py_ssize_t newsize)¶ A way to resize a string object even though it is “immutable”. Only use this tobuild up a brand new string object; don’t use this if the string may already beknown in other parts of the code. It is an error to call this function if therefcount on the input string object is not one. Pass the address of an existingstring object as an lvalue (it may be written into), and the new size desired.On success,*string holds the resized string object and
0is returned;the address in*string may differ from its input value. If the reallocationfails, the original string object at*string is deallocated,*string isset toNULL, a memory exception is set, and-1is returned.Changed in version 2.5:This function used an
inttype fornewsize. This mightrequire changes in your code for properly supporting 64-bit systems.
- PyObject*
PyString_Format(PyObject *format,PyObject *args)¶ - Return value: New reference.
Return a new string object fromformat andargs. Analogous to
format%args. Theargs argument must be a tuple or dict.
- void
PyString_InternInPlace(PyObject **string)¶ Intern the argument*string in place. The argument must be the address of apointer variable pointing to a Python string object. If there is an existinginterned string that is the same as*string, it sets*string to it(decrementing the reference count of the old string object and incrementing thereference count of the interned string object), otherwise it leaves*stringalone and interns it (incrementing its reference count). (Clarification: eventhough there is a lot of talk about reference counts, think of this function asreference-count-neutral; you own the object after the call if and only if youowned it before the call.)
Note
This function is not available in 3.x and does not have a PyBytes alias.
- PyObject*
PyString_InternFromString(const char *v)¶ - Return value: New reference.
A combination of
PyString_FromString()andPyString_InternInPlace(), returning either a new string object that hasbeen interned, or a new (“owned”) reference to an earlier interned string objectwith the same value.Note
This function is not available in 3.x and does not have a PyBytes alias.
- PyObject*
PyString_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)¶ - Return value: New reference.
Create an object by decodingsize bytes of the encoded buffers using thecodec registered forencoding.encoding anderrors have the same meaningas the parameters of the same name in the
unicode()built-in function.The codec to be used is looked up using the Python codec registry. ReturnNULL if an exception was raised by the codec.Note
This function is not available in 3.x and does not have a PyBytes alias.
Changed in version 2.5:This function used an
inttype forsize. This might requirechanges in your code for properly supporting 64-bit systems.
- PyObject*
PyString_AsDecodedObject(PyObject *str, const char *encoding, const char *errors)¶ - Return value: New reference.
Decode a string object by passing it to the codec registered forencoding andreturn the result as Python object.encoding anderrors have the samemeaning as the parameters of the same name in the string
encode()method.The codec to be used is looked up using the Python codec registry. ReturnNULLif an exception was raised by the codec.Note
This function is not available in 3.x and does not have a PyBytes alias.
- PyObject*
PyString_Encode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)¶ - Return value: New reference.
Encode the
charbuffer of the given size by passing it to the codecregistered forencoding and return a Python object.encoding anderrorshave the same meaning as the parameters of the same name in the stringencode()method. The codec to be used is looked up using the Python codecregistry. ReturnNULL if an exception was raised by the codec.Note
This function is not available in 3.x and does not have a PyBytes alias.
Changed in version 2.5:This function used an
inttype forsize. This might requirechanges in your code for properly supporting 64-bit systems.
- PyObject*
PyString_AsEncodedObject(PyObject *str, const char *encoding, const char *errors)¶ - Return value: New reference.
Encode a string object using the codec registered forencoding and return theresult as Python object.encoding anderrors have the same meaning as theparameters of the same name in the string
encode()method. The codec to beused is looked up using the Python codec registry. ReturnNULL if an exceptionwas raised by the codec.Note
This function is not available in 3.x and does not have a PyBytes alias.
