Discussion:
python to C code generator
(too old to reply)
kushal bhattacharya
2018-01-17 11:04:00 UTC
Permalink
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .

Thanks,
Kushal
David Palao
2018-01-17 11:41:17 UTC
Permalink
Hi,
Have a look at Cython.

Best
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
--
https://mail.python.org/mailman/listinfo/python-list
bartc
2018-01-17 12:37:08 UTC
Permalink
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
What C code would you expect to see from this line of Python:

a = b + c

?
kushal bhattacharya
2018-01-23 12:25:59 UTC
Permalink
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
hi,
I have found nuitka as asuitable candidate but it seems that nuitka doesnt generate a simple C code which could be included as a C file in another program.Is there any alternative easier way regarding this?

Thanks
p***@financecentre.club
2018-01-23 12:36:55 UTC
Permalink
What about Cython?
Post by kushal bhattacharya
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
hi,
I have found nuitka as asuitable candidate but it seems that nuitka doesnt generate a simple C code which could be included as a C file in another program.Is there any alternative easier way regarding this?
Thanks
t***@financecentre.club
2018-01-23 14:45:35 UTC
Permalink
Hey Ally,

Cython adds a big chunk of complexity to simple things. That's the problem.

Greetings.
Have you tried cython ?
On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal
Hi,
Is there any python framework or any tool as  which can generate C
code from python code as it is .
Thanks,
Kushal
hi,
I have found nuitka as asuitable candidate but it seems that nuitka
doesnt generate a simple C code which could be included as a C file in
another program.Is there any alternative easier way regarding this?
Thanks
Chris Angelico
2018-01-23 15:14:48 UTC
Permalink
Post by t***@financecentre.club
Hey Ally,
Cython adds a big chunk of complexity to simple things. That's the problem.
That's like saying "Unicode adds a big chunk of complexity to the
simple task of translating a word from Japanese into Russian". No, it
doesn't; the complexity is inherent in the problem. You cannot
translate Python code into C code without either (a) reimplementing
all of Python's semantics, as Cython does; or (b) drastically changing
the semantics, such that even the very simplest of code might behave
quite differently; or (c) manually reading through the code and
writing equivalent C, which is what you might call "porting" or
"rewriting". (Or possibly "prototyping", if the intention was always
to transform it into C.) There is fundamentally NO easy way to
translate code from one language into another and get readable,
idiomatic code at the other end.

ChrisA
kushal bhattacharya
2018-01-23 13:23:13 UTC
Permalink
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
yes i have but it generates a complex C code with python dependencies.I want to call the generated function from another C code but i Cant figure out how to do that
bartc
2018-01-23 13:34:50 UTC
Permalink
Post by kushal bhattacharya
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
yes i have but it generates a complex C code with python dependencies.I want to call the generated function from another C code but i Cant figure out how to do that
Because the translation isn't simply defined.

I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
lines of C. The main purpose seems to be to generate a self-contained
executable corresponding to the Python, but generating first a C
equivalent then using a C compiler and linker.

This equivalent code may just contain all the bits in CPython needed to
do the job, but bypassing all the stuff to do with executing actual
byte-code. But it also seems to do some optimisations (in the generated
C before it uses C compiler optimisations), so that if static types can
be inferred it might make use of that info.

Perhaps you simply want to use Python syntax to write C code? That would
be a different kind of translator. And a simpler one, as 'a=b+c'
translates to 'a+b+c;' in C.
--
bartc
kushal bhattacharya
2018-01-23 13:48:03 UTC
Permalink
Post by bartc
Post by kushal bhattacharya
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
yes i have but it generates a complex C code with python dependencies.I want to call the generated function from another C code but i Cant figure out how to do that
Because the translation isn't simply defined.
I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
lines of C. The main purpose seems to be to generate a self-contained
executable corresponding to the Python, but generating first a C
equivalent then using a C compiler and linker.
This equivalent code may just contain all the bits in CPython needed to
do the job, but bypassing all the stuff to do with executing actual
byte-code. But it also seems to do some optimisations (in the generated
C before it uses C compiler optimisations), so that if static types can
be inferred it might make use of that info.
Perhaps you simply want to use Python syntax to write C code? That would
be a different kind of translator. And a simpler one, as 'a=b+c'
translates to 'a+b+c;' in C.
--
bartc
This is exactly what i meant to say.My goal is to translate the python code into its C equivalent with function name as it is.
Ned Batchelder
2018-01-23 14:00:52 UTC
Permalink
Post by kushal bhattacharya
Post by bartc
Post by kushal bhattacharya
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
yes i have but it generates a complex C code with python dependencies.I want to call the generated function from another C code but i Cant figure out how to do that
Because the translation isn't simply defined.
I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
lines of C. The main purpose seems to be to generate a self-contained
executable corresponding to the Python, but generating first a C
equivalent then using a C compiler and linker.
This equivalent code may just contain all the bits in CPython needed to
do the job, but bypassing all the stuff to do with executing actual
byte-code. But it also seems to do some optimisations (in the generated
C before it uses C compiler optimisations), so that if static types can
be inferred it might make use of that info.
Perhaps you simply want to use Python syntax to write C code? That would
be a different kind of translator. And a simpler one, as 'a=b+c'
translates to 'a+b+c;' in C.
--
bartc
This is exactly what i meant to say.My goal is to translate the python code into its C equivalent with function name as it is.
The best way to do that is to read the Python code, understand what it
does, and re-write it in C.  You won't find an automatic tool that can
do the job you want.  The semantics of Python and C are too different.

