python 解释器, 扩展模块

 

// 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:Python313SrcPython-3.1.3tspy/" )" );

 

//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;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值