new jupiter intrface
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
|
||||
|
||||
|
||||
#include "py4yap.h"
|
||||
#include <VFS.h>
|
||||
|
||||
#include "YapStreams.h"
|
||||
|
||||
atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
|
||||
ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V, ATOM_self, ATOM_nil, ATOM_brackets, ATOM_curly_brackets;
|
||||
ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V, ATOM_self, ATOM_nil,
|
||||
ATOM_brackets, ATOM_curly_brackets;
|
||||
|
||||
functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
|
||||
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
|
||||
FUNCTOR_float1, FUNCTOR_int1, FUNCTOR_iter1, FUNCTOR_iter2, FUNCTOR_long1,
|
||||
FUNCTOR_len1, FUNCTOR_curly1, FUNCTOR_ord1, FUNCTOR_range1, FUNCTOR_range2,
|
||||
FUNCTOR_range3, FUNCTOR_sum1, FUNCTOR_pointer1, FUNCTOR_complex2,
|
||||
FUNCTOR_plus2, FUNCTOR_sub2, FUNCTOR_mul2, FUNCTOR_div2, FUNCTOR_hat2,
|
||||
FUNCTOR_colon2, FUNCTOR_comma2, FUNCTOR_equal2, FUNCTOR_sqbrackets2,
|
||||
FUNCTOR_dot2, FUNCTOR_brackets1;
|
||||
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
|
||||
FUNCTOR_float1, FUNCTOR_int1, FUNCTOR_iter1, FUNCTOR_iter2, FUNCTOR_long1,
|
||||
FUNCTOR_len1, FUNCTOR_curly1, FUNCTOR_ord1, FUNCTOR_range1, FUNCTOR_range2,
|
||||
FUNCTOR_range3, FUNCTOR_sum1, FUNCTOR_pointer1, FUNCTOR_complex2,
|
||||
FUNCTOR_plus2, FUNCTOR_sub2, FUNCTOR_mul2, FUNCTOR_div2, FUNCTOR_hat2,
|
||||
FUNCTOR_colon2, FUNCTOR_comma2, FUNCTOR_equal2, FUNCTOR_sqbrackets2,
|
||||
FUNCTOR_dot2, FUNCTOR_brackets1;
|
||||
|
||||
X_API PyObject *py_Builtin;
|
||||
X_API PyObject *py_Yapex;
|
||||
@@ -23,77 +27,88 @@ PyObject *py_ModDict;
|
||||
|
||||
VFS_t pystream;
|
||||
|
||||
static void *
|
||||
py_open( const char *name, const char *io_mode) {
|
||||
static void *py_open(const char *name, const char *io_mode) {
|
||||
#if HAVE_STRCASESTR
|
||||
if (strcasestr(name,"//python/")== name)
|
||||
if (strcasestr(name, "//python/") == name)
|
||||
name += strlen("//python/");
|
||||
#else
|
||||
if (strstr(name,"//python/")== name)
|
||||
#else
|
||||
if (strstr(name, "//python/") == name)
|
||||
name += strlen("//python/");
|
||||
#endif
|
||||
// we assume object is already open, so there is no need to open it.
|
||||
PyObject *stream = string_to_python( name, true, NULL);
|
||||
PyObject *stream = string_to_python(name, true, NULL);
|
||||
if (stream == Py_None)
|
||||
return NULL;
|
||||
Py_INCREF(stream);
|
||||
return stream;
|
||||
}
|
||||
|
||||
static bool
|
||||
py_close(int sno) {
|
||||
PyObject *s = YAP_foreign_stream(sno);
|
||||
PyObject* fclose = PyObject_GetAttrString(s, "close");
|
||||
PyObject* rc= PyObject_CallObject(fclose, NULL);
|
||||
|
||||
static bool py_close(int sno) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
PyObject *fclose = PyObject_GetAttrString(s->u.private_data, "close");
|
||||
PyObject *rc = PyObject_CallObject(fclose, NULL);
|
||||
bool v = (rc == Py_True);
|
||||
return v;
|
||||
}
|
||||
|
||||
static PyObject * pyw; // buffer
|
||||
static int pyw_kind;
|
||||
PyObject * pyw_data;
|
||||
|
||||
static int
|
||||
py_put(int sno, int ch) {
|
||||
PyObject *s = YAP_foreign_stream(sno);
|
||||
PyUnicode_WRITE( pyw_kind, pyw_data, 0, ch );
|
||||
PyObject* fput = PyObject_GetAttrString(s, "write");
|
||||
PyObject_CallFunctionObjArgs(fput, pyw, NULL);
|
||||
static int py_put(int sno, int ch) {
|
||||
PyObject *pyw; // buffer
|
||||
//int pyw_kind;
|
||||
//PyObject *pyw_data;
|
||||
|
||||
char s[2];
|
||||
StreamDesc *st = YAP_GetStreamFromId(sno);
|
||||
// PyUnicode_WRITE(pyw_kind, pyw_data, 0, ch);
|
||||
PyObject *fput = PyObject_GetAttrString(st->u.private_data, "write");
|
||||
s[0] = ch;
|
||||
s[1] = '\0';
|
||||
PyObject_CallFunctionObjArgs(fput, PyBytes_FromString(s), NULL);
|
||||
return ch;
|
||||
}
|
||||
|
||||
static int
|
||||
py_get(int sno) {
|
||||
PyObject *s = YAP_foreign_stream(sno);
|
||||
PyObject* fget = PyObject_GetAttrString(s, "read");
|
||||
static int py_get(int sno) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
PyObject *fget = PyObject_GetAttrString(s->u.private_data, "read");
|
||||
PyObject *pyr = PyObject_CallFunctionObjArgs(fget, PyLong_FromLong(1), NULL);
|
||||
return PyUnicode_READ_CHAR( pyr, 0) ;
|
||||
return PyUnicode_READ_CHAR(pyr, 0);
|
||||
}
|
||||
|
||||
static int64_t py_seek(int sno, int64_t where, int how) {
|
||||
PyObject *s = YAP_foreign_stream(sno);
|
||||
PyObject* fseek = PyObject_GetAttrString(s, "seek");
|
||||
PyObject *pyr = PyObject_CallFunctionObjArgs(fseek, PyLong_FromLong(where), PyLong_FromLong(how), NULL);
|
||||
static int64_t py_seek(int sno, int64_t where, int how) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
PyObject *fseek = PyObject_GetAttrString(s->u.private_data, "seek");
|
||||
PyObject *pyr = PyObject_CallFunctionObjArgs(fseek, PyLong_FromLong(where),
|
||||
PyLong_FromLong(how), NULL);
|
||||
return PyLong_AsLong(pyr);
|
||||
}
|
||||
|
||||
static void
|
||||
py_flush(int sno) {
|
||||
PyObject *s = YAP_foreign_stream(sno);
|
||||
PyObject* flush = PyObject_GetAttrString(s, "flush");
|
||||
PyObject_CallFunction( flush, NULL);
|
||||
static void py_flush(int sno) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
PyObject *flush = PyObject_GetAttrString(s->u.private_data, "flush");
|
||||
PyObject_CallFunction(flush, NULL);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void python_output(void) {
|
||||
PyObject *stream = string_to_python("sys.stdout", true, NULL);
|
||||
StreamDesc *st = YAP_GetStreamFromId(1);
|
||||
st->u.private_data = stream;
|
||||
st->vfs = &pystream;
|
||||
stream = string_to_python("sys.stderr", true, NULL);
|
||||
st = YAP_GetStreamFromIds(2);
|
||||
st->u.private_data = stream;
|
||||
st->vfs = &pystream;
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool init_python_stream(void) {
|
||||
//pyw = PyUnicode_FromString("x");
|
||||
//pyw_kind = PyUnicode_KIND(pyw);
|
||||
//pyw_data = PyUnicode_DATA(pyw);
|
||||
|
||||
|
||||
static bool
|
||||
init_python_stream(void)
|
||||
{
|
||||
pyw = PyUnicode_FromString("x");
|
||||
pyw_kind = PyUnicode_KIND(pyw);
|
||||
pyw_data = PyUnicode_DATA(pyw);
|
||||
|
||||
pystream.name = "python stream";
|
||||
pystream.vflags = VFS_CAN_WRITE|VFS_CAN_EXEC| VFS_CAN_SEEK|VFS_HAS_PREFIX;
|
||||
pystream.vflags =
|
||||
VFS_CAN_WRITE | VFS_CAN_EXEC | VFS_CAN_SEEK | VFS_HAS_PREFIX;
|
||||
pystream.prefix = "//python/";
|
||||
pystream.suffix = NULL;
|
||||
pystream.open = py_open;
|
||||
@@ -108,7 +123,7 @@ init_python_stream(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
X_API PyObject *py_F2P;
|
||||
X_API PyObject *Py_f2p;
|
||||
|
||||
extern X_API bool python_in_python;
|
||||
|
||||
@@ -125,11 +140,12 @@ static void add_modules(void) {
|
||||
py_Yapex = PyImport_AddModule("yap4py.yapi");
|
||||
if (py_Yapex)
|
||||
Py_INCREF(py_Yapex);
|
||||
py_F2P = PyDict_New();
|
||||
Py_f2p = PythonLookup("f2p", NULL);
|
||||
if (Py_f2p)
|
||||
Py_INCREF(Py_f2p);
|
||||
init_python_stream();
|
||||
}
|
||||
|
||||
|
||||
static void install_py_constants(void) {
|
||||
FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
|
||||
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
|
||||
@@ -176,7 +192,7 @@ static void install_py_constants(void) {
|
||||
FUNCTOR_comma2 = PL_new_functor(PL_new_atom(","), 2);
|
||||
FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
|
||||
FUNCTOR_sqbrackets2 = PL_new_functor(PL_new_atom("[]"), 2);
|
||||
}
|
||||
}
|
||||
|
||||
foreign_t end_python(void) {
|
||||
if (!python_in_python)
|
||||
@@ -189,11 +205,11 @@ static bool libpython_initialized = 0;
|
||||
|
||||
X_API bool do_init_python(void) {
|
||||
// char **argv;
|
||||
if (libpython_initialized)
|
||||
if (libpython_initialized)
|
||||
return true;
|
||||
libpython_initialized = true;
|
||||
|
||||
// PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
// PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
term_t t = PL_new_term_ref();
|
||||
if (!python_in_python)
|
||||
Py_Initialize();
|
||||
@@ -201,7 +217,7 @@ X_API bool do_init_python(void) {
|
||||
PL_reset_term_refs(t);
|
||||
install_pl2pl();
|
||||
// PyGILState_Release(gstate);
|
||||
add_modules();
|
||||
add_modules();
|
||||
// python_output();
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user