[ SEA-GHOST MINI SHELL]

Path : /proc/2/root/proc/2/cwd/usr/share/doc/python-docs-2.7.5/html/howto/
FILE UPLOADER :
Current File : //proc/2/root/proc/2/cwd/usr/share/doc/python-docs-2.7.5/html/howto/cporting.html


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


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Porting Extension Modules to Python 3 &mdash; Python 2.7.5 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.5',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 2.7.5 documentation" href="../index.html" />
    <link rel="up" title="Python HOWTOs" href="index.html" />
    <link rel="next" title="Curses Programming with Python" href="curses.html" />
    <link rel="prev" title="Porting Python 2 Code to Python 3" href="pyporting.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="curses.html" title="Curses Programming with Python"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pyporting.html" title="Porting Python 2 Code to Python 3"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 documentation</a> &raquo;
        </li>

          <li><a href="index.html" accesskey="U">Python HOWTOs</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="porting-extension-modules-to-python-3">
<span id="cporting-howto"></span><h1>Porting Extension Modules to Python 3<a class="headerlink" href="#porting-extension-modules-to-python-3" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">author:</th><td class="field-body">Benjamin Peterson</td>
</tr>
</tbody>
</table>
<div class="topic">
<p class="topic-title first">Abstract</p>
<p>Although changing the C-API was not one of Python 3&#8217;s objectives,
the many Python-level changes made leaving Python 2&#8217;s API intact
impossible.  In fact, some changes such as <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> and
<a class="reference internal" href="../library/functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a> unification are more obvious on the C level.  This
document endeavors to document incompatibilities and how they can
be worked around.</p>
</div>
<div class="section" id="conditional-compilation">
<h2>Conditional compilation<a class="headerlink" href="#conditional-compilation" title="Permalink to this headline">¶</a></h2>
<p>The easiest way to compile only some code for Python 3 is to check
if <tt class="xref c c-macro docutils literal"><span class="pre">PY_MAJOR_VERSION</span></tt> is greater than or equal to 3.</p>
<div class="highlight-c"><div class="highlight"><pre><span class="cp">#if PY_MAJOR_VERSION &gt;= 3</span>
<span class="cp">#define IS_PY3K</span>
<span class="cp">#endif</span>
</pre></div>
</div>
<p>API functions that are not present can be aliased to their equivalents within
conditional blocks.</p>
</div>
<div class="section" id="changes-to-object-apis">
<h2>Changes to Object APIs<a class="headerlink" href="#changes-to-object-apis" title="Permalink to this headline">¶</a></h2>
<p>Python 3 merged together some types with similar functions while cleanly
separating others.</p>
<div class="section" id="str-unicode-unification">
<h3>str/unicode Unification<a class="headerlink" href="#str-unicode-unification" title="Permalink to this headline">¶</a></h3>
<p>Python 3&#8217;s <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> (<tt class="docutils literal"><span class="pre">PyString_*</span></tt> functions in C) type is equivalent to
Python 2&#8217;s <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><tt class="xref py py-func docutils literal"><span class="pre">unicode()</span></tt></a> (<tt class="docutils literal"><span class="pre">PyUnicode_*</span></tt>).  The old 8-bit string type has
become <tt class="xref py py-func docutils literal"><span class="pre">bytes()</span></tt>.  Python 2.6 and later provide a compatibility header,
<tt class="file docutils literal"><span class="pre">bytesobject.h</span></tt>, mapping <tt class="docutils literal"><span class="pre">PyBytes</span></tt> names to <tt class="docutils literal"><span class="pre">PyString</span></tt> ones.  For best
compatibility with Python 3, <tt class="xref c c-type docutils literal"><span class="pre">PyUnicode</span></tt> should be used for textual data and
<tt class="xref c c-type docutils literal"><span class="pre">PyBytes</span></tt> for binary data.  It&#8217;s also important to remember that
<tt class="xref c c-type docutils literal"><span class="pre">PyBytes</span></tt> and <tt class="xref c c-type docutils literal"><span class="pre">PyUnicode</span></tt> in Python 3 are not interchangeable like
<tt class="xref c c-type docutils literal"><span class="pre">PyString</span></tt> and <tt class="xref c c-type docutils literal"><span class="pre">PyUnicode</span></tt> are in Python 2.  The following example
shows best practices with regards to <tt class="xref c c-type docutils literal"><span class="pre">PyUnicode</span></tt>, <tt class="xref c c-type docutils literal"><span class="pre">PyString</span></tt>,
and <tt class="xref c c-type docutils literal"><span class="pre">PyBytes</span></tt>.</p>
<div class="highlight-c"><div class="highlight"><pre><span class="cp">#include &quot;stdlib.h&quot;</span>
<span class="cp">#include &quot;Python.h&quot;</span>
<span class="cp">#include &quot;bytesobject.h&quot;</span>

