# HG changeset patch # User Tassilo Philipp # Date 1586286997 -7200 # Node ID edbbd467f50a37856447f3a8c3c19b120024873f # Parent 18e1f1bb19459e407dbba017056a2ce1ed9ba8a7 python binding: - update to dyncall 1.1 - Python 3 support (supports both, Python 2 and 3) - using the Capsule API over PyCObject, when available - support for python unicode strings (for both, Python 2 and 3) - doc cleanup ruby binding: - doc cleanup diff -r 18e1f1bb1945 -r edbbd467f50a python/pydc/README.txt --- a/python/pydc/README.txt Sun Jan 12 12:10:38 2020 +0100 +++ b/python/pydc/README.txt Tue Apr 07 21:16:37 2020 +0200 @@ -1,8 +1,12 @@ dyncall python bindings -(C) 2007-2016 Daniel Adler -Dec 4, 2007: initial -Mar 22,2016: update to dyncall 0.9, includes breaking sig char changes -Apr 19,2018: update to dyncall 1.0 +Copyright 2007-2016 Daniel Adler + 2018-2020 Tassilo Philipp + +Dec 4, 2007: initial +Mar 22, 2016: update to dyncall 0.9, includes breaking sig char changes +Apr 19, 2018: update to dyncall 1.0 +Apr 7, 2020: update to dyncall 1.1, Python 3 support, using the Capsule + API, as well as support for python unicode strings BUILD/INSTALLATION @@ -15,7 +19,7 @@ python setup.py install -Building an egg isn't supported, currently. +Building a wheel package isn't supported, currently. API @@ -35,31 +39,32 @@ x is positional parameter-type charcode, y is result-type charcode - SIG | FROM PYTHON | C/C++ | TO PYTHON - ----+------------------------------------+--------------------+----------------------------------- - 'v' | | void | - 'B' | PyBool | bool | PyBool - 'c' | PyInt (range checked) | char | PyInt - 'C' | PyInt (range checked) | unsigned char | PyInt - 's' | PyInt (range checked) | short | PyInt - 'S' | PyInt (range checked) | unsigned short | PyInt - 'i' | PyInt | int | PyInt - 'I' | PyInt | unsigned int | PyInt - 'j' | PyLong | long | PyLong - 'J' | PyLong | unsigned long | PyLong - 'l' | PyLongLong | long long | PyLongLong - 'L' | PyLongLong | unsigned long long | PyLongLong - 'f' | PyFloat (cast to single precision) | float | PyFloat (cast to double precision) - 'd' | PyFloat | double | PyFloat - 'p' | PyCObject | void* | PyCObject encapsulating a void* - 'Z' | PyString | const char* | PyString + SIG | FROM PYTHON 2 | FROM PYTHON 3 @@@ | C/C++ | TO PYTHON 2 | TO PYTHON 3 @@@ + ----+------------------------------------+------------------------------------+---------------------------------+------------------------------------+----------------------------------- + 'v' | | | void | | + 'B' | PyBool | PyBool | bool | PyBool | PyBool + 'c' | PyInt (range checked) | PyInt (range checked) | char | PyInt | PyInt + 'C' | PyInt (range checked) | PyInt (range checked) | unsigned char | PyInt | PyInt + 's' | PyInt (range checked) | PyInt (range checked) | short | PyInt | PyInt + 'S' | PyInt (range checked) | PyInt (range checked) | unsigned short | PyInt | PyInt + 'i' | PyInt | PyInt | int | PyInt | PyInt + 'I' | PyInt | PyInt | unsigned int | PyInt | PyInt + 'j' | PyLong | PyLong | long | PyLong | PyLong + 'J' | PyLong | PyLong | unsigned long | PyLong | PyLong + 'l' | PyLongLong | PyLongLong | long long | PyLongLong | PyLongLong + 'L' | PyLongLong | PyLongLong | unsigned long long | PyLongLong | PyLongLong + 'f' | PyFloat (cast to single precision) | PyFloat (cast to single precision) | float | PyFloat (cast to double precision) | PyFloat (cast to double precision) + 'd' | PyFloat | PyFloat | double | PyFloat | PyFloat + 'p' | PyUnicode/PyString/PyLong | PyUnicode/PyBytes/PyLong | void* | Py_ssize_t | Py_ssize_t + 'Z' | PyUnicode/PyString | PyUnicode/PyBytes | const char* (UTF-8 for unicode) | PyString | PyUnicode TODO ==== -- support signature suffixes used to indicate calling conventions, are not supported yet! -- not sure if returning 'p' is working, creating PyCObject, check and write test code +- signature suffixes used to indicate calling conventions are not supported yet! +- not sure if returning 'p' is working, creating PyCObject, check and write test code @@@ +- callback support BUGS @@ -70,9 +75,11 @@ solution: installation of latest python for os x (MacPython 2.5) - build log: - python setup.py install +EXAMPLE BUILD +============= + + $ python setup.py install running install running build running build_py diff -r 18e1f1bb1945 -r edbbd467f50a python/pydc/examples/atoi.py --- a/python/pydc/examples/atoi.py Sun Jan 12 12:10:38 2020 +0100 +++ b/python/pydc/examples/atoi.py Tue Apr 07 21:16:37 2020 +0200 @@ -7,7 +7,8 @@ elif sys.platform == "darwin": libc = load("/usr/lib/libc.dylib") elif "bsd" in sys.platform: - libc = load("/usr/lib/libc.so") + #libc = load("/usr/lib/libc.so") + libc = load("/lib/libc.so.7") elif platform.architecture()[0] == "64bit": libc = load("/lib64/libc.so.6") else: @@ -18,9 +19,9 @@ -def atoi(s): return call(fp_atoi,"p)i",s) -def atod(s): return call(fp_atof,"p)d",s) +def atoi(s): return call(fp_atoi,"Z)i",s) +def atod(s): return call(fp_atof,"Z)d",s) -print atoi( "3".join(["12","45"]) ) -print atod( "3".join(["12","45"]) ) +print(atoi("3".join(["12","45"]))) +print(atod("3".join(["12","45"]))) diff -r 18e1f1bb1945 -r edbbd467f50a python/pydc/examples/sintest.py --- a/python/pydc/examples/sintest.py Sun Jan 12 12:10:38 2020 +0100 +++ b/python/pydc/examples/sintest.py Tue Apr 07 21:16:37 2020 +0200 @@ -15,28 +15,32 @@ else: libm = pydc.load("/lib/libm.so.6") -fpsin = pydc.find(libm,"sin") +fp_sin = pydc.find(libm,"sin") def f1(n): - for x in xrange(n): + x = 0 + while x < n: math.sin(x) + x += 1 # filter( math.sin, range(0,n) ) -def libmsin(x): pass +#def libmsin(x): pass def f2(n): - for x in xrange(n): - pydc.call(fpsin,"d)d",float(x)) + x = 0 + while x < n: + pydc.call(fp_sin,"d)d",float(x)) + x += 1 # libmsin(i) # filter( libmsin , range(0,n) ) -print "start_native"+str(os.times()) +print("start_native "+str(os.times())) f1(10000000) -print "start_dc"+str(os.times()) +print("start_dc "+str(os.times())) f2(10000000) -print "end"+str(os.times()) +print("end "+str(os.times())) diff -r 18e1f1bb1945 -r edbbd467f50a python/pydc/pydcext.c --- a/python/pydc/pydcext.c Sun Jan 12 12:10:38 2020 +0100 +++ b/python/pydc/pydcext.c Tue Apr 07 21:16:37 2020 +0200 @@ -3,9 +3,14 @@ ** pydc - python dyncall package ** ** python extension package in C - ** Copyright 2007 Daniel Adler. + ** Copyright 2007-2016 Daniel Adler + ** 2018-2020 Tassilo Philipp ** - ** December 04, 2007 + ** December 04, 2007: initial + ** March 22, 2016: update to dyncall 0.9, includes breaking sig char changes + ** April 19, 2018: update to dyncall 1.0 + ** April 7, 2020: update to dyncall 1.1, Python 3 support, using the Capsule + ** API, as well as support for python unicode strings ** *****************************************************************************/ @@ -13,14 +18,52 @@ #include "dynload.h" #include + + +#if ( (PY_VERSION_HEX < 0x02070000) \ + || ((PY_VERSION_HEX >= 0x03000000) \ + && (PY_VERSION_HEX < 0x03010000)) ) +# define DcPyCObject_FromVoidPtr(ptr, dtor) PyCObject_FromVoidPtr((ptr), (dtor)) +# define DcPyCObject_AsVoidPtr(ppobj) PyCObject_AsVoidPtr((ppobj)) +# define DcPyCObject_SetVoidPtr(ppobj, ptr) PyCObject_SetVoidPtr((ppobj), (ptr)) +#else +# define USE_CAPSULE_API +# define DcPyCObject_FromVoidPtr(ptr, dtor) PyCapsule_New((ptr), NULL, (dtor)) +# define DcPyCObject_AsVoidPtr(ppobj) PyCapsule_GetPointer((ppobj), NULL) +# define DcPyCObject_SetVoidPtr(ppobj, ptr) PyCapsule_SetPointer((ppobj), (ptr)) // this might need to call the dtor to behave like PyCObject_SetVoidPtr? +#endif + +#if PY_MAJOR_VERSION >= 3 +# define DcPyString_GET_SIZE PyBytes_GET_SIZE +# define DcPyString_Check PyBytes_Check +# define DcPyString_AsString PyBytes_AsString +# define DcPyInt_Check PyLong_Check +# define DcPyInt_AsLong PyLong_AsLong +# define DcPyInt_AS_LONG PyLong_AS_LONG +#else +# define DcPyString_GET_SIZE PyString_GET_SIZE +# define DcPyString_Check PyString_Check +# define DcPyString_AsString PyString_AsString +# define DcPyInt_Check PyInt_Check +# define DcPyInt_AsLong PyInt_AsLong +# define DcPyInt_AS_LONG PyInt_AS_LONG +#endif + /* PyCObject destructor callback for libhandle */ +#if defined(USE_CAPSULE_API) +void free_library(PyObject* capsule) +{ + void* libhandle = PyCapsule_GetPointer(capsule, NULL); +#else void free_library(void* libhandle) { +#endif if (libhandle != 0) dlFreeLibrary(libhandle); } + /* load function */ static PyObject* @@ -29,13 +72,15 @@ const char* libpath; void* libhandle; - if ( !PyArg_ParseTuple(args,"s", &libpath) ) return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) missing"); + if (!PyArg_ParseTuple(args,"s", &libpath)) + return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) missing"); libhandle = dlLoadLibrary(libpath); - if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath); + if (!libhandle) + return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath); - return PyCObject_FromVoidPtr(libhandle, &free_library); + return DcPyCObject_FromVoidPtr(libhandle, &free_library); } /* find function */ @@ -48,17 +93,18 @@ void* libhandle; void* funcptr; - if ( !PyArg_ParseTuple(args,"Os", &pcobj, &symbol) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); + if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol)) + return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); - libhandle = PyCObject_AsVoidPtr(pcobj); - - if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "libhandle is null"); + libhandle = DcPyCObject_AsVoidPtr(pcobj); + if (!libhandle) + return PyErr_Format(PyExc_RuntimeError, "libhandle is null"); funcptr = dlFindSymbol(libhandle, symbol); if (!funcptr) return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol); - return PyCObject_FromVoidPtr(funcptr, NULL); + return DcPyCObject_FromVoidPtr(funcptr, NULL); } /* free function */ @@ -69,17 +115,20 @@ PyObject* pcobj; void* libhandle; - if ( !PyArg_ParseTuple(args,"o", &pcobj) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); + if (!PyArg_ParseTuple(args, "o", &pcobj)) + return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); - libhandle = PyCObject_AsVoidPtr(pcobj); - - if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL"); + libhandle = DcPyCObject_AsVoidPtr(pcobj); + if (!libhandle) + return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL"); dlFreeLibrary(libhandle); - PyCObject_SetVoidPtr(pcobj,0); + DcPyCObject_SetVoidPtr(pcobj, NULL); + Py_RETURN_NONE; } + #include "dyncall.h" #include "dyncall_signature.h" @@ -99,9 +148,13 @@ int pos; void* pfunc; - if ( !PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); - pfunc = PyCObject_AsVoidPtr(pcobj_funcptr); - if ( !pfunc ) return PyErr_Format( PyExc_RuntimeError, "function pointer is NULL" ); + if (!PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args)) + return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); + + pfunc = DcPyCObject_AsVoidPtr(pcobj_funcptr); + if (!pfunc) + return PyErr_Format( PyExc_RuntimeError, "function pointer is NULL" ); + l = PyTuple_Size(args); ptr = signature; @@ -133,20 +186,20 @@ case DC_SIGCHAR_UCHAR: { DCchar c; - if ( PyString_Check(po) ) + if ( DcPyString_Check(po) ) { // Py_ssize_t l; size_t l; char* s; - l = PyString_GET_SIZE(po); + l = DcPyString_GET_SIZE(po); if (l != 1) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string with length of 1 (a char string)", index ); - s = PyString_AsString(po); + s = DcPyString_AsString(po); c = (DCchar) s[0]; } - else if ( PyInt_Check(po) ) + else if ( DcPyInt_Check(po) ) { long l; - l = PyInt_AsLong(po); + l = DcPyInt_AsLong(po); if ( (l > CHAR_MAX) || (l < CHAR_MIN)) return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a char code", index ); c = (DCchar) l; } @@ -159,9 +212,9 @@ { DCshort s; long v; - if ( !PyInt_Check(po) ) + if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a short int", index ); - v = PyInt_AS_LONG(po); + v = DcPyInt_AS_LONG(po); if ( (v < SHRT_MIN) || (v > SHRT_MAX) ) return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a short value", index ); s = (DCshort) v; @@ -172,8 +225,8 @@ case DC_SIGCHAR_UINT: { long v; - if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); - v = PyInt_AS_LONG(po); + if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); + v = DcPyInt_AS_LONG(po); dcArgInt(gpCall, (DCint) v ); } break; @@ -181,8 +234,8 @@ case DC_SIGCHAR_ULONG: { long v; - if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); - v = PyInt_AsLong(po); + if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); + v = DcPyInt_AsLong(po); } break; @@ -215,23 +268,40 @@ break; case DC_SIGCHAR_POINTER: { - DCpointer ptr; - if ( PyString_Check(po) ) { - ptr = (DCpointer) PyString_AsString(po); + DCpointer p; + if ( PyUnicode_Check(po) ) { + PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@ + if (bo) { + p = PyBytes_AS_STRING(bo); // Borrowed pointer + //p = strdup(my_result); + //Py_DECREF(bo); + } + } else if ( DcPyString_Check(po) ) { + p = (DCpointer) DcPyString_AsString(po); } else if ( PyLong_Check(po) ) { - ptr = (DCpointer) ( (DCint) PyLong_AsLongLong(po) ); + p = (DCpointer) PyLong_AsVoidPtr(po); } else { return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a promoting pointer-type (int,string)", index ); } - dcArgPointer(gpCall, ptr ); + dcArgPointer(gpCall, p); } break; case DC_SIGCHAR_STRING: { - char* p; - if (!PyString_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string", index ); - p = PyString_AsString(po); - dcArgPointer(gpCall, (DCpointer) p ); + const char* p; + if ( PyUnicode_Check(po) ) { + PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@ + if (bo) { + p = PyBytes_AS_STRING(bo); // Borrowed pointer + //p = strdup(my_result); + //Py_DECREF(bo); + } + } else if ( DcPyString_Check(po) ) { + p = DcPyString_AsString(po); + } else { + return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string", index ); + } + dcArgPointer(gpCall, (DCpointer) p); } break; default: return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); @@ -264,27 +334,55 @@ 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)); // @@@ probably not working, there is no "p" + case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // @@@test, this used to be 'p' which doesn't exist, 'n' is for "Py_ssize_t" default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); } } -static PyMethodDef pydcMethods[] = { - {"load", pydc_load, METH_VARARGS, "load library"}, - {"find", pydc_find, METH_VARARGS, "find symbols"}, - {"free", pydc_free, METH_VARARGS, "free library"}, - {"call", pydc_call, METH_VARARGS, "call function"}, - {NULL,NULL,0,NULL} -}; + + + +#define PYDC_TO_STR_(x) #x +#define PYDC_TO_STR(x) PYDC_TO_STR_(x) +#define PYDC_CONCAT_(x, y) x ## y +#define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y) + +#define PYDC_MOD_NAME pydcext +#define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME) +#define PYDC_MOD_DESC_STR "dyncall bindings for python" + +#if PY_MAJOR_VERSION >= 3 +# define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME) +#else +# define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME) +#endif + PyMODINIT_FUNC -initpydcext(void) +PY_MOD_INIT_FUNC_NAME(void) { + static PyMethodDef pydcMethods[] = { + {"load", pydc_load, METH_VARARGS, "load library"}, + {"find", pydc_find, METH_VARARGS, "find symbols"}, + {"free", pydc_free, METH_VARARGS, "free library"}, + {"call", pydc_call, METH_VARARGS, "call function"}, + {NULL,NULL,0,NULL} + }; + PyObject* m; - m = Py_InitModule("pydcext", pydcMethods); - if (m == NULL) - return; - gpCall = dcNewCallVM(4096); +#if PY_MAJOR_VERSION >= 3 + static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, NULL }; + m = PyModule_Create(&moddef); +#else + m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); +#endif + + if(m) + gpCall = dcNewCallVM(4096); + +#if PY_MAJOR_VERSION >= 3 + return m; +#endif } diff -r 18e1f1bb1945 -r edbbd467f50a python/pydc/setup.py --- a/python/pydc/setup.py Sun Jan 12 12:10:38 2020 +0100 +++ b/python/pydc/setup.py Tue Apr 07 21:16:37 2020 +0200 @@ -1,28 +1,28 @@ from distutils.core import setup, Extension pydcext = Extension('pydcext', - sources = ['pydcext.c'] -,libraries=['dyncall_s','dynload_s'] + sources = ['pydcext.c'] +, libraries = ['dyncall_s','dynload_s'] ) setup( name = 'pydc' -, version = '1.0' +, version = '1.1' , author = 'Daniel Adler' , author_email = 'dadler@dyncall.org' , maintainer = 'Daniel Adler' , maintainer_email = 'dadler@dyncall.org' -, url = 'http://www.dyncall.org' +, url = 'https://www.dyncall.org' +, download_url = 'https://www.dyncall.org/download' +, classifiers = [] +#, packages = ['pydc'] +#, package_dir = ['dir'] +, ext_modules = [pydcext] +, py_modules = ['pydc'] , description = 'dynamic call bindings for python' , long_description = ''' dynamic call library allows to call arbitrary C library functions with a single call code (written in assembly) ''' -, download_url = 'http://www.dyncall.org/download' -, classifiers=[] -#, packages=['pydc'] -#, package_dir['dir'] -, ext_modules = [pydcext] -, py_modules = ['pydc'] ) diff -r 18e1f1bb1945 -r edbbd467f50a ruby/rbdc/README.txt --- a/ruby/rbdc/README.txt Sun Jan 12 12:10:38 2020 +0100 +++ b/ruby/rbdc/README.txt Tue Apr 07 21:16:37 2020 +0200 @@ -63,7 +63,7 @@ TODO ==== -- support signature suffixes used to indicate calling conventions, are not supported yet! +- signature suffixes used to indicate calling conventions are not supported yet! - C pointer -> ruby... array? - callback support