[C++-sig] test/embedding.cpp in bpl 1.31

dique chezdique at yahoo.com
Thu Aug 12 11:02:51 CEST 2004


This is because your program linked with a different
runtime library (msvcr70.dll or msvcr71.dll I guess)
from the one used by Python (msvcrt.dll). The crash, I
supposed, was caused by passing around the FILE*
pointer to different version of runtime library. What
I did was to use the Python built-in function execfile
instead:

using namespace boost::python

void runFile(const char* fileName)
{
    object
mainModule(handle<>(borrowed(PyImport_AddModule("__main__")
)));
    object mainNamespace =
mainModule.attr("__dict__");
    mainNamespace["_pythonscript_file_name"] =
fileName;
    const char execString[] =
        "execfile(_pythonscript_file_name)\n"
        "del _pythonscript_file_name\n";
    try
    {
        object
result(handle<>(PyRun_String(execString,
Py_file_input, mainNamespace.ptr(),
mainNamespace.ptr())));
    }
    catch(error_already_set)
    {
        PyErr_Print();
    }
}

--- Stefan Seefeld <seefeld at sympatico.ca> wrote:

> hi there,
> 
> debugging my applications, I'v had a closer
> look into the test/embedding.cpp file, and I'd
> like to suggest two changes, one cosmetic, and one
> fix.
> 
> The fix concerns line line 108/109, which read:
> 
>      python::object py_base = PythonDerived();
>      Base& py = python::extract<Base&>(py_base)();
> 
> PythonDerived is a type object extracted from
> a python script, so the first line calls the
> constructor. The second takes the instance and
> extracts a 'Base' reference for later use.
> But what's the '()' operator supposed to do in
> that line ? If I remove it, the test still
> performs correctly, so it seems it doesn't
> have any effect (at least in this context).
> Can anybody explain this behavior ?
> 
> The other change concerns the way the
> 'PythonDerived'
> type object is obtained. Instead of running
> 'PyRun_String'
> I'd just extract it from the 'main_namespace'
> dictionary
> after the first script has been evaluated:
> 
>      python::object PythonDerived =
> main_namespace["PythonDerived"];
> 
> Both changes are contained in the attached patch...
> 
> ---
> 
> My original goal looking into this file was to
> understand a crash I get when running 'PyRun_File',
> so I tried to do what the comment in embedding.cpp
> suggests,
> i.e. I put the script defining the 'PythonDerived'
> class into a
> separate file and then use 'PyRun_File' to evaluate
> that script
> instead of 'PyRun_String'. While this works fine on
> linux, the
> application crashes on windows (msvc) somewhere
> inside the PyRun_File
> call.
> 
> I did the appropriate (I think) changes to
> 'test/embedding.cpp' for
> comparison, and I observed the same crash (i.e. just
> recompiling
> the modified 'embedding' test inside the boost build
> system).
> 
> I'd very much appreciate if anybody could either
> confirm this
> behavior on windows (msvc) or show what changes need
> to be done
> to the code to make it work. (I believe this use
> case is sufficiently
> important to warrant a demo /test in its own right.)
> 
> Thanks a lot !
> 
> Stefan
> > 93,98c93
> <     python::handle<> class_ptr( 
> <         PyRun_String("PythonDerived\n",
> Py_eval_input, 
> <                      main_namespace.ptr(),
> main_namespace.ptr()) );
> < 
> <     // Wrap the raw Python object in a
> Boost.Python object
> <     python::object PythonDerived(class_ptr);
> ---
> >     python::object PythonDerived =
> main_namespace["PythonDerived"];
> 109c104
> <     Base& py = python::extract<Base&>(py_base)();
> ---
> >     Base& py = python::extract<Base&>(py_base);
> > _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail



More information about the Cplusplus-sig mailing list