Re: Possible to register a foreign function dynamically?



On 2007-05-30, yuce <yucetekol@xxxxxxxxx> wrote:
(My previous post didn't appear, sending it again)

Success!

Yes! After some time I managed to get it right. This must be my lucky
wednesday :) Anyway, here's the complete Python program:

from pyswip import *
from pyswip.util import Prolog

def atom_checksum(a0, arity, context):
s = c_char_p("\x00"*MAXSTR)
if PL_get_atom_chars(a0, addressof(s)):

Hmmm. PL_get_atom_chars stores a pointer to a string over the
second argument. The "\x00"*MAXSTR looks a bit odd to me.

sum = 0
for c in s.value:
sum += ord(c)&0xFF;
return PL_unify_integer(a0 + 1, sum&0xFF)
else:
return 0

funtype = CFUNCTYPE(foreign_t, term_t, c_int, c_void_p)
p = Prolog()
PL_register_foreign("atom_checksum", 2, funtype(atom_checksum),
PL_FA_VARARGS)
print list(p.query("X='Python', atom_checksum(X, Y)",
catcherrors=True))

Outputs: [{'Y': '130', 'X': 'Python'}]

I've tried disabling signals but I got segmentation faults when the
program wasn't working right; but now everything seems fine [I don't
want to disable signals, do you think disabling them would be better?]

As long as Prolog signal handling isn't conflicting with the signal
handling of another component (i.e. python) there is no reason to
disable it.

--- Jan
.