Mapping C constants into Python namespace

18 views
Skip to first unread message

Toni Barth

unread,
Feb 10, 2018, 1:35:46 PM2/10/18
to cython...@googlegroups.com
Hello all,


I've got the following situation:

I want to create a project which assembles a server for an already
existing project, but in Python (usually its in C), and attach this
server to another already existing Python project. Because the C-type
server brings important constants and functions, I want to use some of
its includes to utilize this server in Cython.


Most of the things I need are actually constants from a c-type file like
the following:

#ifndef __SRV_HEADER__

  #define __SRV_HEADER__

  #define CONST_1 1

  #define CONST_2 2

  ...

#endif


The most easiest thing would be to include this header file in Cython
now, like with

cdef extern from "constants.h":

  ...


Is there an easy way to make all constants available in this C file now
available to any external Python program, like I include this Cython
module and type "constants_wrapper.CONST_1", without re-defining all
constants inside the Cython module manually? This would reduce
maintenance, because I don't need to adopt the whole Cython code
whenever the server devs change the content of one macro. I actually
know that most of those constants are ints or strings (or at least
multiple chars), so no shortener constants or functions, conditions or
actual macros are in there.


But I imagine this isn't possible at all, because the C compiler doesn't
know the underlying C type on runtime, because those are preprocessor
makros.


Anyway, thought I would simply ask here, maybe someone of knows more
about it.


Thanks for your help.


Best Regards.


Toni

Robert Bradshaw

unread,
Feb 10, 2018, 3:47:22 PM2/10/18
to cython...@googlegroups.com
If you don't want to enumerate them by hand, your best bet might be
writing a small script to extract the constants of interest out of the
header file and generate the .pyx file. You could also use tricks like

echo '#include "header.h"' | gcc - -dM -E
> --
>
> --- You received this message because you are subscribed to the Google
> Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages