您的位置:首页 > 编程语言 > Python开发

python 解释器, 扩展模块

2011-03-31 07:44 232 查看
// MyTest.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "python.h"

#include <process.h>

#include <windows.h>

#include <io.h>

#include <Fcntl.h>

#include <python-ast.h>

#include <string>

using namespace std;

unsigned int __stdcall PythonRoute( void * pData )

{

::PyEval_AcquireLock();

PyThreadState * pState = ::Py_NewInterpreter();

PyEval_SaveThread();

for( int i = 0; i < 100000; i++ )

{

// PyRun_SimpleString( "pass" );

printf( "++%d/n", i );

Sleep( 500 );

}

//int nRet = PyRun_SimpleString( "import sys" );

//nRet = PyRun_SimpleString( "sys.path.append( /"Z:////Python313Src////Python-3.1.3////tspy/" )" );

//PyObject * pModule = ::PyImport_ImportModule( "AA" );

//PyObject * pErr = ::PyErr_Occurred();

//if( pErr != NULL )

//{

// PyObject *exception, *v, *tb;

// ::PyErr_Fetch( &exception, &v, &tb );

// PyErr_NormalizeException(&exception, &v, &tb);

// if (tb == NULL) {

// tb = Py_None;

// Py_INCREF(tb);

// }

// PyException_SetTraceback(v, tb);

// PyObject *m1 = PyImport_AddModule("io");

// PyObject *dict = PyModule_GetDict(m1);

// PyObject *pType = PyDict_GetItemString( dict, "StringIO" );

// PyObject*a = PyTuple_New(0);

// PyObject*pObj = ::PyObject_Call( pType, a, NULL );

//

// PyObject * p1 = ::PySys_GetObject( "stderr" );

// PyObject * p2 = ::PySys_GetObject( "__stderr__" );

// ::PySys_SetObject( "stderr", pObj );

// ::PySys_SetObject( "__stderr__", pObj );

// ::PyErr_Display( exception, v, tb );

// ::PySys_SetObject( "stderr", p1 );

// ::PySys_SetObject( "__stderr__", p2 );

// PyObject* pD = PyObject_Dir(pObj) ;

// PyObject * pFunc = PyObject_GenericGetAttr(pObj, PyUnicode_FromString("getvalue") );

// PyObject * pUStr = PyObject_CallObject( pFunc, a );

// PyObject * pAStr = ::PyUnicode_AsASCIIString( pUStr );

// char * str = ::PyBytes_AsString( pAStr );

// Py_DECREF(pObj);

// PyErr_Restore(exception, v, tb);

//}

PyEval_RestoreThread(pState);

Py_EndInterpreter( pState );

::PyEval_ReleaseLock();

return 0;

}

int _tmain(int argc, _TCHAR* argv[])

{

::Py_Initialize();

PyEval_InitThreads();

PyThreadState * p =PyEval_SaveThread();

// PyCodeObject *co = NULL;

// struct _mod *mod;

// PyCompilerFlags flags;

// PyArena *arena = PyArena_New();

// if (arena == NULL)

// return NULL;

//FILE *fp;

//fopen_s( &fp, "Z://Python313Src//Python-3.1.3//tspy//AA.py" , "r" );

// flags.cf_flags = 0;

// mod = PyParser_ASTFromFile(fp, "Z://Python313Src//Python-3.1.3//tspy//AA.py", NULL,

// Py_file_input, 0, 0, &flags,

// NULL, arena);

//asdl_seq * pseq = mod->v.Module.body;

//for( int i = 0; i < asdl_seq_LEN( pseq ); i++ )

//{

// _stmt * pstmt = (_stmt * )asdl_seq_GET( pseq, i );

// if( pstmt->kind == FunctionDef_kind )

// {

// PyObject * pp = pstmt->v.FunctionDef.name;

// PyObject * pp1 = ::PyUnicode_AsASCIIString( pp );

// char * s = ::PyBytes_AsString( pp1 );

// printf(s );

// asdl_seq * pseq1 = pstmt->v.FunctionDef.body;

// _stmt * pstmt1 = (_stmt * )asdl_seq_GET( pseq1, 0 );

// if( pstmt1->kind == Expr_kind )

// {

// int k =1;

// }

// }

//}

HANDLE hThread=NULL;;

unsigned threadID;

printf( "Creating second thread.../n" );

// Create the second thread.

hThread = (HANDLE)_beginthreadex( NULL, 0, &PythonRoute, NULL, 0, &threadID );

for( int i = 0; i < 100000; i++ )

{

PyEval_RestoreThread( p );

PyRun_SimpleString( "pass" );

PyEval_SaveThread();

printf( "--%d/n", i );

Sleep( 500 );

}

// Wait until second thread terminates. If you comment out the line

// below, Counter will not be correct because the thread has not

// terminated, and Counter most likely has not been incremented to

// 1000000 yet.

WaitForSingleObject( hThread, INFINITE );

// Destroy the thread object.

CloseHandle( hThread );

Py_Finalize();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: