Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52450

Re: Calling Python macro from ctypes

From Peter Otten <__peter__@web.de>
Subject Re: Calling Python macro from ctypes
Date 2013-08-13 12:25 +0200
Organization None
References <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> <mailman.495.1376307727.1251.python-list@python.org> <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.526.1376389526.1251.python-list@python.org> (permalink)

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Calling Python macro from ctypes Steven D'Aprano <steve@pearwood.info> - 2013-08-12 10:01 +0000
  Re: Calling Python macro from ctypes Peter Otten <__peter__@web.de> - 2013-08-12 13:42 +0200
    Re: Calling Python macro from ctypes Steven D'Aprano <steve@pearwood.info> - 2013-08-13 06:25 +0000
      Re: Calling Python macro from ctypes Stefan Behnel <stefan_ml@behnel.de> - 2013-08-13 10:11 +0200
        Re: Calling Python macro from ctypes Steven D'Aprano <steve@pearwood.info> - 2013-08-13 09:13 +0000
      Re: Calling Python macro from ctypes Peter Otten <__peter__@web.de> - 2013-08-13 12:25 +0200
  Re: Calling Python macro from ctypes Dave Angel <davea@davea.name> - 2013-08-12 17:11 +0000

csiph-web