extensions
This commit is contained in:
@@ -25,130 +25,6 @@ X_API PyObject *py_Sys;
|
||||
PyObject *py_Context;
|
||||
PyObject *py_ModDict;
|
||||
|
||||
VFS_t pystream;
|
||||
|
||||
static void *
|
||||
py_open(VFS_t *me, int sno, const char *name,
|
||||
const char *io_mode) {
|
||||
#if HAVE_STRCASESTR
|
||||
if (strcasestr(name, "/python/") == name)
|
||||
name += strlen("/python/");
|
||||
#else
|
||||
if (strstr(name, "/python/") == name)
|
||||
name += strlen("/python/");
|
||||
#endif
|
||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||
fprintf(stderr,"opened %d\n", sno);
|
||||
// we assume object is already open, so there is no need to open it.
|
||||
PyObject *stream = string_to_python(name, true, NULL);
|
||||
if (stream == Py_None)
|
||||
return NULL;
|
||||
Py_INCREF(stream);
|
||||
st->u.private_data = stream;
|
||||
st->vfs = me;
|
||||
if (strchr(io_mode,'r'))
|
||||
st->status = Input_Stream_f;
|
||||
else
|
||||
st->status = Append_Stream_f | Output_Stream_f;
|
||||
Yap_DefaultStreamOps(st);
|
||||
return stream;
|
||||
}
|
||||
|
||||
static bool
|
||||
py_close(int sno) {
|
||||
return true;
|
||||
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 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 *err, *fput = PyObject_GetAttrString(st->u.private_data, "write");
|
||||
s[0] = ch;
|
||||
s[1] = '\0';
|
||||
PyObject_CallFunctionObjArgs(fput, PyBytes_FromString(s), NULL);
|
||||
if ((err = PyErr_Occurred())) {
|
||||
PyErr_SetString(
|
||||
err,
|
||||
"Error in put\n"); // %s:%s:%d!\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static int py_peek(int sno) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
PyObject *fget = PyObject_GetAttrString(s->u.private_data, "peek");
|
||||
PyObject *pyr = PyObject_CallFunctionObjArgs(fget, PyLong_FromLong(1), NULL);
|
||||
return PyUnicode_READ_CHAR(pyr, 0);
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
|
||||
pystream.name = "python stream";
|
||||
pystream.vflags =
|
||||
VFS_CAN_WRITE | VFS_CAN_EXEC | VFS_CAN_READ | VFS_HAS_PREFIX;
|
||||
pystream.prefix = "/python/";
|
||||
pystream.suffix = NULL;
|
||||
pystream.open = py_open;
|
||||
pystream.close = py_close;
|
||||
pystream.get_char = py_get;
|
||||
pystream.peek_char = py_peek;
|
||||
pystream.put_char = py_put;
|
||||
pystream.flush = py_flush;
|
||||
pystream.seek = py_seek;
|
||||
pystream.next = GLOBAL_VFS;
|
||||
GLOBAL_VFS = &pystream;
|
||||
// NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
X_API PyObject *Py_f2p;
|
||||
|
||||
extern X_API bool python_in_python;
|
||||
@@ -170,7 +46,7 @@ static void add_modules(void) {
|
||||
Py_f2p = PythonLookup("f2p", NULL);
|
||||
if (Py_f2p)
|
||||
Py_INCREF(Py_f2p);
|
||||
init_python_stream();
|
||||
init_python_vfs();
|
||||
}
|
||||
|
||||
static void install_py_constants(void) {
|
||||
|
||||
Reference in New Issue
Block a user