python

Support for Python, detect the headers and libraries and provide use variables to link C/C++ programs against them:

def options(opt):
        opt.load('compiler_c python')
def configure(conf):
        conf.load('compiler_c python')
        conf.check_python_version((2,4,2))
        conf.check_python_headers()
def build(bld):
        bld.program(features='pyembed', source='a.c', target='myprog')
        bld.shlib(features='pyext', source='b.c', target='mylib')
waflib.Tools.python.FRAG = '\n#include <Python.h>\n#ifdef __cplusplus\nextern "C" {\n#endif\n\tvoid Py_Initialize(void);\n\tvoid Py_Finalize(void);\n#ifdef __cplusplus\n}\n#endif\nint main(int argc, char **argv)\n{\n (void)argc; (void)argv;\n Py_Initialize();\n Py_Finalize();\n return 0;\n}\n'

Piece of C/C++ code used in waflib.Tools.python.check_python_headers()

waflib.Tools.python.INST = '\nimport sys, py_compile\npy_compile.compile(sys.argv[1], sys.argv[2], sys.argv[3], True)\n'

Piece of Python code used in waflib.Tools.python.pyo and waflib.Tools.python.pyc for byte-compiling python files

waflib.Tools.python.feature_py(self)[source]

Task generator method

Create tasks to byte-compile .py files and install them, if requested

Feature:py
waflib.Tools.python.process_py(self, node)[source]

Add signature of .py file, so it will be byte-compiled when necessary

class waflib.Tools.python.pyc(*k, **kw)[source]

Bases: waflib.Task.Task

Byte-compiling python files

color = 'PINK'
hcode = "\tdef run(self):\n\t\tcmd = [Utils.subst_vars('${PYTHON}', self.env), '-c', INST, self.inputs[0].abspath(), self.outputs[0].abspath(), self.pyd]\n\t\tret = self.generator.bld.exec_command(cmd)\n\t\treturn ret\n"
class waflib.Tools.python.pyo(*k, **kw)[source]

Bases: waflib.Task.Task

Byte-compiling python files

color = 'PINK'
hcode = "\tdef run(self):\n\t\tcmd = [Utils.subst_vars('${PYTHON}', self.env), Utils.subst_vars('${PYFLAGS_OPT}', self.env), '-c', INST, self.inputs[0].abspath(), self.outputs[0].abspath(), self.pyd]\n\t\tret = self.generator.bld.exec_command(cmd)\n\t\treturn ret\n"
waflib.Tools.python.init_pyext(self)[source]

Task generator method

Change the values of cshlib_PATTERN and cxxshlib_PATTERN to remove the lib prefix from library names.

Feature:pyext
waflib.Tools.python.set_bundle(self)[source]

Task generator method

Mac-specific pyext extension that enables bundles from c_osx.py
feature:pyext
waflib.Tools.python.init_pyembed(self)[source]

Task generator method

Add the PYEMBED variable.

Feature:pyembed
waflib.Tools.python.get_python_variables(self, variables, imports=None)[source]

Configuration Method bound to waflib.Configure.ConfigurationContext

Spawn a new python process to dump configuration variables

Parameters:
  • variables (list of string) – variables to print
  • imports (list of string) – one import by element
Returns:

the variable values

Return type:

list of string

waflib.Tools.python.test_pyembed(self, mode, msg='Testing pyembed configuration')[source]

Configuration Method bound to waflib.Configure.ConfigurationContext

waflib.Tools.python.test_pyext(self, mode, msg='Testing pyext configuration')[source]

Configuration Method bound to waflib.Configure.ConfigurationContext

waflib.Tools.python.python_cross_compile(self, features='pyembed pyext')[source]

Configuration Method bound to waflib.Configure.ConfigurationContext

For cross-compilation purposes, it is possible to bypass the normal detection and set the flags that you want: PYTHON_VERSION=‘3.4’ PYTAG=’cpython34’ pyext_PATTERN=”%s.so” PYTHON_LDFLAGS=’-lpthread -ldl’ waf configure

The following variables are used: PYTHON_VERSION required PYTAG required PYTHON_LDFLAGS required pyext_PATTERN required PYTHON_PYEXT_LDFLAGS PYTHON_PYEMBED_LDFLAGS

waflib.Tools.python.check_python_headers(conf, features='pyembed pyext')[source]

Configuration Method bound to waflib.Configure.ConfigurationContext

Check for headers and libraries necessary to extend or embed python by using the module distutils. On success the environment variables xxx_PYEXT and xxx_PYEMBED are added:

  • PYEXT: for compiling python extensions
  • PYEMBED: for embedding a python interpreter
waflib.Tools.python.check_python_version(conf, minver=None)[source]

Configuration Method bound to waflib.Configure.ConfigurationContext

Check if the python interpreter is found matching a given minimum version. minver should be a tuple, eg. to check for python >= 2.4.2 pass (2,4,2) as minver.

If successful, PYTHON_VERSION is defined as ‘MAJOR.MINOR’ (eg. ‘2.4’) of the actual python version found, and PYTHONDIR and PYTHONARCHDIR are defined, pointing to the site-packages directories appropriate for this python version, where modules/packages/extensions should be installed.

Parameters:minver (tuple of int) – minimum version
waflib.Tools.python.check_python_module(conf, module_name, condition='')[source]

Configuration Method bound to waflib.Configure.ConfigurationContext

Check if the selected python interpreter can import the given python module:

def configure(conf):
        conf.check_python_module('pygccxml')
        conf.check_python_module('re', condition="ver > num(2, 0, 4) and ver <= num(3, 0, 0)")
Parameters:module_name (string) – module
waflib.Tools.python.conf(f)

Decorator: attach new configuration functions to waflib.Build.BuildContext and waflib.Configure.ConfigurationContext. The methods bound will accept a parameter named ‘mandatory’ to disable the configuration errors:

def configure(conf):
        conf.find_program('abc', mandatory=False)
Parameters:f (function) – method to bind
waflib.Tools.python.configure(conf)[source]

Detect the python interpreter

waflib.Tools.python.feature(*k)

Decorator that registers a task generator method that will be executed when the object attribute feature contains the corresponding key(s):

from waflib.Task import feature
@feature('myfeature')
def myfunction(self):
        print('that is my feature!')
def build(bld):
        bld(features='myfeature')
Parameters:k (list of string) – feature names
waflib.Tools.python.before_method(*k)[source]

Decorator that registera task generator method which will be executed before the functions of given name(s):

from waflib.TaskGen import feature, before
@feature('myfeature')
@before_method('fun2')
def fun1(self):
        print('feature 1!')
@feature('myfeature')
def fun2(self):
        print('feature 2!')
def build(bld):
        bld(features='myfeature')
Parameters:k (list of string) – method names
waflib.Tools.python.after_method(*k)[source]

Decorator that registers a task generator method which will be executed after the functions of given name(s):

from waflib.TaskGen import feature, after
@feature('myfeature')
@after_method('fun2')
def fun1(self):
        print('feature 1!')
@feature('myfeature')
def fun2(self):
        print('feature 2!')
def build(bld):
        bld(features='myfeature')
Parameters:k (list of string) – method names
waflib.Tools.python.extension(*k)

Decorator that registers a task generator method which will be invoked during the processing of source files for the extension given:

from waflib import Task
class mytask(Task):
        run_str = 'cp ${SRC} ${TGT}'
@extension('.moo')
def create_maa_file(self, node):
        self.create_task('mytask', node, node.change_ext('.maa'))
def build(bld):
        bld(source='foo.moo')
waflib.Tools.python.options(opt)[source]

Add python-specific options

Features defined in this module: