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

Nikolaus Rath Nikolaus at rath.org
Mon Dec 28 16:57:10 CET 2009


Roman Yakovenko <roman.yakovenko at gmail.com> writes:
> 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?

Not quite. I'd rather have the "use simple regex to search for "#define
FUSE_*" expression" part in Py++ rather than in my own code. Also, I'm
not sure if a simple regex is really a good solution for this. The
following will probably fail already (i.e., give the value of FUSE_FOO
as the string 'FUSE_BLA' rather than the number 42):

#define FUSE_BLA 42
#define FUSE_FOO FUSE_BLA


Best,


   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C



More information about the Cplusplus-sig mailing list