PyMarshal Commands crash on Windows

Gabriel Genellina gagsl-py2 at
Sun Sep 14 21:32:04 CEST 2008

En Sun, 14 Sep 2008 08:28:01 -0300,  
<spammaster.10.webmaster at> escribió:

> int main (int argc, char * const argv[]) {
>     Py_Initialize();
>     FILE* fp = fopen("/Users/test/Desktop/123.pyc","wb");
>     PyCodeObject* op = (PyCodeObject*)Py_CompileString("import sys
> \nprint 'hello'","<>",Py_file_input);
>     PyMarshal_WriteObjectToFile((PyObject *)op, fp,
>     Py_Finalize();
>     return 0;
> }
> This Code crashs on Windows, and I can't explain why. I want to
> convert a PyCodeObject to a PyObject and save it to the harddisk.
> PyMarshal_ReadObjectFromFile(FILE *P) crashs too if I want to read a
> byte-compiled object.

Your code should check every operation for errors. fopen may return NULL,  
Py_CompileString may fail and return NULL, PyMarshal_xxx might fail  
(although it's not documented).

Why "123.pyc"? Do you want to generate a compiled module? Use the  
py_compile standard module instead (or write equivalent C code, or see how  
import.c does that)

Gabriel Genellina

More information about the Python-list mailing list