Linux cpanel.rrshost.in 5.15.0-25-generic #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 x86_64
Apache
: 109.123.238.221 | : 172.69.17.164
128 Domain
8.2.28
aev999
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python2.7 /
html /
howto /
[ HOME SHELL ]
Name
Size
Permission
Action
argparse.html
78.22
KB
-rw-r--r--
cporting.html
30.28
KB
-rw-r--r--
curses.html
44.61
KB
-rw-r--r--
descriptor.html
56.77
KB
-rw-r--r--
doanddont.html
39.16
KB
-rw-r--r--
functional.html
132.84
KB
-rw-r--r--
index.html
8.19
KB
-rw-r--r--
logging-cookbook.html
203.46
KB
-rw-r--r--
logging.html
108.86
KB
-rw-r--r--
pyporting.html
45.45
KB
-rw-r--r--
regex.html
146.87
KB
-rw-r--r--
sockets.html
41.31
KB
-rw-r--r--
sorting.html
49.13
KB
-rw-r--r--
unicode.html
69.18
KB
-rw-r--r--
urllib2.html
62.82
KB
-rw-r--r--
webservers.html
55.95
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : cporting.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>Porting Extension Modules to Python 3 — Python 2.7.18 documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/classic.css" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.18 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.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" /> <link rel="canonical" href="file:///usr/share/doc/python2.7/html/howto/cporting.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;"> This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>. You should install the python3 and python3-doc packages and read the <a href="file:///usr/share/doc/python3-doc/html/howto/cporting.html"> Python documentation for the Python3 version packaged in this release</a>. </div> <div class="related" role="navigation" aria-label="related navigation"> <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="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> »</li> <li class="nav-item nav-item-this"><a href="">Porting Extension Modules to Python 3</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <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> <dl class="field-list simple"> <dt class="field-odd">author</dt> <dd class="field-odd"><p>Benjamin Peterson</p> </dd> </dl> <div class="topic"> <p class="topic-title">Abstract</p> <p>Although changing the C-API was not one of Python 3’s objectives, the many Python-level changes made leaving Python 2’s API intact impossible. In fact, some changes such as <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> and <a class="reference internal" href="../library/functions.html#long" title="long"><code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code></a> unification are more obvious on the C level. This document endeavors to document incompatibilities and how they can be worked around.</p> </div> <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 <code class="xref c c-macro docutils literal notranslate"><span class="pre">PY_MAJOR_VERSION</span></code> is greater than or equal to 3.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1">#if PY_MAJOR_VERSION >= 3</span> <span class="c1">#define IS_PY3K</span> <span class="c1">#endif</span> </pre></div> </div> <p>API functions that are not present can be aliased to their equivalents within conditional blocks.</p> </section> <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> <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’s <a class="reference internal" href="../library/functions.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> type is equivalent to Python 2’s <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicode()</span></code></a>; the C functions are called <code class="docutils literal notranslate"><span class="pre">PyUnicode_*</span></code> for both. The old 8-bit string type has become <code class="xref py py-func docutils literal notranslate"><span class="pre">bytes()</span></code>, with C functions called <code class="docutils literal notranslate"><span class="pre">PyBytes_*</span></code>. Python 2.6 and later provide a compatibility header, <code class="file docutils literal notranslate"><span class="pre">bytesobject.h</span></code>, mapping <code class="docutils literal notranslate"><span class="pre">PyBytes</span></code> names to <code class="docutils literal notranslate"><span class="pre">PyString</span></code> ones. For best compatibility with Python 3, <code class="xref c c-type docutils literal notranslate"><span class="pre">PyUnicode</span></code> should be used for textual data and <code class="xref c c-type docutils literal notranslate"><span class="pre">PyBytes</span></code> for binary data. It’s also important to remember that <code class="xref c c-type docutils literal notranslate"><span class="pre">PyBytes</span></code> and <code class="xref c c-type docutils literal notranslate"><span class="pre">PyUnicode</span></code> in Python 3 are not interchangeable like <code class="xref c c-type docutils literal notranslate"><span class="pre">PyString</span></code> and <code class="xref c c-type docutils literal notranslate"><span class="pre">PyUnicode</span></code> are in Python 2. The following example shows best practices with regards to <code class="xref c c-type docutils literal notranslate"><span class="pre">PyUnicode</span></code>, <code class="xref c c-type docutils literal notranslate"><span class="pre">PyString</span></code>, and <code class="xref c c-type docutils literal notranslate"><span class="pre">PyBytes</span></code>.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>#include "stdlib.h" #include "Python.h" #include "bytesobject.h" /* text example */ static PyObject * say_hello(PyObject *self, PyObject *args) { PyObject *name, *result; if (!PyArg_ParseTuple(args, "U:say_hello", &name)) return NULL; result = PyUnicode_FromFormat("Hello, %S!", name); return result; } /* just a forward */ static char * do_encode(PyObject *); /* bytes example */ static PyObject * encode_object(PyObject *self, PyObject *args) { char *encoded; PyObject *result, *myobj; if (!PyArg_ParseTuple(args, "O:encode_object", &myobj)) return NULL; encoded = do_encode(myobj); if (encoded == NULL) return NULL; result = PyBytes_FromString(encoded); free(encoded); return result; } </pre></div> </div> </section> <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"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a>. But it actually corresponds to Python 2’s <a class="reference internal" href="../library/functions.html#long" title="long"><code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code></a> type—the <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> type used in Python 2 was removed. In the C-API, <code class="docutils literal notranslate"><span class="pre">PyInt_*</span></code> functions are replaced by their <code class="docutils literal notranslate"><span class="pre">PyLong_*</span></code> equivalents.</p> </section> </section> <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="https://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-default notranslate"><div class="highlight"><pre><span></span><span class="c1">#include "Python.h"</span> <span class="n">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="c1">#if PY_MAJOR_VERSION >= 3</span> <span class="c1">#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))</span> <span class="c1">#else</span> <span class="c1">#define GETSTATE(m) (&_state)</span> <span class="n">static</span> <span class="n">struct</span> <span class="n">module_state</span> <span class="n">_state</span><span class="p">;</span> <span class="c1">#endif</span> <span class="n">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="n">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="n">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">-></span><span class="n">error</span><span class="p">,</span> <span class="s2">"something bad happened"</span><span class="p">);</span> <span class="k">return</span> <span class="n">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">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="s2">"error_out"</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="n">NULL</span><span class="p">},</span> <span class="p">{</span><span class="n">NULL</span><span class="p">,</span> <span class="n">NULL</span><span class="p">}</span> <span class="p">};</span> <span class="c1">#if PY_MAJOR_VERSION >= 3</span> <span class="n">static</span> <span class="nb">int</span> <span class="n">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="n">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">-></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="n">static</span> <span class="nb">int</span> <span class="n">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">-></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="n">static</span> <span class="n">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="s2">"myextension"</span><span class="p">,</span> <span class="n">NULL</span><span class="p">,</span> <span class="n">sizeof</span><span class="p">(</span><span class="n">struct</span> <span class="n">module_state</span><span class="p">),</span> <span class="n">myextension_methods</span><span class="p">,</span> <span class="n">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="n">NULL</span> <span class="p">};</span> <span class="c1">#define INITERROR return NULL</span> <span class="n">PyMODINIT_FUNC</span> <span class="n">PyInit_myextension</span><span class="p">(</span><span class="n">void</span><span class="p">)</span> <span class="c1">#else</span> <span class="c1">#define INITERROR return</span> <span class="n">void</span> <span class="n">initmyextension</span><span class="p">(</span><span class="n">void</span><span class="p">)</span> <span class="c1">#endif</span> <span class="p">{</span> <span class="c1">#if PY_MAJOR_VERSION >= 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">&</span><span class="n">moduledef</span><span class="p">);</span> <span class="c1">#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="s2">"myextension"</span><span class="p">,</span> <span class="n">myextension_methods</span><span class="p">);</span> <span class="c1">#endif</span> <span class="k">if</span> <span class="p">(</span><span class="n">module</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">)</span> <span class="n">INITERROR</span><span class="p">;</span> <span class="n">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">-></span><span class="n">error</span> <span class="o">=</span> <span class="n">PyErr_NewException</span><span class="p">(</span><span class="s2">"myextension.Error"</span><span class="p">,</span> <span class="n">NULL</span><span class="p">,</span> <span class="n">NULL</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">st</span><span class="o">-></span><span class="n">error</span> <span class="o">==</span> <span class="n">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="c1">#if PY_MAJOR_VERSION >= 3</span> <span class="k">return</span> <span class="n">module</span><span class="p">;</span> <span class="c1">#endif</span> <span class="p">}</span> </pre></div> </div> </section> <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 <code class="xref c c-type docutils literal notranslate"><span class="pre">Capsule</span></code> object was introduced in Python 3.1 and 2.7 to replace <code class="xref c c-type docutils literal notranslate"><span class="pre">CObject</span></code>. CObjects were useful, but the <code class="xref c c-type docutils literal notranslate"><span class="pre">CObject</span></code> API was problematic: it didn’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="https://bugs.python.org/issue5630">bpo-5630</a>.)</p> <p>If you’re currently using CObjects, and you want to migrate to 3.1 or newer, you’ll need to switch to Capsules. <code class="xref c c-type docutils literal notranslate"><span class="pre">CObject</span></code> 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 <code class="xref c c-type docutils literal notranslate"><span class="pre">Capsule</span></code>. If you need to support Python 3.0, or versions of Python earlier than 2.7, you’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 <code class="file docutils literal notranslate"><span class="pre">capsulethunk.h</span></code> may solve the problem for you. Simply write your code against the <code class="xref c c-type docutils literal notranslate"><span class="pre">Capsule</span></code> API and include this header file after <code class="file docutils literal notranslate"><span class="pre">Python.h</span></code>. Your code will automatically use Capsules in versions of Python with Capsules, and switch to CObjects when Capsules are unavailable.</p> <p><code class="file docutils literal notranslate"><span class="pre">capsulethunk.h</span></code> simulates Capsules using CObjects. However, <code class="xref c c-type docutils literal notranslate"><span class="pre">CObject</span></code> provides no place to store the capsule’s “name”. As a result the simulated <code class="xref c c-type docutils literal notranslate"><span class="pre">Capsule</span></code> objects created by <code class="file docutils literal notranslate"><span class="pre">capsulethunk.h</span></code> behave slightly differently from real Capsules. Specifically:</p> <blockquote> <div><ul class="simple"> <li><p>The name parameter passed in to <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_New" title="PyCapsule_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_New()</span></code></a> is ignored.</p></li> <li><p>The name parameter passed in to <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_IsValid" title="PyCapsule_IsValid"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_IsValid()</span></code></a> and <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_GetPointer" title="PyCapsule_GetPointer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_GetPointer()</span></code></a> is ignored, and no error checking of the name is performed.</p></li> <li><p><a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_GetName" title="PyCapsule_GetName"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_GetName()</span></code></a> always returns NULL.</p></li> <li><p><a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_SetName" title="PyCapsule_SetName"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_SetName()</span></code></a> always raises an exception and returns failure. (Since there’s no way to store a name in a CObject, noisy failure of <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_SetName" title="PyCapsule_SetName"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_SetName()</span></code></a> was deemed preferable to silent failure here. If this is inconvenient, feel free to modify your local copy as you see fit.)</p></li> </ul> </div></blockquote> <p>You can find <code class="file docutils literal notranslate"><span class="pre">capsulethunk.h</span></code> in the Python source distribution as <a class="reference external" href="https://github.com/python/cpython/tree/2.7/Doc/includes/capsulethunk.h">Doc/includes/capsulethunk.h</a>. We also include it here for your convenience:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>#ifndef __CAPSULETHUNK_H #define __CAPSULETHUNK_H #if ( (PY_VERSION_HEX < 0x02070000) \ || ((PY_VERSION_HEX >= 0x03000000) \ && (PY_VERSION_HEX < 0x03010000)) ) #define __PyCapsule_GetField(capsule, field, default_value) \ ( PyCapsule_CheckExact(capsule) \ ? (((PyCObject *)capsule)->field) \ : (default_value) \ ) \ #define __PyCapsule_SetField(capsule, field, value) \ ( PyCapsule_CheckExact(capsule) \ ? (((PyCObject *)capsule)->field = value), 1 \ : 0 \ ) \ #define PyCapsule_Type PyCObject_Type #define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule)) #define PyCapsule_IsValid(capsule, name) (PyCObject_Check(capsule)) #define PyCapsule_New(pointer, name, destructor) \ (PyCObject_FromVoidPtr(pointer, destructor)) #define PyCapsule_GetPointer(capsule, name) \ (PyCObject_AsVoidPtr(capsule)) /* Don't call PyCObject_SetPointer here, it fails if there's a destructor */ #define PyCapsule_SetPointer(capsule, pointer) \ __PyCapsule_SetField(capsule, cobject, pointer) #define PyCapsule_GetDestructor(capsule) \ __PyCapsule_GetField(capsule, destructor) #define PyCapsule_SetDestructor(capsule, dtor) \ __PyCapsule_SetField(capsule, destructor, dtor) /* * Sorry, there's simply no place * to store a Capsule "name" in a CObject. */ #define PyCapsule_GetName(capsule) NULL static int PyCapsule_SetName(PyObject *capsule, const char *unused) { unused = unused; PyErr_SetString(PyExc_NotImplementedError, "can't use PyCapsule_SetName with CObjects"); return 1; } #define PyCapsule_GetContext(capsule) \ __PyCapsule_GetField(capsule, descr) #define PyCapsule_SetContext(capsule, context) \ __PyCapsule_SetField(capsule, descr, context) static void * PyCapsule_Import(const char *name, int no_block) { PyObject *object = NULL; void *return_value = NULL; char *trace; size_t name_length = (strlen(name) + 1) * sizeof(char); char *name_dup = (char *)PyMem_MALLOC(name_length); if (!name_dup) { return NULL; } memcpy(name_dup, name, name_length); trace = name_dup; while (trace) { char *dot = strchr(trace, '.'); if (dot) { *dot++ = '\0'; } if (object == NULL) { if (no_block) { object = PyImport_ImportModuleNoBlock(trace); } else { object = PyImport_ImportModule(trace); if (!object) { PyErr_Format(PyExc_ImportError, "PyCapsule_Import could not " "import module \"%s\"", trace); } } } else { PyObject *object2 = PyObject_GetAttrString(object, trace); Py_DECREF(object); object = object2; } if (!object) { goto EXIT; } trace = dot; } if (PyCObject_Check(object)) { PyCObject *cobject = (PyCObject *)object; return_value = cobject->cobject; } else { PyErr_Format(PyExc_AttributeError, "PyCapsule_Import \"%s\" is not valid", name); } EXIT: Py_XDECREF(object); if (name_dup) { PyMem_FREE(name_dup); } return return_value; } #endif /* #if PY_VERSION_HEX < 0x02070000 */ #endif /* __CAPSULETHUNK_H */ </pre></div> </div> </section> <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://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> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <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> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/howto/cporting.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3 id="searchlabel">Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/> <input type="submit" value="Go" /> </form> </div> </div> <script>$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <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="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.18 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >Python HOWTOs</a> »</li> <li class="nav-item nav-item-this"><a href="">Porting Extension Modules to Python 3</a></li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on December 09, 2024. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 4.3.2. </div> </body> </html>
Close