This commit is contained in:
Vitor Santos Costa 2018-12-23 15:38:56 +00:00
parent 664c17f3b3
commit 3699a715ce
5 changed files with 56 additions and 171 deletions

View File

@ -601,132 +601,6 @@ static long get_len_of_range(long lo, long hi, long step) {
{"A29", NULL}, {"A29", NULL}, {"A30", NULL}, {"A31", NULL}, {"A32", NULL},
{NULL, NULL}};
static PyObject *structseq_str(PyStructSequence *obj ) {
/* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100
bool removelast = false;
PyTypeObject *typ = Py_TYPE(obj);
const char *type_name = typ->tp_name;
Py_ssize_t len, i;
char buf[REPR_BUFFER_SIZE];
char *endofbuf, *pbuf = buf;
/* pointer to end of writeable buffer; safes space for "...)\0" */
endofbuf = &buf[REPR_BUFFER_SIZE - 5];
/* "typename(", limited to TYPE_MAXSIZE */
len =
strnlen(type_name, TYPE_MAXSIZE);
strncpy(pbuf, type_name, len);
pbuf += len;
*pbuf++ = '(';
for (i = 0; i < ((PyStructSequence *)obj)->ob_base.ob_size; i++) {
PyObject *val, *repr;
const char *crepr;
val = PyStructSequence_GET_ITEM(obj, i);
repr = PyObject_Str(val);
if (repr == NULL)
return Py_None;
crepr = PyUnicode_AsUTF8(repr);
if (crepr == NULL) {
Py_DECREF(repr);
return Py_None;
}
/* + 3: keep space for ", " */
len = strlen(crepr) + 2;
if ((pbuf + len) <= endofbuf) {
strcpy(pbuf, crepr);
pbuf += strlen(crepr);
*pbuf++ = ',';
*pbuf++ = ' ';
removelast = 1;
Py_DECREF(repr);
} else {
strcpy(pbuf, "...");
pbuf += 3;
removelast = 0;
Py_DECREF(repr);
break;
}
}
if (removelast) {
/* overwrite last ", " */
pbuf -= 2;
}
*pbuf++ = ')';
*pbuf = '\0';
return PyUnicode_FromString(buf);
}
static PyObject *structseq_repr(PyObject *iobj) {
/* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100
PyStructSequence *obj = (PyStructSequence *)iobj;
PyTypeObject *typ = Py_TYPE(obj);
const char *type_name = typ->tp_name;
bool removelast = false;
Py_ssize_t len, i;
char buf[REPR_BUFFER_SIZE];
char *endofbuf, *pbuf = buf;
/* pointer to end of writeable buffer; safes space for "...)\0" */
endofbuf = &buf[REPR_BUFFER_SIZE - 5];
/* "typename(", limited to TYPE_MAXSIZE */
len =
strnlen(type_name, TYPE_MAXSIZE);
strncpy(pbuf, type_name, len);
pbuf += len;
*pbuf++ = '(';
for (i = 0; i < ((PyStructSequence *)obj)->ob_base.ob_size; i++) {
PyObject *val, *repr;
const char *crepr;
val = PyStructSequence_GET_ITEM(obj, i);
repr = PyObject_Repr(val);
if (repr == NULL)
return NULL;
crepr = PyUnicode_AsUTF8(repr);
if (crepr == NULL) {
Py_DECREF(repr);
return NULL;
}
/* + 3: keep space for ", " */
len = strlen(crepr) + 2;
if ((pbuf + len) <= endofbuf) {
strcpy(pbuf, crepr);
pbuf += strlen(crepr);
*pbuf++ = ',';
*pbuf++ = ' ';
removelast = 1;
Py_DECREF(repr);
} else {
strcpy(pbuf, "...");
pbuf += 3;
removelast = 0;
Py_DECREF(repr);
break;
}
}
if (removelast) {
/* overwrite last ", " */
pbuf -= 2;
}
*pbuf++ = ')';
*pbuf = '\0';
return PyUnicode_FromString(buf);
}
#endif
static bool legal_symbol(const char *s) {
@ -761,8 +635,9 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
typp = (PyTypeObject *)d;
} else {
PyStructSequence_Desc *desc = PyMem_Calloc(sizeof(PyStructSequence_Desc), 1);
desc->name = PyMem_Malloc(strlen(s) + 1);
strcpy(desc->name, s);
char *tnp;
desc->name = tnp = PyMem_Malloc(strlen(s) + 1);
strcpy(tnp, s);
desc->doc = "YAPTerm";
desc->fields = pnull;
desc->n_in_sequence = arity;
@ -773,7 +648,7 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
return NULL;
typp->tp_traverse = NULL;
typp->tp_flags |=
Py_TPFLAGS_TUPLE_SUBCLASS|
// Py_TPFLAGS_TUPLE_SUBCLASS|
Py_TPFLAGS_BASETYPE|
Py_TPFLAGS_HEAPTYPE;
// don't do this: we cannot add a type as an atribute.
@ -783,8 +658,6 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
Py_INCREF(key);
Py_INCREF(typp);
}
typp->tp_repr = structseq_repr;
typp->tp_str = structseq_str;
}
PyObject *o = PyStructSequence_New(typp);
Py_INCREF(typp);
@ -915,7 +788,14 @@ PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt) {
PyTuple_SET_ITEM(n, 1, out);
return n;
}
return term_to_nametuple(s, arity, out);
if (cvt)
return term_to_nametuple(s, arity, out);
else {
PyObject *rc = PyTuple_New(2);
PyTuple_SetItem(rc, 0, PyUnicode_FromString(s));
PyTuple_SetItem(rc, 1, out);
return rc;
}
}
}
@ -1146,7 +1026,13 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
// PyObject_Print(rc, stderr, 0);
// DebugPrintf("CallObject %p\n", rc);
} else {
if (cvt)
rc = term_to_nametuple(s, arity, pArgs);
else {
rc = PyTuple_New(2);
PyTuple_SetItem(rc, 0, ys);
PyTuple_SetItem(rc, 1, pArgs);
}
}
return rc;

