[Cython] Bug: Memoryview of struct with adjacent string fields does not detect string boundaries

2014-02-06 Thread Joshua Adelman
This discussion was initially started on the cython user google group, but I 
wanted to move the issue over to the dev list, as per the suggestion on 
cython_trac. 

Given a numpy recarray containing two or more fixed-length string fields, if 
those string fields are adjacent to one another cython does not properly detect 
the boundary between the string fields. A concise test case demonstrating the 
problem is:

```cython
cimport numpy as np

cdef packed struct tstruct:
np.float32_t a
np.int16_t b
char[6] c
char[4] d

def test_struct(tstruct[:] x):
pass
```

We then define some data on the python side:

```python
import numpy as np

a = np.recarray(3, dtype=[('a', np.float32),  ('b', np.int16), ('c', '|S6'), 
('d', '|S4')])
a[0] = (1.1, 1, 'abcde', 'fgh')
a[1] = (2.1, 2, 'ijklm', 'nop')
a[2] = (3.1, 3, 'qrstu', 'vwx')

test_struct(a)
```

This results in the error:

---
ValueError   Traceback (most recent call last)

 in ()
> 1 test_struct(a)

ValueError: Expected a dimension of size 6, got 10


If we swap the order of the fields in the recarray and `tstruct` to (a,c,b,d) 
so that there is a numerical field between the string fields, then the function 
can parse the memory view correctly. 

The relevant line of code that catches the incorrect value of `enc_count` is: 
https://github.com/cython/cython/blob/master/Cython/Utility/Buffer.c#L468 

``` 
if (ctx->enc_count != ctx->head->field->type->arraysize[0]) { 
PyErr_Format(PyExc_ValueError, 
 "Expected a dimension of size %zu, got %zu", 
 ctx->head->field->type->arraysize[0], ctx->enc_count); 
return -1; 
} 
``` 

My naive guess is that there is something going on in: 
https://github.com/cython/cython/blob/master/Cython/Utility/Buffer.c#L738 

since that appears to be the only place where `enc_count` is being incremented. 
That would seem like the place where a boundary between two string fields might 
not be properly handled (the comment in the line above "Continue pooling same 
type" is suggestive.

I'll cross-post this on the cython trac once I have access and will then submit 
a pull request on Github of a test case once I have the trac issue number.

Josh




___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


[Cython] undefined reference to `__Pyx_PyInt_As_unsigned_int error

2014-05-20 Thread Joshua Adelman
Hello all,

I wanted to try to get some traction on this issue that I raised on the 
Cython-user google group list:
https://groups.google.com/forum/#!topic/cython-users/l9wD4w2lcw8

The basic issue is that I get an error of "undefined reference to 
`__Pyx_PyInt_As_unsigned_int". The strange part is I then get non-deterministic 
codegen, wherein the first time I compile the code it fails, but if I delete 
the .c file and try again, it will then compile without errors. A minimal 
example is provided here:
https://gist.github.com/synapticarbors/11060931

This issue seems to have cropped up somewhere between 0.19.2 (where it's fine) 
and 0.20.1. It also is still present in 0.21dev from github. The error seems to 
happen when running on a Windows machine (Windows 2008 server), but I can't 
reproduce the error on Mac OSX (running the same version of the Continuum 
Anaconda distribution).

Any suggestions or confirmation of this issue would be appreciated.

Thanks,
Josh___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel