For Programmers: Free Programming Magazines  


Home > Archive > Prolog > June 2007 > Re: 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 Re: Possible to register a foreign function dynamically?
yuce

2007-05-30, 7:05 pm

(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)):
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?]

Thanks again,

Yuce

Krispy

2007-06-01, 10:39 am

http://Angelina-Jolie-doing-it.info...p?movie=1673286
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com