View File

@ -31,7 +31,7 @@ static foreign_t python_represent( term_t name, term_t tobj) {
term_t stackp = python_acquire_GIL();
PyObject *e;
e = term_to_python(tobj, false, NULL, true);
e = term_to_python(tobj, false, NULL, false);
if (e == NULL) {
python_release_GIL(stackp);
pyErrorAndReturn(false);

View File

@ -74,48 +74,46 @@ argi(N,I,I1) :-
python_query( Caller, String ) :-
atomic_to_term( String, Goal, VarNames ),
query_to_answer( Goal, VarNames, Status, Bindings),
query_to_answer( Goal, _, Status, VarNames, Bindings),
Caller.port := Status,
write_query_answer( Bindings ),
answer := {},
foldl(ground_dict(answer), Bindings, [], Ts),
term_variables( Ts, Hidden),
foldl(bv, Hidden , 0, _),
output(Caller, Bindings).
output( _, Bindings ) :-
write_query_answer( Bindings ),
fail.
output( Caller, Bindings ) :-
answer := {},
foldl(ground_dict(answer), Bindings, [], Ts),
term_variables( Ts, Hidden),
foldl(bv, Hidden , 0, _),
maplist(into_dict(answer),Ts),
Caller.answer := json.dumps(answer),
S := Caller.answer,
format(user_error, '~nor ~s~n~n',S).
S := Caller.answer,
format(user_error, '~nor ~s~n~n',S),
fail.
output(_Caller, _Bindings).
bv(V,I,I1) :-
atomic_concat(['__',I],V),
I1 is I+1.
into_dict(D,V0=T) :-
D[V0] := T.
python_represents(D[V0], T).
/**
*
*/
ground_dict(_Dict, var([V,V]), I, I) :-
ground_dict(_Dict,var([_V]), I, I) :-
!.
ground_dict(Dict, nonvar([V0|Vs], T),I0, [V0=T| I0]) :-
ground_dict(_Dict,var([V,V]), I, I) :-
!.
ground_dict(Dict, nonvar([V0|Vs],T),I0, [V0=T| I0]) :-
!,
ground_dict( Dict, var([V0|Vs]), I0, I0).
ground_dict(Dict, var([V0,V|Vs]), I, I) :-
ground_dict(Dict, var([V0|Vs]),I0, I0).
ground_dict(Dict, var([V0,V1|Vs]), I, I) :-
!,
Dict[V]=V0,
ground_dict( Dict, var([V0|Vs]), I, I).
ground_dict(_, _, _, _).
Dict[V1] := V0,
ground_dict(Dict, var([V0|Vs]), I, I).
bound_dict(Dict, nonvar([V0|Vs], T)) :-
!,
Dict[V0] := T,
bound_dict( Dict, var([V0|Vs])).
bound_dict(Dict, var([V0,V|Vs])) :-
!,
Dict[V] := V0,
bound_dict( Dict, var([V0|Vs])).
bound_dict(_, _).

View File

@ -389,20 +389,22 @@ version(T) :-
fail.
'$set_toplevel_hook'(_).
query_to_answer(G, V, Status, Vs) :-
gated_call( true, (G,'$delayed_goals'(G, V, Vs, LGs, _DCP)), Status, '$answer'( Status, LGs, Vs ) ).
query_to_answer(G, V, Status, Vs, Bindings ) :-
gated_call( true, (G,'$delayed_goals'(G, V, Vs, LGs, _DCP)), Status, '$answer'( Status, LGs, Vs, Bindings ) ).
'$answer'( exit, LGs, Vs) :-
!. %,
%'$process_answer'(Vs, LGs).
'$answer'( answer, LGs, Vs) :-
!. %,
% '$process_answer'(Vs, LGs, Bindings).
'$answer'(!, _, _).
'$answer'(fail,_,_).
'$answer'( exit, LGs, Vs, Bindings ) :-
!,
'$sort'(Vs, NVs),
'$prep_answer_var_by_var'(NVs, Bindings , LGs).
'$answer'( answer, LGs, Vs, Bindings) :-
!,
'$sort'(Vs, NVs),
'$prep_answer_var_by_var'(NVs, Bindings , LGs).
'$answer'(!, _, _,_).
'$answer'(fail,_,_,_).
'$answer'(exception(E),_,_,_) :-
'$LoopError'(E,error).
'$answer'(external_exception(_),_,_).
'$answer'(external_exception(_),_,_,_).
%% @}

View File

@ -113,7 +113,6 @@ undefined_query(G0, M0, Cut) :-
% undef handler
'$undefp'([M0|G0],_) :-
start_low_level_trace,
% make sure we do not loop on undefined predicates
setup_call_catcher_cleanup(
'$undef_set'(Action,Debug,Current),