antony
antony

Reputation: 2997

Cython memoryviews on Windows

When trying to use Cython on Windows (Anaconda-based install, using TDM-GCC as I need support for OpenMP), I ran into an error when using typed memoryviews.

test1.pyx
def test(int x): pass

test2.pyx
def test(int[:] x): pass

Both modules can be compiled with a basic setup.py (using cythonize), but while test1 can be imported with no problem, importing test2 raises the following:

python3 -c "import test2" (<- Note the use of Python3 -- I haven't tried with Python2)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "stringsource", line 275, in init test2 (test2.c:13146)
UnicodeDecodeError: 'utf-8' codec can't decode byte in position 1: invalid start byte.

with nothing special at line 13146 of test.c, apparently.

Is this a known issue? Or am I doing something wrong? Any help would be welcome.

(crossposted from Cython-users)

Clarifications:

but a longer setup.py such as the one suggested by Saullo Castro doesn't help either.

Bounty awarded to Saullo Castro for pointing out that MinGW-64bit is not simply supported, even though I ended up using a different solution.

Upvotes: 3

Views: 451

Answers (2)

antony
antony

Reputation: 2997

As it turns out, the simplest solution was just to switch everything to 32bit, as TDM-GCC 32bit works fine and I don't have any hard dependencies on 64bit-Python.

Upvotes: 1

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58895

I am using Windows 7 64-bit, Python 2.7.5 64 bit and Cython 0.20.1 and your code works for me.

I tested your original code and this:

def test(int[:] x):
    s = np.shape(x)[0]
    for i in range(s):
        print x[i]

without problems. I will describe here how I compiled by Cython and how I configured my C compiler to use with Cython with the hope that you can solve your problem following these steps.

SET DISTUTILS_USE_SDK=1
setenv /x64 /release
  • Compile Cython (simply doing python setup.py should work)

  • Have a nice setup.py for your .pyx files, here it follows a sample that I use to enable support to OpenMP:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension('test1',
                         ['test1.pyx'],
                         extra_compile_args=['/openmp', '/O2',
                                             '/favor:INTEL64'])]
setup(name = 'test1',
      cmdclass = {'build_ext': build_ext},
      ext_modules = ext_modules)
  • use import pyximport; pyximport.install() when applicable

Upvotes: 4

Related Questions