Python: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
#include "stdafx.h" | #include "stdafx.h" | ||
#include "CFUtils.h" | #include "CFUtils.h" | ||
#include "CThreads.h" | |||
#include <Python/Python.h> | #include <Python/Python.h> | ||
#include "CPython.h" | #include "CPython.h" | ||
#define kEmbeddedModuleName "emb" | |||
#define kCustomPrintFuncName "custom_print" | |||
static PyObject* emb_print(PyObject *self, PyObject *args) | static PyObject* emb_print(PyObject *self, PyObject *args) | ||
{ | { | ||
PyObject *resultObjP = NULL; | |||
return | const char *utf8_strZ = NULL; | ||
if (PyArg_ParseTuple(args, "s", &utf8_strZ)) { | |||
Log(utf8_strZ, false); | |||
Py_INCREF(Py_None); | |||
resultObjP = Py_None; | |||
} | |||
return resultObjP; | |||
} | } | ||
static const PyMethodDef EmbMethods[] = { | static const PyMethodDef EmbMethods[] = { | ||
{ | {kCustomPrintFuncName, emb_print, METH_VARARGS, "Calls custom print function."}, | ||
{NULL, NULL, 0, NULL} | {NULL, NULL, 0, NULL} | ||
}; | }; | ||
static const char *s_RedirectPrint = | |||
static const char * | "import " kEmbeddedModuleName "\n" | ||
"import | "import sys\n" | ||
" | |||
"\n" | "\n" | ||
" def write(self, | "class CustomPrintClass:\n" | ||
" | " def write(self, stuff):\n" | ||
" | " " kEmbeddedModuleName "." kCustomPrintFuncName "(stuff)\n" | ||
" | "\n" | ||
" | "sys.stdout = CustomPrintClass()\n" | ||
"sys.stderr = CustomPrintClass()\n" | |||
"\n"; | "\n"; | ||
| Line 39: | Line 50: | ||
Py_Initialize(); | Py_Initialize(); | ||
pyObjectP = Py_InitModule( | pyObjectP = Py_InitModule(kEmbeddedModuleName, const_cast<PyMethodDef*>(EmbMethods)); | ||
ERR_NULL(pyObjectP, tsmUnsupScriptLanguageErr); | ERR_NULL(pyObjectP, tsmUnsupScriptLanguageErr); | ||
// don't DECREF this object?? leave it hanging? | // don't DECREF this object?? leave it hanging? | ||
ERR(PyRun_SimpleString(s_RedirectPrint)); | |||
return err; | return err; | ||
| Line 68: | Line 66: | ||
void CPython_Test() | void CPython_Test() | ||
{ | { | ||
CCritical sc(GetSprintfMutex()); | |||
Log("---Python START:"); | |||
PyRun_SimpleString( | PyRun_SimpleString( | ||
"from time import time, ctime\n" | "from time import time, ctime\n" | ||
"print 'Today is', ctime(time())\n"); | "print 'Today is', ctime(time())\n"); | ||
Log("---Python END:"); | |||
} | } | ||
</pre> | </pre> | ||
Revision as of 19:34, 24 July 2013
// CPython.cpp
#include "stdafx.h"
#include "CFUtils.h"
#include "CThreads.h"
#include <Python/Python.h>
#include "CPython.h"
#define kEmbeddedModuleName "emb"
#define kCustomPrintFuncName "custom_print"
static PyObject* emb_print(PyObject *self, PyObject *args)
{
PyObject *resultObjP = NULL;
const char *utf8_strZ = NULL;
if (PyArg_ParseTuple(args, "s", &utf8_strZ)) {
Log(utf8_strZ, false);
Py_INCREF(Py_None);
resultObjP = Py_None;
}
return resultObjP;
}
static const PyMethodDef EmbMethods[] = {
{kCustomPrintFuncName, emb_print, METH_VARARGS, "Calls custom print function."},
{NULL, NULL, 0, NULL}
};
static const char *s_RedirectPrint =
"import " kEmbeddedModuleName "\n"
"import sys\n"
"\n"
"class CustomPrintClass:\n"
" def write(self, stuff):\n"
" " kEmbeddedModuleName "." kCustomPrintFuncName "(stuff)\n"
"\n"
"sys.stdout = CustomPrintClass()\n"
"sys.stderr = CustomPrintClass()\n"
"\n";
OSStatus CPython_PreAlloc(const char *utf8Z)
{
OSStatus err = noErr;
PyObject *pyObjectP = NULL;
Py_SetProgramName(const_cast<char *>(utf8Z));
Py_Initialize();
pyObjectP = Py_InitModule(kEmbeddedModuleName, const_cast<PyMethodDef*>(EmbMethods));
ERR_NULL(pyObjectP, tsmUnsupScriptLanguageErr);
// don't DECREF this object?? leave it hanging?
ERR(PyRun_SimpleString(s_RedirectPrint));
return err;
}
void CPython_PostDispose()
{
Py_Finalize();
}
void CPython_Test()
{
CCritical sc(GetSprintfMutex());
Log("---Python START:");
PyRun_SimpleString(
"from time import time, ctime\n"
"print 'Today is', ctime(time())\n");
Log("---Python END:");
}