Created
April 2, 2015 15:49
-
-
Save synapticarbors/0a7cf2b3f4516b22826d to your computer and use it in GitHub Desktop.
Strange Cython error with char arrays of particular length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I ran into a strange cython issue where structs containing members of type char[], were failing when they | |
| were a particular length. | |
| This gist contains a simple example that reproduces the issue: | |
| ``` | |
| $ python gen_pyx.py | |
| $ python setup.py build_ext --inplace | |
| $ python runtest.py | |
| ``` | |
| Output from `runtest.py`: | |
| ``` | |
| Failed length 19:Expected a dimension of size 19, got 9 | |
| Failed length 29:Expected a dimension of size 29, got 9 | |
| Failed length 39:Expected a dimension of size 39, got 9 | |
| Failed length 49:Expected a dimension of size 49, got 9 | |
| Failed length 59:Expected a dimension of size 59, got 9 | |
| Failed length 69:Expected a dimension of size 69, got 9 | |
| Failed length 79:Expected a dimension of size 79, got 9 | |
| Failed length 89:Expected a dimension of size 89, got 9 | |
| Failed length 99:Expected a dimension of size 99, got 9 | |
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template = '''\ | |
| cdef packed struct L{L}_type: | |
| char[{L}] x | |
| def test_L{L}(x): | |
| cdef: | |
| L{L}_type[:] y | |
| y = x | |
| return y[0].x | |
| ''' | |
| if __name__ == '__main__': | |
| with open('chartest.pyx', 'wt') as f: | |
| for k in xrange(1, 100): | |
| f.write(template.format(L=k)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import chartest | |
| def test_item(L): | |
| test_method = getattr(chartest, 'test_L{len}'.format(len=L)) | |
| dtype = [('x', 'S{len}'.format(len=L)),] | |
| x = np.recarray(10, dtype=dtype) | |
| x.x = b'x' | |
| try: | |
| r = test_method(x) | |
| except ValueError, e: | |
| print 'Failed length {}:'.format(L) + str(e) | |
| if __name__ == '__main__': | |
| for k in xrange(1, 100): | |
| test_item(k) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from distutils.core import setup | |
| from Cython.Build import cythonize | |
| setup( | |
| ext_modules = cythonize("chartest.pyx") | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment