This is an archival dump of old wiki content --- see scipy.org for current material

This page will (for now) hold information and discussion about transitioning to Python 3k such as C API changes in Python/C API and how to deal with them.


Note that some effort in documenting the migration is currently happening upstream:

of some interest, too:


Link to some documents which describe changes in Python 3 :

1. http://docs.python.org/dev/3.0/whatsnew/3.0.html Mostly describes language and standard library changes. Has a very tiny paragraph on API changes. A change to the buffer is potentially very important?

Findings :

After forcefully trying to compile NumPy 1.1.1 against Python 3k headers, immediately lots of errors pop up. Those seem to be related to the following issues:

1.String has disappeared. I think it has split into Bytes and Unicode type. Robert Kern said that possibly occurences of String will need to be replaced with Bytes.

2. PyInt_Type has disappeared. Only PyLong_Type exists now. As for API functions such as PyInt_something or pyLong_something, they seem to have been aliased to refer to the same thing. This aliasing will be removed in Python 3.1

3. PyNumberMethods has less fields now. For example, hex, oct, divide and nonzero seem to be removed. There appears to be some autogenerated code using these methods so need to remove some of these names from autogeneration.

4. Buffer interface has changed. This is the biggest change.

typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);

typedef struct {
        readbufferproc bf_getreadbuffer;
        writebufferproc bf_getwritebuffer;
        segcountproc bf_getsegcount;
        charbufferproc bf_getcharbuffer;
} PyBufferProcs;

These functions would then be somehow used under the hood by the following C-API functions:

 int PyObject_AsCharBuffer (PyObject *obj,
                            const char **buffer,
                            Py_ssize_t *buffer_len);

 int PyObject_CheckReadBuffer (PyObject *obj);

 int PyObject_AsReadBuffer (PyObject *obj,
                            const void **buffer,
                            Py_ssize_t *buffer_len);


 int PyObject_AsWriteBuffer (PyObject *obj,
                             void **buffer,
                             Py_ssize_t *buffer_len);

In py3k, the above 4 functions have been deprecated (deprecated but still usable, although, as the mechanism to lock buffers isn't in python (yet?), some problems will pop up if you use these functions in new code: don't do that) in favor of the following 2:

 bool PyObject_CheckBuffer (PyObject* obj);       /* really a macro */

 int PyObject_GetBuffer (PyObject *obj, 
                         Py_buffer *view, 
                         int flags);

The protocol, on the other hand, is now defined by:

typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
typedef void (*releasebufferproc)(PyObject *, Py_buffer *);

typedef struct {
     getbufferproc bf_getbuffer;
     releasebufferproc bf_releasebuffer;
} PyBufferProcs;

5. PyObject_VAR_HEAD has changed. However this doesnt seem like a change that requires much thought. We can probably blindly replacing stuff like obj->obj_size with obj->obj_base.obj_size.

SciPy: Python3k (last edited 2015-10-24 17:48:23 by anonymous)