使用 PythranExtension 在 python 包中导入 pythran 模块

import pythran module in a python packages with PythranExtension

我尝试使用优秀的 pythran.

制作一个 python 包

采用这种文件结构

$ tree ../proj
../proj
├── ccompile.py
├── Makefile
├── proj
│   ├── __init__.py
│   └── lib.py
└── setup.py

我的 pythran 东西在 ccompile.py:

$ cat ccompile.py
# pythran export get_fast_results(int[], int[][], (int, int):int dict)
def get_fast_results(mylist_of_int, myarray, dict_with_int_tuples_keys):
    # do stuff update dict_with_int_tuples_keys
    return dict_with_int_tuples_keys

setup.py调用PythranExtension:

$ cat setup.py
from setuptools import setup
from pythran.dist import PythranExtension

setup(name='proj',
      version=0.42,
      packages=['proj'],
      ext_modules=[PythranExtension("ccompile", ["ccompile.py"])],
      # ext_modules=[PythranExtension("proj.ccompile", ["proj/ccompile.py"])],
      zip_safe=False)

一切正常 python setup.py install 编译 ccompile.py 并在 egg 文件夹的根目录下安装 .so::

$ tree
~/venv/lib/python2.7/site-packages/proj-0.42-py2.7-linux-x86_64.egg/
├── ccompile.py
├── ccompile.pyc
├── ccompile.so
├── EGG-INFO
│   └── ...
└── proj
    ├── __init__.py
    ├── __init__.pyc
    ├── lib.py
    └── lib.pyc

问题是 我必须手动 mv ccompile.* 从 site-packages/proj-0.42-py2.7-linux-x86_64.egg/site-packages/proj-0.42-py2.7-linux-x86_64.egg/proj/ 能够导入 来自 proj 包的 pythran 函数::

/tmp$ python -c 'from proj.ccompile import get_fast_results as gfr; print gfr.__doc__'
Supported prototypes:
    - get_fast_results(int[], int[][], (int, int):int dict)
    - get_fast_results(int[], int[][].T, (int, int):int dict)
 make it fast
/tmp$

如果我使用 distutils Extension 的语法( Cf. https://docs.python.org/2/distutils/examples.html#single-extension-module) 例如ext_modules=[PythranExtension("proj.ccompile", ["proj/ccompile.py"])], 在setup.py中(在proj中移动ccompile)当pythran编译它时我得到一个错误:

$ tree ../proj
../proj
├── Makefile
├── proj
│   ├── ccompile.cpp
│   ├── ccompile.py
│   ├── __init__.py
│   └── lib.py
└── setup.py

与::

$ more setup.py 
from setuptools import setup
from pythran.dist import PythranExtension

setup(...
      ext_modules=[PythranExtension("proj.ccompile", ["proj/ccompile.py"])],
      zip_safe=False)

我得到::

$ python setup.py install
running install
...
running egg_info
...
copying proj/__init__.py -> build/lib.linux-x86_64-2.7/proj
copying proj/ccompile.py -> build/lib.linux-x86_64-2.7/proj
copying proj/lib.py -> build/lib.linux-x86_64-2.7/proj
running build_ext
building 'proj.ccompile' extension
C compiler: x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC
creating build/temp.linux-x86_64-2.7
creating build/temp.linux-x86_64-2.7/proj
compile options: '-DUSE_GMP -DENABLE_PYTHON_MODULE -I~/venv/local/lib/python2.7/site-packages/pythran -I/usr/include/python2.7 -c'
extra options: '-std=c++11 -fno-math-errno -w'
x86_64-linux-gnu-gcc: proj/ccompile.cpp
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
proj/ccompile.cpp:35:25: error: expected ‘{’ before ‘.’ token
 namespace __pythran_proj.ccompile
                         ^
proj/ccompile.cpp:35:25: error: expected unqualified-id before ‘.’ token
proj/ccompile.cpp:610:13: error: expected ‘}’ at end of input
             }
             ^
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
proj/ccompile.cpp:35:25: error: expected ‘{’ before ‘.’ token
 namespace __pythran_proj.ccompile
                         ^
proj/ccompile.cpp:35:25: error: expected unqualified-id before ‘.’ token
proj/ccompile.cpp:610:13: error: expected ‘}’ at end of input
             }
             ^
error: Command "x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DUSE_GMP -DENABLE_PYTHON_MODULE -I~/venv/local/lib/python2.7/site-packages/pythran -I/usr/include/python2.7 -c proj/ccompile.cpp -o build/temp.linux-x86_64-2.7/proj/ccompile.o -std=c++11
-fno-math-errno -w" failed with exit status 1
$

这是一个 pythran 问题。它已在提交 18aefe0e2383.

中修复