data-apis / array-api-strict

Strict implementation of the Python array API (previously numpy.array_api)
http://data-apis.org/array-api-strict/
Other
7 stars 4 forks source link

Fix asarray(copy=False) #15

Closed asmeurer closed 6 months ago

asmeurer commented 6 months ago

For NumPy 2.0, this is implemented directly. For NumPy 1, we emulate it by checking if asarray() creates a copy or not.

This also removes support for the np._CopyMode enum in asarray(), as this is not portable.

@rgommers I can't remember if there was a good reason that we didn't just emulate copy=False in this way originally (instead, we raised NotImplementedError).

asmeurer commented 6 months ago

Maybe it had to do with when obj is not an array. Are there instances where copy=False could fail with the buffer protocol?

rgommers commented 6 months ago

Maybe it had to do with when obj is not an array. Are there instances where copy=False could fail with the buffer protocol?

No, neither the buffer protocol nor __array_interface__ have that.

asmeurer commented 6 months ago

Searched through the discussion at https://github.com/numpy/numpy/pull/18585 and couldn't find anything. I think I just wasn't really clear at the time that numpy copy=False worked like copy=None.

asmeurer commented 6 months ago

OK, I updated this to handle Python built-ins and buffer protocol objects properly, and added some more tests since I don't think the test suite really checks copy that sufficiently.