port(2P step 2): ScriptLib/PythonLauncher on the embedded interpreter

Link mtpython into port_logic and copy 40250's ScriptLib launcher across:
PythonLauncher, PythonUtils and PythonMarshal, plus the 12 forwarding headers
that make its `#include <Python-2.7/*>` lines compile unchanged. symtable.h
forward-declares mod_ty rather than including Python-ast.h, whose one-word
macros (Delete, Module, Set, Print) collide with EterLib/Pool.h — the same
clash 40250 sidestepped by commenting out PySymtable_Build.

Two `// PORT:` edits: `unsigned char(val)` is an MSVC extension (PythonUtils.cpp,
twice), and PyObject_AsCharBuffer wants a Py_ssize_t* (TraceFunc).

port_python_launcher_test drives the launcher in the order UserInterface.cpp
does (:241-434): Create(), __DEBUG__/__COMMAND_LINE__, TRUE/FALSE, a CRLF script
through RunMemoryTextFile, a .pyc through RunCompiledFile, then RunFile reading
system.py out of the real pack. It stops at `ImportError: No module named app`,
i.e. the first C++ module RunMainScript registers — that is the 2V0 slice, and
the stdlib behind it is step 3. The host flags (Py_NoSiteFlag and friends) sit
in the test for now; step 3 moves them into the platform layer.

