Написание оберток для Python 3 против Python 2

Есть ли какая-то разница в том, как писать Python-обертки для библиотек c ++ в Python, если мы хотим сделать его совместимым с Py3 против Py2?

Или оболочка, изначально написанная для Py2, должна работать одинаково для Py3 и наоборот? Насколько я понимаю, обертки Python написаны на c ++ в файле .cxx, который затем должен быть скомпилирован.

Я спрашиваю, потому что я хочу установить Aggdraw (оболочку для библиотеки рисования c ++ Anti-Grain Geometry), которая работает в Python 2.6-7, но приводит к ошибкам в Python 3.4 (32-битная Windows 7, через «PATH = C: / Python27 [или Python34] «и» python setup.py install «в командной строке). Я надеюсь возродить этот удивительный модуль-обертку для Python 3, и надеялся, что кому-то еще будет интересно помочь.

Я могу скомпилировать его на Python 2.6 и 2.7 без каких-либо проблем, и у меня есть и Visual C ++ 2008 и 2010, так что это не проблема компилятора. Проблема, кажется, в фактическом коде оболочки .cxx.

Может ли быть так, что обертка Aggdraw была написана только для Python 2x (почти 10 лет назад), и поэтому она не учитывает проблемы, которые могут возникнуть в Python 3x? Мое лучшее предположение заключается в том, что оболочка не может преобразовать определенные объекты / типы c ++ в Python из-за устаревших и измененных функций в версии 3.4.

Я мог бы хотеть пройти через код оболочки .cxx, чтобы изменить необходимые детали, если кто-то может помочь мне определить, что это за проблемы (см. Коды ошибок командной строки ниже)? Если мы исправим это, я обновлю этот пост со ссылкой на «оживший» код оболочки.

Если между обертками Python 2 и 3 не должно быть разницы, есть идеи, почему я получаю коды ошибок ниже в Py3, а не в Py2?

Спасибо!

Оригинальный код упаковки: кликните сюда

