Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > e870e6598e1c7e3918555a3d0ba5f3d4 > files > 476

python3-docs-3.1.1-2mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Unicode Objects and Codecs &mdash; Python v3.1.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.1.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.1.1 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.1.1 documentation" href="../index.html" />
    <link rel="up" title="Concrete Objects Layer" href="concrete.html" />
    <link rel="next" title="Buffer Objects" href="buffer.html" />
    <link rel="prev" title="Byte Array Objects" href="bytearray.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="buffer.html" title="Buffer Objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="bytearray.html" title="Byte Array Objects"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="unicode-objects-and-codecs">
<span id="unicodeobjects"></span><h1>Unicode Objects and Codecs<a class="headerlink" href="#unicode-objects-and-codecs" title="Permalink to this headline">¶</a></h1>
<div class="section" id="unicode-objects">
<h2>Unicode Objects<a class="headerlink" href="#unicode-objects" title="Permalink to this headline">¶</a></h2>
<p>These are the basic Unicode object types used for the Unicode implementation in
Python:</p>
<dl class="ctype">
<dt id="Py_UNICODE">
<tt class="descname">Py_UNICODE</tt><a class="headerlink" href="#Py_UNICODE" title="Permalink to this definition">¶</a></dt>
<dd>This type represents the storage type which is used by Python internally as
basis for holding Unicode ordinals.  Python&#8217;s default builds use a 16-bit type
for <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> and store Unicode values internally as UCS2. It is also
possible to build a UCS4 version of Python (most recent Linux distributions come
with UCS4 builds of Python). These builds then use a 32-bit type for
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> and store Unicode data internally as UCS4. On platforms
where <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> is available and compatible with the chosen Python
Unicode build variant, <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> is a typedef alias for
<tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> to enhance native platform compatibility. On all other
platforms, <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> is a typedef alias for either <tt class="xref docutils literal"><span class="pre">unsigned</span>
<span class="pre">short</span></tt> (UCS2) or <tt class="xref docutils literal"><span class="pre">unsigned</span> <span class="pre">long</span></tt> (UCS4).</dd></dl>

<p>Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep
this in mind when writing extensions or interfaces.</p>
<dl class="ctype">
<dt id="PyUnicodeObject">
<tt class="descname">PyUnicodeObject</tt><a class="headerlink" href="#PyUnicodeObject" title="Permalink to this definition">¶</a></dt>
<dd>This subtype of <a title="PyObject" class="reference external" href="structures.html#PyObject"><tt class="xref docutils literal"><span class="pre">PyObject</span></tt></a> represents a Python Unicode object.</dd></dl>

<dl class="cvar">
<dt id="PyUnicode_Type">
<a title="PyTypeObject" class="reference external" href="type.html#PyTypeObject">PyTypeObject</a> <tt class="descname">PyUnicode_Type</tt><a class="headerlink" href="#PyUnicode_Type" title="Permalink to this definition">¶</a></dt>
<dd>This instance of <a title="PyTypeObject" class="reference external" href="type.html#PyTypeObject"><tt class="xref docutils literal"><span class="pre">PyTypeObject</span></tt></a> represents the Python Unicode type.  It
is exposed to Python code as <tt class="docutils literal"><span class="pre">str</span></tt>.</dd></dl>

<p>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:</p>
<dl class="cfunction">
<dt id="PyUnicode_Check">
int <tt class="descname">PyUnicode_Check</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_Check" title="Permalink to this definition">¶</a></dt>
<dd>Return true if the object <em>o</em> is a Unicode object or an instance of a Unicode
subtype.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_CheckExact">
int <tt class="descname">PyUnicode_CheckExact</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_CheckExact" title="Permalink to this definition">¶</a></dt>
<dd>Return true if the object <em>o</em> is a Unicode object, but not an instance of a
subtype.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_GET_SIZE">
Py_ssize_t <tt class="descname">PyUnicode_GET_SIZE</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_GET_SIZE" title="Permalink to this definition">¶</a></dt>
<dd>Return the size of the object.  <em>o</em> has to be a <a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not
checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_GET_DATA_SIZE">
Py_ssize_t <tt class="descname">PyUnicode_GET_DATA_SIZE</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_GET_DATA_SIZE" title="Permalink to this definition">¶</a></dt>
<dd>Return the size of the object&#8217;s internal buffer in bytes.  <em>o</em> has to be a
<a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AS_UNICODE">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a>* <tt class="descname">PyUnicode_AS_UNICODE</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_AS_UNICODE" title="Permalink to this definition">¶</a></dt>
<dd>Return a pointer to the internal <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the object.  <em>o</em>
has to be a <a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AS_DATA">
const char* <tt class="descname">PyUnicode_AS_DATA</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_AS_DATA" title="Permalink to this definition">¶</a></dt>
<dd>Return a pointer to the internal buffer of the object. <em>o</em> has to be a
<a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_ClearFreeList">
int <tt class="descname">PyUnicode_ClearFreeList</tt><big>(</big><big>)</big><a class="headerlink" href="#PyUnicode_ClearFreeList" title="Permalink to this definition">¶</a></dt>
<dd>Clear the free list. Return the total number of freed items.</dd></dl>

