Calling Python macro from ctypes

Peter Otten __peter__ at web.de
Tue Aug 13 06:25:37 EDT 2013


Steven D'Aprano wrote:

> On Mon, 12 Aug 2013 13:42:14 +0200, Peter Otten wrote:
> 
>> Steven D'Aprano wrote:
>> 
>>> Is it possible to call a Python macro from ctypes? For example, Python
>>> 3.3 introduces some new macros for querying the internal representation
>>> of strings:
>>> 
>>> http://www.python.org/dev/peps/pep-0393/#new-api
> [...]
> 
>> That's not possible. It may look like a function, but a preprocessor
>> replaces the C macro in the C source before compilation.
> 
> That's what I feared.
> 
> In that case, how would I use ctypes to access the underlying fields in
> the new string implementation?

You'd have to replicate the structure of Python's unicode type with ctypes. 
You could take shortcuts like determining sizeof(PyObject) and then fill in 
bytes for that instead of the actual fields, but it's still tedious.

So while I have no idea why you would want to do this I took your question 
as an occasion to take a glimpse into the C extension tutorial. I then 
adapted the example code to wrap said macro. Here's the result so far 
(bugfixes welcome):

$ cat macrowrapper.c 
#include <Python.h>

static PyObject *
macrowrapper_max_char_value(PyObject *self, PyObject *args)
{
    PyObject *unicode;
    Py_UCS4 maxchar;

    if (!PyArg_ParseTuple(args, "O", &unicode))
        return NULL;
    if (!PyUnicode_Check(unicode)) {
      PyErr_BadArgument();
      return NULL;
    }
    maxchar = PyUnicode_MAX_CHAR_VALUE(unicode);
    return PyLong_FromLong(maxchar);
}

static PyMethodDef MacroWrapperMethods[] = {
    {"max_char_value",  macrowrapper_max_char_value, METH_VARARGS,
     "Wrap PyUnicode_MAX_CHAR_VALUE() macro."},

    {NULL, NULL, 0, NULL}        /* Sentinel */
};

static struct PyModuleDef macrowrapper = {
   PyModuleDef_HEAD_INIT,
   "macrowrapper",   /* name of module */
   NULL, /* module documentation, may be NULL */
   -1,       /* size of per-interpreter state of the module,
                or -1 if the module keeps state in global variables. */
   MacroWrapperMethods
};

PyMODINIT_FUNC
PyInit_macrowrapper(void)
{
    return PyModule_Create(&macrowrapper);
}
$ cat setup.py 
from distutils.core import setup, Extension

module = Extension('macrowrapper',
                    sources = ['macrowrapper.c'])

setup (name = 'MacroWrapper',
       version = '1.0',
       description = 'Wrap the PyUnicode_MAX_CHAR_VALUE macro',
       ext_modules = [module])
$ python3.3 setup.py install --user

[snip]

$ python3.3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 30 2012, 14:49:00) 
[GCC 4.6.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from macrowrapper import max_char_value
>>> max_char_value("")
127
>>> max_char_value("Löblich")
255
>>> max_char_value("εὕρηκα")
65535
>>> max_char_value(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad argument type for built-in operation
>>> max_char_value()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes exactly 1 argument (0 given)





More information about the Python-list mailing list