<span class="cm">/* text example */</span>
<span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span>
<span class="nf">say_hello</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">result</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">PyArg_ParseTuple</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s">&quot;U:say_hello&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">name</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>

    <span class="n">result</span> <span class="o">=</span> <span class="n">PyUnicode_FromFormat</span><span class="p">(</span><span class="s">&quot;Hello, %S!&quot;</span><span class="p">,</span> <span class="n">name</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/* just a forward */</span>
<span class="k">static</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">do_encode</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">);</span>

<span class="cm">/* bytes example */</span>
<span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span>
<span class="nf">encode_object</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">char</span> <span class="o">*</span><span class="n">encoded</span><span class="p">;</span>
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">result</span><span class="p">,</span> <span class="o">*</span><span class="n">myobj</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">PyArg_ParseTuple</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s">&quot;O:encode_object&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">myobj</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>

    <span class="n">encoded</span> <span class="o">=</span> <span class="n">do_encode</span><span class="p">(</span><span class="n">myobj</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">encoded</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">result</span> <span class="o">=</span> <span class="n">PyBytes_FromString</span><span class="p">(</span><span class="n">encoded</span><span class="p">);</span>
    <span class="n">free</span><span class="p">(</span><span class="n">encoded</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="long-int-unification">
<h3>long/int Unification<a class="headerlink" href="#long-int-unification" title="Permalink to this headline">¶</a></h3>
<p>Python 3 has only one integer type, <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a>.  But it actually
corresponds to Python 2&#8217;s <a class="reference internal" href="../library/functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a> type&#8211;the <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> type
used in Python 2 was removed.  In the C-API, <tt class="docutils literal"><span class="pre">PyInt_*</span></tt> functions
are replaced by their <tt class="docutils literal"><span class="pre">PyLong_*</span></tt> equivalents.</p>
</div>
</div>
<div class="section" id="module-initialization-and-state">
<h2>Module initialization and state<a class="headerlink" href="#module-initialization-and-state" title="Permalink to this headline">¶</a></h2>
<p>Python 3 has a revamped extension module initialization system.  (See
<span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3121"><strong>PEP 3121</strong></a>.)  Instead of storing module state in globals, they should
be stored in an interpreter specific structure.  Creating modules that
act correctly in both Python 2 and Python 3 is tricky.  The following
simple example demonstrates how.</p>
<div class="highlight-c"><div class="highlight"><pre><span class="cp">#include &quot;Python.h&quot;</span>

<span class="k">struct</span> <span class="n">module_state</span> <span class="p">{</span>
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">error</span><span class="p">;</span>
<span class="p">};</span>

<span class="cp">#if PY_MAJOR_VERSION &gt;= 3</span>
<span class="cp">#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))</span>
<span class="cp">#else</span>
<span class="cp">#define GETSTATE(m) (&amp;_state)</span>
<span class="k">static</span> <span class="k">struct</span> <span class="n">module_state</span> <span class="n">_state</span><span class="p">;</span>
<span class="cp">#endif</span>

<span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span>
<span class="nf">error_out</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">m</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">struct</span> <span class="n">module_state</span> <span class="o">*</span><span class="n">st</span> <span class="o">=</span> <span class="n">GETSTATE</span><span class="p">(</span><span class="n">m</span><span class="p">);</span>
    <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">st</span><span class="o">-&gt;</span><span class="n">error</span><span class="p">,</span> <span class="s">&quot;something bad happened&quot;</span><span class="p">);</span>
    <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">myextension_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
    <span class="p">{</span><span class="s">&quot;error_out&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">error_out</span><span class="p">,</span> <span class="n">METH_NOARGS</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">},</span>
    <span class="p">{</span><span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">}</span>
<span class="p">};</span>

<span class="cp">#if PY_MAJOR_VERSION &gt;= 3</span>

<span class="k">static</span> <span class="kt">int</span> <span class="nf">myextension_traverse</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">m</span><span class="p">,</span> <span class="n">visitproc</span> <span class="n">visit</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">arg</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">Py_VISIT</span><span class="p">(</span><span class="n">GETSTATE</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">error</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">static</span> <span class="kt">int</span> <span class="nf">myextension_clear</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">m</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">Py_CLEAR</span><span class="p">(</span><span class="n">GETSTATE</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">error</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>


<span class="k">static</span> <span class="k">struct</span> <span class="n">PyModuleDef</span> <span class="n">moduledef</span> <span class="o">=</span> <span class="p">{</span>
        <span class="n">PyModuleDef_HEAD_INIT</span><span class="p">,</span>
        <span class="s">&quot;myextension&quot;</span><span class="p">,</span>
        <span class="nb">NULL</span><span class="p">,</span>
        <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">module_state</span><span class="p">),</span>
        <span class="n">myextension_methods</span><span class="p">,</span>
        <span class="nb">NULL</span><span class="p">,</span>
        <span class="n">myextension_traverse</span><span class="p">,</span>
        <span class="n">myextension_clear</span><span class="p">,</span>
        <span class="nb">NULL</span>
<span class="p">};</span>

<span class="cp">#define INITERROR return NULL</span>

<span class="n">PyObject</span> <span class="o">*</span>
<span class="n">PyInit_myextension</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>

<span class="cp">#else</span>
<span class="cp">#define INITERROR return</span>

<span class="kt">void</span>
<span class="n">initmyextension</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="cp">#endif</span>
<span class="p">{</span>
<span class="cp">#if PY_MAJOR_VERSION &gt;= 3</span>
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">module</span> <span class="o">=</span> <span class="n">PyModule_Create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">moduledef</span><span class="p">);</span>
<span class="cp">#else</span>
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">module</span> <span class="o">=</span> <span class="n">Py_InitModule</span><span class="p">(</span><span class="s">&quot;myextension&quot;</span><span class="p">,</span> <span class="n">myextension_methods</span><span class="p">);</span>
<span class="cp">#endif</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">module</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
        <span class="n">INITERROR</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">module_state</span> <span class="o">*</span><span class="n">st</span> <span class="o">=</span> <span class="n">GETSTATE</span><span class="p">(</span><span class="n">module</span><span class="p">);</span>

    <span class="n">st</span><span class="o">-&gt;</span><span class="n">error</span> <span class="o">=</span> <span class="n">PyErr_NewException</span><span class="p">(</span><span class="s">&quot;myextension.Error&quot;</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">st</span><span class="o">-&gt;</span><span class="n">error</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">module</span><span class="p">);</span>
        <span class="n">INITERROR</span><span class="p">;</span>
    <span class="p">}</span>

<span class="cp">#if PY_MAJOR_VERSION &gt;= 3</span>
    <span class="k">return</span> <span class="n">module</span><span class="p">;</span>
<span class="cp">#endif</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="cobject-replaced-with-capsule">
<h2>CObject replaced with Capsule<a class="headerlink" href="#cobject-replaced-with-capsule" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="xref c c-type docutils literal"><span class="pre">Capsule</span></tt> object was introduced in Python 3.1 and 2.7 to replace
<tt class="xref c c-type docutils literal"><span class="pre">CObject</span></tt>.  CObjects were useful,
but the <tt class="xref c c-type docutils literal"><span class="pre">CObject</span></tt> API was problematic: it didn&#8217;t permit distinguishing
between valid CObjects, which allowed mismatched CObjects to crash the
interpreter, and some of its APIs relied on undefined behavior in C.
(For further reading on the rationale behind Capsules, please see <a class="reference external" href="http://bugs.python.org/issue5630">issue 5630</a>.)</p>
<p>If you&#8217;re currently using CObjects, and you want to migrate to 3.1 or newer,
you&#8217;ll need to switch to Capsules.
<tt class="xref c c-type docutils literal"><span class="pre">CObject</span></tt> was deprecated in 3.1 and 2.7 and completely removed in
Python 3.2.  If you only support 2.7, or 3.1 and above, you
can simply switch to <tt class="xref c c-type docutils literal"><span class="pre">Capsule</span></tt>.  If you need to support Python 3.0,
or versions of Python earlier than 2.7,
you&#8217;ll have to support both CObjects and Capsules.
(Note that Python 3.0 is no longer supported, and it is not recommended
for production use.)</p>
<p>The following example header file <tt class="file docutils literal"><span class="pre">capsulethunk.h</span></tt> may
solve the problem for you.  Simply write your code against the
<tt class="xref c c-type docutils literal"><span class="pre">Capsule</span></tt> API and include this header file after
<tt class="file docutils literal"><span class="pre">Python.h</span></tt>.  Your code will automatically use Capsules
in versions of Python with Capsules, and switch to CObjects
when Capsules are unavailable.</p>
<p><tt class="file docutils literal"><span class="pre">capsulethunk.h</span></tt> simulates Capsules using CObjects.  However,
<tt class="xref c c-type docutils literal"><span class="pre">CObject</span></tt> provides no place to store the capsule&#8217;s &#8220;name&#8221;.  As a
result the simulated <tt class="xref c c-type docutils literal"><span class="pre">Capsule</span></tt> objects created by <tt class="file docutils literal"><span class="pre">capsulethunk.h</span></tt>
behave slightly differently from real Capsules.  Specifically:</p>
<blockquote>
<div><ul class="simple">
<li>The name parameter passed in to <a class="reference internal" href="../c-api/capsule.html#PyCapsule_New" title="PyCapsule_New"><tt class="xref c c-func docutils literal"><span class="pre">PyCapsule_New()</span></tt></a> is ignored.</li>
<li>The name parameter passed in to <a class="reference internal" href="../c-api/capsule.html#PyCapsule_IsValid" title="PyCapsule_IsValid"><tt class="xref c c-func docutils literal"><span class="pre">PyCapsule_IsValid()</span></tt></a> and
<a class="reference internal" href="../c-api/capsule.html#PyCapsule_GetPointer" title="PyCapsule_GetPointer"><tt class="xref c c-func docutils literal"><span class="pre">PyCapsule_GetPointer()</span></tt></a> is ignored, and no error checking
of the name is performed.</li>
<li><a class="reference internal" href="../c-api/capsule.html#PyCapsule_GetName" title="PyCapsule_GetName"><tt class="xref c c-func docutils literal"><span class="pre">PyCapsule_GetName()</span></tt></a> always returns NULL.</li>
<li><a class="reference internal" href="../c-api/capsule.html#PyCapsule_SetName" title="PyCapsule_SetName"><tt class="xref c c-func docutils literal"><span class="pre">PyCapsule_SetName()</span></tt></a> always raises an exception and
returns failure.  (Since there&#8217;s no way to store a name
in a CObject, noisy failure of <a class="reference internal" href="../c-api/capsule.html#PyCapsule_SetName" title="PyCapsule_SetName"><tt class="xref c c-func docutils literal"><span class="pre">PyCapsule_SetName()</span></tt></a>
was deemed preferable to silent failure here.  If this is
inconvenient, feel free to modify your local
copy as you see fit.)</li>
</ul>
</div></blockquote>
<p>You can find <tt class="file docutils literal"><span class="pre">capsulethunk.h</span></tt> in the Python source distribution
as <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Doc/includes/capsulethunk.h">Doc/includes/capsulethunk.h</a>.  We also include it here for
your convenience:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="cp">#ifndef __CAPSULETHUNK_H</span>
<span class="cp">#define __CAPSULETHUNK_H</span>

<span class="cp">#if (    (PY_VERSION_HEX &lt;  0x02070000) \</span>
<span class="cp">     || ((PY_VERSION_HEX &gt;= 0x03000000) \</span>
<span class="cp">      &amp;&amp; (PY_VERSION_HEX &lt;  0x03010000)) )</span>

<span class="cp">#define __PyCapsule_GetField(capsule, field, default_value) \</span>
<span class="cp">    ( PyCapsule_CheckExact(capsule) \</span>
<span class="cp">        ? (((PyCObject *)capsule)-&gt;field) \</span>
<span class="cp">        : (default_value) \</span>
<span class="cp">    ) \</span>

<span class="cp">#define __PyCapsule_SetField(capsule, field, value) \</span>
<span class="cp">    ( PyCapsule_CheckExact(capsule) \</span>
<span class="cp">        ? (((PyCObject *)capsule)-&gt;field = value), 1 \</span>
<span class="cp">        : 0 \</span>
<span class="cp">    ) \</span>


<span class="cp">#define PyCapsule_Type PyCObject_Type</span>

<span class="cp">#define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule))</span>
<span class="cp">#define PyCapsule_IsValid(capsule, name) (PyCObject_Check(capsule))</span>


<span class="cp">#define PyCapsule_New(pointer, name, destructor) \</span>
<span class="cp">    (PyCObject_FromVoidPtr(pointer, destructor))</span>


<span class="cp">#define PyCapsule_GetPointer(capsule, name) \</span>
<span class="cp">    (PyCObject_AsVoidPtr(capsule))</span>

<span class="cp">/* Don&#39;t call PyCObject_SetPointer here, it fails if there&#39;s a destructor */</span>
<span class="cp">#define PyCapsule_SetPointer(capsule, pointer) \</span>
<span class="cp">    __PyCapsule_SetField(capsule, cobject, pointer)</span>


<span class="cp">#define PyCapsule_GetDestructor(capsule) \</span>
<span class="cp">    __PyCapsule_GetField(capsule, destructor)</span>

<span class="cp">#define PyCapsule_SetDestructor(capsule, dtor) \</span>
<span class="cp">    __PyCapsule_SetField(capsule, destructor, dtor)</span>


<span class="cm">/*</span>
<span class="cm"> * Sorry, there&#39;s simply no place</span>
<span class="cm"> * to store a Capsule &quot;name&quot; in a CObject.</span>
<span class="cm"> */</span>
<span class="cp">#define PyCapsule_GetName(capsule) NULL</span>

<span class="k">static</span> <span class="kt">int</span>
<span class="nf">PyCapsule_SetName</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">capsule</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">unused</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">unused</span> <span class="o">=</span> <span class="n">unused</span><span class="p">;</span>
    <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_NotImplementedError</span><span class="p">,</span>
        <span class="s">&quot;can&#39;t use PyCapsule_SetName with CObjects&quot;</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>



<span class="cp">#define PyCapsule_GetContext(capsule) \</span>
<span class="cp">    __PyCapsule_GetField(capsule, descr)</span>

<span class="cp">#define PyCapsule_SetContext(capsule, context) \</span>
<span class="cp">    __PyCapsule_SetField(capsule, descr, context)</span>


<span class="k">static</span> <span class="kt">void</span> <span class="o">*</span>
<span class="nf">PyCapsule_Import</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">,</span> <span class="kt">int</span> <span class="n">no_block</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">object</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="kt">void</span> <span class="o">*</span><span class="n">return_value</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="kt">char</span> <span class="o">*</span><span class="n">trace</span><span class="p">;</span>
    <span class="kt">size_t</span> <span class="n">name_length</span> <span class="o">=</span> <span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="p">);</span>
    <span class="kt">char</span> <span class="o">*</span><span class="n">name_dup</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span><span class="n">PyMem_MALLOC</span><span class="p">(</span><span class="n">name_length</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">name_dup</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">memcpy</span><span class="p">(</span><span class="n">name_dup</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">name_length</span><span class="p">);</span>

    <span class="n">trace</span> <span class="o">=</span> <span class="n">name_dup</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">trace</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">char</span> <span class="o">*</span><span class="n">dot</span> <span class="o">=</span> <span class="n">strchr</span><span class="p">(</span><span class="n">trace</span><span class="p">,</span> <span class="sc">&#39;.&#39;</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dot</span><span class="p">)</span> <span class="p">{</span>
            <span class="o">*</span><span class="n">dot</span><span class="o">++</span> <span class="o">=</span> <span class="sc">&#39;\0&#39;</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">object</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">no_block</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">object</span> <span class="o">=</span> <span class="n">PyImport_ImportModuleNoBlock</span><span class="p">(</span><span class="n">trace</span><span class="p">);</span>
            <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                <span class="n">object</span> <span class="o">=</span> <span class="n">PyImport_ImportModule</span><span class="p">(</span><span class="n">trace</span><span class="p">);</span>
                <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">object</span><span class="p">)</span> <span class="p">{</span>
                    <span class="n">PyErr_Format</span><span class="p">(</span><span class="n">PyExc_ImportError</span><span class="p">,</span>
                        <span class="s">&quot;PyCapsule_Import could not &quot;</span>
                        <span class="s">&quot;import module </span><span class="se">\&quot;</span><span class="s">%s</span><span class="se">\&quot;</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">trace</span><span class="p">);</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="n">PyObject</span> <span class="o">*</span><span class="n">object2</span> <span class="o">=</span> <span class="n">PyObject_GetAttrString</span><span class="p">(</span><span class="n">object</span><span class="p">,</span> <span class="n">trace</span><span class="p">);</span>
            <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">object</span><span class="p">);</span>
            <span class="n">object</span> <span class="o">=</span> <span class="n">object2</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">object</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">goto</span> <span class="n">EXIT</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="n">trace</span> <span class="o">=</span> <span class="n">dot</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">PyCObject_Check</span><span class="p">(</span><span class="n">object</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">PyCObject</span> <span class="o">*</span><span class="n">cobject</span> <span class="o">=</span> <span class="p">(</span><span class="n">PyCObject</span> <span class="o">*</span><span class="p">)</span><span class="n">object</span><span class="p">;</span>
        <span class="n">return_value</span> <span class="o">=</span> <span class="n">cobject</span><span class="o">-&gt;</span><span class="n">cobject</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="n">PyErr_Format</span><span class="p">(</span><span class="n">PyExc_AttributeError</span><span class="p">,</span>
            <span class="s">&quot;PyCapsule_Import </span><span class="se">\&quot;</span><span class="s">%s</span><span class="se">\&quot;</span><span class="s"> is not valid&quot;</span><span class="p">,</span>
            <span class="n">name</span><span class="p">);</span>
    <span class="p">}</span>

<span class="nl">EXIT:</span>
    <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">object</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">name_dup</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">PyMem_FREE</span><span class="p">(</span><span class="n">name_dup</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">return_value</span><span class="p">;</span>
<span class="p">}</span>

<span class="cp">#endif </span><span class="cm">/* #if PY_VERSION_HEX &lt; 0x02070000 */</span><span class="cp"></span>

<span class="cp">#endif </span><span class="cm">/* __CAPSULETHUNK_H */</span><span class="cp"></span>
</pre></div>
</div>
</div>
<div class="section" id="other-options">
<h2>Other options<a class="headerlink" href="#other-options" title="Permalink to this headline">¶</a></h2>
<p>If you are writing a new extension module, you might consider <a class="reference external" href="http://www.cython.org">Cython</a>.  It translates a Python-like language to C.  The
extension modules it creates are compatible with Python 3 and Python 2.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Porting Extension Modules to Python 3</a><ul>
<li><a class="reference internal" href="#conditional-compilation">Conditional compilation</a></li>
<li><a class="reference internal" href="#changes-to-object-apis">Changes to Object APIs</a><ul>
<li><a class="reference internal" href="#str-unicode-unification">str/unicode Unification</a></li>
<li><a class="reference internal" href="#long-int-unification">long/int Unification</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-initialization-and-state">Module initialization and state</a></li>
<li><a class="reference internal" href="#cobject-replaced-with-capsule">CObject replaced with Capsule</a></li>
<li><a class="reference internal" href="#other-options">Other options</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="pyporting.html"
                        title="previous chapter">Porting Python 2 Code to Python 3</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="curses.html"
                        title="next chapter">Curses Programming with Python</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/howto/cporting.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="curses.html" title="Curses Programming with Python"
             >next</a> |</li>
        <li class="right" >
          <a href="pyporting.html" title="Porting Python 2 Code to Python 3"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >Python HOWTOs</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Oct 13, 2020.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>

  </body>
</html>

SEA-GHOST - SHELL CODING BY SEA-GHOST