<p>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.</p>
<dl class="cfunction">
<dt id="Py_UNICODE_ISSPACE">
int <tt class="descname">Py_UNICODE_ISSPACE</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISSPACE" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a whitespace character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISLOWER">
int <tt class="descname">Py_UNICODE_ISLOWER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISLOWER" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a lowercase character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISUPPER">
int <tt class="descname">Py_UNICODE_ISUPPER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISUPPER" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is an uppercase character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISTITLE">
int <tt class="descname">Py_UNICODE_ISTITLE</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISTITLE" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a titlecase character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISLINEBREAK">
int <tt class="descname">Py_UNICODE_ISLINEBREAK</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISLINEBREAK" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a linebreak character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISDECIMAL">
int <tt class="descname">Py_UNICODE_ISDECIMAL</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISDECIMAL" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a decimal character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISDIGIT">
int <tt class="descname">Py_UNICODE_ISDIGIT</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISDIGIT" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a digit character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISNUMERIC">
int <tt class="descname">Py_UNICODE_ISNUMERIC</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISNUMERIC" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a numeric character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISALPHA">
int <tt class="descname">Py_UNICODE_ISALPHA</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISALPHA" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is an alphabetic character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISALNUM">
int <tt class="descname">Py_UNICODE_ISALNUM</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISALNUM" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is an alphanumeric character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISPRINTABLE">
int <tt class="descname">Py_UNICODE_ISPRINTABLE</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISPRINTABLE" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a printable character.
Nonprintable characters are those characters defined in the Unicode character
database as &#8220;Other&#8221; or &#8220;Separator&#8221;, excepting the ASCII space (0x20) which is
considered printable.  (Note that printable characters in this context are
those which should not be escaped when <a title="repr" class="reference external" href="../library/functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a> is invoked on a string.
It has no bearing on the handling of strings written to <a title="sys.stdout" class="reference external" href="../library/sys.html#sys.stdout"><tt class="xref docutils literal"><span class="pre">sys.stdout</span></tt></a> or
<a title="sys.stderr" class="reference external" href="../library/sys.html#sys.stderr"><tt class="xref docutils literal"><span class="pre">sys.stderr</span></tt></a>.)</dd></dl>

<p>These APIs can be used for fast direct character conversions:</p>
<dl class="cfunction">
<dt id="Py_UNICODE_TOLOWER">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a> <tt class="descname">Py_UNICODE_TOLOWER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TOLOWER" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to lower case.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TOUPPER">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a> <tt class="descname">Py_UNICODE_TOUPPER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TOUPPER" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to upper case.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TOTITLE">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a> <tt class="descname">Py_UNICODE_TOTITLE</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TOTITLE" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to title case.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TODECIMAL">
int <tt class="descname">Py_UNICODE_TODECIMAL</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TODECIMAL" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to a decimal positive integer.  Return
<tt class="docutils literal"><span class="pre">-1</span></tt> if this is not possible.  This macro does not raise exceptions.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TODIGIT">
int <tt class="descname">Py_UNICODE_TODIGIT</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TODIGIT" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to a single digit integer. Return <tt class="docutils literal"><span class="pre">-1</span></tt> if
this is not possible.  This macro does not raise exceptions.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TONUMERIC">
double <tt class="descname">Py_UNICODE_TONUMERIC</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TONUMERIC" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to a double. Return <tt class="docutils literal"><span class="pre">-1.0</span></tt> if this is not
possible.  This macro does not raise exceptions.</dd></dl>

