예제 #1
0
void pycon_open(){
	//initialise python console
	Py_Initialize();
	PyRun_SimpleString("print 'python present.'");
	Py_InitModule("tpt", EmbMethods);

	//change the path to find all the correct modules
	PyRun_SimpleString("import sys\nsys.path.append('./tptPython.zip')\nsys.path.append('.')");
	//load the console module and whatnot
#ifdef PYEXT
	PyRun_SimpleString(tpt_console_py);
	printf("using external python console file.\n");
	pname=PyString_FromString("tpt_console");//create string object
	pmodule = PyImport_Import(pname);//import module
	Py_DECREF(pname);//throw away string
#else
	tpt_console_obj = PyMarshal_ReadObjectFromString(tpt_console_pyc+8, sizeof(tpt_console_pyc)-8);
	pmodule=PyImport_ExecCodeModule("tpt_console", tpt_console_obj);
#endif
	
	if (pmodule!=NULL)
	{
		pfunc=PyObject_GetAttrString(pmodule,"handle");//get the handler function
		if (pfunc && PyCallable_Check(pfunc))//check if it's really a function
		{
			printf("python console ready to go.\n");
		}
		else
		{
			PyErr_Print();
			printf("unable to find handle function, mangled console.py?\n");
			pyready = 0;
			pygood = 0;
		}

		pstep=PyObject_GetAttrString(pmodule,"step");//get the handler function
		if (pstep && PyCallable_Check(pstep))//check if it's really a function
		{
			printf("step function found.\n");
		}
		else
		{
			printf("unable to find step function. ignoring.\n");
		}

		pkey=PyObject_GetAttrString(pmodule,"keypress");//get the handler function
		if (pstep && PyCallable_Check(pkey))//check if it's really a function
		{
			printf("key function found.\n");
		}
		else
		{
			printf("unable to find key function. ignoring.\n");
		}
	}
	else
	{
		//sys.stderr
		PyErr_Print();
		printf("unable to find console module, missing file or mangled console.py?\n");
		pyready = 0;
		pygood = 0;
	}
}
/*
 * This routine needs to emulate load_compiled_module in import.c in
 * python, but with DataSectionPtr using FILE*
 */
PyObject* PyResMgrImportLoader::load_compiled_module( const std::string& name,
        BinaryPtr pyc_data, bool known_valid /* = false */ )
{
    std::string compiledExtension = "pyc";
    if ( Py_OptimizeFlag )
        compiledExtension = "pyo";
    std::string moduleName = name;
    std::string::size_type dotPos = name.rfind( "." );
    if ( dotPos != std::string::npos )
    {
        moduleName = name.substr( dotPos + 1 );
    }

    std::string modulePathStub;
    if ( modules_[ name ].first == PKG_DIRECTORY )
    {
        modulePathStub = path_ + "/" + moduleName + "/__init__";
    }
    else
    {
        modulePathStub = path_ + "/" + moduleName;
    }

    // Remove this from the cache. We have it now, and will feed it to Python.
    // This ensures that a reload() call will work correctly.
    modules_.erase( name );

    if ( !known_valid && !check_compiled_module( name, pyc_data ) )
    {
        PyErr_Format( PyExc_ImportError,
                      "%s.%s is not a valid Python Object file",
                      modulePathStub.c_str(), compiledExtension.c_str()
                    );
        return NULL;
    }

    // The first four bytes are magic
    // the second four are the source modification date
    // This does the same thing as read_compiled_module in import.c
    PyObject* codeObject = PyMarshal_ReadObjectFromString(
                               pyc_data->cdata() + 8, pyc_data->len() - 8 );
    if ( !PyCode_Check( codeObject ) )
    {
        PyErr_Format( PyExc_ImportError,
                      "%s.%s is a non-code object",
                      modulePathStub.c_str(), compiledExtension.c_str()
                    );
        Py_DECREF( codeObject );
        return NULL;
    }
    PyObject* module = PyImport_ExecCodeModuleEx(
                           const_cast< char* >( name.c_str() ), codeObject,
                           const_cast< char* >( ( modulePathStub + compiledExtension ).c_str() )
                       );
    Py_DECREF( codeObject );

    if ( module )
    {
        PyObject *moduleDict = PyModule_GetDict( module );
        int err = PyDict_SetItemString( moduleDict, "__loader__", this );
        if ( err != 0 )
        {
            Py_DECREF( module );
            return NULL;
        }

//		TRACE_MSG( "PyResMgrImportLoader(%s)::load_compiled_module: loaded %s\n",
//			path_.c_str(), name.c_str() );
    }
    return module;
}