--Ned.
Kirill Balunov
2018-01-23 14:11:03 UTC
Permalink
You can look at SymPy code generator
http://docs.sympy.org/latest/modules/utilities/codegen.html
Perhaps this is exactly what you need.

With kind regards,
-gdg
Post by Ned Batchelder
Post by kushal bhattacharya
Post by bartc
Post by kushal bhattacharya
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C
code from python code as it is .
Thanks,
Kushal
yes i have but it generates a complex C code with python dependencies.I
want to call the generated function from another C code but i Cant figure
out how to do that
Because the translation isn't simply defined.
I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
lines of C. The main purpose seems to be to generate a self-contained
executable corresponding to the Python, but generating first a C
equivalent then using a C compiler and linker.
This equivalent code may just contain all the bits in CPython needed to
do the job, but bypassing all the stuff to do with executing actual
byte-code. But it also seems to do some optimisations (in the generated
C before it uses C compiler optimisations), so that if static types can
be inferred it might make use of that info.
Perhaps you simply want to use Python syntax to write C code? That would
be a different kind of translator. And a simpler one, as 'a=b+c'
translates to 'a+b+c;' in C.
--
bartc
This is exactly what i meant to say.My goal is to translate the python
code into its C equivalent with function name as it is.
The best way to do that is to read the Python code, understand what it
does, and re-write it in C. You won't find an automatic tool that can do
the job you want. The semantics of Python and C are too different.
--Ned.
--
https://mail.python.org/mailman/listinfo/python-list
Andrew Z
2018-01-23 14:25:27 UTC
Permalink
Id go this way too. Basic C is straightforward. I usually consider
learning a new "thing " if the time to support potwntially combersome
solution using existing methods justifies the effort.
Post by Ned Batchelder
Post by kushal bhattacharya
Post by bartc
Post by kushal bhattacharya
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C
code from python code as it is .
Thanks,
Kushal
yes i have but it generates a complex C code with python dependencies.I
want to call the generated function from another C code but i Cant figure
out how to do that
Because the translation isn't simply defined.
I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
lines of C. The main purpose seems to be to generate a self-contained
executable corresponding to the Python, but generating first a C
equivalent then using a C compiler and linker.
This equivalent code may just contain all the bits in CPython needed to
do the job, but bypassing all the stuff to do with executing actual
byte-code. But it also seems to do some optimisations (in the generated
C before it uses C compiler optimisations), so that if static types can
be inferred it might make use of that info.
Perhaps you simply want to use Python syntax to write C code? That would
be a different kind of translator. And a simpler one, as 'a=b+c'
translates to 'a+b+c;' in C.
--
bartc
This is exactly what i meant to say.My goal is to translate the python
code into its C equivalent with function name as it is.
The best way to do that is to read the Python code, understand what it
does, and re-write it in C. You won't find an automatic tool that can do
the job you want. The semantics of Python and C are too different.
--Ned.
--
https://mail.python.org/mailman/listinfo/python-list
bartc
2018-01-23 17:43:18 UTC
Permalink
Perhaps you simply want to use Python syntax to write C code? That would > be a different kind of translator. And a simpler one, as 'a=b+c' >
translates to 'a+b+c;' in C.
Or rather, 'a=b+c;'

(I've written source to source translators, some of which could target
C, but not Python to C.

It would be feasible to write C code in a syntax that looks rather like
Python, but it won't be real Python, and you can't run it as Python.

It wouldn't be a satisfactory way of writing C programs. So, although
I'm not that big a fan of C syntax, it might be better to write C as C,
and Python as Python, to avoid confusion.)
--
bartc
Steven D'Aprano
2018-01-24 07:04:11 UTC
Permalink
Post by bartc
It wouldn't be a satisfactory way of writing C programs. So, although
I'm not that big a fan of C syntax, it might be better to write C as C,
and Python as Python, to avoid confusion.)
This.

The fundamental reality is that `a + b` means different things in C and
Python. Even if you limit yourself to integers and not arbitrary values
(fractions, lists, strings, etc) the semantics are different:

- in C, ints have a fixed number of bits and any addition which
ends up out of range is undefined behaviour[1];

- while Python uses BigInts, overflow is impossible, and the
only possible error is that you run out of memory and an
exception is raised (although the addition can take an
indefinite long amount of time).


Often the difference doesn't matter... but when it does matter, it
*really* matters.




[1] If anyone thinks that it is addition with overflow, you are wrong.
Some C compilers *may* use overflow, but the language strictly defines it
as undefined behaviour, so the compiler can equally choose to set your
computer on fire[2] if it prefers.

https://blog.regehr.org/archives/213



[2] http://www.catb.org/jargon/html/H/HCF.html
--
Steve
kushal bhattacharya
2018-01-23 14:49:06 UTC
Permalink
Post by kushal bhattacharya
Hi,
Is there any python framework or any tool as which can generate C code from python code as it is .
Thanks,
Kushal
ok so which python tool would be the best one which can be included and parameters can be passed to from another C code file
Loading...