By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,629 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,629 IT Pros & Developers. It's quick & easy.

How to translate python into C

P: n/a
Hi,
First, I want to know whether the python interpreter translate the
code directly into machine code, or translate it into C then into
machine code?
Second, if the codes are translated directly into machine codes, how
can I translate the codes into C COMPLETELY the same? if the codes are
translated first into C, where can I get the C source?
Thanks for your help.

Regards,
Johnny

Oct 28 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
python creates bytecode (like java classes)
you cannot translate python directly to c or machine code, but there
are some projects you probably want to look into
Pypy is a python implemetation in python and it can be used to
translate a python scrip to c or llvm code. (large project, work in
progress)
http://codespeak.net/pypy/dist/pypy/doc/news.html
Shedskin translates python code to c++ (not all language features
supported)
http://shed-skin.blogspot.com/
Pyrex is a nice language where you can use python and c like code and
it translates into c code. (it is useful for creating fast python
extension modules or a python wrapper around an existing c library)
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/

Oct 28 '05 #2

P: n/a

Szabolcs Nagy wrote:
python creates bytecode (like java classes)
you cannot translate python directly to c or machine code, but there
are some projects you probably want to look into
Pypy is a python implemetation in python and it can be used to
translate a python scrip to c or llvm code. (large project, work in
progress)
http://codespeak.net/pypy/dist/pypy/doc/news.html
Shedskin translates python code to c++ (not all language features
supported)
http://shed-skin.blogspot.com/
Pyrex is a nice language where you can use python and c like code and
it translates into c code. (it is useful for creating fast python
extension modules or a python wrapper around an existing c library)
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/


Thanks, Szabolcs. In fact, I want to reproduce a crush on cygwin. I
used a session of python code to produce the crush, and want to
translate it into C and reproduce it. Is the tools provided by you help
with these issues? Of coz, I'll try them first. :)

Regards,
Johnny

Oct 28 '05 #3

P: n/a
Johnny Lee wrote:
Hi,
First, I want to know whether the python interpreter translate the
code directly into machine code, or translate it into C then into
machine code?
Neither this nor that. The interpreter first compiles the code into python
'byte code' - something similar to machine code, but not it is not targeted
at the CPU of your system, but for a portable virtual machine. This virtual
machine will then execute the byte code, just like a CPU would execute
machine code.
Second, if the codes are translated directly into machine codes, how
can I translate the codes into C COMPLETELY the same? if the codes are
translated first into C, where can I get the C source?


You may have a look at PyPy. I do not know what it exactly can do, but this
might be interesting for you:
http://codespeak.net/pypy/dist/pypy/...y-own-programs

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Oct 28 '05 #4

P: n/a
Thanks for your tips Niemann:)

Regards,
Johnny

Oct 28 '05 #5

P: n/a
python script crashed and you want to debug it?

if no trace back provided with the line number where the exception
raised, then the crash caused by an extension module (most likely
written in C), i don't know howto debug it, but at least you can find
the place where the crash occures by adding lots of print statements
with debug information.

i don't think compiling to c would make it easier.
there are python debuggers but i've never used one (these can only
debug python scripts not the binary extension modules).

pdb builtin module:
http://docs.python.org/lib/module-pdb.html

nice debugger with gui:
http://www.digitalpeers.com/pythondebugger/

Oct 28 '05 #6

P: n/a
Il 2005-10-28, Johnny Lee <jo************@hotmail.com> ha scritto:
Thanks, Szabolcs. In fact, I want to reproduce a crush on cygwin. I
used a session of python code to produce the crush, and want to
translate it into C and reproduce it. Is the tools provided by you help
with these issues? Of coz, I'll try them first. :)


Do you mean "crash" ?
Take a look at this: http://pycrash.sourceforge.net
--
Lawrence
http://www.oluyede.org/blog
Oct 28 '05 #7

P: n/a
Thanks Szabolcs and Laurence, it's not the crash of python but the
crash of cygwin. We can locate the line number but when we submit the
crash to cygwin's mail list, they told us they don't speak python. So
I'm just trying to re-produce the crash in C.

Regards,
Johnny

Oct 30 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.