C:\Users\BIGKIMO\Desktop\aggdraw-master>python setup.py build
=== freetype not available (edit setup.py to enable)
running install
running build
running build_ext
building 'aggdraw' extension
C:\Program Files\Microsoft Visual Studio 10.0\VC\BIN\cl.exe /c /nologo /Ox /MD /
W3 /GS- /DNDEBUG -Iagg2/include -IC:\Python34\include -IC:\Python34\include /Tpa
ggdraw.cxx /Fobuild\temp.win32-3.4\Release\aggdraw.obj
aggdraw.cxx
aggdraw.cxx(124) : error C2440: 'initializing' : cannot convert from 'const char
[5]' to 'Py_ssize_t'
There is no context in which this conversion is possible
aggdraw.cxx(126) : error C2440: 'initializing' : cannot convert from 'destructor
' to 'printfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(128) : error C2440: 'initializing' : cannot convert from 'getattrfun
c' to 'setattrfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(142) : error C2440: 'initializing' : cannot convert from 'const char
[4]' to 'Py_ssize_t'
There is no context in which this conversion is possible
aggdraw.cxx(144) : error C2440: 'initializing' : cannot convert from 'destructor
' to 'printfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(161) : error C2440: 'initializing' : cannot convert from 'const char
[6]' to 'Py_ssize_t'
There is no context in which this conversion is possible
aggdraw.cxx(163) : error C2440: 'initializing' : cannot convert from 'destructor
' to 'printfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(187) : error C2440: 'initializing' : cannot convert from 'const char
[5]' to 'Py_ssize_t'
There is no context in which this conversion is possible
aggdraw.cxx(189) : error C2440: 'initializing' : cannot convert from 'destructor
' to 'printfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(191) : error C2440: 'initializing' : cannot convert from 'getattrfun
c' to 'setattrfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(207) : error C2440: 'initializing' : cannot convert from 'const char
[5]' to 'Py_ssize_t'
There is no context in which this conversion is possible
aggdraw.cxx(209) : error C2440: 'initializing' : cannot convert from 'destructor
' to 'printfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(211) : error C2440: 'initializing' : cannot convert from 'getattrfun
c' to 'setattrfunc'
This conversion requires a reinterpret_cast, a C-style cast or function-
style cast
aggdraw.cxx(488) : error C3861: 'PyString_Check': identifier not found
aggdraw.cxx(489) : error C3861: 'PyString_AS_STRING': identifier not found
aggdraw.cxx(575) : error C3861: 'PyString_Check': identifier not found
aggdraw.cxx(583) : error C3861: 'PyString_AS_STRING': identifier not found
aggdraw.cxx(584) : error C3861: 'PyString_GET_SIZE': identifier not found
aggdraw.cxx(730) : error C3861: 'PyInt_Check': identifier not found
aggdraw.cxx(730) : error C3861: 'PyInt_AS_LONG': identifier not found
aggdraw.cxx(731) : error C3861: 'PyInt_Check': identifier not found
aggdraw.cxx(731) : error C3861: 'PyInt_AS_LONG': identifier not found
aggdraw.cxx(735) : error C3861: 'PyInt_Check': identifier not found
aggdraw.cxx(735) : error C3861: 'PyInt_AS_LONG': identifier not found
aggdraw.cxx(736) : error C3861: 'PyInt_Check': identifier not found
aggdraw.cxx(736) : error C3861: 'PyInt_AS_LONG': identifier not found
aggdraw.cxx(742) : error C3861: 'PyInt_Check': identifier not found
aggdraw.cxx(742) : error C3861: 'PyInt_AS_LONG': identifier not found
aggdraw.cxx(745) : error C3861: 'PyInt_Check': identifier not found
aggdraw.cxx(745) : error C3861: 'PyInt_AS_LONG': identifier not found
aggdraw.cxx(759) : error C3861: 'PyInt_Check': identifier not found
aggdraw.cxx(760) : error C3861: 'PyInt_AsLong': identifier not found
aggdraw.cxx(763) : error C3861: 'PyString_Check': identifier not found
aggdraw.cxx(765) : error C3861: 'PyString_AS_STRING': identifier not found
aggdraw.cxx(788) : error C3861: 'PyString_Check': identifier not found
aggdraw.cxx(789) : error C3861: 'PyString_AS_STRING': identifier not found
aggdraw.cxx(1157) : error C3861: 'PyString_FromStringAndSize': identifier not fo
und
aggdraw.cxx(1289) : error C3861: 'PyString_FromString': identifier not found
aggdraw.cxx(1294) : error C3861: 'Py_FindMethod': identifier not found
aggdraw.cxx(1482) : error C3861: 'Py_FindMethod': identifier not found
aggdraw.cxx(1890) : error C3861: 'Py_FindMethod': identifier not found
aggdraw.cxx(1910) : error C3646: 'initaggdraw' : unknown override specifier
aggdraw.cxx(1911) : error C2091: function returns function
aggdraw.cxx(1911) : error C4430: missing type specifier - int assumed. Note: C++
does not support default-int
aggdraw.cxx(1912) : error C2039: 'ob_type' : is not a member of '_typeobject'
c:\python34\include\object.h(334) : see declaration of '_typeobject'
aggdraw.cxx(1912) : error C2039: 'ob_type' : is not a member of '_typeobject'
c:\python34\include\object.h(334) : see declaration of '_typeobject'
aggdraw.cxx(1913) : error C2039: 'ob_type' : is not a member of '_typeobject'
c:\python34\include\object.h(334) : see declaration of '_typeobject'
aggdraw.cxx(1913) : error C2039: 'ob_type' : is not a member of '_typeobject'
c:\python34\include\object.h(334) : see declaration of '_typeobject'
aggdraw.cxx(1913) : error C2039: 'ob_type' : is not a member of '_typeobject'
c:\python34\include\object.h(334) : see declaration of '_typeobject'
aggdraw.cxx(1915) : error C3861: 'Py_InitModule': identifier not found
aggdraw.cxx(1940) : warning C4508: 'DL_EXPORT' : function should return a value;
'void' return type assumed

2

Решение

Библиотека Python FFI (ctype) содержит изменения между каждой версией python. Вы должны были бы пройти через список изменений и посмотреть, что именно отличается и почему он не работает.

Например, ssizet и строки различаются в двух версиях Python.

https://docs.python.org/3/whatsnew/3.2.html#ctypes
Различия в типах между Python 2 и 3

2

Другие решения

В целях расширения предыдущего ответа есть много функций, которые действуют на строку в Py2.7 и теперь действуют на байты или юникод в Py3. Аналогично, то, что упоминается в int, может теперь относиться к long. Как только один пример этого, который вызвал проблему здесь, PyString_Check был объявлен устаревшим pyBytes_Check. Прочитайте следующее для более подробной информации:

https://docs.python.org/3/howto/cporting.html?highlight=pystring

0