Python: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 12: | Line 12: | ||
} | } | ||
static PyMethodDef EmbMethods[] = { | static const PyMethodDef EmbMethods[] = { | ||
{"print", emb_print, METH_VARARGS, "Calls custom print function."}, | {"print", emb_print, METH_VARARGS, "Calls custom print function."}, | ||
{NULL, NULL, 0, NULL} | {NULL, NULL, 0, NULL} | ||
| Line 27: | Line 27: | ||
" self.old_stdout.write('CustomPrint called')\n" | " self.old_stdout.write('CustomPrint called')\n" | ||
" text = text.rstrip()\n" | " text = text.rstrip()\n" | ||
" if len(text) | " if len(text) != 0:\n" | ||
" | " emb.print(self, text)\n" | ||
"\n"; | "\n"; | ||
| Line 35: | Line 34: | ||
{ | { | ||
OSStatus err = noErr; | OSStatus err = noErr; | ||
PyObject *pyObjectP = NULL; | |||
PyObject * | |||
Py_SetProgramName(const_cast<char *>(utf8Z)); | Py_SetProgramName(const_cast<char *>(utf8Z)); | ||
Py_Initialize(); | Py_Initialize(); | ||
flags.cf_flags = PyCF_SOURCE_IS_UTF8; | pyObjectP = Py_InitModule("emb", const_cast<PyMethodDef*>(EmbMethods)); | ||
ERR_NULL(pyObjectP, tsmUnsupScriptLanguageErr); | |||
// don't DECREF this object?? leave it hanging? | |||
if (!err) { | |||
PyCompilerFlags flags; | |||
flags.cf_flags = PyCF_SOURCE_IS_UTF8; | |||
pyObjectP = Py_CompileStringFlags(s_CustomPrint, "CustomPrint", Py_single_input, &flags); | |||
ERR_NULL(pyObjectP, tsmUnsupScriptLanguageErr); | |||
} | |||
if (!err) { | if (!err) { | ||
ERR(PySys_SetObject("stdout", | ERR(PySys_SetObject("stdout", pyObjectP)); | ||
ERR(PySys_SetObject("stderr", | ERR(PySys_SetObject("stderr", pyObjectP)); | ||
Py_DECREF( | Py_DECREF(pyObjectP); | ||
} | } | ||
return err; | return err; | ||
| Line 69: | Line 72: | ||
"print 'Today is', ctime(time())\n"); | "print 'Today is', ctime(time())\n"); | ||
} | } | ||
</pre> | </pre> | ||
Revision as of 18:37, 24 July 2013
// CPython.cpp
#include "stdafx.h"
#include "CFUtils.h"
#include <Python/Python.h>
#include "CPython.h"
static PyObject* emb_print(PyObject *self, PyObject *args)
{
Log("emb_print called");
return self;
}
static const PyMethodDef EmbMethods[] = {
{"print", emb_print, METH_VARARGS, "Calls custom print function."},
{NULL, NULL, 0, NULL}
};
// old name was "s_printFunc"
static const char *s_CustomPrint =
"import sys, emb\n"
"class CustomPrint():\n"
" def __init__(self):\n"
" self.old_stdout=sys.stdout\n"
"\n"
" def write(self, text):\n"
" self.old_stdout.write('CustomPrint called')\n"
" text = text.rstrip()\n"
" if len(text) != 0:\n"
" emb.print(self, text)\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("emb", const_cast<PyMethodDef*>(EmbMethods));
ERR_NULL(pyObjectP, tsmUnsupScriptLanguageErr);
// don't DECREF this object?? leave it hanging?
if (!err) {
PyCompilerFlags flags;
flags.cf_flags = PyCF_SOURCE_IS_UTF8;
pyObjectP = Py_CompileStringFlags(s_CustomPrint, "CustomPrint", Py_single_input, &flags);
ERR_NULL(pyObjectP, tsmUnsupScriptLanguageErr);
}
if (!err) {
ERR(PySys_SetObject("stdout", pyObjectP));
ERR(PySys_SetObject("stderr", pyObjectP));
Py_DECREF(pyObjectP);
}
return err;
}
void CPython_PostDispose()
{
Py_Finalize();
}
void CPython_Test()
{
PyRun_SimpleString(
"from time import time, ctime\n"
"print 'Today is', ctime(time())\n");
}