Jump to content
  • Advertisement
Sign in to follow this  
SpacedOut

Weird error I'm getting with PyRun_SimpleFile()

This topic is 4479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's my C++ code, it compiles fine, the file exists:
#include <Python/python.h>
#include <iostream>
#include <cstdio>
using namespace std;

int main(int argc, char **argv)
{
	Py_Initialize();

	PyObject *main_module = PyImport_AddModule("__main__");
	PyObject *main_dict = PyModule_GetDict(main_module);
	
	FILE *f = fopen("test.py", "r");
	PyRun_SimpleFile(f, "test.py");

	if (PyDict_GetItemString(main_dict, "num") == NULL)
		cout << "No" << endl;
	else
		cout << "Yes" << endl;

	Py_Finalize();
	return 0;
}
when I compile and run it, I get this: Unhandled exception at 0x7c918fea in Embed_practice.exe: 0xC0000005: Access violation writing location 0x00000010. and it calls attention to this line of code in a locked file in VS2005:
/***
* _lock_file - Lock a FILE
*
*Purpose:
*       Assert the lock for a stdio-level file
*
*Entry:
*       pf = __piob[] entry (pointer to a FILE or _FILEX)
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

void __cdecl _lock_file (
        FILE *pf
        )
{
        /*
         * The way the FILE (pointed to by pf) is locked depends on whether
         * it is part of _iob[] or not
         */
        if ( (pf >= _iob) && (pf <= (&_iob[_IOB_ENTRIES-1])) )
            /*
             * FILE lies in _iob[] so the lock lies in _locktable[].
             */
            _lock( _STREAM_LOCKS + (int)(pf - _iob) );
        else
            /*
             * Not part of _iob[]. Therefore, *pf is a _FILEX and the
             * lock field of the struct is an initialized critical
             * section.
             */
            EnterCriticalSection( &(((_FILEX *)pf)->lock) );
}
I'm not even sure what locking a file is, and I have no idea how to fix this.

Share this post


Link to post
Share on other sites
Advertisement
From the Very High Level Layer section of the Python Embedding/Extending guide:
Quote:

Note also that several of these functions take FILE* parameters. On particular issue which needs to be handled carefully is that the FILE structure for different C libraries can be different and incompatible. Under Windows (at least), it is possible for dynamically linked extensions to actually use different libraries, so care should be taken that FILE* parameters are only passed to these functions if it is certain that they were created by the same library that the Python runtime is using.

It's less fragile to read the file into a string and use PyRun_SimpleString() instead of PyRun_SimpleFile().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!