<p>To create Unicode objects and access their basic sequence properties, use these
APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_FromUnicode">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromUnicode</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *u</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_FromUnicode" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode Object from the Py_UNICODE buffer <em>u</em> of the given size. <em>u</em>
may be <em>NULL</em> which causes the contents to be undefined. It is the user&#8217;s
responsibility to fill in the needed data.  The buffer is copied into the new
object. If the buffer is not <em>NULL</em>, the return value might be a shared object.
Therefore, modification of the resulting Unicode object is only allowed when <em>u</em>
is <em>NULL</em>.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromStringAndSize">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromStringAndSize</tt><big>(</big>const char<em> *u</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_FromStringAndSize" title="Permalink to this definition">¶</a></dt>
<dd>Create a Unicode Object from the char buffer <em>u</em>.  The bytes will be interpreted
as being UTF-8 encoded.  <em>u</em> may also be <em>NULL</em> which
causes the contents to be undefined. It is the user&#8217;s responsibility to fill in
the needed data.  The buffer is copied into the new object. If the buffer is not
<em>NULL</em>, the return value might be a shared object. Therefore, modification of
the resulting Unicode object is only allowed when <em>u</em> is <em>NULL</em>.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a> *<tt class="descname">PyUnicode_FromString</tt><big>(</big>const char<em> *u</em><big>)</big><a class="headerlink" href="#PyUnicode_FromString" title="Permalink to this definition">¶</a></dt>
<dd>Create a Unicode object from an UTF-8 encoded null-terminated char buffer
<em>u</em>.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromFormat">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromFormat</tt><big>(</big>const char<em> *format</em>, ...<big>)</big><a class="headerlink" href="#PyUnicode_FromFormat" title="Permalink to this definition">¶</a></dt>
<dd><p>Take a C <tt class="xref docutils literal"><span class="pre">printf()</span></tt>-style <em>format</em> string and a variable number of
arguments, calculate the size of the resulting Python unicode string and return
a string with the values formatted into it.  The variable arguments must be C
types and must correspond exactly to the format characters in the <em>format</em>
string.  The following format characters are allowed:</p>
<table border="1" class="docutils">
<colgroup>
<col width="26%" />
<col width="29%" />
<col width="44%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Format Characters</th>
<th class="head">Type</th>
<th class="head">Comment</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="xref docutils literal"><span class="pre">%%</span></tt></td>
<td><em>n/a</em></td>
<td>The literal % character.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%c</span></tt></td>
<td>int</td>
<td>A single character,
represented as an C int.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%d</span></tt></td>
<td>int</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%d&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%u</span></tt></td>
<td>unsigned int</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%u&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%ld</span></tt></td>
<td>long</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%ld&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%lu</span></tt></td>
<td>unsigned long</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%lu&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%zd</span></tt></td>
<td>Py_ssize_t</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%zd&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%zu</span></tt></td>
<td>size_t</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%zu&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%i</span></tt></td>
<td>int</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%i&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%x</span></tt></td>
<td>int</td>
<td>Exactly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%x&quot;)</span></tt>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%s</span></tt></td>
<td>char*</td>
<td>A null-terminated C character
array.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%p</span></tt></td>
<td>void*</td>
<td>The hex representation of a C
pointer. Mostly equivalent to
<tt class="docutils literal"><span class="pre">printf(&quot;%p&quot;)</span></tt> except that
it is guaranteed to start with
the literal <tt class="docutils literal"><span class="pre">0x</span></tt> regardless
of what the platform&#8217;s
<tt class="docutils literal"><span class="pre">printf</span></tt> yields.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%A</span></tt></td>
<td>PyObject*</td>
<td>The result of calling
<a title="ascii" class="reference external" href="../library/functions.html#ascii"><tt class="xref docutils literal"><span class="pre">ascii()</span></tt></a>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%U</span></tt></td>
<td>PyObject*</td>
<td>A unicode object.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%V</span></tt></td>
<td>PyObject*, char *</td>
<td>A unicode object (which may be
<em>NULL</em>) and a null-terminated
C character array as a second
parameter (which will be used,
if the first parameter is
<em>NULL</em>).</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%S</span></tt></td>
<td>PyObject*</td>
<td>The result of calling
<a title="PyObject_Str" class="reference external" href="object.html#PyObject_Str"><tt class="xref docutils literal"><span class="pre">PyObject_Str()</span></tt></a>.</td>
</tr>
<tr><td><tt class="xref docutils literal"><span class="pre">%R</span></tt></td>
<td>PyObject*</td>
<td>The result of calling
<a title="PyObject_Repr" class="reference external" href="object.html#PyObject_Repr"><tt class="xref docutils literal"><span class="pre">PyObject_Repr()</span></tt></a>.</td>
</tr>
</tbody>
</table>
<p>An unrecognized format character causes all the rest of the format string to be
copied as-is to the result string, and any extra arguments discarded.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromFormatV">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromFormatV</tt><big>(</big>const char<em> *format</em>, va_list<em> vargs</em><big>)</big><a class="headerlink" href="#PyUnicode_FromFormatV" title="Permalink to this definition">¶</a></dt>
<dd>Identical to <a title="PyUnicode_FromFormat" class="reference internal" href="#PyUnicode_FromFormat"><tt class="xref docutils literal"><span class="pre">PyUnicode_FromFormat()</span></tt></a> except that it takes exactly two
arguments.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUnicode">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a>* <tt class="descname">PyUnicode_AsUnicode</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUnicode" title="Permalink to this definition">¶</a></dt>
<dd>Return a read-only pointer to the Unicode object&#8217;s internal <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a>
buffer, <em>NULL</em> if <em>unicode</em> is not a Unicode object.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_GetSize">
Py_ssize_t <tt class="descname">PyUnicode_GetSize</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_GetSize" title="Permalink to this definition">¶</a></dt>
<dd>Return the length of the Unicode object.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromEncodedObject">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromEncodedObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *obj</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_FromEncodedObject" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Coerce an encoded object <em>obj</em> to an Unicode object and return a reference with
incremented refcount.</p>
<p>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
<em>NULL</em> to have the interface use the default values (see the next section for
details).</p>
<p>All other objects, including Unicode objects, cause a <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> to be
set.</p>
<p>The API returns <em>NULL</em> if there was an error.  The caller is responsible for
decref&#8217;ing the returned objects.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromObject">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *obj</em><big>)</big><a class="headerlink" href="#PyUnicode_FromObject" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Shortcut for <tt class="docutils literal"><span class="pre">PyUnicode_FromEncodedObject(obj,</span> <span class="pre">NULL,</span> <span class="pre">&quot;strict&quot;)</span></tt> which is used
throughout the interpreter whenever coercion to Unicode is needed.</p>
</dd></dl>

