From: Stefan v. d. W. <st...@su...> - 2006-10-12 10:22:06
|
On Thu, Oct 12, 2006 at 12:38:51AM -0600, Travis Oliphant wrote: >=20 > I made some fixes to the "asbuffer" code which let me feel better about= =20 > exposing it in NumPy (where it is now named int_asbuffer). >=20 > This code takes a Python integer and a size and returns a buffer object= =20 > that points to that memory. A little test is performed by trying to=20 > read (and possibly write if a writeable buffer is requested) to the=20 > first and last elements of the buffer. Any segfault is trapped and use= d=20 > to raise a Python error indicating you can't use that area of memory.=20 >=20 > It doesn't guarantee you won't shoot yourself, but it does make it more= =20 > difficult to segfault Python. Previously a simple int_asbuffer(3423423= ,=20 > 5) would have segfaulted (unless by chance you the memory area 3423423=20 > happens to be owned by Python). Some argument checking and a docstring would be helpful. I guessed wrong the first time: In [1]: x =3D N.array([1,2,3]) In [4]: y =3D N.int_asbuffer(x.size,x.__array_interface__['data'][0]) In [5]: y[0] Segmentation fault Out of curiosity, why does the following outputs differ: In [39]: y =3D N.int_asbuffer(0L,x.size) -------------------------------------------------------------------------= -- exceptions.SystemError Traceback (most rece= nt call last) /home/stefan/<ipython console>=20 SystemError: error return without exception set In [40]: y =3D N.int_asbuffer(1L,x.size) -------------------------------------------------------------------------= -- exceptions.ValueError Traceback (most rece= nt call last) /home/stefan/<ipython console>=20 ValueError: cannot use memory location as a buffer. Thanks for your work on this. Cheers St=E9fan |