Mercurial > pub > dyncall > bindings
changeset 5:bf5625bb6f05
- brought python binding up to dc v0.9
author | cslag |
---|---|
date | Tue, 22 Mar 2016 01:49:34 +0100 |
parents | 4c5019f00f5b |
children | 80273969f043 |
files | python/pydc/README.txt python/pydc/examples/atoi.py python/pydc/examples/sintest.py python/pydc/pydcext.c python/pydc/sintest-timings.txt |
diffstat | 5 files changed, 66 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- a/python/pydc/README.txt Tue Mar 22 00:55:15 2016 +0100 +++ b/python/pydc/README.txt Tue Mar 22 01:49:34 2016 +0100 @@ -1,6 +1,8 @@ dyncall python bindings -(C) 2007 Daniel Adler. -2007 04. December +(C) 2007-2016 Daniel Adler. +Dec 4, 2007: initial +Mar 22,2016: brought up to dyncall 0.9 + INSTALLATION ------------ @@ -15,8 +17,8 @@ --- libhandle = load(libpath) -funcptr = find(libhandle, symbolname ) -call( funcptr,signature, ... ) +funcptr = find(libhandle, symbolname) +call(funcptr, signature, ...) SIGNATURE FORMAT @@ -26,20 +28,31 @@ x is positional parameter-type charcode - 'b' C++: bool <- Python: PyBool + 'B' C++: bool <- Python: PyBool + 'c' C: char <- Python: PyInt (range checked) + 's' C: short <- Python: PyInt (range checked) 'i' C: int <- Python: PyInt + 'j' C: long <- Python: PyLong 'l' C: long long <- Python: PyLongLong + 'f' C: float <- Python: PyFloat (cast to single precision) + 'd' C: double <- Python: PyFloat 'p' C: void* <- Python: PyCObject - 's' C: const char* <- Python: PyString + 'Z' C: const char* <- Python: PyString y is result-type charcode 'v' void + 'B' bool -> PyBool + 'c' char -> PyInt + 's' short -> PyInt 'i' int -> PyInt - 'l' long -> PyLong + 'j' long -> PyLong + 'l' long long -> PyLongLong + 'f' float -> PyFloat (cast to double precision) + 'd' double -> PyFloat 'p' ptr -> PyCObject encapsulating a void* - 'f' float -> PyFloat (upcast to double) - 'd' double -> PyFloat + 'Z' const char* -> PyString + BUGS ----
--- a/python/pydc/examples/atoi.py Tue Mar 22 00:55:15 2016 +0100 +++ b/python/pydc/examples/atoi.py Tue Mar 22 01:49:34 2016 +0100 @@ -1,16 +1,23 @@ from pydc import * -from sys import platform +import sys +import platform -if platform == "win32": +if sys.platform == "win32": libc = load("msvcrt") -elif platform == "darwin": +elif sys.platform == "darwin": libc = load("/usr/lib/libc.dylib") +elif "bsd" in sys.platform: + libc = load("/usr/lib/libc.so") +elif platform.architecture()[0] == "64bit": + libc = load("/lib64/libc.so.6") else: libc = load("/lib/libc.so.6") fp_atoi = find(libc,"atoi") fp_atof = find(libc,"atof") + + def atoi(s): return call(fp_atoi,"p)i",s) def atod(s): return call(fp_atof,"p)d",s)
--- a/python/pydc/examples/sintest.py Tue Mar 22 00:55:15 2016 +0100 +++ b/python/pydc/examples/sintest.py Tue Mar 22 01:49:34 2016 +0100 @@ -1,33 +1,42 @@ import math import os import pydc +import sys +import platform + +if sys.platform == "win32": + libm = pydc.load("msvcrt") +elif sys.platform == "darwin": + libm = pydc.load("/usr/lib/libm.dylib") +elif "bsd" in sys.platform: + libm = pydc.load("/usr/lib/libm.so") +elif platform.architecture()[0] == "64bit": + libm = pydc.load("/lib64/libm.so.6") +else: + libm = pydc.load("/lib/libm.so.6") + +fpsin = pydc.find(libm,"sin") + + def f1(n): for x in xrange(n): math.sin(x) # filter( math.sin, range(0,n) ) - -libc = pydc.load("msvcrt") -fpsin = pydc.find(libc,"sin") - -def libcsin(x): pass +def libmsin(x): pass def f2(n): for x in xrange(n): pydc.call(fpsin,"d)d",float(x)) -# libcsin(i) +# libmsin(i) -# filter( libcsin , range(0,n) ) +# filter( libmsin , range(0,n) ) -#b = os.times() +print "start_native"+str(os.times()) f1(10000000) -#f2(10000000) -e = os.times() +print "start_dc"+str(os.times()) +f2(10000000) +print "end"+str(os.times()) -print e - - - -
--- a/python/pydc/pydcext.c Tue Mar 22 00:55:15 2016 +0100 +++ b/python/pydc/pydcext.c Tue Mar 22 01:49:34 2016 +0100 @@ -221,7 +221,7 @@ dcArgPointer(gpCall, ptr ); } break; - case 'S': + case DC_SIGCHAR_STRING: { char* p; if (!PyString_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string", index ); @@ -244,15 +244,15 @@ switch(ch) { - case DC_SIGCHAR_VOID: dcCallVoid(gpCall, pfunc); Py_RETURN_NONE; - case DC_SIGCHAR_BOOL: return Py_BuildValue("i", dcCallBool(gpCall, pfunc) ); - case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt(gpCall, pfunc) ); - case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", (unsigned long long) dcCallLongLong(gpCall, pfunc) ); - case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat(gpCall, pfunc) ); - case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble(gpCall, pfunc) ); - case 's': return Py_BuildValue("s", dcCallPointer(gpCall, pfunc) ); - case DC_SIGCHAR_POINTER: return Py_BuildValue("p", dcCallPointer(gpCall, pfunc) ); - default: return PyErr_Format( PyExc_RuntimeError, "invalid return type signature" ); + case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; + case DC_SIGCHAR_BOOL: return Py_BuildValue("i", dcCallBool (gpCall, pfunc)); + case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); + case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", (unsigned long long)dcCallLongLong(gpCall, pfunc)); + case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); + case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); + case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); + case DC_SIGCHAR_POINTER: return Py_BuildValue("p", dcCallPointer (gpCall, pfunc)); + default: return PyErr_Format( PyExc_RuntimeError, "invalid return type signature"); } }
--- a/python/pydc/sintest-timings.txt Tue Mar 22 00:55:15 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ -sintest.py! -==========! - - -* f2 - - C:\lab\dyncall\bindings\python\pydc\examples>sintest.py - (0.1875, 26.265625, 0.0, 0.0, 0.0) - - -* f1 - - C:\lab\dyncall\bindings\python\pydc\examples>sintest.py - (0.171875, 4.1875, 0.0, 0.0, 0.0) - -