This commit is contained in:
Vitor Santos Costa
2018-09-14 11:28:24 +01:00
parent a079139060
commit f8d0703983
12 changed files with 46 additions and 18 deletions

View File

@@ -90,7 +90,10 @@ else if (PyUnicode_Check(pVal)) {
rc = rc && PL_unify_atom_chars(t, s);
else
#endif
if (pyStringToString)
return MkStringTerm(s);
else
return MkAtomTerm(Yap_LookupAtom(s));
}
else if (PyByteArray_Check(pVal)) {
return MkStringTerm(PyByteArray_AsString(pVal));

View File

@@ -93,6 +93,7 @@ extern PyObject *py_Sys;
#define py_Builtin PyEval_GetBuiltins()
extern X_API bool python_in_python;
extern bool pyStringToString;
extern bool python_release_GIL(term_t gstate);
extern term_t python_acquire_GIL(void);

View File

@@ -231,6 +231,24 @@ static foreign_t assign_python(term_t exp, term_t name) {
pyErrorAndReturn(b);
}
static foreign_t python_string_to(term_t f) {
if (PL_is_atom(f)) {
char *s = NULL;
if (!PL_get_chars(f, &s, CVT_ALL | CVT_EXCEPTION | REP_UTF8)) {
pyErrorAndReturn(false);
}
if (!strcmp(s,"atom"))
pyStringToString = false;
if (!strcmp(s,"string"))
pyStringToString = true;
else
return false;
return true;
}
return true;
}
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
PyErr_Clear();
PyObject *pI, *pArgs, *pOut;
@@ -759,6 +777,7 @@ install_t install_pypreds(void) {
PL_register_foreign("python_access", 3, python_access, 0);
PL_register_foreign("python_threaded", 0, p_python_threaded, 0);
PL_register_foreign("python_clear_errors", 0, python_clear_errors, 0);
PL_register_foreign("python_string_to", 0, python_string_to, 1);
init_python_vfs();
}

View File

@@ -28,6 +28,8 @@ PyObject *py_Context;
X_API PyObject *Py_f2p;
bool pyStringToString;
extern X_API bool python_in_python;
static void add_modules(void) {

View File

@@ -28,6 +28,7 @@
array_to_python_tuple/4,
array_to_python_view/5,
python/2,
python_string_to/1,
acquire_GIL/0,
release_GIL/0,
python_threaded/0,