<p>If the platform supports <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> and provides a header file wchar.h,
Python can interface directly to this type using the following functions.
Support is optimized if Python&#8217;s own <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> type is identical to
the system&#8217;s <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt>.</p>
<dl class="cfunction">
<dt id="PyUnicode_FromWideChar">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromWideChar</tt><big>(</big>const wchar_t<em> *w</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_FromWideChar" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object from the <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> buffer <em>w</em> of the given size.
Passing -1 as the size indicates that the function must itself compute the length,
using wcslen.
Return <em>NULL</em> on failure.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsWideChar">
Py_ssize_t <tt class="descname">PyUnicode_AsWideChar</tt><big>(</big><a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject">PyUnicodeObject</a><em> *unicode</em>, wchar_t<em> *w</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_AsWideChar" title="Permalink to this definition">¶</a></dt>
<dd>Copy the Unicode object contents into the <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> buffer <em>w</em>.  At most
<em>size</em> <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> characters are copied (excluding a possibly trailing
0-termination character).  Return the number of <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> characters
copied or -1 in case of an error.  Note that the resulting <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt>
string may or may not be 0-terminated.  It is the responsibility of the caller
to make sure that the <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> string is 0-terminated in case this is
required by the application.</dd></dl>

</div>
<div class="section" id="built-in-codecs">
<span id="builtincodecs"></span><h2>Built-in Codecs<a class="headerlink" href="#built-in-codecs" title="Permalink to this headline">¶</a></h2>
<p>Python provides a set of built-in codecs which are written in C for speed. All of
these codecs are directly usable via the following functions.</p>
<p>Many of the following APIs take two arguments encoding and errors. These
parameters encoding and errors have the same semantics as the ones of the
built-in <tt class="xref docutils literal"><span class="pre">unicode()</span></tt> Unicode object constructor.</p>
<p>Setting encoding to <em>NULL</em> causes the default encoding to be used
which is ASCII.  The file system calls should use
<a title="PyUnicode_FSConverter" class="reference internal" href="#PyUnicode_FSConverter"><tt class="xref docutils literal"><span class="pre">PyUnicode_FSConverter()</span></tt></a> for encoding file names. This uses the
variable <tt class="xref docutils literal"><span class="pre">Py_FileSystemDefaultEncoding</span></tt> internally. This
variable should be treated as read-only: On some systems, it will be a
pointer to a static string, on others, it will change at run-time
(such as when the application invokes setlocale).</p>
<p>Error handling is set by errors which may also be set to <em>NULL</em> meaning to use
the default handling defined for the codec.  Default error handling for all
built-in codecs is &#8220;strict&#8221; (<a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> is raised).</p>
<p>The codecs all use a similar interface.  Only deviation from the following
generic ones are documented for simplicity.</p>
<p>These are the generic codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_Decode">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Decode</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_Decode" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the encoded string <em>s</em>.
<em>encoding</em> and <em>errors</em> have the same meaning as the parameters of the same name
in the <tt class="xref docutils literal"><span class="pre">unicode()</span></tt> built-in function.  The codec to be used is looked up
using the Python codec registry.  Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Encode">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Encode</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_Encode" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size and return a Python
bytes object.  <em>encoding</em> and <em>errors</em> have the same meaning as the
parameters of the same name in the Unicode <tt class="xref docutils literal"><span class="pre">encode()</span></tt> method.  The codec
to be used is looked up using the Python codec registry.  Return <em>NULL</em> if an
exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsEncodedString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsEncodedString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_AsEncodedString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object and return the result as Python bytes object.
<em>encoding</em> and <em>errors</em> have the same meaning as the parameters of the same
name in the Unicode <tt class="xref docutils literal"><span class="pre">encode()</span></tt> method. The codec to be used is looked up
using the Python codec registry. Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<p>These are the UTF-8 codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF8">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF8</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF8" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the UTF-8 encoded string
<em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF8Stateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF8Stateful</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, Py_ssize_t<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF8Stateful" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeUTF8" class="reference internal" href="#PyUnicode_DecodeUTF8"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF8()</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, trailing incomplete UTF-8 byte sequences will not be
treated as an error. Those bytes will not be decoded and the number of bytes
that have been decoded will be stored in <em>consumed</em>.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUTF8">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUTF8</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUTF8" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using UTF-8 and
return a Python bytes object.  Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUTF8String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUTF8String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUTF8String" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using UTF-8 and return the result as Python bytes
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was
raised by the codec.</p>
</dd></dl>

<p>These are the UTF-32 codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF32">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF32</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF32" title="Permalink to this definition">¶</a></dt>
<dd><p>Decode <em>length</em> bytes from a UTF-32 encoded buffer string and return the
corresponding Unicode object.  <em>errors</em> (if non-<em>NULL</em>) defines the error
handling. It defaults to &#8220;strict&#8221;.</p>
<p>If <em>byteorder</em> is non-<em>NULL</em>, the decoder starts decoding using the given byte
order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">order</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>and then switches if the first four bytes of the input data are a byte order mark
(BOM) and the specified byte order is native order.  This BOM is not copied into
the resulting Unicode string.  After completion, <em>*byteorder</em> is set to the
current byte order at the end of input data.</p>
<p>In a narrow build codepoints outside the BMP will be decoded as surrogate pairs.</p>
<p>If <em>byteorder</em> is <em>NULL</em>, the codec starts in native order mode.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF32Stateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF32Stateful</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em>, Py_ssize_t<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF32Stateful" title="Permalink to this definition">¶</a></dt>
<dd>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeUTF32" class="reference internal" href="#PyUnicode_DecodeUTF32"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF32()</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, <a title="PyUnicode_DecodeUTF32Stateful" class="reference internal" href="#PyUnicode_DecodeUTF32Stateful"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF32Stateful()</span></tt></a> will not treat
trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
by four) as an error. Those bytes will not be decoded and the number of bytes
that have been decoded will be stored in <em>consumed</em>.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUTF32">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUTF32</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUTF32" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a Python bytes object holding the UTF-32 encoded value of the Unicode
data in <em>s</em>.  If <em>byteorder</em> is not <tt class="docutils literal"><span class="pre">0</span></tt>, output is written according to the
following byte order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">byte</span> <span class="n">order</span> <span class="p">(</span><span class="n">writes</span> <span class="n">a</span> <span class="n">BOM</span> <span class="n">mark</span><span class="p">)</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>If byteorder is <tt class="docutils literal"><span class="pre">0</span></tt>, the output string will always start with the Unicode BOM
mark (U+FEFF). In the other two modes, no BOM mark is prepended.</p>
<p>If <em>Py_UNICODE_WIDE</em> is not defined, surrogate pairs will be output
as a single codepoint.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUTF32String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUTF32String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUTF32String" title="Permalink to this definition">¶</a></dt>
<dd>Return a Python byte string using the UTF-32 encoding in native byte
order. The string always starts with a BOM mark.  Error handling is &#8220;strict&#8221;.
Return <em>NULL</em> if an exception was raised by the codec.</dd></dl>

<p>These are the UTF-16 codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF16">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF16</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF16" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decode <em>length</em> bytes from a UTF-16 encoded buffer string and return the
corresponding Unicode object.  <em>errors</em> (if non-<em>NULL</em>) defines the error
handling. It defaults to &#8220;strict&#8221;.</p>
<p>If <em>byteorder</em> is non-<em>NULL</em>, the decoder starts decoding using the given byte
order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">order</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>and then switches if the first two bytes of the input data are a byte order mark
(BOM) and the specified byte order is native order.  This BOM is not copied into
the resulting Unicode string.  After completion, <em>*byteorder</em> is set to the
current byte order at the end of input data.</p>
<p>If <em>byteorder</em> is <em>NULL</em>, the codec starts in native order mode.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF16Stateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF16Stateful</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em>, Py_ssize_t<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF16Stateful" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeUTF16" class="reference internal" href="#PyUnicode_DecodeUTF16"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF16()</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, <a title="PyUnicode_DecodeUTF16Stateful" class="reference internal" href="#PyUnicode_DecodeUTF16Stateful"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF16Stateful()</span></tt></a> will not treat
trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
split surrogate pair) as an error. Those bytes will not be decoded and the
number of bytes that have been decoded will be stored in <em>consumed</em>.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUTF16">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUTF16</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUTF16" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a Python bytes object holding the UTF-16 encoded value of the Unicode
data in <em>s</em>.  If <em>byteorder</em> is not <tt class="docutils literal"><span class="pre">0</span></tt>, output is written according to the
following byte order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">byte</span> <span class="n">order</span> <span class="p">(</span><span class="n">writes</span> <span class="n">a</span> <span class="n">BOM</span> <span class="n">mark</span><span class="p">)</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>If byteorder is <tt class="docutils literal"><span class="pre">0</span></tt>, the output string will always start with the Unicode BOM
mark (U+FEFF). In the other two modes, no BOM mark is prepended.</p>
<p>If <em>Py_UNICODE_WIDE</em> is defined, a single <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> value may get
represented as a surrogate pair. If it is not defined, each <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a>
values is interpreted as an UCS-2 character.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUTF16String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUTF16String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUTF16String" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a Python byte string using the UTF-16 encoding in native byte
order. The string always starts with a BOM mark.  Error handling is &#8220;strict&#8221;.
Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<p>These are the &#8220;Unicode Escape&#8221; codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUnicodeEscape</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the Unicode-Escape encoded
string <em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUnicodeEscape</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using Unicode-Escape and
return a Python string object.  Return <em>NULL</em> if an exception was raised by the
codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUnicodeEscapeString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUnicodeEscapeString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUnicodeEscapeString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Unicode-Escape and return the result as Python
string object.  Error handling is &#8220;strict&#8221;. Return <em>NULL</em> if an exception was
raised by the codec.</p>
</dd></dl>

