Python: Difference between revisions

From kJams Wiki
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)
{
{
Log("emb_print called");
PyObject *resultObjP = NULL;
return self;
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[] = {
     {"print", emb_print, METH_VARARGS, "Calls custom print function."},
     {kCustomPrintFuncName, emb_print, METH_VARARGS, "Calls custom print function."},
     {NULL, NULL, 0, NULL}
     {NULL, NULL, 0, NULL}
};
};


// old name was "s_printFunc"
static const char *s_RedirectPrint =  
static const char *s_CustomPrint =  
"import " kEmbeddedModuleName "\n"
"import sys, emb\n"
"import sys\n"
"class CustomPrint():\n"
" def __init__(self):\n"
" self.old_stdout=sys.stdout\n"
"\n"
"\n"
" def write(self, text):\n"
"class CustomPrintClass:\n"
" self.old_stdout.write('CustomPrint called')\n"
" def write(self, stuff):\n"
" text = text.rstrip()\n"
" " kEmbeddedModuleName "." kCustomPrintFuncName "(stuff)\n"
" if len(text) != 0:\n"
"\n"
" emb.print(self, text)\n"
"sys.stdout = CustomPrintClass()\n"
"sys.stderr = CustomPrintClass()\n"
"\n";
"\n";


Line 39: Line 50:
Py_Initialize();
Py_Initialize();


pyObjectP = Py_InitModule("emb", const_cast<PyMethodDef*>(EmbMethods));
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?
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);
ERR(PyRun_SimpleString(s_RedirectPrint));
}
if (!err) {
ERR(PySys_SetObject("stdout", pyObjectP));
ERR(PySys_SetObject("stderr", pyObjectP));
Py_DECREF(pyObjectP);
}
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:");
}