[Python-Dev] Extending Python 3000

Rob Crowther weilawei at gmail.com
Tue Sep 18 23:46:58 CEST 2007


I'm attempting to wrap the GNU MP mpf_t type and related functions, as I
need the extra precision and speed for a package I'm writing. The available
wrappers are either unmaintained or undocumented and incompatible with
Python 3000. Following the docs in the Python 3000 section of the website,
I've started off with this:

*mpfmodule.c*


#include <Python.h>
#include <stdio.h>
#include <gmp.h>

typedef struct {
    PyObject_HEAD
    mpf_t ob_val;
} MPFObject;

static PyTypeObject MPFType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "mpf.MPF",                 /*tp_name*/
    sizeof(MPFObject),         /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    0,                         /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,        /*tp_flags*/
    "GNU MP mpf_t objects",    /* tp_doc */
};

static PyMethodDef mpf_methods[] = {
    {NULL}
};

#ifndef PyMODINIT_FUNC
#define PyMODINIT_FUNC void
#endif

PyMODINIT_FUNC initmpf(void) {
    PyObject* m;

    MPFType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&MPFType) < 0)
        return;

    m = Py_InitModule3("mpf", mpf_methods,
                       "Wrapper around GNU MP mpf_t and related methods");

    Py_INCREF(&MPFType);
    PyModule_AddObject(m, "MPF", (PyObject *) &MPFType);
}



Upon running my setup.py script, it gives me numerous warnings. These do not
occur if I attempt to use Python 2.5. It also works fine under Python 2.5.


weilawei at archeron:~/Code/mpf$ python setup.py build
running build
running build_ext
building 'mpf' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
-fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/local/include
-I/usr/local/include/python3.0 -c mpfmodule.c -o build/temp.linux-i686-3.0
/mpfmodule.o
*mpfmodule.c:11: warning: missing braces around initializer
mpfmodule.c:11: warning: (near initialization for
&#8216;MPFType.ob_base.ob_base&#8217;)
mpfmodule.c:13: warning: initialization makes integer from pointer without a
cast
mpfmodule.c:32: warning: initialization from incompatible pointer type*
gcc -pthread -shared build/temp.linux-i686-3.0/mpfmodule.o -L/usr/local/lib
-lgmp -o build/lib.linux-i686-3.0/mpf.so


Inside the Python interpreter:

weilawei at archeron:~/Code/mpf/build/lib.linux-i686-3.0$ python
Python 3.0a1 (py3k, Sep 15 2007, 00:33:44)
[GCC 4.1.3 20070831 (prerelease) (Ubuntu 4.1.2-16ubuntu1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mpf
>>> test = mpf.MPF()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
*MemoryError*
>>> mpf.MPF
*Segmentation fault*



Pointers as to what has changed and what I need to do to compile an
extension for Py3k would be very much appreciated. Thank you all in advance
for your time.

- Rob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20070918/737432ba/attachment-0001.htm 


More information about the Python-Dev mailing list