Popular recipes by airween http://code.activestate.com/recipes/users/4192997/2015-10-16T12:11:58-07:00ActiveState Code RecipesAdd function to Python's __builtin__ module through C API (C)
2015-10-16T12:11:58-07:00airweenhttp://code.activestate.com/recipes/users/4192997/http://code.activestate.com/recipes/579110-add-function-to-pythons-__builtin__-module-through/
<p style="color: grey">
C
recipe 579110
by <a href="/recipes/users/4192997/">airween</a>
(<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/builtins/">builtins</a>, <a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/exten/">exten</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Add function to __builtin__ module through C API</p>
<p>Sometimes it need to embedding a Python script to a C code, and it references to a function, which also provided by the same C code. Then you have to import the module, as you defined in your C code.</p>
<p>But this import would be skipped, if you add your function to your __builtin__ module. In Python3 (3.5), there is the PyModule_AddFunctions() function, but in the previous versions, you can make it like this snippet.</p>
<p>See these recipes:</p>
<p>Makefile:
<a href="https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/</a></p>
<p>Python script:
<a href="https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/</a></p>
Add function to Python's __builtin__ module through C API - makefile (Text)
2015-10-16T12:11:36-07:00airweenhttp://code.activestate.com/recipes/users/4192997/http://code.activestate.com/recipes/579111-add-function-to-pythons-__builtin__-module-through/
<p style="color: grey">
Text
recipe 579111
by <a href="/recipes/users/4192997/">airween</a>
(<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/makefile/">makefile</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Makefile for add function to __builtin__ module through C API.</p>
<p>C source is here:</p>
<p><a href="https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/" rel="nofollow">https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/</a></p>
<p>Python script is here:</p>
<p><a href="https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/</a></p>
Add function to Python's __builtin__ module through C API - Python source (Python)
2015-10-16T12:10:58-07:00airweenhttp://code.activestate.com/recipes/users/4192997/http://code.activestate.com/recipes/579112-add-function-to-pythons-__builtin__-module-through/
<p style="color: grey">
Python
recipe 579112
by <a href="/recipes/users/4192997/">airween</a>
.
Revision 2.
</p>
<p>Add function to __builtin__ module through C API - Python source</p>
<p>References:</p>
<p><a href="https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/" rel="nofollow">https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/</a></p>
<p><a href="https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/</a></p>
<p>Save this source as you want in same directory, where C source and compiled binary exists.</p>
<p>After the "make", you can use it:
$ ./pytestbin this_source</p>
<p>without ".py" extension.</p>