port-map statuses are set only for what the test actually executes: 10 of 12
launcher functions and 4 of 11 marshal ones (the small-.pyc path delegates to
CPython's own reader, so the ported r_object stays unreached). PythonUtils
compiles but nothing calls it until the binding modules arrive, so it stays TODO.

Windows has no mtpython yet, so port_logic drops ScriptLib and the shims there.

Tests: macOS ctest 27/27 incl. port.python_launcher; port_logic (with ScriptLib)
also compiles for android arm64 (NDK API 24) and ios arm64.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-09-23 08:54:48 +09:00
co-authored by Claude Opus 5
parent 5fad769ee8
commit 43bb561551
28 changed files with 1879 additions and 8 deletions
+21 -1
View File
@@ -9,6 +9,12 @@ if(WIN32)
# Stand-ins for Windows SDK headers; on Windows the real ones are used.
list(FILTER MT_PORT_HEADERS EXCLUDE REGEX "/common/shim/win32/")
endif()
# The script layer needs the embedded interpreter (mt3p::python), which has no Windows build yet
# (PORT-PLAN 批次 2P step 4). Without it, ScriptLib and its <Python-2.7/*> shims are left out.
if(NOT TARGET mtpython)
list(FILTER MT_PORT_SOURCES EXCLUDE REGEX "/ScriptLib/")
list(FILTER MT_PORT_HEADERS EXCLUDE REGEX "/ScriptLib/|/common/shim/sdk/Python-2\\.7/")
endif()
add_library(port_logic STATIC ${MT_PORT_SOURCES})
target_include_directories(port_logic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
@@ -20,8 +26,11 @@ if(NOT WIN32)
endif()
target_include_directories(port_logic PUBLIC ${MT_PORT_SHIMS})
# Libraries 40250 links that are vendored as-is: <lzo/lzo1x.h> (EterBase/lzo.h), <cryptopp/*>
# (EterBase/cipher.h).
# (EterBase/cipher.h), <Python-2.7/*> (ScriptLib).
target_link_libraries(port_logic PUBLIC mt3p::minilzo mt3p::cryptopp)
if(TARGET mtpython)
target_link_libraries(port_logic PUBLIC mt3p::python)
endif()
# 40250 sources carry MSVC `#pragma warning(...)` lines verbatim, and non-literal printf formats
# (EterImageLib/StdAfx.h) that the Android toolchain turns into errors.
set(MT_PORT_WARNING_FLAGS)
@@ -123,6 +132,9 @@ endforeach()
add_library(port_header_gate OBJECT ${MT_PORT_GATE_SOURCES})
target_include_directories(port_header_gate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${MT_PORT_SHIMS})
target_link_libraries(port_header_gate PRIVATE mt3p::minilzo mt3p::cryptopp)
if(TARGET mtpython)
target_link_libraries(port_header_gate PRIVATE mt3p::python)
endif()
target_compile_features(port_header_gate PRIVATE cxx_std_20)
target_compile_definitions(port_header_gate PRIVATE "NANOBEGIN=" "NANOEND=")
if(MINGW)
@@ -148,4 +160,12 @@ if(BUILD_TESTING AND CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME)
target_link_libraries(port_eterpack_test PRIVATE port_platform)
add_test(NAME port.eterpack COMMAND $<TARGET_FILE:port_eterpack_test> ${MT_40250_CLIENT})
set_tests_properties(port.eterpack PROPERTIES SKIP_RETURN_CODE 77)
if(TARGET mtpython)
add_executable(port_python_launcher_test ${CMAKE_CURRENT_SOURCE_DIR}/../../tests/port_python_launcher_test.cpp)
target_link_libraries(port_python_launcher_test PRIVATE port_platform)
add_test(NAME port.python_launcher
COMMAND $<TARGET_FILE:port_python_launcher_test> ${MT_40250_CLIENT})
set_tests_properties(port.python_launcher PROPERTIES SKIP_RETURN_CODE 77)
endif()
endif()
@@ -0,0 +1,282 @@
#include "StdAfx.h"
#include <Python-2.7/frameobject.h>
#include "../EterPack/EterPackManager.h"
#include "PythonLauncher.h"
CPythonLauncher::CPythonLauncher()
{
Py_Initialize();
}
CPythonLauncher::~CPythonLauncher()
{
Clear();
}
void CPythonLauncher::Clear()
{
Py_Finalize();
}
std::string g_stTraceBuffer[512];
int g_nCurTraceN = 0;
void Traceback()
{
std::string str;
for (int i = 0; i < g_nCurTraceN; ++i)
{
str.append(g_stTraceBuffer[i]);
str.append("\n");
}
PyObject * exc;
PyObject * v;
PyObject * tb;
const char * errStr;
PyErr_Fetch(&exc, &v, &tb);
if (PyString_Check(v))
{
errStr = PyString_AS_STRING(v);
str.append("Error: ");
str.append(errStr);
Tracef("%s\n", errStr);
}
Py_DECREF(exc);
Py_DECREF(v);
Py_DECREF(tb);
LogBoxf("Traceback:\n\n%s\n", str.c_str());
}
int TraceFunc(PyObject * obj, PyFrameObject * f, int what, PyObject *arg)
{
const char * funcname;
char szTraceBuffer[128];
switch (what)
{
case PyTrace_CALL:
if (g_nCurTraceN >= 512)
return 0;
if (Py_OptimizeFlag)
f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
funcname = PyString_AsString(f->f_code->co_name);
_snprintf(szTraceBuffer, sizeof(szTraceBuffer), "Call: File \"%s\", line %d, in %s",
PyString_AsString(f->f_code->co_filename),
f->f_lineno,
funcname);
g_stTraceBuffer[g_nCurTraceN++]=szTraceBuffer;
break;
case PyTrace_RETURN:
if (g_nCurTraceN > 0)
--g_nCurTraceN;
break;
case PyTrace_EXCEPTION:
if (g_nCurTraceN >= 512)
return 0;
PyObject * exc_type, * exc_value, * exc_traceback;
PyTuple_GetObject(arg, 0, &exc_type);
PyTuple_GetObject(arg, 1, &exc_value);
PyTuple_GetObject(arg, 2, &exc_traceback);
Py_ssize_t len; // PORT: PyObject_AsCharBuffer takes Py_ssize_t*, which is `int` only in a 32-bit build
const char * exc_str;
PyObject_AsCharBuffer(exc_type, &exc_str, &len);
_snprintf(szTraceBuffer, sizeof(szTraceBuffer), "Exception: File \"%s\", line %d, in %s",
PyString_AS_STRING(f->f_code->co_filename),
f->f_lineno,
PyString_AS_STRING(f->f_code->co_name));
g_stTraceBuffer[g_nCurTraceN++]=szTraceBuffer;
break;
}
return 0;
}
void CPythonLauncher::SetTraceFunc(int (*pFunc)(PyObject * obj, PyFrameObject * f, int what, PyObject *arg))
{
PyEval_SetTrace(pFunc, NULL);
}
bool CPythonLauncher::Create(const char* c_szProgramName)
{
NANOBEGIN
Py_SetProgramName((char*)c_szProgramName);
#ifdef _DEBUG
PyEval_SetTrace(TraceFunc, NULL);
#endif
m_poModule = PyImport_AddModule((char *) "__main__");
if (!m_poModule)
return false;
m_poDic = PyModule_GetDict(m_poModule);
PyObject * builtins = PyImport_ImportModule("__builtin__");
PyModule_AddIntConstant(builtins, "TRUE", 1);
PyModule_AddIntConstant(builtins, "FALSE", 0);
PyDict_SetItemString(m_poDic, "__builtins__", builtins);
Py_DECREF(builtins);
if (!RunLine("import __main__"))
return false;
if (!RunLine("import sys"))
return false;
NANOEND
return true;
}
bool CPythonLauncher::RunCompiledFile(const char* c_szFileName)
{
NANOBEGIN
FILE * fp = fopen(c_szFileName, "rb");
if (!fp)
return false;
PyCodeObject *co;
PyObject *v;
long magic;
long PyImport_GetMagicNumber(void);
magic = _PyMarshal_ReadLongFromFile(fp);
if (magic != PyImport_GetMagicNumber())
{
PyErr_SetString(PyExc_RuntimeError, "Bad magic number in .pyc file");
fclose(fp);
return false;
}
_PyMarshal_ReadLongFromFile(fp);
v = _PyMarshal_ReadLastObjectFromFile(fp);
fclose(fp);
if (!v || !PyCode_Check(v))
{
Py_XDECREF(v);
PyErr_SetString(PyExc_RuntimeError, "Bad code object in .pyc file");
return false;
}
co = (PyCodeObject *) v;
v = PyEval_EvalCode(co, m_poDic, m_poDic);
/* if (v && flags)
flags->cf_flags |= (co->co_flags & PyCF_MASK);*/
Py_DECREF(co);
if (!v)
{
Traceback();
return false;
}
Py_DECREF(v);
if (Py_FlushLine())
PyErr_Clear();
NANOEND
return true;
}
bool CPythonLauncher::RunMemoryTextFile(const char* c_szFileName, UINT uFileSize, const VOID* c_pvFileData)
{
NANOBEGIN
const CHAR* c_pcFileData=(const CHAR*)c_pvFileData;
std::string stConvFileData;
stConvFileData.reserve(uFileSize);
stConvFileData+="exec(compile('''";
// ConvertPythonTextFormat
{
for (UINT i=0; i<uFileSize; ++i)
{
if (c_pcFileData[i]!=13)
stConvFileData+=c_pcFileData[i];
}
}
stConvFileData+= "''', ";
stConvFileData+= "'";
stConvFileData+= c_szFileName;
stConvFileData+= "', ";
stConvFileData+= "'exec'))";
const CHAR* c_pcConvFileData=stConvFileData.c_str();
NANOEND
return RunLine(c_pcConvFileData);
}
bool CPythonLauncher::RunFile(const char* c_szFileName)
{
char* acBufData=NULL;
DWORD dwBufSize=0;
{
CMappedFile file;
const VOID* pvData;
CEterPackManager::Instance().Get(file, c_szFileName, &pvData);
dwBufSize=file.Size();
if (dwBufSize==0)
return false;
acBufData=new char[dwBufSize];
memcpy(acBufData, pvData, dwBufSize);
}
bool ret=false;
ret=RunMemoryTextFile(c_szFileName, dwBufSize, acBufData);
delete [] acBufData;
return ret;
}
bool CPythonLauncher::RunLine(const char* c_szSrc)
{
PyObject * v = PyRun_String((char *) c_szSrc, Py_file_input, m_poDic, m_poDic);
if (!v)
{
Traceback();
return false;
}
Py_DECREF(v);
return true;
}
const char* CPythonLauncher::GetError()
{
PyObject* exc;
PyObject* v;
PyObject* tb;
PyErr_Fetch(&exc, &v, &tb);
if (PyString_Check(v))
return PyString_AS_STRING(v);
return "";
}
@@ -0,0 +1,25 @@
#pragma once
#include <Python-2.7/frameobject.h>
#include "../EterBase/Singleton.h"
class CPythonLauncher : public CSingleton<CPythonLauncher>
{
public:
CPythonLauncher();
virtual ~CPythonLauncher();
void Clear();
bool Create(const char* c_szProgramName="eter.python");
void SetTraceFunc(int (*pFunc)(PyObject * obj, PyFrameObject * f, int what, PyObject *arg));
bool RunLine(const char* c_szLine);
bool RunFile(const char* c_szFileName);
bool RunMemoryTextFile(const char* c_szFileName, UINT uFileSize, const VOID* c_pvFileData);
bool RunCompiledFile(const char* c_szFileName);
const char* GetError();
protected:
PyObject* m_poModule;
PyObject* m_poDic;
};
@@ -0,0 +1,478 @@
/* Write Python objects to files and read them back.
This is intended for writing and reading compiled Python code only;
a true persistent storage facility would be much harder, since
it would have to take circular links and sharing into account. */
#include "StdAfx.h"
#include <Python-2.7/longintrepr.h>
/* High water mark to determine when the marshalled object is dangerously deep
* and risks coring the interpreter. When the object stack gets this deep,
* raise an exception instead of continuing.
*/
#define MAX_MARSHAL_STACK_DEPTH 5000
#define TYPE_NULL '0'
#define TYPE_NONE 'N'
#define TYPE_STOPITER 'S'
#define TYPE_ELLIPSIS '.'
#define TYPE_INT 'i'
#define TYPE_INT64 'I'
#define TYPE_FLOAT 'f'
#define TYPE_COMPLEX 'x'
#define TYPE_LONG 'l'
#define TYPE_STRING 's'
#define TYPE_TUPLE '('
#define TYPE_LIST '['
#define TYPE_DICT '{'
#define TYPE_CODE 'c'
#define TYPE_UNICODE 'u'
#define TYPE_UNKNOWN '?'
typedef struct
{
FILE * fp;
int error;
int depth;
PyObject * str;
char * ptr;
char * end;
} WFILE;
typedef WFILE RFILE; /* Same struct with different invariants */
#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
static int r_string(char *s, int n, RFILE *p)
{
if (p->fp != NULL)
return fread(s, 1, n, p->fp);
if (p->end - p->ptr < n)
n = p->end - p->ptr;
memcpy(s, p->ptr, n);
p->ptr += n;
return n;
}
static int r_short(RFILE *p)
{
short x;
x = (short) r_byte(p);
x |= (short) r_byte(p) << 8;
/* Sign-extension, in case short greater than 16 bits */
x |= -(x & 0x8000);
return x;
}
static long r_long(RFILE *p)
{
long x;
FILE *fp = p->fp;
if (fp) {
x = getc(fp);
x |= (long)getc(fp) << 8;
x |= (long)getc(fp) << 16;
x |= (long)getc(fp) << 24;
}
else {
x = rs_byte(p);
x |= (long)rs_byte(p) << 8;
x |= (long)rs_byte(p) << 16;
x |= (long)rs_byte(p) << 24;
}
#if SIZEOF_LONG > 4
/* Sign extension for 64-bit machines */
x |= -(x & 0x80000000L);
#endif
return x;
}
/* r_long64 deals with the TYPE_INT64 code. On a machine with
sizeof(long) > 4, it returns a Python int object, else a Python long
object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
so there's no inefficiency here in returning a PyLong on 32-bit boxes
for everything written via TYPE_INT64 (i.e., if an int is written via
TYPE_INT64, it *needs* more than 32 bits).
*/
static PyObject * r_long64(RFILE *p)
{
long lo4 = r_long(p);
long hi4 = r_long(p);
#if SIZEOF_LONG > 4
long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
return PyInt_FromLong(x);
#else
unsigned char buf[8];
int one = 1;
int is_little_endian = (int)*(char*)&one;
if (is_little_endian) {
memcpy(buf, &lo4, 4);
memcpy(buf+4, &hi4, 4);
}
else {
memcpy(buf, &hi4, 4);
memcpy(buf+4, &lo4, 4);
}
return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
#endif
}
static PyObject * r_object(RFILE *p)
{
PyObject *v, *v2;
long i, n;
int type = r_byte(p);
switch (type) {
case EOF:
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
return NULL;
case TYPE_NULL:
return NULL;
case TYPE_NONE:
Py_INCREF(Py_None);
return Py_None;
case TYPE_STOPITER:
Py_INCREF(PyExc_StopIteration);
return PyExc_StopIteration;
case TYPE_ELLIPSIS:
Py_INCREF(Py_Ellipsis);
return Py_Ellipsis;
case TYPE_INT:
return PyInt_FromLong(r_long(p));
case TYPE_INT64:
return r_long64(p);
case TYPE_LONG:
{
int size;
PyLongObject* ob;
n = r_long(p);
size = n<0 ? -n : n;
ob = _PyLong_New(size);
if (ob == NULL)
return NULL;
ob->ob_size = n;
for (i = 0; i < size; i++)
ob->ob_digit[i] = (short) r_short(p);
return (PyObject *) ob;
}
case TYPE_FLOAT:
{
char buf[256];
double dx;
n = r_byte(p);
if (r_string(buf, (int)n, p) != n) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
return NULL;
}
buf[n] = '\0';
PyFPE_START_PROTECT("atof", return 0)
dx = atof(buf);
PyFPE_END_PROTECT(dx)
return PyFloat_FromDouble(dx);
}
#ifndef WITHOUT_COMPLEX
case TYPE_COMPLEX:
{
char buf[256];
Py_complex c;
n = r_byte(p);
if (r_string(buf, (int)n, p) != n) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
return NULL;
}
buf[n] = '\0';
PyFPE_START_PROTECT("atof", return 0)
c.real = atof(buf);
PyFPE_END_PROTECT(c)
n = r_byte(p);
if (r_string(buf, (int)n, p) != n) {
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
return NULL;
}
buf[n] = '\0';
PyFPE_START_PROTECT("atof", return 0)
c.imag = atof(buf);
PyFPE_END_PROTECT(c)
return PyComplex_FromCComplex(c);
}
#endif
case TYPE_STRING:
n = r_long(p);
if (n < 0) {
PyErr_SetString(PyExc_ValueError, "bad marshal data");
return NULL;
}
v = PyString_FromStringAndSize((char *)NULL, n);
if (v != NULL) {
if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
Py_DECREF(v);
v = NULL;
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
}
}
return v;
#ifdef Py_USING_UNICODE
case TYPE_UNICODE:
{
char *buffer;
n = r_long(p);
if (n < 0) {
PyErr_SetString(PyExc_ValueError, "bad marshal data");
return NULL;
}
buffer = PyMem_NEW(char, n);
if (buffer == NULL)
return PyErr_NoMemory();
if (r_string(buffer, (int)n, p) != n) {
PyMem_DEL(buffer);
PyErr_SetString(PyExc_EOFError,
"EOF read where object expected");
return NULL;
}
v = PyUnicode_DecodeUTF8(buffer, n, NULL);
PyMem_DEL(buffer);
return v;
}
#endif
case TYPE_TUPLE:
n = r_long(p);
if (n < 0) {
PyErr_SetString(PyExc_ValueError, "bad marshal data");
return NULL;
}
v = PyTuple_New((int)n);
if (v == NULL)
return v;
for (i = 0; i < n; i++) {
v2 = r_object(p);
if ( v2 == NULL ) {
Py_DECREF(v);
v = NULL;
break;
}
PyTuple_SET_ITEM(v, (int)i, v2);
}
return v;
case TYPE_LIST:
n = r_long(p);
if (n < 0) {
PyErr_SetString(PyExc_ValueError, "bad marshal data");
return NULL;
}
v = PyList_New((int)n);
if (v == NULL)
return v;
for (i = 0; i < n; i++) {
v2 = r_object(p);
if ( v2 == NULL ) {
Py_DECREF(v);
v = NULL;
break;
}
PyList_SetItem(v, (int)i, v2);
}
return v;
case TYPE_DICT:
v = PyDict_New();
if (v == NULL)
return NULL;
for (;;) {
PyObject *key, *val;
key = r_object(p);
if (key == NULL)
break; /* XXX Assume TYPE_NULL, not an error */
val = r_object(p);
if (val != NULL)
PyDict_SetItem(v, key, val);
Py_DECREF(key);
Py_XDECREF(val);
}
return v;
case TYPE_CODE:
if (PyEval_GetRestricted()) {
PyErr_SetString(PyExc_RuntimeError,
"cannot unmarshal code objects in "
"restricted execution mode");
return NULL;
}
else {
int argcount = r_short(p);
int nlocals = r_short(p);
int stacksize = r_short(p);
int flags = r_short(p);
PyObject *code = NULL;
PyObject *consts = NULL;
PyObject *names = NULL;
PyObject *varnames = NULL;
PyObject *freevars = NULL;
PyObject *cellvars = NULL;
PyObject *filename = NULL;
PyObject *name = NULL;
int firstlineno = 0;
PyObject *lnotab = NULL;
code = r_object(p);
if (code) consts = r_object(p);
if (consts) names = r_object(p);
if (names) varnames = r_object(p);
if (varnames) freevars = r_object(p);
if (freevars) cellvars = r_object(p);
if (cellvars) filename = r_object(p);
if (filename) name = r_object(p);
if (name) {
firstlineno = r_short(p);
lnotab = r_object(p);
}
if (!PyErr_Occurred()) {
v = (PyObject *) PyCode_New(
argcount, nlocals, stacksize, flags,
code, consts, names, varnames,
freevars, cellvars, filename, name,
firstlineno, lnotab);
}
else
v = NULL;
Py_XDECREF(code);
Py_XDECREF(consts);
Py_XDECREF(names);
Py_XDECREF(varnames);
Py_XDECREF(freevars);
Py_XDECREF(cellvars);
Py_XDECREF(filename);
Py_XDECREF(name);
Py_XDECREF(lnotab);
}
return v;
default:
/* Bogus data got written, which isn't ideal.
This will let you keep working and recover. */
PyErr_SetString(PyExc_ValueError, "bad marshal data");
return NULL;
}
}
int _PyMarshal_ReadShortFromFile(FILE *fp)
{
RFILE rf;
rf.fp = fp;
return r_short(&rf);
}
long _PyMarshal_ReadLongFromFile(FILE *fp)
{
RFILE rf;
rf.fp = fp;
return r_long(&rf);
}
/* Return size of file in bytes; < 0 if unknown. */
static off_t getfilesize(FILE *fp)
{
struct stat st;
if (fstat(fileno(fp), &st) != 0)
return -1;
else
return st.st_size;
}
/* If we can get the size of the file up-front, and it's reasonably small,
* read it in one gulp and delegate to ...FromString() instead. Much quicker
* than reading a byte at a time from file; speeds .pyc imports.
* CAUTION: since this may read the entire remainder of the file, don't
* call it unless you know you're done with the file.
*/
PyObject *_PyMarshal_ReadLastObjectFromFile(FILE *fp)
{
/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
*/
#define SMALL_FILE_LIMIT (1L << 14)
#define REASONABLE_FILE_LIMIT (1L << 18)
off_t filesize;
if (PyErr_Occurred())
{
fprintf(stderr, "XXX rd_object called with exception set\n");
return NULL;
}
filesize = getfilesize(fp);
if (filesize > 0) {
char buf[SMALL_FILE_LIMIT];
char* pBuf = NULL;
if (filesize <= SMALL_FILE_LIMIT)
pBuf = buf;
else if (filesize <= REASONABLE_FILE_LIMIT)
pBuf = (char *)PyMem_MALLOC(filesize);
if (pBuf != NULL) {
PyObject* v;
size_t n = fread(pBuf, 1, filesize, fp);
v = PyMarshal_ReadObjectFromString(pBuf, n);
if (pBuf != buf)
PyMem_FREE(pBuf);
return v;
}
}
/* We don't have fstat, or we do but the file is larger than
* REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
*/
return _PyMarshal_ReadObjectFromFile(fp);
#undef SMALL_FILE_LIMIT
#undef REASONABLE_FILE_LIMIT
}
PyObject * _PyMarshal_ReadObjectFromFile(FILE *fp)
{
RFILE rf;
if (PyErr_Occurred()) {
fprintf(stderr, "XXX rd_object called with exception set\n");
return NULL;
}
rf.fp = fp;
return r_object(&rf);
}
PyObject * _PyMarshal_ReadObjectFromString(char *str, int len)
{
RFILE rf;
if (PyErr_Occurred()) {
fprintf(stderr, "XXX rds_object called with exception set\n");
return NULL;
}
rf.fp = NULL;
rf.str = NULL;
rf.ptr = str;
rf.end = str + len;
return r_object(&rf);
}
@@ -0,0 +1,8 @@
#ifndef __INC_ETERMARSHAL_H__
#define __INC_ETERMARSHAL_H__
extern PyObject * _PyMarshal_ReadObjectFromFile(FILE* fp);
extern PyObject * _PyMarshal_ReadLastObjectFromFile(FILE* fp);
extern long _PyMarshal_ReadLongFromFile(FILE *fp);
#endif
@@ -0,0 +1,425 @@
#include "StdAfx.h"
#include "PythonUtils.h"
IPythonExceptionSender * g_pkExceptionSender = NULL;
bool __PyCallClassMemberFunc_ByCString(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, PyObject** poRet);
bool __PyCallClassMemberFunc_ByPyString(PyObject* poClass, PyObject* poFuncName, PyObject* poArgs, PyObject** poRet);
bool __PyCallClassMemberFunc(PyObject* poClass, PyObject* poFunc, PyObject* poArgs, PyObject** poRet);
PyObject * Py_BadArgument()
{
PyErr_BadArgument();
return NULL;
}
PyObject * Py_BuildException(const char * c_pszErr, ...)
{
if (!c_pszErr)
PyErr_Clear();
else
{
char szErrBuf[512+1];
va_list args;
va_start(args, c_pszErr);
vsnprintf(szErrBuf, sizeof(szErrBuf), c_pszErr, args);
va_end(args);
PyErr_SetString(PyExc_RuntimeError, szErrBuf);
}
return Py_BuildNone();
//return NULL;
}
PyObject * Py_BuildNone()
{
Py_INCREF(Py_None);
return Py_None;
}
void Py_ReleaseNone()
{
Py_DECREF(Py_None);
}
bool PyTuple_GetObject(PyObject* poArgs, int pos, PyObject** ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = poItem;
return true;
}
bool PyTuple_GetLong(PyObject* poArgs, int pos, long* ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = PyLong_AsLong(poItem);
return true;
}
bool PyTuple_GetDouble(PyObject* poArgs, int pos, double* ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = PyFloat_AsDouble(poItem);
return true;
}
bool PyTuple_GetFloat(PyObject* poArgs, int pos, float* ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = float(PyFloat_AsDouble(poItem));
return true;
}
bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret)
{
int val;
bool result = PyTuple_GetInteger(poArgs,pos,&val);
*ret = (unsigned char) val; // PORT: `unsigned char(val)` is an MSVC extension; C++ has no multi-word function-style cast
return result;
}
bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned char* ret)
{
int val;
bool result = PyTuple_GetInteger(poArgs,pos,&val);
*ret = (unsigned char) val; // PORT: `unsigned char(val)` is an MSVC extension; C++ has no multi-word function-style cast
return result;
}
bool PyTuple_GetInteger(PyObject* poArgs, int pos, WORD* ret)
{
int val;
bool result = PyTuple_GetInteger(poArgs,pos,&val);
*ret = WORD(val);
return result;
}
bool PyTuple_GetInteger(PyObject* poArgs, int pos, int* ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = PyLong_AsLong(poItem);
return true;
}
bool PyTuple_GetUnsignedLong(PyObject* poArgs, int pos, unsigned long* ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = PyLong_AsUnsignedLong(poItem);
return true;
}
bool PyTuple_GetUnsignedInteger(PyObject* poArgs, int pos, unsigned int* ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = PyLong_AsUnsignedLong(poItem);
return true;
}
bool PyTuple_GetString(PyObject* poArgs, int pos, char** ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
if (!PyString_Check(poItem))
return false;
*ret = PyString_AsString(poItem);
return true;
}
bool PyTuple_GetBoolean(PyObject* poArgs, int pos, bool* ret)
{
if (pos >= PyTuple_Size(poArgs))
return false;
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
if (!poItem)
return false;
*ret = PyLong_AsLong(poItem) ? true : false;
return true;
}
bool PyCallClassMemberFunc(PyObject* poClass, PyObject* poFunc, PyObject* poArgs)
{
PyObject* poRet;
// NOTE : NULL 체크 추가.. - [levites]
if (!poClass)
{
Py_XDECREF(poArgs);
return false;
}
if (!__PyCallClassMemberFunc(poClass, poFunc, poArgs, &poRet))
return false;
Py_DECREF(poRet);
return true;
}
bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs)
{
PyObject* poRet;
// NOTE : NULL 체크 추가.. - [levites]
if (!poClass)
{
Py_XDECREF(poArgs);
return false;
}
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
return false;
Py_DECREF(poRet);
return true;
}
bool PyCallClassMemberFunc_ByPyString(PyObject* poClass, PyObject* poFuncName, PyObject* poArgs)
{
PyObject* poRet;
// NOTE : NULL 체크 추가.. - [levites]
if (!poClass)
{
Py_XDECREF(poArgs);
return false;
}
if (!__PyCallClassMemberFunc_ByPyString(poClass, poFuncName, poArgs, &poRet))
return false;
Py_DECREF(poRet);
return true;
}
bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, bool* pisRet)
{
PyObject* poRet;
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
return false;
if (PyNumber_Check(poRet))
*pisRet = (PyLong_AsLong(poRet) != 0);
else
*pisRet = true;
Py_DECREF(poRet);
return true;
}
bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, long * plRetValue)
{
PyObject* poRet;
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
return false;
if (PyNumber_Check(poRet))
{
*plRetValue = PyLong_AsLong(poRet);
Py_DECREF(poRet);
return true;
}
Py_DECREF(poRet);
return false;
}
/*
* 이 함수를 직접 호출하지 않도록 한다.
* 부득이 하게 직접 호출할 경우에는 반드시 false 가 리턴 됐을 때
* Py_DECREF(poArgs); 를 해준다.
*/
bool __PyCallClassMemberFunc_ByCString(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, PyObject** ppoRet)
{
if (!poClass)
{
Py_XDECREF(poArgs);
return false;
}
PyObject * poFunc = PyObject_GetAttrString(poClass, (char *)c_szFunc); // New Reference
if (!poFunc)
{
PyErr_Clear();
Py_XDECREF(poArgs);
return false;
}
if (!PyCallable_Check(poFunc))
{
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return false;
}
PyObject * poRet = PyObject_CallObject(poFunc, poArgs); // New Reference
if (!poRet)
{
if (g_pkExceptionSender)
g_pkExceptionSender->Clear();
PyErr_Print();
if (g_pkExceptionSender)
g_pkExceptionSender->Send();
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return false;
}
*ppoRet = poRet;
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return true;
}
bool __PyCallClassMemberFunc_ByPyString(PyObject* poClass, PyObject* poFuncName, PyObject* poArgs, PyObject** ppoRet)
{
if (!poClass)
{
Py_XDECREF(poArgs);
return false;
}
PyObject * poFunc = PyObject_GetAttr(poClass, poFuncName); // New Reference
if (!poFunc)
{
PyErr_Clear();
Py_XDECREF(poArgs);
return false;
}
if (!PyCallable_Check(poFunc))
{
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return false;
}
PyObject * poRet = PyObject_CallObject(poFunc, poArgs); // New Reference
if (!poRet)
{
if (g_pkExceptionSender)
g_pkExceptionSender->Clear();
PyErr_Print();
if (g_pkExceptionSender)
g_pkExceptionSender->Send();
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return false;
}
*ppoRet = poRet;
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return true;
}
bool __PyCallClassMemberFunc(PyObject* poClass, PyObject * poFunc, PyObject* poArgs, PyObject** ppoRet)
{
if (!poClass)
{
Py_XDECREF(poArgs);
return false;
}
if (!poFunc)
{
PyErr_Clear();
Py_XDECREF(poArgs);
return false;
}
if (!PyCallable_Check(poFunc))
{
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return false;
}
PyObject * poRet = PyObject_CallObject(poFunc, poArgs); // New Reference
if (!poRet)
{
PyErr_Print();
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return false;
}
*ppoRet = poRet;
Py_DECREF(poFunc);
Py_XDECREF(poArgs);
return true;
}
@@ -0,0 +1,28 @@
#pragma once
#define SET_EXCEPTION(x) PyErr_SetString(PyExc_RuntimeError, #x)
bool PyTuple_GetString(PyObject* poArgs, int pos, char** ret);
bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned char* ret);
bool PyTuple_GetInteger(PyObject* poArgs, int pos, int* ret);
bool PyTuple_GetInteger(PyObject* poArgs, int pos, WORD* ret);
bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret);
bool PyTuple_GetUnsignedInteger(PyObject* poArgs, int pos, unsigned int* ret);
bool PyTuple_GetLong(PyObject* poArgs, int pos, long* ret);
bool PyTuple_GetUnsignedLong(PyObject* poArgs, int pos, unsigned long* ret);
bool PyTuple_GetFloat(PyObject* poArgs, int pos, float* ret);
bool PyTuple_GetDouble(PyObject* poArgs, int pos, double* ret);
bool PyTuple_GetObject(PyObject* poArgs, int pos, PyObject** ret);
bool PyTuple_GetBoolean(PyObject* poArgs, int pos, bool* ret);
bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs);
bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, bool* pisRet);
bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, long * plRetValue);
bool PyCallClassMemberFunc_ByPyString(PyObject* poClass, PyObject* poFuncName, PyObject* poArgs);
bool PyCallClassMemberFunc(PyObject* poClass, PyObject* poFunc, PyObject* poArgs);
PyObject * Py_BuildException(const char * c_pszErr = NULL, ...);
PyObject * Py_BadArgument();
PyObject * Py_BuildNone();
PyObject * Py_BuildEmptyTuple();
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include "../EffectLib/StdAfx.h"
#include "../EterLib/Resource.h"
#include "../EterLib/ResourceManager.h"
enum EResourceTypes
{
RES_TYPE_UNKNOWN,
};
class CPythonResource : public CSingleton<CPythonResource>
{
public:
CPythonResource();
virtual ~CPythonResource();
void Destroy();
void DumpFileList(const char * c_szFileName);
protected:
CResourceManager m_resManager;
};
+54
View File
@@ -0,0 +1,54 @@
#pragma once
#include "../EterLib/StdAfx.h"
#include "../EterGrnLib/StdAfx.h"
//#include <crtdbg.h>
#ifdef _DEBUG
#undef _DEBUG
#include <Python-2.7/python.h>
#define _DEBUG
#else
#include <Python-2.7/python.h>
#endif
#include <Python-2.7/node.h>
#include <Python-2.7/grammar.h>
#include <Python-2.7/token.h>
#include <Python-2.7/parsetok.h>
#include <Python-2.7/errcode.h>
#include <Python-2.7/compile.h>
#include <Python-2.7/symtable.h>
#include <Python-2.7/eval.h>
#include <Python-2.7/marshal.h>
#include "PythonUtils.h"
#include "PythonLauncher.h"
#include "PythonMarshal.h"
#include "Resource.h"
void initdbg();
// PYTHON_EXCEPTION_SENDER
class IPythonExceptionSender
{
public:
void Clear()
{
m_strExceptionString = "";
}
void RegisterExceptionString(const char * c_szString)
{
m_strExceptionString += c_szString;
}
virtual void Send() = 0;
protected:
std::string m_strExceptionString;
};
extern IPythonExceptionSender * g_pkExceptionSender;
void SetExceptionSender(IPythonExceptionSender * pkExceptionSender);
// END_OF_PYTHON_EXCEPTION_SENDER
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/compile.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <compile.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/errcode.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <errcode.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/eval.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <eval.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/frameobject.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <frameobject.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/grammar.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <grammar.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/longintrepr.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <longintrepr.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/marshal.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <marshal.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/node.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <node.h>
@@ -0,0 +1,13 @@
#pragma once
// Shim for 40250's <Python-2.7/parsetok.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
// parsetok.h names `node` and `grammar` without including their headers; 40250's StdAfx includes
// node.h and grammar.h before it, in this order.
#include <node.h>
#include <grammar.h>
#include <parsetok.h>
@@ -0,0 +1,7 @@
#pragma once
// Shim for 40250's <Python-2.7/python.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>.
#include <Python.h>
@@ -0,0 +1,16 @@
#pragma once
// Shim for 40250's <Python-2.7/symtable.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
// CPython's Include/symtable.h is not self-contained: it names mod_ty, which only Python-ast.h
// declares — and that header also defines one-word macros (Module, Delete, Set, Print, Num, ...)
// that collide with 40250 identifiers such as CPool::Delete. 40250's own extern/include copy
// comments the one declaration that needs mod_ty (PySymtable_Build) out; repeating Python-ast.h's
// typedef keeps the upstream header intact and the macros out.
struct _mod;
typedef struct _mod *mod_ty;
#include <symtable.h>
@@ -0,0 +1,9 @@
#pragma once
// Shim for 40250's <Python-2.7/token.h>: the client includes the CPython headers under a
// Python-2.7/ prefix (its extern/include layout). The vendored interpreter
// (extension/third_party/cpython-2.7.18, target mtpython) installs them flat, so each header
// here forwards to the real one. <Python-2.7/python.h> comes first in 40250's StdAfx, exactly
// as CPython requires of <Python.h>; this shim includes it first so the header also stands
// alone under the port header gate.
#include <Python.h>
#include <token.h>