0

I am able to successfully parse a c file containing a function i want to use in python. now i cannot figure how to either a) call said function or b) create a .py file with said function.

All i have is this mysterious "ast" object

  • 3
    `pycparser` does not convert C to Python; it simply parses C code and lets you analyze it. – icktoofay Nov 25 '11 at 04:51
  • really? I am not seeing the utility of that :( any suggestions for being able to call this c function from python? – user1002331 Nov 25 '11 at 04:53
  • 1
    You could use [Cython](http://www.cython.org/) or [SWIG](http://www.swig.org/) to make a small wrapper for the C function you want to call. – icktoofay Nov 25 '11 at 04:54
  • You could also look into [`ctypes`](http://docs.python.org/library/ctypes.html). – icktoofay Nov 25 '11 at 04:57
  • 4
    "I am not seeing the utility of that" Well, possible applications are given in pycparser site. Often it is a good idea to **read the docs** and plan before building the **big thing**. Don't you agree? – joaquin Nov 25 '11 at 06:56

1 Answers1

1

This is a large undertaking. pycparser only gives you the front-end of the compiler. The backend you'll have to implement yourself.

For an existing project that uses pycparser just for this purpose (calling C from Python), see cffi.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412