1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>Unicode Objects and Codecs — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="Concrete Objects Layer" href="concrete.html" />
34
<link rel="next" title="Buffers and Memoryview Objects" href="buffer.html" />
35
<link rel="prev" title="String/Bytes Objects" href="string.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="buffer.html" title="Buffers and Memoryview Objects"
55
accesskey="N">next</a> |</li>
57
<a href="string.html" title="String/Bytes Objects"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> »</li>
68
<li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="unicode-objects-and-codecs">
78
<span id="unicodeobjects"></span><h1>Unicode Objects and Codecs<a class="headerlink" href="#unicode-objects-and-codecs" title="Permalink to this headline">¶</a></h1>
79
<div class="section" id="unicode-objects">
80
<h2>Unicode Objects<a class="headerlink" href="#unicode-objects" title="Permalink to this headline">¶</a></h2>
81
<div class="section" id="unicode-type">
82
<h3>Unicode Type<a class="headerlink" href="#unicode-type" title="Permalink to this headline">¶</a></h3>
83
<p>These are the basic Unicode object types used for the Unicode implementation in
86
<dt id="c.Py_UNICODE">
87
<code class="descname">Py_UNICODE</code><a class="headerlink" href="#c.Py_UNICODE" title="Permalink to this definition">¶</a></dt>
88
<dd><p>This type represents the storage type which is used by Python internally as
89
basis for holding Unicode ordinals. Python’s default builds use a 16-bit type
90
for <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> and store Unicode values internally as UCS2. It is also
91
possible to build a UCS4 version of Python (most recent Linux distributions come
92
with UCS4 builds of Python). These builds then use a 32-bit type for
93
<a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> and store Unicode data internally as UCS4. On platforms
94
where <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> is available and compatible with the chosen Python
95
Unicode build variant, <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> is a typedef alias for
96
<code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> to enhance native platform compatibility. On all other
97
platforms, <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> is a typedef alias for either <code class="xref c c-type docutils literal"><span class="pre">unsigned</span>
98
<span class="pre">short</span></code> (UCS2) or <code class="xref c c-type docutils literal"><span class="pre">unsigned</span> <span class="pre">long</span></code> (UCS4).</p>
101
<p>Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep
102
this in mind when writing extensions or interfaces.</p>
104
<dt id="c.PyUnicodeObject">
105
<code class="descname">PyUnicodeObject</code><a class="headerlink" href="#c.PyUnicodeObject" title="Permalink to this definition">¶</a></dt>
106
<dd><p>This subtype of <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject</span></code></a> represents a Python Unicode object.</p>
110
<dt id="c.PyUnicode_Type">
111
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a> <code class="descname">PyUnicode_Type</code><a class="headerlink" href="#c.PyUnicode_Type" title="Permalink to this definition">¶</a></dt>
112
<dd><p>This instance of <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></code></a> represents the Python Unicode type. It
113
is exposed to Python code as <code class="docutils literal"><span class="pre">unicode</span></code> and <code class="docutils literal"><span class="pre">types.UnicodeType</span></code>.</p>
116
<p>The following APIs are really C macros and can be used to do fast checks and to
117
access internal read-only data of Unicode objects:</p>
118
<dl class="function">
119
<dt id="c.PyUnicode_Check">
120
int <code class="descname">PyUnicode_Check</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Check" title="Permalink to this definition">¶</a></dt>
121
<dd><p>Return true if the object <em>o</em> is a Unicode object or an instance of a Unicode
123
<div class="versionchanged">
124
<p><span class="versionmodified">Changed in version 2.2: </span>Allowed subtypes to be accepted.</p>
128
<dl class="function">
129
<dt id="c.PyUnicode_CheckExact">
130
int <code class="descname">PyUnicode_CheckExact</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_CheckExact" title="Permalink to this definition">¶</a></dt>
131
<dd><p>Return true if the object <em>o</em> is a Unicode object, but not an instance of a
133
<div class="versionadded">
134
<p><span class="versionmodified">New in version 2.2.</span></p>
138
<dl class="function">
139
<dt id="c.PyUnicode_GET_SIZE">
140
Py_ssize_t <code class="descname">PyUnicode_GET_SIZE</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_GET_SIZE" title="Permalink to this definition">¶</a></dt>
141
<dd><p>Return the size of the object. <em>o</em> has to be a <a class="reference internal" href="#c.PyUnicodeObject" title="PyUnicodeObject"><code class="xref c c-type docutils literal"><span class="pre">PyUnicodeObject</span></code></a> (not
143
<div class="versionchanged">
144
<p><span class="versionmodified">Changed in version 2.5: </span>This function returned an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type. This might require changes
145
in your code for properly supporting 64-bit systems.</p>
149
<dl class="function">
150
<dt id="c.PyUnicode_GET_DATA_SIZE">
151
Py_ssize_t <code class="descname">PyUnicode_GET_DATA_SIZE</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_GET_DATA_SIZE" title="Permalink to this definition">¶</a></dt>
152
<dd><p>Return the size of the object’s internal buffer in bytes. <em>o</em> has to be a
153
<a class="reference internal" href="#c.PyUnicodeObject" title="PyUnicodeObject"><code class="xref c c-type docutils literal"><span class="pre">PyUnicodeObject</span></code></a> (not checked).</p>
154
<div class="versionchanged">
155
<p><span class="versionmodified">Changed in version 2.5: </span>This function returned an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type. This might require changes
156
in your code for properly supporting 64-bit systems.</p>
160
<dl class="function">
161
<dt id="c.PyUnicode_AS_UNICODE">
162
<a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a>* <code class="descname">PyUnicode_AS_UNICODE</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AS_UNICODE" title="Permalink to this definition">¶</a></dt>
163
<dd><p>Return a pointer to the internal <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the object. <em>o</em>
164
has to be a <a class="reference internal" href="#c.PyUnicodeObject" title="PyUnicodeObject"><code class="xref c c-type docutils literal"><span class="pre">PyUnicodeObject</span></code></a> (not checked).</p>
167
<dl class="function">
168
<dt id="c.PyUnicode_AS_DATA">
169
const char* <code class="descname">PyUnicode_AS_DATA</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AS_DATA" title="Permalink to this definition">¶</a></dt>
170
<dd><p>Return a pointer to the internal buffer of the object. <em>o</em> has to be a
171
<a class="reference internal" href="#c.PyUnicodeObject" title="PyUnicodeObject"><code class="xref c c-type docutils literal"><span class="pre">PyUnicodeObject</span></code></a> (not checked).</p>
174
<dl class="function">
175
<dt id="c.PyUnicode_ClearFreeList">
176
int <code class="descname">PyUnicode_ClearFreeList</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_ClearFreeList" title="Permalink to this definition">¶</a></dt>
177
<dd><p>Clear the free list. Return the total number of freed items.</p>
178
<div class="versionadded">
179
<p><span class="versionmodified">New in version 2.6.</span></p>
184
<div class="section" id="unicode-character-properties">
185
<h3>Unicode Character Properties<a class="headerlink" href="#unicode-character-properties" title="Permalink to this headline">¶</a></h3>
186
<p>Unicode provides many different character properties. The most often needed ones
187
are available through these macros which are mapped to C functions depending on
188
the Python configuration.</p>
189
<dl class="function">
190
<dt id="c.Py_UNICODE_ISSPACE">
191
int <code class="descname">Py_UNICODE_ISSPACE</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISSPACE" title="Permalink to this definition">¶</a></dt>
192
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is a whitespace character.</p>
195
<dl class="function">
196
<dt id="c.Py_UNICODE_ISLOWER">
197
int <code class="descname">Py_UNICODE_ISLOWER</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISLOWER" title="Permalink to this definition">¶</a></dt>
198
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is a lowercase character.</p>
201
<dl class="function">
202
<dt id="c.Py_UNICODE_ISUPPER">
203
int <code class="descname">Py_UNICODE_ISUPPER</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISUPPER" title="Permalink to this definition">¶</a></dt>
204
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is an uppercase character.</p>
207
<dl class="function">
208
<dt id="c.Py_UNICODE_ISTITLE">
209
int <code class="descname">Py_UNICODE_ISTITLE</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISTITLE" title="Permalink to this definition">¶</a></dt>
210
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is a titlecase character.</p>
213
<dl class="function">
214
<dt id="c.Py_UNICODE_ISLINEBREAK">
215
int <code class="descname">Py_UNICODE_ISLINEBREAK</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISLINEBREAK" title="Permalink to this definition">¶</a></dt>
216
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is a linebreak character.</p>
219
<dl class="function">
220
<dt id="c.Py_UNICODE_ISDECIMAL">
221
int <code class="descname">Py_UNICODE_ISDECIMAL</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISDECIMAL" title="Permalink to this definition">¶</a></dt>
222
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is a decimal character.</p>
225
<dl class="function">
226
<dt id="c.Py_UNICODE_ISDIGIT">
227
int <code class="descname">Py_UNICODE_ISDIGIT</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISDIGIT" title="Permalink to this definition">¶</a></dt>
228
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is a digit character.</p>
231
<dl class="function">
232
<dt id="c.Py_UNICODE_ISNUMERIC">
233
int <code class="descname">Py_UNICODE_ISNUMERIC</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISNUMERIC" title="Permalink to this definition">¶</a></dt>
234
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is a numeric character.</p>
237
<dl class="function">
238
<dt id="c.Py_UNICODE_ISALPHA">
239
int <code class="descname">Py_UNICODE_ISALPHA</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISALPHA" title="Permalink to this definition">¶</a></dt>
240
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is an alphabetic character.</p>
243
<dl class="function">
244
<dt id="c.Py_UNICODE_ISALNUM">
245
int <code class="descname">Py_UNICODE_ISALNUM</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_ISALNUM" title="Permalink to this definition">¶</a></dt>
246
<dd><p>Return 1 or 0 depending on whether <em>ch</em> is an alphanumeric character.</p>
249
<p>These APIs can be used for fast direct character conversions:</p>
250
<dl class="function">
251
<dt id="c.Py_UNICODE_TOLOWER">
252
<a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a> <code class="descname">Py_UNICODE_TOLOWER</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_TOLOWER" title="Permalink to this definition">¶</a></dt>
253
<dd><p>Return the character <em>ch</em> converted to lower case.</p>
256
<dl class="function">
257
<dt id="c.Py_UNICODE_TOUPPER">
258
<a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a> <code class="descname">Py_UNICODE_TOUPPER</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_TOUPPER" title="Permalink to this definition">¶</a></dt>
259
<dd><p>Return the character <em>ch</em> converted to upper case.</p>
262
<dl class="function">
263
<dt id="c.Py_UNICODE_TOTITLE">
264
<a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a> <code class="descname">Py_UNICODE_TOTITLE</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_TOTITLE" title="Permalink to this definition">¶</a></dt>
265
<dd><p>Return the character <em>ch</em> converted to title case.</p>
268
<dl class="function">
269
<dt id="c.Py_UNICODE_TODECIMAL">
270
int <code class="descname">Py_UNICODE_TODECIMAL</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_TODECIMAL" title="Permalink to this definition">¶</a></dt>
271
<dd><p>Return the character <em>ch</em> converted to a decimal positive integer. Return
272
<code class="docutils literal"><span class="pre">-1</span></code> if this is not possible. This macro does not raise exceptions.</p>
275
<dl class="function">
276
<dt id="c.Py_UNICODE_TODIGIT">
277
int <code class="descname">Py_UNICODE_TODIGIT</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_TODIGIT" title="Permalink to this definition">¶</a></dt>
278
<dd><p>Return the character <em>ch</em> converted to a single digit integer. Return <code class="docutils literal"><span class="pre">-1</span></code> if
279
this is not possible. This macro does not raise exceptions.</p>
282
<dl class="function">
283
<dt id="c.Py_UNICODE_TONUMERIC">
284
double <code class="descname">Py_UNICODE_TONUMERIC</code><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> ch</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNICODE_TONUMERIC" title="Permalink to this definition">¶</a></dt>
285
<dd><p>Return the character <em>ch</em> converted to a double. Return <code class="docutils literal"><span class="pre">-1.0</span></code> if this is not
286
possible. This macro does not raise exceptions.</p>
290
<div class="section" id="plain-py-unicode">
291
<h3>Plain Py_UNICODE<a class="headerlink" href="#plain-py-unicode" title="Permalink to this headline">¶</a></h3>
292
<p>To create Unicode objects and access their basic sequence properties, use these
294
<dl class="function">
295
<dt id="c.PyUnicode_FromUnicode">
296
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_FromUnicode</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *u</em>, Py_ssize_t<em> size</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromUnicode" title="Permalink to this definition">¶</a></dt>
297
<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>
298
may be <em>NULL</em> which causes the contents to be undefined. It is the user’s
299
responsibility to fill in the needed data. The buffer is copied into the new
300
object. If the buffer is not <em>NULL</em>, the return value might be a shared object.
301
Therefore, modification of the resulting Unicode object is only allowed when <em>u</em>
302
is <em>NULL</em>.</p>
303
<div class="versionchanged">
304
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
305
changes in your code for properly supporting 64-bit systems.</p>
309
<dl class="function">
310
<dt id="c.PyUnicode_FromStringAndSize">
311
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_FromStringAndSize</code><span class="sig-paren">(</span>const char<em> *u</em>, Py_ssize_t<em> size</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromStringAndSize" title="Permalink to this definition">¶</a></dt>
312
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object from the char buffer <em>u</em>. The bytes will be interpreted
313
as being UTF-8 encoded. <em>u</em> may also be <em>NULL</em> which
314
causes the contents to be undefined. It is the user’s responsibility to fill in
315
the needed data. The buffer is copied into the new object. If the buffer is not
316
<em>NULL</em>, the return value might be a shared object. Therefore, modification of
317
the resulting Unicode object is only allowed when <em>u</em> is <em>NULL</em>.</p>
318
<div class="versionadded">
319
<p><span class="versionmodified">New in version 2.6.</span></p>
323
<dl class="function">
324
<dt id="c.PyUnicode_FromString">
325
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a> *<code class="descname">PyUnicode_FromString</code><span class="sig-paren">(</span>const char<em> *u</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromString" title="Permalink to this definition">¶</a></dt>
326
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object from an UTF-8 encoded null-terminated char buffer
328
<div class="versionadded">
329
<p><span class="versionmodified">New in version 2.6.</span></p>
333
<dl class="function">
334
<dt id="c.PyUnicode_FromFormat">
335
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_FromFormat</code><span class="sig-paren">(</span>const char<em> *format</em>, ...<span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromFormat" title="Permalink to this definition">¶</a></dt>
336
<dd><em class="refcount">Return value: New reference.</em><p>Take a C <code class="xref c c-func docutils literal"><span class="pre">printf()</span></code>-style <em>format</em> string and a variable number of
337
arguments, calculate the size of the resulting Python unicode string and return
338
a string with the values formatted into it. The variable arguments must be C
339
types and must correspond exactly to the format characters in the <em>format</em>
340
string. The following format characters are allowed:</p>
341
<table border="1" class="docutils">
347
<thead valign="bottom">
348
<tr class="row-odd"><th class="head">Format Characters</th>
349
<th class="head">Type</th>
350
<th class="head">Comment</th>
354
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%%</span></code></td>
355
<td><em>n/a</em></td>
356
<td>The literal % character.</td>
358
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%c</span></code></td>
360
<td>A single character,
361
represented as an C int.</td>
363
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%d</span></code></td>
365
<td>Exactly equivalent to
366
<code class="docutils literal"><span class="pre">printf("%d")</span></code>.</td>
368
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%u</span></code></td>
369
<td>unsigned int</td>
370
<td>Exactly equivalent to
371
<code class="docutils literal"><span class="pre">printf("%u")</span></code>.</td>
373
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%ld</span></code></td>
375
<td>Exactly equivalent to
376
<code class="docutils literal"><span class="pre">printf("%ld")</span></code>.</td>
378
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%lu</span></code></td>
379
<td>unsigned long</td>
380
<td>Exactly equivalent to
381
<code class="docutils literal"><span class="pre">printf("%lu")</span></code>.</td>
383
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%zd</span></code></td>
385
<td>Exactly equivalent to
386
<code class="docutils literal"><span class="pre">printf("%zd")</span></code>.</td>
388
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%zu</span></code></td>
390
<td>Exactly equivalent to
391
<code class="docutils literal"><span class="pre">printf("%zu")</span></code>.</td>
393
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%i</span></code></td>
395
<td>Exactly equivalent to
396
<code class="docutils literal"><span class="pre">printf("%i")</span></code>.</td>
398
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%x</span></code></td>
400
<td>Exactly equivalent to
401
<code class="docutils literal"><span class="pre">printf("%x")</span></code>.</td>
403
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%s</span></code></td>
405
<td>A null-terminated C character
408
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%p</span></code></td>
410
<td>The hex representation of a C
411
pointer. Mostly equivalent to
412
<code class="docutils literal"><span class="pre">printf("%p")</span></code> except that
413
it is guaranteed to start with
414
the literal <code class="docutils literal"><span class="pre">0x</span></code> regardless
415
of what the platform’s
416
<code class="docutils literal"><span class="pre">printf</span></code> yields.</td>
418
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%U</span></code></td>
420
<td>A unicode object.</td>
422
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%V</span></code></td>
423
<td>PyObject*, char *</td>
424
<td>A unicode object (which may be
425
<em>NULL</em>) and a null-terminated
426
C character array as a second
427
parameter (which will be used,
428
if the first parameter is
431
<tr class="row-even"><td><code class="xref py py-attr docutils literal"><span class="pre">%S</span></code></td>
433
<td>The result of calling
434
<code class="xref py py-func docutils literal"><span class="pre">PyObject_Unicode()</span></code>.</td>
436
<tr class="row-odd"><td><code class="xref py py-attr docutils literal"><span class="pre">%R</span></code></td>
438
<td>The result of calling
439
<code class="xref py py-func docutils literal"><span class="pre">PyObject_Repr()</span></code>.</td>
443
<p>An unrecognized format character causes all the rest of the format string to be
444
copied as-is to the result string, and any extra arguments discarded.</p>
445
<div class="versionadded">
446
<p><span class="versionmodified">New in version 2.6.</span></p>
450
<dl class="function">
451
<dt id="c.PyUnicode_FromFormatV">
452
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_FromFormatV</code><span class="sig-paren">(</span>const char<em> *format</em>, va_list<em> vargs</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromFormatV" title="Permalink to this definition">¶</a></dt>
453
<dd><em class="refcount">Return value: New reference.</em><p>Identical to <code class="xref py py-func docutils literal"><span class="pre">PyUnicode_FromFormat()</span></code> except that it takes exactly two
455
<div class="versionadded">
456
<p><span class="versionmodified">New in version 2.6.</span></p>
460
<dl class="function">
461
<dt id="c.PyUnicode_AsUnicode">
462
<a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a>* <code class="descname">PyUnicode_AsUnicode</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsUnicode" title="Permalink to this definition">¶</a></dt>
463
<dd><p>Return a read-only pointer to the Unicode object’s internal
464
<a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer, <em>NULL</em> if <em>unicode</em> is not a Unicode object.
465
Note that the resulting <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE*</span></code></a> string may contain embedded
466
null characters, which would cause the string to be truncated when used in
467
most C functions.</p>
470
<dl class="function">
471
<dt id="c.PyUnicode_GetSize">
472
Py_ssize_t <code class="descname">PyUnicode_GetSize</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_GetSize" title="Permalink to this definition">¶</a></dt>
473
<dd><p>Return the length of the Unicode object.</p>
474
<div class="versionchanged">
475
<p><span class="versionmodified">Changed in version 2.5: </span>This function returned an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type. This might require changes
476
in your code for properly supporting 64-bit systems.</p>
480
<dl class="function">
481
<dt id="c.PyUnicode_FromEncodedObject">
482
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_FromEncodedObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *obj</em>, const char<em> *encoding</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromEncodedObject" title="Permalink to this definition">¶</a></dt>
483
<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
484
incremented refcount.</p>
485
<p>String and other char buffer compatible objects are decoded according to the
486
given encoding and using the error handling defined by errors. Both can be
487
<em>NULL</em> to have the interface use the default values (see the next section for
489
<p>All other objects, including Unicode objects, cause a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> to be
491
<p>The API returns <em>NULL</em> if there was an error. The caller is responsible for
492
decref’ing the returned objects.</p>
495
<dl class="function">
496
<dt id="c.PyUnicode_FromObject">
497
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_FromObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *obj</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromObject" title="Permalink to this definition">¶</a></dt>
498
<dd><em class="refcount">Return value: New reference.</em><p>Shortcut for <code class="docutils literal"><span class="pre">PyUnicode_FromEncodedObject(obj,</span> <span class="pre">NULL,</span> <span class="pre">"strict")</span></code> which is used
499
throughout the interpreter whenever coercion to Unicode is needed.</p>
502
<p>If the platform supports <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> and provides a header file wchar.h,
503
Python can interface directly to this type using the following functions.
504
Support is optimized if Python’s own <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> type is identical to
505
the system’s <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code>.</p>
507
<div class="section" id="wchar-t-support">
508
<h3>wchar_t Support<a class="headerlink" href="#wchar-t-support" title="Permalink to this headline">¶</a></h3>
509
<p><code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> support for platforms which support it:</p>
510
<dl class="function">
511
<dt id="c.PyUnicode_FromWideChar">
512
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_FromWideChar</code><span class="sig-paren">(</span>const wchar_t<em> *w</em>, Py_ssize_t<em> size</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_FromWideChar" title="Permalink to this definition">¶</a></dt>
513
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object from the <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> buffer <em>w</em> of the given <em>size</em>.
514
Return <em>NULL</em> on failure.</p>
515
<div class="versionchanged">
516
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
517
changes in your code for properly supporting 64-bit systems.</p>
521
<dl class="function">
522
<dt id="c.PyUnicode_AsWideChar">
523
Py_ssize_t <code class="descname">PyUnicode_AsWideChar</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyUnicodeObject" title="PyUnicodeObject">PyUnicodeObject</a><em> *unicode</em>, wchar_t<em> *w</em>, Py_ssize_t<em> size</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsWideChar" title="Permalink to this definition">¶</a></dt>
524
<dd><p>Copy the Unicode object contents into the <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> buffer <em>w</em>. At most
525
<em>size</em> <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> characters are copied (excluding a possibly trailing
526
0-termination character). Return the number of <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> characters
527
copied or -1 in case of an error. Note that the resulting <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code>
528
string may or may not be 0-terminated. It is the responsibility of the caller
529
to make sure that the <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code> string is 0-terminated in case this is
530
required by the application. Also, note that the <code class="xref c c-type docutils literal"><span class="pre">wchar_t*</span></code> string
531
might contain null characters, which would cause the string to be truncated
532
when used with most C functions.</p>
533
<div class="versionchanged">
534
<p><span class="versionmodified">Changed in version 2.5: </span>This function returned an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type and used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code>
535
type for <em>size</em>. This might require changes in your code for properly
536
supporting 64-bit systems.</p>
542
<div class="section" id="built-in-codecs">
543
<span id="builtincodecs"></span><h2>Built-in Codecs<a class="headerlink" href="#built-in-codecs" title="Permalink to this headline">¶</a></h2>
544
<p>Python provides a set of built-in codecs which are written in C for speed. All of
545
these codecs are directly usable via the following functions.</p>
546
<p>Many of the following APIs take two arguments encoding and errors, and they
547
have the same semantics as the ones of the built-in <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><code class="xref py py-func docutils literal"><span class="pre">unicode()</span></code></a> Unicode
548
object constructor.</p>
549
<p>Setting encoding to <em>NULL</em> causes the default encoding to be used which is
550
ASCII. The file system calls should use <code class="xref c c-data docutils literal"><span class="pre">Py_FileSystemDefaultEncoding</span></code>
551
as the encoding for file names. This variable should be treated as read-only: on
552
some systems, it will be a pointer to a static string, on others, it will change
553
at run-time (such as when the application invokes setlocale).</p>
554
<p>Error handling is set by errors which may also be set to <em>NULL</em> meaning to use
555
the default handling defined for the codec. Default error handling for all
556
built-in codecs is “strict” (<a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> is raised).</p>
557
<p>The codecs all use a similar interface. Only deviation from the following
558
generic ones are documented for simplicity.</p>
559
<div class="section" id="generic-codecs">
560
<h3>Generic Codecs<a class="headerlink" href="#generic-codecs" title="Permalink to this headline">¶</a></h3>
561
<p>These are the generic codec APIs:</p>
562
<dl class="function">
563
<dt id="c.PyUnicode_Decode">
564
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Decode</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *encoding</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Decode" title="Permalink to this definition">¶</a></dt>
565
<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>.
566
<em>encoding</em> and <em>errors</em> have the same meaning as the parameters of the same name
567
in the <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><code class="xref py py-func docutils literal"><span class="pre">unicode()</span></code></a> built-in function. The codec to be used is looked up
568
using the Python codec registry. Return <em>NULL</em> if an exception was raised by
570
<div class="versionchanged">
571
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
572
changes in your code for properly supporting 64-bit systems.</p>
576
<dl class="function">
577
<dt id="c.PyUnicode_Encode">
578
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Encode</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *encoding</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Encode" title="Permalink to this definition">¶</a></dt>
579
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer <em>s</em> of the given <em>size</em> and return a Python
580
string object. <em>encoding</em> and <em>errors</em> have the same meaning as the parameters
581
of the same name in the Unicode <code class="xref py py-meth docutils literal"><span class="pre">encode()</span></code> method. The codec
582
to be used is looked up using the Python codec registry. Return <em>NULL</em> if
583
an exception was raised by the codec.</p>
584
<div class="versionchanged">
585
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
586
changes in your code for properly supporting 64-bit systems.</p>
590
<dl class="function">
591
<dt id="c.PyUnicode_AsEncodedString">
592
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsEncodedString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em>, const char<em> *encoding</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsEncodedString" title="Permalink to this definition">¶</a></dt>
593
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object and return the result as Python string object.
594
<em>encoding</em> and <em>errors</em> have the same meaning as the parameters of the same name
595
in the Unicode <code class="xref py py-meth docutils literal"><span class="pre">encode()</span></code> method. The codec to be used is looked up using
596
the Python codec registry. Return <em>NULL</em> if an exception was raised by the
601
<div class="section" id="utf-8-codecs">
602
<h3>UTF-8 Codecs<a class="headerlink" href="#utf-8-codecs" title="Permalink to this headline">¶</a></h3>
603
<p>These are the UTF-8 codec APIs:</p>
604
<dl class="function">
605
<dt id="c.PyUnicode_DecodeUTF8">
606
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF8</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF8" title="Permalink to this definition">¶</a></dt>
607
<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
608
<em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
609
<div class="versionchanged">
610
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
611
changes in your code for properly supporting 64-bit systems.</p>
615
<dl class="function">
616
<dt id="c.PyUnicode_DecodeUTF8Stateful">
617
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF8Stateful</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, Py_ssize_t<em> *consumed</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF8Stateful" title="Permalink to this definition">¶</a></dt>
618
<dd><em class="refcount">Return value: New reference.</em><p>If <em>consumed</em> is <em>NULL</em>, behave like <a class="reference internal" href="#c.PyUnicode_DecodeUTF8" title="PyUnicode_DecodeUTF8"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeUTF8()</span></code></a>. If
619
<em>consumed</em> is not <em>NULL</em>, trailing incomplete UTF-8 byte sequences will not be
620
treated as an error. Those bytes will not be decoded and the number of bytes
621
that have been decoded will be stored in <em>consumed</em>.</p>
622
<div class="versionadded">
623
<p><span class="versionmodified">New in version 2.4.</span></p>
625
<div class="versionchanged">
626
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
627
changes in your code for properly supporting 64-bit systems.</p>
631
<dl class="function">
632
<dt id="c.PyUnicode_EncodeUTF8">
633
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeUTF8</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeUTF8" title="Permalink to this definition">¶</a></dt>
634
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer <em>s</em> of the given <em>size</em> using UTF-8 and return a
635
Python string object. Return <em>NULL</em> if an exception was raised by the codec.</p>
636
<div class="versionchanged">
637
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
638
changes in your code for properly supporting 64-bit systems.</p>
642
<dl class="function">
643
<dt id="c.PyUnicode_AsUTF8String">
644
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsUTF8String</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsUTF8String" title="Permalink to this definition">¶</a></dt>
645
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using UTF-8 and return the result as Python string
646
object. Error handling is “strict”. Return <em>NULL</em> if an exception was raised
651
<div class="section" id="utf-32-codecs">
652
<h3>UTF-32 Codecs<a class="headerlink" href="#utf-32-codecs" title="Permalink to this headline">¶</a></h3>
653
<p>These are the UTF-32 codec APIs:</p>
654
<dl class="function">
655
<dt id="c.PyUnicode_DecodeUTF32">
656
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF32</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF32" title="Permalink to this definition">¶</a></dt>
657
<dd><p>Decode <em>size</em> bytes from a UTF-32 encoded buffer string and return the
658
corresponding Unicode object. <em>errors</em> (if non-<em>NULL</em>) defines the error
659
handling. It defaults to “strict”.</p>
660
<p>If <em>byteorder</em> is non-<em>NULL</em>, the decoder starts decoding using the given byte
662
<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>
663
<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>
664
<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>
667
<p>If <code class="docutils literal"><span class="pre">*byteorder</span></code> is zero, and the first four bytes of the input data are a
668
byte order mark (BOM), the decoder switches to this byte order and the BOM is
669
not copied into the resulting Unicode string. If <code class="docutils literal"><span class="pre">*byteorder</span></code> is <code class="docutils literal"><span class="pre">-1</span></code> or
670
<code class="docutils literal"><span class="pre">1</span></code>, any byte order mark is copied to the output.</p>
671
<p>After completion, <em>*byteorder</em> is set to the current byte order at the end
673
<p>In a narrow build code points outside the BMP will be decoded as surrogate pairs.</p>
674
<p>If <em>byteorder</em> is <em>NULL</em>, the codec starts in native order mode.</p>
675
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
676
<div class="versionadded">
677
<p><span class="versionmodified">New in version 2.6.</span></p>
681
<dl class="function">
682
<dt id="c.PyUnicode_DecodeUTF32Stateful">
683
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF32Stateful</code><span class="sig-paren">(</span>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><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF32Stateful" title="Permalink to this definition">¶</a></dt>
684
<dd><p>If <em>consumed</em> is <em>NULL</em>, behave like <a class="reference internal" href="#c.PyUnicode_DecodeUTF32" title="PyUnicode_DecodeUTF32"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeUTF32()</span></code></a>. If
685
<em>consumed</em> is not <em>NULL</em>, <a class="reference internal" href="#c.PyUnicode_DecodeUTF32Stateful" title="PyUnicode_DecodeUTF32Stateful"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeUTF32Stateful()</span></code></a> will not treat
686
trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
687
by four) as an error. Those bytes will not be decoded and the number of bytes
688
that have been decoded will be stored in <em>consumed</em>.</p>
689
<div class="versionadded">
690
<p><span class="versionmodified">New in version 2.6.</span></p>
694
<dl class="function">
695
<dt id="c.PyUnicode_EncodeUTF32">
696
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeUTF32</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> byteorder</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeUTF32" title="Permalink to this definition">¶</a></dt>
697
<dd><p>Return a Python bytes object holding the UTF-32 encoded value of the Unicode
698
data in <em>s</em>. Output is written according to the following byte order:</p>
699
<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>
700
<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>
701
<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>
704
<p>If byteorder is <code class="docutils literal"><span class="pre">0</span></code>, the output string will always start with the Unicode BOM
705
mark (U+FEFF). In the other two modes, no BOM mark is prepended.</p>
706
<p>If <em>Py_UNICODE_WIDE</em> is not defined, surrogate pairs will be output
707
as a single code point.</p>
708
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
709
<div class="versionadded">
710
<p><span class="versionmodified">New in version 2.6.</span></p>
714
<dl class="function">
715
<dt id="c.PyUnicode_AsUTF32String">
716
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsUTF32String</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsUTF32String" title="Permalink to this definition">¶</a></dt>
717
<dd><p>Return a Python string using the UTF-32 encoding in native byte order. The
718
string always starts with a BOM mark. Error handling is “strict”. Return
719
<em>NULL</em> if an exception was raised by the codec.</p>
720
<div class="versionadded">
721
<p><span class="versionmodified">New in version 2.6.</span></p>
726
<div class="section" id="utf-16-codecs">
727
<h3>UTF-16 Codecs<a class="headerlink" href="#utf-16-codecs" title="Permalink to this headline">¶</a></h3>
728
<p>These are the UTF-16 codec APIs:</p>
729
<dl class="function">
730
<dt id="c.PyUnicode_DecodeUTF16">
731
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF16</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF16" title="Permalink to this definition">¶</a></dt>
732
<dd><em class="refcount">Return value: New reference.</em><p>Decode <em>size</em> bytes from a UTF-16 encoded buffer string and return the
733
corresponding Unicode object. <em>errors</em> (if non-<em>NULL</em>) defines the error
734
handling. It defaults to “strict”.</p>
735
<p>If <em>byteorder</em> is non-<em>NULL</em>, the decoder starts decoding using the given byte
737
<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>
738
<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>
739
<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>
742
<p>If <code class="docutils literal"><span class="pre">*byteorder</span></code> is zero, and the first two bytes of the input data are a
743
byte order mark (BOM), the decoder switches to this byte order and the BOM is
744
not copied into the resulting Unicode string. If <code class="docutils literal"><span class="pre">*byteorder</span></code> is <code class="docutils literal"><span class="pre">-1</span></code> or
745
<code class="docutils literal"><span class="pre">1</span></code>, any byte order mark is copied to the output (where it will result in
746
either a <code class="docutils literal"><span class="pre">\ufeff</span></code> or a <code class="docutils literal"><span class="pre">\ufffe</span></code> character).</p>
747
<p>After completion, <em>*byteorder</em> is set to the current byte order at the end
749
<p>If <em>byteorder</em> is <em>NULL</em>, the codec starts in native order mode.</p>
750
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
751
<div class="versionchanged">
752
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
753
changes in your code for properly supporting 64-bit systems.</p>
757
<dl class="function">
758
<dt id="c.PyUnicode_DecodeUTF16Stateful">
759
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF16Stateful</code><span class="sig-paren">(</span>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><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF16Stateful" title="Permalink to this definition">¶</a></dt>
760
<dd><em class="refcount">Return value: New reference.</em><p>If <em>consumed</em> is <em>NULL</em>, behave like <a class="reference internal" href="#c.PyUnicode_DecodeUTF16" title="PyUnicode_DecodeUTF16"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeUTF16()</span></code></a>. If
761
<em>consumed</em> is not <em>NULL</em>, <a class="reference internal" href="#c.PyUnicode_DecodeUTF16Stateful" title="PyUnicode_DecodeUTF16Stateful"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeUTF16Stateful()</span></code></a> will not treat
762
trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
763
split surrogate pair) as an error. Those bytes will not be decoded and the
764
number of bytes that have been decoded will be stored in <em>consumed</em>.</p>
765
<div class="versionadded">
766
<p><span class="versionmodified">New in version 2.4.</span></p>
768
<div class="versionchanged">
769
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em> and an <code class="xref c c-type docutils literal"><span class="pre">int</span> <span class="pre">*</span></code>
770
type for <em>consumed</em>. This might require changes in your code for
771
properly supporting 64-bit systems.</p>
775
<dl class="function">
776
<dt id="c.PyUnicode_EncodeUTF16">
777
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeUTF16</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> byteorder</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeUTF16" title="Permalink to this definition">¶</a></dt>
778
<dd><em class="refcount">Return value: New reference.</em><p>Return a Python string object holding the UTF-16 encoded value of the Unicode
779
data in <em>s</em>. Output is written according to the following byte order:</p>
780
<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>
781
<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>
782
<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>
785
<p>If byteorder is <code class="docutils literal"><span class="pre">0</span></code>, the output string will always start with the Unicode BOM
786
mark (U+FEFF). In the other two modes, no BOM mark is prepended.</p>
787
<p>If <em>Py_UNICODE_WIDE</em> is defined, a single <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> value may get
788
represented as a surrogate pair. If it is not defined, each <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a>
789
values is interpreted as an UCS-2 character.</p>
790
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
791
<div class="versionchanged">
792
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
793
changes in your code for properly supporting 64-bit systems.</p>
797
<dl class="function">
798
<dt id="c.PyUnicode_AsUTF16String">
799
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsUTF16String</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsUTF16String" title="Permalink to this definition">¶</a></dt>
800
<dd><em class="refcount">Return value: New reference.</em><p>Return a Python string using the UTF-16 encoding in native byte order. The
801
string always starts with a BOM mark. Error handling is “strict”. Return
802
<em>NULL</em> if an exception was raised by the codec.</p>
806
<div class="section" id="utf-7-codecs">
807
<h3>UTF-7 Codecs<a class="headerlink" href="#utf-7-codecs" title="Permalink to this headline">¶</a></h3>
808
<p>These are the UTF-7 codec APIs:</p>
809
<dl class="function">
810
<dt id="c.PyUnicode_DecodeUTF7">
811
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF7</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF7" title="Permalink to this definition">¶</a></dt>
812
<dd><p>Create a Unicode object by decoding <em>size</em> bytes of the UTF-7 encoded string
813
<em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
816
<dl class="function">
817
<dt id="c.PyUnicode_DecodeUTF7Stateful">
818
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUTF7Stateful</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, Py_ssize_t<em> *consumed</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUTF7Stateful" title="Permalink to this definition">¶</a></dt>
819
<dd><p>If <em>consumed</em> is <em>NULL</em>, behave like <a class="reference internal" href="#c.PyUnicode_DecodeUTF7" title="PyUnicode_DecodeUTF7"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeUTF7()</span></code></a>. If
820
<em>consumed</em> is not <em>NULL</em>, trailing incomplete UTF-7 base-64 sections will not
821
be treated as an error. Those bytes will not be decoded and the number of
822
bytes that have been decoded will be stored in <em>consumed</em>.</p>
825
<dl class="function">
826
<dt id="c.PyUnicode_EncodeUTF7">
827
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeUTF7</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, int<em> base64SetO</em>, int<em> base64WhiteSpace</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeUTF7" title="Permalink to this definition">¶</a></dt>
828
<dd><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given size using UTF-7 and
829
return a Python bytes object. Return <em>NULL</em> if an exception was raised by
831
<p>If <em>base64SetO</em> is nonzero, “Set O” (punctuation that has no otherwise
832
special meaning) will be encoded in base-64. If <em>base64WhiteSpace</em> is
833
nonzero, whitespace will be encoded in base-64. Both are set to zero for the
834
Python “utf-7” codec.</p>
838
<div class="section" id="unicode-escape-codecs">
839
<h3>Unicode-Escape Codecs<a class="headerlink" href="#unicode-escape-codecs" title="Permalink to this headline">¶</a></h3>
840
<p>These are the “Unicode Escape” codec APIs:</p>
841
<dl class="function">
842
<dt id="c.PyUnicode_DecodeUnicodeEscape">
843
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeUnicodeEscape</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeUnicodeEscape" title="Permalink to this definition">¶</a></dt>
844
<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
845
string <em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
846
<div class="versionchanged">
847
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
848
changes in your code for properly supporting 64-bit systems.</p>
852
<dl class="function">
853
<dt id="c.PyUnicode_EncodeUnicodeEscape">
854
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeUnicodeEscape</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeUnicodeEscape" title="Permalink to this definition">¶</a></dt>
855
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given <em>size</em> using Unicode-Escape and
856
return a Python string object. Return <em>NULL</em> if an exception was raised by the
858
<div class="versionchanged">
859
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
860
changes in your code for properly supporting 64-bit systems.</p>
864
<dl class="function">
865
<dt id="c.PyUnicode_AsUnicodeEscapeString">
866
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsUnicodeEscapeString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsUnicodeEscapeString" title="Permalink to this definition">¶</a></dt>
867
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Unicode-Escape and return the result as Python
868
string object. Error handling is “strict”. Return <em>NULL</em> if an exception was
869
raised by the codec.</p>
873
<div class="section" id="raw-unicode-escape-codecs">
874
<h3>Raw-Unicode-Escape Codecs<a class="headerlink" href="#raw-unicode-escape-codecs" title="Permalink to this headline">¶</a></h3>
875
<p>These are the “Raw Unicode Escape” codec APIs:</p>
876
<dl class="function">
877
<dt id="c.PyUnicode_DecodeRawUnicodeEscape">
878
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeRawUnicodeEscape</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeRawUnicodeEscape" title="Permalink to this definition">¶</a></dt>
879
<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
880
encoded string <em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
881
<div class="versionchanged">
882
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
883
changes in your code for properly supporting 64-bit systems.</p>
887
<dl class="function">
888
<dt id="c.PyUnicode_EncodeRawUnicodeEscape">
889
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeRawUnicodeEscape</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeRawUnicodeEscape" title="Permalink to this definition">¶</a></dt>
890
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given <em>size</em> using Raw-Unicode-Escape
891
and return a Python string object. Return <em>NULL</em> if an exception was raised by
893
<div class="versionchanged">
894
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
895
changes in your code for properly supporting 64-bit systems.</p>
899
<dl class="function">
900
<dt id="c.PyUnicode_AsRawUnicodeEscapeString">
901
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsRawUnicodeEscapeString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsRawUnicodeEscapeString" title="Permalink to this definition">¶</a></dt>
902
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Raw-Unicode-Escape and return the result as
903
Python string object. Error handling is “strict”. Return <em>NULL</em> if an exception
904
was raised by the codec.</p>
908
<div class="section" id="latin-1-codecs">
909
<h3>Latin-1 Codecs<a class="headerlink" href="#latin-1-codecs" title="Permalink to this headline">¶</a></h3>
910
<p>These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
911
ordinals and only these are accepted by the codecs during encoding.</p>
912
<dl class="function">
913
<dt id="c.PyUnicode_DecodeLatin1">
914
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeLatin1</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeLatin1" title="Permalink to this definition">¶</a></dt>
915
<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
916
<em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
917
<div class="versionchanged">
918
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
919
changes in your code for properly supporting 64-bit systems.</p>
923
<dl class="function">
924
<dt id="c.PyUnicode_EncodeLatin1">
925
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeLatin1</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeLatin1" title="Permalink to this definition">¶</a></dt>
926
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given <em>size</em> using Latin-1 and return
927
a Python string object. Return <em>NULL</em> if an exception was raised by the codec.</p>
928
<div class="versionchanged">
929
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
930
changes in your code for properly supporting 64-bit systems.</p>
934
<dl class="function">
935
<dt id="c.PyUnicode_AsLatin1String">
936
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsLatin1String</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsLatin1String" title="Permalink to this definition">¶</a></dt>
937
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Latin-1 and return the result as Python string
938
object. Error handling is “strict”. Return <em>NULL</em> if an exception was raised
943
<div class="section" id="ascii-codecs">
944
<h3>ASCII Codecs<a class="headerlink" href="#ascii-codecs" title="Permalink to this headline">¶</a></h3>
945
<p>These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other
946
codes generate errors.</p>
947
<dl class="function">
948
<dt id="c.PyUnicode_DecodeASCII">
949
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeASCII</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeASCII" title="Permalink to this definition">¶</a></dt>
950
<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
951
<em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
952
<div class="versionchanged">
953
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
954
changes in your code for properly supporting 64-bit systems.</p>
958
<dl class="function">
959
<dt id="c.PyUnicode_EncodeASCII">
960
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeASCII</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeASCII" title="Permalink to this definition">¶</a></dt>
961
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given <em>size</em> using ASCII and return a
962
Python string object. Return <em>NULL</em> if an exception was raised by the codec.</p>
963
<div class="versionchanged">
964
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
965
changes in your code for properly supporting 64-bit systems.</p>
969
<dl class="function">
970
<dt id="c.PyUnicode_AsASCIIString">
971
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsASCIIString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsASCIIString" title="Permalink to this definition">¶</a></dt>
972
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using ASCII and return the result as Python string
973
object. Error handling is “strict”. Return <em>NULL</em> if an exception was raised
978
<div class="section" id="character-map-codecs">
979
<h3>Character Map Codecs<a class="headerlink" href="#character-map-codecs" title="Permalink to this headline">¶</a></h3>
980
<p>This codec is special in that it can be used to implement many different codecs
981
(and this is in fact what was done to obtain most of the standard codecs
982
included in the <code class="xref py py-mod docutils literal"><span class="pre">encodings</span></code> package). The codec uses mapping to encode and
983
decode characters.</p>
984
<p>Decoding mappings must map single string characters to single Unicode
985
characters, integers (which are then interpreted as Unicode ordinals) or None
986
(meaning “undefined mapping” and causing an error).</p>
987
<p>Encoding mappings must map single Unicode characters to single string
988
characters, integers (which are then interpreted as Latin-1 ordinals) or None
989
(meaning “undefined mapping” and causing an error).</p>
990
<p>The mapping objects provided must only support the __getitem__ mapping
992
<p>If a character lookup fails with a LookupError, the character is copied as-is
993
meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
994
resp. Because of this, mappings only need to contain those mappings which map
995
characters to different code points.</p>
996
<p>These are the mapping codec APIs:</p>
997
<dl class="function">
998
<dt id="c.PyUnicode_DecodeCharmap">
999
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeCharmap</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *mapping</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeCharmap" title="Permalink to this definition">¶</a></dt>
1000
<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
1001
the given <em>mapping</em> object. Return <em>NULL</em> if an exception was raised by the
1002
codec. If <em>mapping</em> is <em>NULL</em> latin-1 decoding will be done. Else it can be a
1003
dictionary mapping byte or a unicode string, which is treated as a lookup table.
1004
Byte values greater that the length of the string and U+FFFE “characters” are
1005
treated as “undefined mapping”.</p>
1006
<div class="versionchanged">
1007
<p><span class="versionmodified">Changed in version 2.4: </span>Allowed unicode string as mapping argument.</p>
1009
<div class="versionchanged">
1010
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
1011
changes in your code for properly supporting 64-bit systems.</p>
1015
<dl class="function">
1016
<dt id="c.PyUnicode_EncodeCharmap">
1017
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeCharmap</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *mapping</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeCharmap" title="Permalink to this definition">¶</a></dt>
1018
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given <em>size</em> using the given
1019
<em>mapping</em> object and return a Python string object. Return <em>NULL</em> if an
1020
exception was raised by the codec.</p>
1021
<div class="versionchanged">
1022
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
1023
changes in your code for properly supporting 64-bit systems.</p>
1027
<dl class="function">
1028
<dt id="c.PyUnicode_AsCharmapString">
1029
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsCharmapString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *mapping</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsCharmapString" title="Permalink to this definition">¶</a></dt>
1030
<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
1031
as Python string object. Error handling is “strict”. Return <em>NULL</em> if an
1032
exception was raised by the codec.</p>
1035
<p>The following codec API is special in that maps Unicode to Unicode.</p>
1036
<dl class="function">
1037
<dt id="c.PyUnicode_TranslateCharmap">
1038
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_TranslateCharmap</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *table</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_TranslateCharmap" title="Permalink to this definition">¶</a></dt>
1039
<dd><em class="refcount">Return value: New reference.</em><p>Translate a <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given <em>size</em> by applying a
1040
character mapping <em>table</em> to it and return the resulting Unicode object. Return
1041
<em>NULL</em> when an exception was raised by the codec.</p>
1042
<p>The <em>mapping</em> table must map Unicode ordinal integers to Unicode ordinal
1043
integers or None (causing deletion of the character).</p>
1044
<p>Mapping tables need only provide the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a> interface; dictionaries
1045
and sequences work well. Unmapped character ordinals (ones which cause a
1046
<a class="reference internal" href="../library/exceptions.html#exceptions.LookupError" title="exceptions.LookupError"><code class="xref py py-exc docutils literal"><span class="pre">LookupError</span></code></a>) are left untouched and are copied as-is.</p>
1047
<div class="versionchanged">
1048
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
1049
changes in your code for properly supporting 64-bit systems.</p>
1054
<div class="section" id="mbcs-codecs-for-windows">
1055
<h3>MBCS codecs for Windows<a class="headerlink" href="#mbcs-codecs-for-windows" title="Permalink to this headline">¶</a></h3>
1056
<p>These are the MBCS codec APIs. They are currently only available on Windows and
1057
use the Win32 MBCS converters to implement the conversions. Note that MBCS (or
1058
DBCS) is a class of encodings, not just one. The target encoding is defined by
1059
the user settings on the machine running the codec.</p>
1060
<dl class="function">
1061
<dt id="c.PyUnicode_DecodeMBCS">
1062
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeMBCS</code><span class="sig-paren">(</span>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeMBCS" title="Permalink to this definition">¶</a></dt>
1063
<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>.
1064
Return <em>NULL</em> if an exception was raised by the codec.</p>
1065
<div class="versionchanged">
1066
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
1067
changes in your code for properly supporting 64-bit systems.</p>
1071
<dl class="function">
1072
<dt id="c.PyUnicode_DecodeMBCSStateful">
1073
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_DecodeMBCSStateful</code><span class="sig-paren">(</span>const char<em> *s</em>, int<em> size</em>, const char<em> *errors</em>, int<em> *consumed</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_DecodeMBCSStateful" title="Permalink to this definition">¶</a></dt>
1074
<dd><p>If <em>consumed</em> is <em>NULL</em>, behave like <a class="reference internal" href="#c.PyUnicode_DecodeMBCS" title="PyUnicode_DecodeMBCS"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeMBCS()</span></code></a>. If
1075
<em>consumed</em> is not <em>NULL</em>, <a class="reference internal" href="#c.PyUnicode_DecodeMBCSStateful" title="PyUnicode_DecodeMBCSStateful"><code class="xref c c-func docutils literal"><span class="pre">PyUnicode_DecodeMBCSStateful()</span></code></a> will not decode
1076
trailing lead byte and the number of bytes that have been decoded will be stored
1077
in <em>consumed</em>.</p>
1078
<div class="versionadded">
1079
<p><span class="versionmodified">New in version 2.5.</span></p>
1083
<dl class="function">
1084
<dt id="c.PyUnicode_EncodeMBCS">
1085
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_EncodeMBCS</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_EncodeMBCS" title="Permalink to this definition">¶</a></dt>
1086
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a class="reference internal" href="#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal"><span class="pre">Py_UNICODE</span></code></a> buffer of the given <em>size</em> using MBCS and return a
1087
Python string object. Return <em>NULL</em> if an exception was raised by the codec.</p>
1088
<div class="versionchanged">
1089
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>size</em>. This might require
1090
changes in your code for properly supporting 64-bit systems.</p>
1094
<dl class="function">
1095
<dt id="c.PyUnicode_AsMBCSString">
1096
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_AsMBCSString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *unicode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_AsMBCSString" title="Permalink to this definition">¶</a></dt>
1097
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using MBCS and return the result as Python string
1098
object. Error handling is “strict”. Return <em>NULL</em> if an exception was raised
1103
<div class="section" id="methods-slots">
1104
<h3>Methods & Slots<a class="headerlink" href="#methods-slots" title="Permalink to this headline">¶</a></h3>
1107
<div class="section" id="methods-and-slot-functions">
1108
<span id="unicodemethodsandslots"></span><h2>Methods and Slot Functions<a class="headerlink" href="#methods-and-slot-functions" title="Permalink to this headline">¶</a></h2>
1109
<p>The following APIs are capable of handling Unicode objects and strings on input
1110
(we refer to them as strings in the descriptions) and return Unicode objects or
1111
integers as appropriate.</p>
1112
<p>They all return <em>NULL</em> or <code class="docutils literal"><span class="pre">-1</span></code> if an exception occurs.</p>
1113
<dl class="function">
1114
<dt id="c.PyUnicode_Concat">
1115
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Concat</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *left</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *right</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Concat" title="Permalink to this definition">¶</a></dt>
1116
<dd><em class="refcount">Return value: New reference.</em><p>Concat two strings giving a new Unicode string.</p>
1119
<dl class="function">
1120
<dt id="c.PyUnicode_Split">
1121
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Split</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *s</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *sep</em>, Py_ssize_t<em> maxsplit</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Split" title="Permalink to this definition">¶</a></dt>
1122
<dd><em class="refcount">Return value: New reference.</em><p>Split a string giving a list of Unicode strings. If <em>sep</em> is <em>NULL</em>, splitting
1123
will be done at all whitespace substrings. Otherwise, splits occur at the given
1124
separator. At most <em>maxsplit</em> splits will be done. If negative, no limit is
1125
set. Separators are not included in the resulting list.</p>
1126
<div class="versionchanged">
1127
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>maxsplit</em>. This might require
1128
changes in your code for properly supporting 64-bit systems.</p>
1132
<dl class="function">
1133
<dt id="c.PyUnicode_Splitlines">
1134
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Splitlines</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *s</em>, int<em> keepend</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Splitlines" title="Permalink to this definition">¶</a></dt>
1135
<dd><em class="refcount">Return value: New reference.</em><p>Split a Unicode string at line breaks, returning a list of Unicode strings.
1136
CRLF is considered to be one line break. If <em>keepend</em> is 0, the Line break
1137
characters are not included in the resulting strings.</p>
1140
<dl class="function">
1141
<dt id="c.PyUnicode_Translate">
1142
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Translate</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *str</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *table</em>, const char<em> *errors</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Translate" title="Permalink to this definition">¶</a></dt>
1143
<dd><em class="refcount">Return value: New reference.</em><p>Translate a string by applying a character mapping table to it and return the
1144
resulting Unicode object.</p>
1145
<p>The mapping table must map Unicode ordinal integers to Unicode ordinal integers
1146
or None (causing deletion of the character).</p>
1147
<p>Mapping tables need only provide the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a> interface; dictionaries
1148
and sequences work well. Unmapped character ordinals (ones which cause a
1149
<a class="reference internal" href="../library/exceptions.html#exceptions.LookupError" title="exceptions.LookupError"><code class="xref py py-exc docutils literal"><span class="pre">LookupError</span></code></a>) are left untouched and are copied as-is.</p>
1150
<p><em>errors</em> has the usual meaning for codecs. It may be <em>NULL</em> which indicates to
1151
use the default error handling.</p>
1154
<dl class="function">
1155
<dt id="c.PyUnicode_Join">
1156
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Join</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *separator</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *seq</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Join" title="Permalink to this definition">¶</a></dt>
1157
<dd><em class="refcount">Return value: New reference.</em><p>Join a sequence of strings using the given <em>separator</em> and return the resulting
1161
<dl class="function">
1162
<dt id="c.PyUnicode_Tailmatch">
1163
Py_ssize_t <code class="descname">PyUnicode_Tailmatch</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *str</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em>, int<em> direction</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Tailmatch" title="Permalink to this definition">¶</a></dt>
1164
<dd><p>Return 1 if <em>substr</em> matches <code class="docutils literal"><span class="pre">str[start:end]</span></code> at the given tail end
1165
(<em>direction</em> == -1 means to do a prefix match, <em>direction</em> == 1 a suffix match),
1166
0 otherwise. Return <code class="docutils literal"><span class="pre">-1</span></code> if an error occurred.</p>
1167
<div class="versionchanged">
1168
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>start</em> and <em>end</em>. This
1169
might require changes in your code for properly supporting 64-bit
1174
<dl class="function">
1175
<dt id="c.PyUnicode_Find">
1176
Py_ssize_t <code class="descname">PyUnicode_Find</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *str</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em>, int<em> direction</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Find" title="Permalink to this definition">¶</a></dt>
1177
<dd><p>Return the first position of <em>substr</em> in <code class="docutils literal"><span class="pre">str[start:end]</span></code> using the given
1178
<em>direction</em> (<em>direction</em> == 1 means to do a forward search, <em>direction</em> == -1 a
1179
backward search). The return value is the index of the first match; a value of
1180
<code class="docutils literal"><span class="pre">-1</span></code> indicates that no match was found, and <code class="docutils literal"><span class="pre">-2</span></code> indicates that an error
1181
occurred and an exception has been set.</p>
1182
<div class="versionchanged">
1183
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>start</em> and <em>end</em>. This
1184
might require changes in your code for properly supporting 64-bit
1189
<dl class="function">
1190
<dt id="c.PyUnicode_Count">
1191
Py_ssize_t <code class="descname">PyUnicode_Count</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *str</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Count" title="Permalink to this definition">¶</a></dt>
1192
<dd><p>Return the number of non-overlapping occurrences of <em>substr</em> in
1193
<code class="docutils literal"><span class="pre">str[start:end]</span></code>. Return <code class="docutils literal"><span class="pre">-1</span></code> if an error occurred.</p>
1194
<div class="versionchanged">
1195
<p><span class="versionmodified">Changed in version 2.5: </span>This function returned an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type and used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code>
1196
type for <em>start</em> and <em>end</em>. This might require changes in your code for
1197
properly supporting 64-bit systems.</p>
1201
<dl class="function">
1202
<dt id="c.PyUnicode_Replace">
1203
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Replace</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *str</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *substr</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *replstr</em>, Py_ssize_t<em> maxcount</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Replace" title="Permalink to this definition">¶</a></dt>
1204
<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
1205
return the resulting Unicode object. <em>maxcount</em> == -1 means replace all
1207
<div class="versionchanged">
1208
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type for <em>maxcount</em>. This might
1209
require changes in your code for properly supporting 64-bit systems.</p>
1213
<dl class="function">
1214
<dt id="c.PyUnicode_Compare">
1215
int <code class="descname">PyUnicode_Compare</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *left</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *right</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Compare" title="Permalink to this definition">¶</a></dt>
1216
<dd><p>Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
1220
<dl class="function">
1221
<dt id="c.PyUnicode_RichCompare">
1222
int <code class="descname">PyUnicode_RichCompare</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *left</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *right</em>, int<em> op</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_RichCompare" title="Permalink to this definition">¶</a></dt>
1223
<dd><p>Rich compare two unicode strings and return one of the following:</p>
1225
<li><code class="docutils literal"><span class="pre">NULL</span></code> in case an exception was raised</li>
1226
<li><code class="xref py py-const docutils literal"><span class="pre">Py_True</span></code> or <code class="xref py py-const docutils literal"><span class="pre">Py_False</span></code> for successful comparisons</li>
1227
<li><code class="xref py py-const docutils literal"><span class="pre">Py_NotImplemented</span></code> in case the type combination is unknown</li>
1229
<p>Note that <code class="xref py py-const docutils literal"><span class="pre">Py_EQ</span></code> and <code class="xref py py-const docutils literal"><span class="pre">Py_NE</span></code> comparisons can cause a
1230
<a class="reference internal" href="../library/exceptions.html#exceptions.UnicodeWarning" title="exceptions.UnicodeWarning"><code class="xref py py-exc docutils literal"><span class="pre">UnicodeWarning</span></code></a> in case the conversion of the arguments to Unicode fails
1231
with a <a class="reference internal" href="../library/exceptions.html#exceptions.UnicodeDecodeError" title="exceptions.UnicodeDecodeError"><code class="xref py py-exc docutils literal"><span class="pre">UnicodeDecodeError</span></code></a>.</p>
1232
<p>Possible values for <em>op</em> are <code class="xref py py-const docutils literal"><span class="pre">Py_GT</span></code>, <code class="xref py py-const docutils literal"><span class="pre">Py_GE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">Py_EQ</span></code>,
1233
<code class="xref py py-const docutils literal"><span class="pre">Py_NE</span></code>, <code class="xref py py-const docutils literal"><span class="pre">Py_LT</span></code>, and <code class="xref py py-const docutils literal"><span class="pre">Py_LE</span></code>.</p>
1236
<dl class="function">
1237
<dt id="c.PyUnicode_Format">
1238
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyUnicode_Format</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *format</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *args</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Format" title="Permalink to this definition">¶</a></dt>
1239
<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
1240
<code class="docutils literal"><span class="pre">format</span> <span class="pre">%</span> <span class="pre">args</span></code>.</p>
1243
<dl class="function">
1244
<dt id="c.PyUnicode_Contains">
1245
int <code class="descname">PyUnicode_Contains</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *container</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *element</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnicode_Contains" title="Permalink to this definition">¶</a></dt>
1246
<dd><p>Check whether <em>element</em> is contained in <em>container</em> and return true or false
1248
<p><em>element</em> has to coerce to a one element Unicode string. <code class="docutils literal"><span class="pre">-1</span></code> is returned if
1249
there was an error.</p>
1259
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
1260
<div class="sphinxsidebarwrapper">
1261
<h3><a href="../contents.html">Table Of Contents</a></h3>
1263
<li><a class="reference internal" href="#">Unicode Objects and Codecs</a><ul>
1264
<li><a class="reference internal" href="#unicode-objects">Unicode Objects</a><ul>
1265
<li><a class="reference internal" href="#unicode-type">Unicode Type</a></li>
1266
<li><a class="reference internal" href="#unicode-character-properties">Unicode Character Properties</a></li>
1267
<li><a class="reference internal" href="#plain-py-unicode">Plain Py_UNICODE</a></li>
1268
<li><a class="reference internal" href="#wchar-t-support">wchar_t Support</a></li>
1271
<li><a class="reference internal" href="#built-in-codecs">Built-in Codecs</a><ul>
1272
<li><a class="reference internal" href="#generic-codecs">Generic Codecs</a></li>
1273
<li><a class="reference internal" href="#utf-8-codecs">UTF-8 Codecs</a></li>
1274
<li><a class="reference internal" href="#utf-32-codecs">UTF-32 Codecs</a></li>
1275
<li><a class="reference internal" href="#utf-16-codecs">UTF-16 Codecs</a></li>
1276
<li><a class="reference internal" href="#utf-7-codecs">UTF-7 Codecs</a></li>
1277
<li><a class="reference internal" href="#unicode-escape-codecs">Unicode-Escape Codecs</a></li>
1278
<li><a class="reference internal" href="#raw-unicode-escape-codecs">Raw-Unicode-Escape Codecs</a></li>
1279
<li><a class="reference internal" href="#latin-1-codecs">Latin-1 Codecs</a></li>
1280
<li><a class="reference internal" href="#ascii-codecs">ASCII Codecs</a></li>
1281
<li><a class="reference internal" href="#character-map-codecs">Character Map Codecs</a></li>
1282
<li><a class="reference internal" href="#mbcs-codecs-for-windows">MBCS codecs for Windows</a></li>
1283
<li><a class="reference internal" href="#methods-slots">Methods & Slots</a></li>
1286
<li><a class="reference internal" href="#methods-and-slot-functions">Methods and Slot Functions</a></li>
1291
<h4>Previous topic</h4>
1292
<p class="topless"><a href="string.html"
1293
title="previous chapter">String/Bytes Objects</a></p>
1295
<p class="topless"><a href="buffer.html"
1296
title="next chapter">Buffers and Memoryview Objects</a></p>
1298
<ul class="this-page-menu">
1299
<li><a href="../bugs.html">Report a Bug</a></li>
1300
<li><a href="../_sources/c-api/unicode.txt"
1301
rel="nofollow">Show Source</a></li>
1304
<div id="searchbox" style="display: none" role="search">
1305
<h3>Quick search</h3>
1306
<form class="search" action="../search.html" method="get">
1307
<input type="text" name="q" />
1308
<input type="submit" value="Go" />
1309
<input type="hidden" name="check_keywords" value="yes" />
1310
<input type="hidden" name="area" value="default" />
1312
<p class="searchtip" style="font-size: 90%">
1313
Enter search terms or a module, class or function name.
1316
<script type="text/javascript">$('#searchbox').show(0);</script>
1319
<div class="clearer"></div>
1321
<div class="related" role="navigation" aria-label="related navigation">
1324
<li class="right" style="margin-right: 10px">
1325
<a href="../genindex.html" title="General Index"
1328
<a href="../py-modindex.html" title="Python Module Index"
1331
<a href="buffer.html" title="Buffers and Memoryview Objects"
1334
<a href="string.html" title="String/Bytes Objects"
1335
>previous</a> |</li>
1336
<li><img src="../_static/py.png" alt=""
1337
style="vertical-align: middle; margin-top: -1px"/></li>
1338
<li><a href="https://www.python.org/">Python</a> »</li>
1340
<span class="version_switcher_placeholder">2.7.11</span>
1341
<a href="../index.html">Documentation</a> »
1344
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> »</li>
1345
<li class="nav-item nav-item-2"><a href="concrete.html" >Concrete Objects Layer</a> »</li>
1348
<div class="footer">
1349
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
1351
The Python Software Foundation is a non-profit corporation.
1352
<a href="https://www.python.org/psf/donations/">Please donate.</a>
1354
Last updated on Jan 23, 2016.
1355
<a href="../bugs.html">Found a bug</a>?
1357
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'