Python/C API

This is the place for queries that don't fit in any of the other categories.

Python/C API

Postby march » Sun Dec 29, 2013 4:06 pm

Hello!
Can anyone help me with Python/C API?
I try to use Python/C API in my C++ app. And i have problems with code below:
main.cpp
Code: Select all
#include <stdio.h>
#include <Python.h>
#include <marshal.h>

int main(int , char **)
{
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print ('Today is', ctime(time()))\n");

    FILE* fp = fopen("c:/test.pyc", "wb");

    if (fp != NULL)
    {
        long mn = PyImport_GetMagicNumber();
        PyMarshal_WriteLongToFile(mn, fp, Py_MARSHAL_VERSION);
        fclose(fp);
    }

    Py_Finalize();
    return 0;
}

I compile this example in Win XP SP3(32bit) with mingw4.8-32bit (and I also tried VS2010 - same result) and it compiles successfully. When I try to run my app it fails when call PyMarshal_WriteLongToFile. What am I doing wrong? I tried to use Python 2.6, 2.7 and 3.3 - same problem...
Last edited by stranac on Sun Dec 29, 2013 4:34 pm, edited 1 time in total.
Reason: First post lock.
march
 
Posts: 3
Joined: Sun Dec 29, 2013 3:15 pm

Re: Python/C API

Postby casevh » Sun Dec 29, 2013 5:33 pm

I compiled a slightly edited version of your example on Linux. It produced the output I expected: a .pyc file containing the marshal version number as the first 4, and only 4, bytes.

When you say it failed, please provide more information regarding how it failed. What did you expect? What happened?
casevh
 
Posts: 56
Joined: Sat Feb 09, 2013 7:35 am

Re: Python/C API

Postby march » Sun Dec 29, 2013 6:08 pm

It's a simplified example.
I'm just trying to write 4 bytes in file using function PyMarshal_WriteLongToFile. And I always have an exception, that instruction tries to write a value at '0x00000010'-address in memory. In Win XP.
And I just want to understand - it's a bug in python-lib on Windows or I'm doing something wrong...
march
 
Posts: 3
Joined: Sun Dec 29, 2013 3:15 pm

Re: Python/C API

Postby casevh » Sun Dec 29, 2013 6:28 pm

I'm just guessing here...

It is risky(*) to pass file descriptors between C run time libraries. PyMarshal_WriteLongToFile expects a file descriptor created with the same run time library that is used by Python. Python 2.6 to 3.2 use VS2008. Python 3.3 uses VS2010. I would try to get it working with Python 3.3 and VS2010 first.

You might want to import the io module and use it to create a file object and then use PyObject_AsFileDescriptor to get the file descriptor. I have not tried this.

(*) It breaks entirely between different version of VS. I don't know the behavior of GCC.
casevh
 
Posts: 56
Joined: Sat Feb 09, 2013 7:35 am

Re: Python/C API

Postby march » Sun Dec 29, 2013 6:50 pm

Yes, you're right.
I compiled this example for Python 3.3 using VS2010. And it's work correctly. But I tried to compile it for Python 2.7.6 with VS2010 and it still did not work.
Many thanks for your help!
march
 
Posts: 3
Joined: Sun Dec 29, 2013 3:15 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], igotapochahontas and 4 guests