<p>These are the &#8220;Raw Unicode Escape&#8221; codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeRawUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeRawUnicodeEscape</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeRawUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the Raw-Unicode-Escape
encoded string <em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeRawUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeRawUnicodeEscape</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeRawUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using Raw-Unicode-Escape
and return a Python string object.  Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsRawUnicodeEscapeString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsRawUnicodeEscapeString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsRawUnicodeEscapeString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Raw-Unicode-Escape and return the result as
Python string object. Error handling is &#8220;strict&#8221;. Return <em>NULL</em> if an exception
was raised by the codec.</p>
</dd></dl>

<p>These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
ordinals and only these are accepted by the codecs during encoding.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeLatin1">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeLatin1</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeLatin1" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the Latin-1 encoded string
<em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeLatin1">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeLatin1</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeLatin1" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using Latin-1 and
return a Python bytes object.  Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsLatin1String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsLatin1String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsLatin1String" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Latin-1 and return the result as Python bytes
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was
raised by the codec.</p>
</dd></dl>

<p>These are the ASCII codec APIs.  Only 7-bit ASCII data is accepted. All other
codes generate errors.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeASCII">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeASCII</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeASCII" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the ASCII encoded string
<em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeASCII">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeASCII</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeASCII" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using ASCII and
return a Python bytes object.  Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsASCIIString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsASCIIString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsASCIIString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using ASCII and return the result as Python bytes
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was
raised by the codec.</p>
</dd></dl>

