Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed Oct 23, 2024
1 parent 102498b commit de22427
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/pyswip/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,9 @@ def PL_STRINGS_MARK():
PL_register_foreign = check_strings(0, None)(PL_register_foreign)

PL_register_foreign_in_module = _lib.PL_register_foreign_in_module
PL_register_foreign_in_module = check_strings([0, 1], None)(PL_register_foreign_in_module)
PL_register_foreign_in_module = check_strings([0, 1], None)(
PL_register_foreign_in_module
)

PL_new_atom = _lib.PL_new_atom
PL_new_atom.argtypes = [c_char_p]
Expand Down
102 changes: 79 additions & 23 deletions src/pyswip/easy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,81 @@
from typing import Union, Callable, Optional

from pyswip.core import (
PL_new_atom, PL_register_atom, PL_atom_wchars, PL_get_atom,
PL_unregister_atom, PL_new_term_ref, PL_compare, PL_get_chars,
PL_copy_term_ref, PL_unify_atom, PL_unify_string_chars, PL_unify_integer,
PL_unify_bool, PL_unify_float, PL_unify_list, PL_unify_nil,
PL_term_type, PL_put_term, PL_new_functor, PL_functor_name,
PL_functor_arity, PL_get_functor, PL_new_term_refs, PL_get_arg,
PL_cons_functor_v, PL_put_atom_chars, PL_put_integer, PL_put_functor,
PL_put_nil, PL_cons_list, PL_get_long, PL_get_float,
PL_is_list, PL_get_list, PL_register_foreign_in_module, PL_call,
PL_new_module, PL_pred, PL_open_query, PL_next_solution,
PL_cut_query, PL_close_query,
PL_VARIABLE, PL_STRINGS_MARK, PL_TERM, PL_DICT,
PL_ATOM, PL_STRING, PL_INTEGER, PL_FLOAT,
PL_Q_NODEBUG, PL_Q_CATCH_EXCEPTION, PL_FA_NONDETERMINISTIC,
CVT_VARIABLE, BUF_RING, REP_UTF8, CVT_ATOM, CVT_STRING,
PL_new_atom,
PL_register_atom,
PL_atom_wchars,
PL_get_atom,
PL_unregister_atom,
PL_new_term_ref,
PL_compare,
PL_get_chars,
PL_copy_term_ref,
PL_unify_atom,
PL_unify_string_chars,
PL_unify_integer,
PL_unify_bool,
PL_unify_float,
PL_unify_list,
PL_unify_nil,
PL_term_type,
PL_put_term,
PL_new_functor,
PL_functor_name,
PL_functor_arity,
PL_get_functor,
PL_new_term_refs,
PL_get_arg,
PL_cons_functor_v,
PL_put_atom_chars,
PL_put_integer,
PL_put_functor,
PL_put_nil,
PL_cons_list,
PL_get_long,
PL_get_float,
PL_is_list,
PL_get_list,
PL_register_foreign_in_module,
PL_call,
PL_new_module,
PL_pred,
PL_open_query,
PL_next_solution,
PL_cut_query,
PL_close_query,
PL_VARIABLE,
PL_STRINGS_MARK,
PL_TERM,
PL_DICT,
PL_ATOM,
PL_STRING,
PL_INTEGER,
PL_FLOAT,
PL_Q_NODEBUG,
PL_Q_CATCH_EXCEPTION,
PL_FA_NONDETERMINISTIC,
CVT_VARIABLE,
BUF_RING,
REP_UTF8,
CVT_ATOM,
CVT_STRING,
CFUNCTYPE,
cleaned, cast,
c_size_t, byref, c_void_p, atom_t, create_string_buffer,
c_char_p, functor_t, c_int, c_long, c_double,
foreign_t, term_t, control_t, module_t,
cleaned,
cast,
c_size_t,
byref,
c_void_p,
atom_t,
create_string_buffer,
c_char_p,
functor_t,
c_int,
c_long,
c_double,
foreign_t,
term_t,
control_t,
module_t,
)


Expand Down Expand Up @@ -177,9 +232,6 @@ def __init__(self, handle=None, name=None):
if (self.chars is not None) and not isinstance(self.chars, str):
self.chars = self.chars.decode()

def value(self):
return getTerm(self.handle)

def unify(self, value):
if self.handle is None:
t = PL_new_term_ref(self.handle)
Expand Down Expand Up @@ -537,6 +589,7 @@ def _callbackWrapper(arity=1, nondeterministic=False):
def _foreignWrapper(fun, nondeterministic=False):
res = funwraps.get(fun)
if res is None:

def wrapper(*args):
if nondeterministic:
args = [getTerm(arg) for arg in args[:-1]] + [args[-1]]
Expand All @@ -553,7 +606,9 @@ def wrapper(*args):
cwraps = []


def registerForeign(func: Callable, name: str="", arity: Optional[int]=None, flags: int=0):
def registerForeign(
func: Callable, name: str = "", arity: Optional[int] = None, flags: int = 0
):
"""
Registers a Python callable as a Prolog predicate
Expand Down Expand Up @@ -627,6 +682,7 @@ def newModule(name: Union[str, Atom]) -> module_t:
"""
return module(name)


def module(name: Union[str, Atom]) -> module_t:
"""
Returns a module with the given name.
Expand Down
15 changes: 13 additions & 2 deletions src/pyswip/prolog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""
Provides the basic Prolog interface.
"""

import functools
import inspect
from typing import Union, Generator, Callable, Optional
Expand Down Expand Up @@ -71,6 +72,7 @@ class NestedQueryError(PrologError):
SWI-Prolog does not accept nested queries, that is, opening a query while the previous one was not closed.
As this error may be somewhat difficult to debug in foreign code, it is automatically treated inside PySwip
"""

pass


Expand Down Expand Up @@ -384,11 +386,20 @@ def wrapper(*args):
args = [getTerm(arg) for arg in args]
r = fun(*args)
return True if r is None else r

return wrapper

@classmethod
def register_foreign(cls, func: Callable, /, name: str = "", arity: Optional[int] = None, *, module: str = "",
nondeterministic: bool=False):
def register_foreign(
cls,
func: Callable,
/,
name: str = "",
arity: Optional[int] = None,
*,
module: str = "",
nondeterministic: bool = False,
):
"""
Registers a Python callable as a Prolog predicate
Expand Down
2 changes: 0 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import pytest

from pyswip import *

examples = [
"create_term.py",
"father.py",
Expand Down

0 comments on commit de22427

Please sign in to comment.