Skip to content

Commit

Permalink
py_is_module_unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Aug 26, 2024
1 parent 7814ea8 commit 13e4fae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion scripts/test_in_metta.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ process_file() {
echo "$DEBUG_MESSAGE" | tee -a "$LOGFILE"

# Write the line to /tmp/SHARED.UNITS
echo "| $TEST_NAME | $PASS_OR_FAIL | [$TEST_NAME]($HTML_LINK) | $TEST_CMD | $TEST_EXIT_CODE | 7 | $ELAPSED_TIME seconds | $LOGFILE |" >> /tmp/SHARED.UNITS
echo "| $TEST_NAME | $PASS_OR_FAIL | [$TEST_NAME]($HTML_LINK) | $TEST_CMD | $TEST_EXIT_CODE | 7 | $ELAPSED_TIME | $LOGFILE |" >> /tmp/SHARED.UNITS

# Delete the HTML file if it was planned for deletion
if [ $SHOULD_DELETE_HTML -eq 1 ]; then
Expand Down
16 changes: 2 additions & 14 deletions src/canary/metta_printer.pl
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,7 @@
once(Mesg),nb_setval(W,false)),nb_setval(W,false).

:- dynamic(py_is_enabled/0).
py_is_enabled:- predicate_property(py_is_object(_),foreign), asserta((py_is_enabled:-!)).

py_is_py(_):- \+ py_is_enabled, !, fail.
py_is_py(V):-var(V), get_attr(V,pyobj,X),!,nonvar(X),!.
py_is_py(V):-atom(V), py_is_object(V),!.
py_is_py(V):- is_list(V),!,fail.
py_is_py(V):-py_is_tuple(V),!.
py_is_py(V):-py_is_pdict(V),!.

py_is_tuple(V):- \+ var(V), \+ is_list(V), py_tuple(V,T),py_tuple(T,TT),T==TT, \+ py_type(V,str).
py_is_pdict(V):- \+ var(V), py_dict(V,T),py_dict(T,TT),T==TT.
py_is_list(V):- \+ var(V), \+ is_list(V),py_type(V,list).
%py_is_list(V):- py_is_tuple(V).
py_is_enabled:- predicate_property(py_ppp(_),foreign), asserta((py_is_enabled:-!)).

%write_src(V):- !, \+ \+ quietly(pp_sex(V)),!.
write_src(V):- \+ \+ notrace(pp_sex(V)),!.
Expand All @@ -161,7 +149,7 @@
% Various 'write_src' and 'pp_sex' rules are handling the writing of the source,
% dealing with different types of values, whether they are lists, atoms, numbers, strings, compounds, or symbols.
pp_sexi(V):- is_final_write(V),!.
pp_sexi(V):- py_is_py(V),!,py_ppp(V),!.
pp_sexi(V):- py_is_enabled,py_is_py(V),!,py_ppp(V),!.
pp_sexi(V):- is_dict(V),!,print(V).
pp_sexi((USER:Body)) :- USER==user,!, pp_sex(Body).
pp_sexi(V):- allow_concepts,!,with_concepts('False',pp_sex(V)),flush_output.
Expand Down
24 changes: 23 additions & 1 deletion src/canary/metta_python.pl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,29 @@
py_call_c(G):- py_catch(py_call(G)).
py_call_c(G,R):- py_catch(py_call(G,R)).

py_is_module(M):-notrace((with_safe_argv(catch((py_call(M,X),py_type(X,module)),_,fail)))).
py_is_module(M):-notrace((with_safe_argv(py_is_module_unsafe(M)))).

py_is_module_unsafe(M):- py_is_object(M),!,py_type(M,module).
py_is_module_unsafe(M):- catch((py_call(M,X),py_type(X,module)),_,fail).

py_is_py(_):- \+ py_is_enabled, !, fail.
py_is_py(V):- var(V),!, get_attr(V,pyobj,_),!.
py_is_py(V):- atomic(V), !, py_is_object(V),!.
py_is_py(V):- \+ callable(V),!,fail.
py_is_py(V):- is_list(V),!,fail.
py_is_py(V):- py_is_tuple(V),!.
py_is_py(V):- py_is_py_dict(V),!.
py_is_py(V):- py_is_list(V),!.

py_resolve(V,Py):- var(V),!, get_attr(V,pyobj,Py),!.
py_resolve(V,Py):- \+ compound(V),!,py_is_object(V),Py=V.
py_resolve(V,Py):- is_list(V),!,fail,maplist(py_resolve,V,Py).
py_resolve(V,Py):- V=Py.

py_is_tuple(X):- py_resolve(X,V), py_tuple(V,T),py_tuple(T,TT),T==TT, \+ py_type(V,str).
py_is_py_dict(X):- py_resolve(X,V), py_dict(V,T), py_dict(T,TT), T==TT.
py_is_list(X):- py_resolve(X,V), py_type(V,list).
%py_is_list(V):- py_is_tuple(V).

% Evaluations and Iterations
load_builtin_module:- py_module(builtin_module,
Expand Down

0 comments on commit 13e4fae

Please sign in to comment.