<p>These are the mapping codec APIs:</p>
<p>This codec is special in that it can be used to implement many different codecs
(and this is in fact what was done to obtain most of the standard codecs
included in the <tt class="xref docutils literal"><span class="pre">encodings</span></tt> package). The codec uses mapping to encode and
decode characters.</p>
<p>Decoding mappings must map single string characters to single Unicode
characters, integers (which are then interpreted as Unicode ordinals) or None
(meaning &#8220;undefined mapping&#8221; and causing an error).</p>
<p>Encoding mappings must map single Unicode characters to single string
characters, integers (which are then interpreted as Latin-1 ordinals) or None
(meaning &#8220;undefined mapping&#8221; and causing an error).</p>
<p>The mapping objects provided must only support the __getitem__ mapping
interface.</p>
<p>If a character lookup fails with a LookupError, the character is copied as-is
meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
resp. Because of this, mappings only need to contain those mappings which map
characters to different code points.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeCharmap">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeCharmap</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *mapping</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeCharmap" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the encoded string <em>s</em> using
the given <em>mapping</em> object.  Return <em>NULL</em> if an exception was raised by the
codec. If <em>mapping</em> is <em>NULL</em> latin-1 decoding will be done. Else it can be a
dictionary mapping byte or a unicode string, which is treated as a lookup table.
Byte values greater that the length of the string and U+FFFE &#8220;characters&#8221; are
treated as &#8220;undefined mapping&#8221;.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeCharmap">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeCharmap</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *mapping</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeCharmap" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using the given
<em>mapping</em> object and return a Python string object. Return <em>NULL</em> if an
exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsCharmapString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsCharmapString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *mapping</em><big>)</big><a class="headerlink" href="#PyUnicode_AsCharmapString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using the given <em>mapping</em> object and return the result
as Python string object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an
exception was raised by the codec.</p>
</dd></dl>

<p>The following codec API is special in that maps Unicode to Unicode.</p>
<dl class="cfunction">
<dt id="PyUnicode_TranslateCharmap">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_TranslateCharmap</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *table</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_TranslateCharmap" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Translate a <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given length by applying a
character mapping <em>table</em> to it and return the resulting Unicode object.  Return
<em>NULL</em> when an exception was raised by the codec.</p>
<p>The <em>mapping</em> table must map Unicode ordinal integers to Unicode ordinal
integers or None (causing deletion of the character).</p>
<p>Mapping tables need only provide the <a title="object.__getitem__" class="reference external" href="../reference/datamodel.html#object.__getitem__"><tt class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a> interface; dictionaries
and sequences work well.  Unmapped character ordinals (ones which cause a
<a title="exceptions.LookupError" class="reference external" href="../library/exceptions.html#exceptions.LookupError"><tt class="xref docutils literal"><span class="pre">LookupError</span></tt></a>) are left untouched and are copied as-is.</p>
</dd></dl>

<p>These are the MBCS codec APIs. They are currently only available on Windows and
use the Win32 MBCS converters to implement the conversions.  Note that MBCS (or
DBCS) is a class of encodings, not just one.  The target encoding is defined by
the user settings on the machine running the codec.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeMBCS">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeMBCS</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeMBCS" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the MBCS encoded string <em>s</em>.
Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeMBCSStateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeMBCSStateful</tt><big>(</big>const char<em> *s</em>, int<em> size</em>, const char<em> *errors</em>, int<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeMBCSStateful" title="Permalink to this definition">¶</a></dt>
<dd>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeMBCS" class="reference internal" href="#PyUnicode_DecodeMBCS"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeMBCS()</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, <a title="PyUnicode_DecodeMBCSStateful" class="reference internal" href="#PyUnicode_DecodeMBCSStateful"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeMBCSStateful()</span></tt></a> will not decode
trailing lead byte and the number of bytes that have been decoded will be stored
in <em>consumed</em>.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeMBCS">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeMBCS</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeMBCS" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using MBCS and return
a Python bytes object.  Return <em>NULL</em> if an exception was raised by the
codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsMBCSString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsMBCSString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsMBCSString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using MBCS and return the result as Python bytes
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was
raised by the codec.</p>
</dd></dl>

<p>For decoding file names and other environment strings, <tt class="xref docutils literal"><span class="pre">Py_FileSystemEncoding</span></tt>
should be used as the encoding, and <tt class="docutils literal"><span class="pre">&quot;surrogateescape&quot;</span></tt> should be used as the error
handler. For encoding file names during argument parsing, the <tt class="docutils literal"><span class="pre">O&amp;</span></tt> converter should
be used, passsing PyUnicode_FSConverter as the conversion function:</p>
<dl class="cfunction">
<dt id="PyUnicode_FSConverter">
int <tt class="descname">PyUnicode_FSConverter</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>*<em> obj</em>, void*<em> result</em><big>)</big><a class="headerlink" href="#PyUnicode_FSConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert <em>obj</em> into <em>result</em>, using the file system encoding, and the <tt class="docutils literal"><span class="pre">surrogateescape</span></tt>
error handler. <em>result</em> must be a <tt class="docutils literal"><span class="pre">PyObject*</span></tt>, yielding a bytes or bytearray object
which must be released if it is no longer used.</p>
<p>
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

</div>
<div class="section" id="methods-and-slot-functions">
<span id="unicodemethodsandslots"></span><h2>Methods and Slot Functions<a class="headerlink" href="#methods-and-slot-functions" title="Permalink to this headline">¶</a></h2>
<p>The following APIs are capable of handling Unicode objects and strings on input
(we refer to them as strings in the descriptions) and return Unicode objects or
integers as appropriate.</p>
<p>They all return <em>NULL</em> or <tt class="docutils literal"><span class="pre">-1</span></tt> if an exception occurs.</p>
<dl class="cfunction">
<dt id="PyUnicode_Concat">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Concat</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *left</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *right</em><big>)</big><a class="headerlink" href="#PyUnicode_Concat" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Concat two strings giving a new Unicode string.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Split">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Split</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *s</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *sep</em>, Py_ssize_t<em> maxsplit</em><big>)</big><a class="headerlink" href="#PyUnicode_Split" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Split a string giving a list of Unicode strings.  If sep is <em>NULL</em>, splitting
will be done at all whitespace substrings.  Otherwise, splits occur at the given
separator.  At most <em>maxsplit</em> splits will be done.  If negative, no limit is
set.  Separators are not included in the resulting list.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Splitlines">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Splitlines</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *s</em>, int<em> keepend</em><big>)</big><a class="headerlink" href="#PyUnicode_Splitlines" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Split a Unicode string at line breaks, returning a list of Unicode strings.
CRLF is considered to be one line break.  If <em>keepend</em> is 0, the Line break
characters are not included in the resulting strings.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Translate">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Translate</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *table</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_Translate" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Translate a string by applying a character mapping table to it and return the
resulting Unicode object.</p>
<p>The mapping table must map Unicode ordinal integers to Unicode ordinal integers
or None (causing deletion of the character).</p>
<p>Mapping tables need only provide the <a title="object.__getitem__" class="reference external" href="../reference/datamodel.html#object.__getitem__"><tt class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a> interface; dictionaries
and sequences work well.  Unmapped character ordinals (ones which cause a
<a title="exceptions.LookupError" class="reference external" href="../library/exceptions.html#exceptions.LookupError"><tt class="xref docutils literal"><span class="pre">LookupError</span></tt></a>) are left untouched and are copied as-is.</p>
<p><em>errors</em> has the usual meaning for codecs. It may be <em>NULL</em> which indicates to
use the default error handling.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Join">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Join</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *separator</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *seq</em><big>)</big><a class="headerlink" href="#PyUnicode_Join" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Join a sequence of strings using the given separator and return the resulting
Unicode string.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Tailmatch">
int <tt class="descname">PyUnicode_Tailmatch</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em>, int<em> direction</em><big>)</big><a class="headerlink" href="#PyUnicode_Tailmatch" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return 1 if <em>substr</em> matches <em>str*[*start</em>:<em>end</em>] at the given tail end
(<em>direction</em> == -1 means to do a prefix match, <em>direction</em> == 1 a suffix match),
0 otherwise. Return <tt class="docutils literal"><span class="pre">-1</span></tt> if an error occurred.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Find">
Py_ssize_t <tt class="descname">PyUnicode_Find</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em>, int<em> direction</em><big>)</big><a class="headerlink" href="#PyUnicode_Find" title="Permalink to this definition">¶</a></dt>
<dd>Return the first position of <em>substr</em> in <em>str*[*start</em>:<em>end</em>] using the given
<em>direction</em> (<em>direction</em> == 1 means to do a forward search, <em>direction</em> == -1 a
backward search).  The return value is the index of the first match; a value of
<tt class="docutils literal"><span class="pre">-1</span></tt> indicates that no match was found, and <tt class="docutils literal"><span class="pre">-2</span></tt> indicates that an error
occurred and an exception has been set.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Count">
Py_ssize_t <tt class="descname">PyUnicode_Count</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em><big>)</big><a class="headerlink" href="#PyUnicode_Count" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of non-overlapping occurrences of <em>substr</em> in
<tt class="docutils literal"><span class="pre">str[start:end]</span></tt>.  Return <tt class="docutils literal"><span class="pre">-1</span></tt> if an error occurred.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Replace">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Replace</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *replstr</em>, Py_ssize_t<em> maxcount</em><big>)</big><a class="headerlink" href="#PyUnicode_Replace" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Replace at most <em>maxcount</em> occurrences of <em>substr</em> in <em>str</em> with <em>replstr</em> and
return the resulting Unicode object. <em>maxcount</em> == -1 means replace all
occurrences.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Compare">
int <tt class="descname">PyUnicode_Compare</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *left</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *right</em><big>)</big><a class="headerlink" href="#PyUnicode_Compare" title="Permalink to this definition">¶</a></dt>
<dd>Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
respectively.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_CompareWithASCIIString">
int <tt class="descname">PyUnicode_CompareWithASCIIString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *uni</em>, char<em> *string</em><big>)</big><a class="headerlink" href="#PyUnicode_CompareWithASCIIString" title="Permalink to this definition">¶</a></dt>
<dd>Compare a unicode object, <em>uni</em>, with <em>string</em> and return -1, 0, 1 for less
than, equal, and greater than, respectively.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_RichCompare">
int <tt class="descname">PyUnicode_RichCompare</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *left</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *right</em>, int<em> op</em><big>)</big><a class="headerlink" href="#PyUnicode_RichCompare" title="Permalink to this definition">¶</a></dt>
<dd><p>Rich compare two unicode strings and return one of the following:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">NULL</span></tt> in case an exception was raised</li>
<li><a title="Py_True" class="reference external" href="bool.html#Py_True"><tt class="xref docutils literal"><span class="pre">Py_True</span></tt></a> or <a title="Py_False" class="reference external" href="bool.html#Py_False"><tt class="xref docutils literal"><span class="pre">Py_False</span></tt></a> for successful comparisons</li>
<li><tt class="xref docutils literal"><span class="pre">Py_NotImplemented</span></tt> in case the type combination is unknown</li>
</ul>
<p>Note that <tt class="xref docutils literal"><span class="pre">Py_EQ</span></tt> and <tt class="xref docutils literal"><span class="pre">Py_NE</span></tt> comparisons can cause a
<a title="exceptions.UnicodeWarning" class="reference external" href="../library/exceptions.html#exceptions.UnicodeWarning"><tt class="xref docutils literal"><span class="pre">UnicodeWarning</span></tt></a> in case the conversion of the arguments to Unicode fails
with a <a title="exceptions.UnicodeDecodeError" class="reference external" href="../library/exceptions.html#exceptions.UnicodeDecodeError"><tt class="xref docutils literal"><span class="pre">UnicodeDecodeError</span></tt></a>.</p>
<p>Possible values for <em>op</em> are <tt class="xref docutils literal"><span class="pre">Py_GT</span></tt>, <tt class="xref docutils literal"><span class="pre">Py_GE</span></tt>, <tt class="xref docutils literal"><span class="pre">Py_EQ</span></tt>,
<tt class="xref docutils literal"><span class="pre">Py_NE</span></tt>, <tt class="xref docutils literal"><span class="pre">Py_LT</span></tt>, and <tt class="xref docutils literal"><span class="pre">Py_LE</span></tt>.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Format">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Format</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *format</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *args</em><big>)</big><a class="headerlink" href="#PyUnicode_Format" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a new string object from <em>format</em> and <em>args</em>; this is analogous to
<tt class="docutils literal"><span class="pre">format</span> <span class="pre">%</span> <span class="pre">args</span></tt>.  The <em>args</em> argument must be a tuple.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Contains">
int <tt class="descname">PyUnicode_Contains</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *container</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *element</em><big>)</big><a class="headerlink" href="#PyUnicode_Contains" title="Permalink to this definition">¶</a></dt>
<dd><p>Check whether <em>element</em> is contained in <em>container</em> and return true or false
accordingly.</p>
<p><em>element</em> has to coerce to a one element Unicode string. <tt class="docutils literal"><span class="pre">-1</span></tt> is returned if
there was an error.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_InternInPlace">
void <tt class="descname">PyUnicode_InternInPlace</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> **string</em><big>)</big><a class="headerlink" href="#PyUnicode_InternInPlace" title="Permalink to this definition">¶</a></dt>
<dd>Intern the argument <em>*string</em> in place.  The argument must be the address of a
pointer variable pointing to a Python unicode string object.  If there is an
existing interned string that is the same as <em>*string</em>, it sets <em>*string</em> to
it (decrementing the reference count of the old string object and incrementing
the reference count of the interned string object), otherwise it leaves
<em>*string</em> alone and interns it (incrementing its reference count).
(Clarification: even though there is a lot of talk about reference counts, think
of this function as reference-count-neutral; you own the object after the call
if and only if you owned it before the call.)</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_InternFromString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_InternFromString</tt><big>(</big>const char<em> *v</em><big>)</big><a class="headerlink" href="#PyUnicode_InternFromString" title="Permalink to this definition">¶</a></dt>
<dd>A combination of <a title="PyUnicode_FromString" class="reference internal" href="#PyUnicode_FromString"><tt class="xref docutils literal"><span class="pre">PyUnicode_FromString()</span></tt></a> and
<a title="PyUnicode_InternInPlace" class="reference internal" href="#PyUnicode_InternInPlace"><tt class="xref docutils literal"><span class="pre">PyUnicode_InternInPlace()</span></tt></a>, returning either a new unicode string object
that has been interned, or a new (&#8220;owned&#8221;) reference to an earlier interned
string object with the same value.</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Unicode Objects and Codecs</a><ul>
<li><a class="reference external" href="#unicode-objects">Unicode Objects</a></li>
<li><a class="reference external" href="#built-in-codecs">Built-in Codecs</a></li>
<li><a class="reference external" href="#methods-and-slot-functions">Methods and Slot Functions</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="bytearray.html"
                                  title="previous chapter">Byte Array Objects</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="buffer.html"
                                  title="next chapter">Buffer Objects</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/c-api/unicode.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="buffer.html" title="Buffer Objects"
             >next</a> |</li>
        <li class="right" >
          <a href="bytearray.html" title="Byte Array Objects"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="concrete.html" >Concrete Objects Layer</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 16, 2009.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
    </div>

  </body>
</html>