/* * C interface for Miki Tebeka's num2eng (written in Python) * Rick Ratzel * 3/1/2004 * * Copyright (c) 2004 Richard L. Ratzel * see file "COPYRIGHT" for more details... * * Additional information on embedding Python can be found at: * http://www.python.org/doc/ext/embedding.html */ #include static char runString[10000]; static PyObject* evalModule; static PyObject* evalDict; static PyObject* evalVal; extern int Py_VerboseFlag; /* print out all imports, loaded modules, etc. */ extern int Py_NoSiteFlag; /* load the site-wide configuration */ char* num2eng( int num ) { char* retString = NULL; if( !Py_IsInitialized() ) { Py_VerboseFlag = 0; Py_NoSiteFlag = 1; Py_Initialize(); PyRun_SimpleString( (char*)"import num2eng" ); } sprintf( runString, "result = num2eng.num2eng( %d )", num ); PyRun_SimpleString( runString ); /* * evalModule, evalDict, and evalVal are all borrowed references * ...no DECREF calls should be made */ evalModule = PyImport_AddModule( (char*)"__main__" ); evalDict = PyModule_GetDict( evalModule ); evalVal = PyDict_GetItemString( evalDict, "result" ); if( evalVal == NULL ) { PyErr_Print(); exit( 1 ); } else { /* * PyString_AsString returns char* repr of PyObject, which should not be * modified in any way...this should probably be copied for safety */ retString = PyString_AsString( evalVal ); } return retString; }