Home > Archive > Prolog > June 2007 > [SWI-Prolog] Possible to register a foreign function dynamically?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
[SWI-Prolog] Possible to register a foreign function dynamically?
|
|
|
| Hello,
I am writing a Python package to interface SWI-Prolog's foreign
language API. The package uses SWI-Prolog as a shared library;
currently I can query it using PL_open_query just fine, the package is
at (for interested people):
http://code.google.com/p/pyswip
I've converted atom_checksum example at:
http://gollem.science.uva.nl/SWI-Pr...igninclude.html to
Python, there's no problem in the initialization, and
PL_register_foreign works somehow , but when I run a query such as `X
is 10, atom_checksum(X, Y)`, i get a "ERROR: atom_checksum/2: Caught
signal 11 (segv)".
Here's my question: Is it possible to register a foreign function
dynamically without requiring a compilation step? I ask this because
all the examples I've found about it requires to have a function
called `install` which is calling `PL_register_foreign`.
Here's the Python code, I've changed `atom_checksum` so it always
returns false (the longer version is also problematic).
# ...
def atom_checksum(a0, arity, context):
return False
funtype = CFUNCTYPE(foreign_t, term_t, c_int, c_void_p)
PL_register_foreign("atom_checksum", 2, funtype(atom_checksum),
PL_FA_VARARGS)
list( prolog.query("X is 10, atom_checksum(X, Y)",
catcherrors=False) )
Thanks,
Yuce Tekol
| |
| Jan Wielemaker 2007-05-29, 7:06 pm |
| On 2007-05-29, yuce <yucetekol@gmail.com> wrote:
> Hello,
>
> I am writing a Python package to interface SWI-Prolog's foreign
> language API. The package uses SWI-Prolog as a shared library;
> currently I can query it using PL_open_query just fine, the package is
> at (for interested people):
> http://code.google.com/p/pyswip
>
> I've converted atom_checksum example at:
> http://gollem.science.uva.nl/SWI-Pr...igninclude.html to
> Python, there's no problem in the initialization, and
> PL_register_foreign works somehow , but when I run a query such as `X
> is 10, atom_checksum(X, Y)`, i get a "ERROR: atom_checksum/2: Caught
> signal 11 (segv)".
>
> Here's my question: Is it possible to register a foreign function
> dynamically without requiring a compilation step? I ask this because
> all the examples I've found about it requires to have a function
> called `install` which is calling `PL_register_foreign`.
Thats because load_foreign_library/1 loads a DLL/so file, looks for
install() in there and calls it. Anything else that can get hold of a
pointer to function of the right signature can call PL_register_foreign
without problems.
> Here's the Python code, I've changed `atom_checksum` so it always
> returns false (the longer version is also problematic).
>
> # ...
> def atom_checksum(a0, arity, context):
> return False
>
> funtype = CFUNCTYPE(foreign_t, term_t, c_int, c_void_p)
> PL_register_foreign("atom_checksum", 2, funtype(atom_checksum),
> PL_FA_VARARGS)
> list( prolog.query("X is 10, atom_checksum(X, Y)",
> catcherrors=False) )
You definitely have a problem returning False. Does that make the
function actually return 0? Maybe you can run it under a debugger
and get a clue?
Success --- Jan
P.s. There is already an interface called pyprolog. Rumours say
that isn't very complete nor maintained. I have no clue.
| |
|
|
|
|
|
|
|
|
|
|
|
|
|