How to translate python into C

J

Johnny Lee

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
 
S

Szabolcs Nagy

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/
 
J

Johnny Lee

Szabolcs said:
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
 
B

Benjamin Niemann

Johnny said:
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/doc/faq.html#how-do-i-compile-my-own-programs
 
S

Szabolcs Nagy

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/
 
L

Lawrence Oluyede

Il 2005-10-28 said:
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
 
J

Johnny Lee

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top