view python/pydc/pydcext.c @ 28:edbbd467f50a

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
author Tassilo Philipp
date Tue, 07 Apr 2020 21:16:37 +0200
parents a40084782546
children 6cc2b7fc7ea2
line wrap: on
line source

/******************************************************************************
 **
 **       pydc - python dyncall package
 **
 **       python extension package in C
 **       Copyright 2007-2016 Daniel Adler
 **                 2018-2020 Tassilo Philipp
 **
 **       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
 **
 *****************************************************************************/

#include <Python.h>
#include "dynload.h"
#include <limits.h>



#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*
pydc_load(PyObject* self, PyObject* args)
{
  const char* libpath;
  void* libhandle;

  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);

  return DcPyCObject_FromVoidPtr(libhandle, &free_library);
}

/* find function */

static PyObject*
pydc_find(PyObject* self, PyObject* args)
{
  PyObject* pcobj;
  const char* symbol;
  void* libhandle;
  void* funcptr;

  if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol))
    return PyErr_Format(PyExc_RuntimeError, "argument mismatch");

  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 DcPyCObject_FromVoidPtr(funcptr, NULL);
}

/* free function */

static PyObject*
pydc_free(PyObject* self, PyObject* args)
{
  PyObject* pcobj;
  void* libhandle;

  if (!PyArg_ParseTuple(args, "o", &pcobj))
    return PyErr_Format(PyExc_RuntimeError, "argument mismatch");

  libhandle = DcPyCObject_AsVoidPtr(pcobj);
  if (!libhandle)
    return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL");

  dlFreeLibrary(libhandle);
  DcPyCObject_SetVoidPtr(pcobj, NULL);

  Py_RETURN_NONE;
}


#include "dyncall.h"
#include "dyncall_signature.h"

DCCallVM* gpCall;

/* call function */

static PyObject*
pydc_call(PyObject* self, PyObject* in_args)
{
  PyObject*   pcobj_funcptr;
  const char* signature;
  PyObject*   args;
  int         l;
  const char* ptr;
  char        ch;
  int         pos;
  void*       pfunc;

  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;
  pos = 0;

  dcReset(gpCall);

  while ( (ch = *ptr) != '\0' && ch != ')' )
  {
    PyObject* po;

    int index = pos+1;

    if (pos > l) return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" );

    po = PyTuple_GetItem(args,pos);

    switch(ch)
    {
      case DC_SIGCHAR_BOOL:
      {
        DCbool b;
        if ( !PyBool_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a bool", index );
        b = (Py_True == po) ? DC_TRUE : DC_FALSE;
        dcArgBool(gpCall, b);
      }
      break;
      case DC_SIGCHAR_CHAR:
      case DC_SIGCHAR_UCHAR:
      {
        DCchar c;
        if ( DcPyString_Check(po) )
        {
          // Py_ssize_t l;
          size_t l;
          char* s;
          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 = DcPyString_AsString(po);
          c = (DCchar) s[0];
        }
        else if ( DcPyInt_Check(po) )
        {
          long l;
          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;
        }
        else return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a char", index );
        dcArgChar(gpCall, c);
      }
      break;
      case DC_SIGCHAR_SHORT:
      case DC_SIGCHAR_USHORT:
      {
        DCshort s;
        long v;
        if ( !DcPyInt_Check(po) )
          return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a short int", index );
        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;
        dcArgShort(gpCall, s);
      }
      break;
      case DC_SIGCHAR_INT:
      case DC_SIGCHAR_UINT:
      {
        long v;
        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;
      case DC_SIGCHAR_LONG:
      case DC_SIGCHAR_ULONG:
      {
        long v;
        if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index );
        v = DcPyInt_AsLong(po);

      }
      break;
      case DC_SIGCHAR_LONGLONG:
      case DC_SIGCHAR_ULONGLONG:
      {
        PY_LONG_LONG pl;
        DClonglong dl;
        if ( !PyLong_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a long long", index );
        pl = PyLong_AsLongLong(po);
        dl = (DClonglong) pl;
        dcArgLongLong(gpCall, dl );
      }
      break;
      case DC_SIGCHAR_FLOAT:
      {
        DCfloat f;
        if (!PyFloat_Check(po)) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expeecting a float", index );
        f = (float) PyFloat_AsDouble(po);
        dcArgFloat(gpCall, f);
      }
      break;
      case DC_SIGCHAR_DOUBLE:
      {
        double d;
        if (!PyFloat_Check(po)) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expeecting a float", index );
        d = PyFloat_AsDouble(po);
        dcArgDouble(gpCall, d);
      }
      break;
      case DC_SIGCHAR_POINTER:
      {
        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) ) {
          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, p);
      }
      break;
      case DC_SIGCHAR_STRING:
      {
        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);
    }

    ++pos; ++ptr;

  }

  if (pos != l) return PyErr_Format( PyExc_RuntimeError, "too many arguments");

  if (ch == '\0') return PyErr_Format( PyExc_RuntimeError, "return value missing in signature");

  ch = *++ptr;

  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_CHAR:      return Py_BuildValue("b", dcCallChar    (gpCall, pfunc));
    case DC_SIGCHAR_UCHAR:     return Py_BuildValue("B", dcCallChar    (gpCall, pfunc));
    case DC_SIGCHAR_SHORT:     return Py_BuildValue("h", dcCallShort   (gpCall, pfunc));
    case DC_SIGCHAR_USHORT:    return Py_BuildValue("H", dcCallShort   (gpCall, pfunc));
    case DC_SIGCHAR_INT:       return Py_BuildValue("i", dcCallInt     (gpCall, pfunc));
    case DC_SIGCHAR_UINT:      return Py_BuildValue("I", dcCallInt     (gpCall, pfunc));
    case DC_SIGCHAR_LONG:      return Py_BuildValue("l", dcCallLong    (gpCall, pfunc));
    case DC_SIGCHAR_ULONG:     return Py_BuildValue("k", dcCallLong    (gpCall, pfunc));
    case DC_SIGCHAR_LONGLONG:  return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc));
    case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", 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("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");
  }
}





#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
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;
#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
}