Welcome, guest | Sign In | My Account | Store | Cart

Add function to __builtin__ module through C API - Python source

References:

https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/

https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/

Save this source as you want in same directory, where C source and compiled binary exists.

After the "make", you can use it: $ ./pytestbin this_source

without ".py" extension.

Python, 13 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/python
# -*- coding: utf8 -*-

# import mymodule
# ^^^^^^^^^^^^^^^ <- no need to import module
#
# for more info, please see the references - this is just an ugly demo

def bar(d):
    usleep(500000) # there isn't function by this name in std Python
    print d
    print type(d)
    return int(d['a'] + d['b'])
Created by airween on Thu, 15 Oct 2015 (GPL)
Python recipes (4591)
airween's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks

  • Licensed under the GPL 2
  • Viewed 7653 times
  • Revision 2 (updated 8 years ago)