This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/python/python.c

117 lines
4.1 KiB
C
Raw Normal View History

2012-10-08 23:58:22 +01:00
2016-07-31 16:09:21 +01:00
#include "python.h"
2016-07-31 16:09:21 +01:00
int assign_python(PyObject *root, term_t t, PyObject *e);
2016-07-31 16:09:21 +01:00
atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
2016-12-10 07:01:10 +00:00
ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V, ATOM_self;
2016-07-31 16:09:21 +01:00
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,
2016-12-10 07:01:10 +00:00
FUNCTOR_colon2, FUNCTOR_equal2, FUNCTOR_sqbrackets2, FUNCTOR_dot2;
2012-10-23 10:16:32 +01:00
2016-07-31 16:09:21 +01:00
PyObject *py_Main;
PyObject *py_Builtin;
PyObject *py_Yapex;
PyObject *term_to_python(term_t t, bool eval);
foreign_t python_to_ptr(PyObject *pVal, term_t t);
2015-09-21 23:05:36 +01:00
2016-07-31 16:09:21 +01:00
PyObject *py_F2P;
PyObject *term_to_python(term_t t, bool eval);
2015-09-21 23:05:36 +01:00
2016-07-31 16:09:21 +01:00
int assign_python(PyObject *root, term_t t, PyObject *e);
2012-10-25 00:33:02 +01:00
2016-07-31 16:09:21 +01:00
PyObject *ActiveModules[32];
int active_modules = 0;
2012-10-25 00:33:02 +01:00
2016-09-30 23:11:13 +01:00
bool python_in_python;
2016-07-31 16:09:21 +01:00
static void install_py_constants(void) {
FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
2012-10-08 23:58:22 +01:00
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
// FUNCTOR_boolop1 = PL_new_functor(PL_new_atom("@"), 1);
2016-07-31 16:09:21 +01:00
ATOM_A = PL_new_atom("A");
ATOM_V = PL_new_atom("V");
2015-09-21 23:05:36 +01:00
ATOM_builtin = PL_new_atom("__builtin__");
ATOM_comma = PL_new_atom(",");
2012-11-27 00:16:34 +00:00
ATOM_colon = PL_new_atom(":");
ATOM_true = PL_new_atom("true");
2012-10-17 10:56:44 +01:00
ATOM_false = PL_new_atom("false");
ATOM_dot = PL_new_atom(".");
ATOM_none = PL_new_atom("none");
2016-09-30 23:11:13 +01:00
ATOM_self = PL_new_atom("self");
2012-10-26 00:24:07 +01:00
ATOM_t = PL_new_atom("t");
2012-11-25 23:37:28 +00:00
FUNCTOR_abs1 = PL_new_functor(PL_new_atom("abs"), 1);
FUNCTOR_all1 = PL_new_functor(PL_new_atom("all"), 1);
FUNCTOR_any1 = PL_new_functor(PL_new_atom("any"), 1);
FUNCTOR_bin1 = PL_new_functor(PL_new_atom("bin"), 1);
FUNCTOR_ord1 = PL_new_functor(PL_new_atom("ord"), 1);
2012-11-27 00:16:34 +00:00
FUNCTOR_int1 = PL_new_functor(PL_new_atom("int"), 1);
FUNCTOR_long1 = PL_new_functor(PL_new_atom("long"), 1);
FUNCTOR_float1 = PL_new_functor(PL_new_atom("float"), 1);
2012-11-25 23:37:28 +00:00
FUNCTOR_curly1 = PL_new_functor(PL_new_atom("{}"), 1);
2012-10-25 00:33:02 +01:00
FUNCTOR_dollar1 = PL_new_functor(PL_new_atom("$"), 1);
FUNCTOR_pointer1 = PL_new_functor(PL_new_atom("__obj__"), 1);
2012-11-05 13:49:15 +00:00
FUNCTOR_dir1 = PL_new_functor(PL_new_atom("dir"), 1);
FUNCTOR_iter1 = PL_new_functor(PL_new_atom("iter"), 1);
2012-11-27 12:10:41 +00:00
FUNCTOR_iter2 = PL_new_functor(PL_new_atom("iter"), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_len1 = PL_new_functor(PL_new_atom("len"), 1);
2012-11-27 12:10:41 +00:00
FUNCTOR_range1 = PL_new_functor(PL_new_atom("range"), 1);
FUNCTOR_range2 = PL_new_functor(PL_new_atom("range"), 2);
FUNCTOR_range3 = PL_new_functor(PL_new_atom("range"), 3);
FUNCTOR_sum1 = PL_new_functor(PL_new_atom("sum"), 1);
2015-08-07 22:57:53 +01:00
FUNCTOR_complex2 = PL_new_functor(PL_new_atom("i"), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_plus2 = PL_new_functor(PL_new_atom("+"), 2);
2012-10-25 00:33:02 +01:00
FUNCTOR_sub2 = PL_new_functor(PL_new_atom("-"), 2);
FUNCTOR_mul2 = PL_new_functor(PL_new_atom("*"), 2);
FUNCTOR_div2 = PL_new_functor(PL_new_atom("/"), 2);
FUNCTOR_hat2 = PL_new_functor(PL_new_atom("^"), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_colon2 = PL_new_functor(PL_new_atom(":"), 2);
2012-11-25 23:37:28 +00:00
FUNCTOR_comma2 = PL_new_functor(PL_new_atom(","), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
FUNCTOR_sqbrackets2 = PL_new_functor(PL_new_atom("[]"), 2);
2016-07-31 16:09:21 +01:00
py_Main = PyImport_AddModule("__main__");
py_Builtin = PyImport_AddModule("__builtin__");
py_Yapex = PyImport_AddModule("yapex");
}
foreign_t end_python(void) {
Py_Finalize();
return TRUE;
}
2016-07-31 16:09:21 +01:00
X_API bool init_python(void) {
char **argv;
2016-09-30 23:11:13 +01:00
python_in_python = false;
2016-07-31 16:09:21 +01:00
if (YAP_DelayInit(init_python, "python")) {
// wait for YAP_Init
return false;
}
2016-10-28 18:24:52 +01:00
// PyGILState_STATE gstate = PyGILState_Ensure();
2016-07-31 16:09:21 +01:00
term_t t = PL_new_term_ref();
2016-09-30 23:11:13 +01:00
if (!Py_IsInitialized()) {
python_in_python = true;
YAP_Argv(&argv);
if (argv) {
2016-07-31 16:09:21 +01:00
#if PY_MAJOR_VERSION < 3
2016-09-30 23:11:13 +01:00
Py_SetProgramName(argv[0]);
2016-07-31 16:09:21 +01:00
#else
2016-09-30 23:11:13 +01:00
wchar_t *buf = Py_DecodeLocale(argv[0], NULL);
2016-12-10 07:01:10 +00:00
Py_SetProgramName(buf);
2016-07-31 16:09:21 +01:00
#endif
2016-09-30 23:11:13 +01:00
}
Py_Initialize();
2016-07-31 16:09:21 +01:00
}
install_py_constants();
PL_reset_term_refs(t);
install_pypreds();
install_pl2pl();
2016-12-10 07:01:10 +00:00
// PyGILState_Release(gstate);
2016-09-30 23:11:13 +01:00
return !python_in_python;
2012-10-08 23:58:22 +01:00
}