[C++-sig] [Py++] Export #define directives to ctypes

Roman Yakovenko roman.yakovenko at gmail.com
Mon Dec 28 06:36:46 CET 2009


On Mon, Dec 28, 2009 at 12:54 AM, Nikolaus Rath <Nikolaus at rath.org> wrote:
> Hello,
>
> Is there a way to export preprocessor definitions (#define ...) with the
> ctypes code generator?
>
> At the moment I'm using this rather nasty hack:
>
> /* Header file */
> // FUSE_SET_ATTR_* is defined with #define in fuse_lowlevel.h
> #include <fuse_lowlevel.h>
> const int D_FUSE_SET_ATTR_MODE = FUSE_SET_ATTR_MODE;
> const int D_FUSE_SET_ATTR_UID = FUSE_SET_ATTR_UID;
> // ...
>
> # Code generator
> mb.write_module(os.path.join(code_path, 'ctypes_api.py'))
> fh = open(os.path.join(code_path, 'ctypes_api.py'), 'a')
> fh.write('\n')
> for var in mb.global_ns.decls(lambda f: f.name.startswith('D_')):
>    fh.write('{0} = {1}\n'.format(var.name[2:], var._value))
> fh.close()
>
>
> But it'd be nice if there was a better solution (so that I don't have to
> define a new variable for every #define that I want to export).

ctypes code generator is missing an important future to make it work -
adding user code. I will try to implement it pretty soon.

Let me explain.

mb = ctypes_module_builder_t( ... )
for file_path in pygccxml.declarations.declaration_files( mb.global_ns ):
    if file_path is not under "fuse" source code directory:
         continue
    else:
         use simple regex to search for "#define FUSE_*" expression,
extract its value and add it to mb.

Does this solution makes sense to you? If so, I will take a look on it
this evening.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/


More information about the Cplusplus-sig mailing list