Skip to content

Support pinning of IPythonBuffer#1411

Merged
slozier merged 4 commits intoIronLanguages:masterfrom
BCSharp:pythonbuffer_pin
Apr 21, 2022
Merged

Support pinning of IPythonBuffer#1411
slozier merged 4 commits intoIronLanguages:masterfrom
BCSharp:pythonbuffer_pin

Conversation

@BCSharp
Copy link
Copy Markdown
Member

@BCSharp BCSharp commented Apr 19, 2022

Resolves #1297 though more can be done. Currently the lifetime of MemoryHolder is not managed efficiently when used from C#. It was like that before this change and it does not affect Python code, which relies on the finalizers and the GC, but for C# calls, it can be done more efficiently using Dispose(). I think there are more improvements/modernizations/cleanups possible here, and likely in the whole ctypes module, but that is for another PR (probably several).

I have locally other changes in ctypes that would address the remaining part from #1404 and I used them for testing, not sure should they go in the separate PR (since they address a separate issue) or here (since they validate the code changes).

@BCSharp BCSharp marked this pull request as draft April 19, 2022 19:11
@slozier
Copy link
Copy Markdown
Contributor

slozier commented Apr 19, 2022

Looks like a pretty clean solution. I like it.

I have locally other changes in ctypes that would address the remaining part from #1404 and I used them for testing, not sure should they go in the separate PR (since they address a separate issue) or here (since they validate the code changes).

I'm fine either way if the changes are small and doesn't make it hard to review this PR.

@BCSharp BCSharp marked this pull request as ready for review April 20, 2022 20:35
Copy link
Copy Markdown
Contributor

@slozier slozier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me (other than the change in memoryview error messages).

Comment thread Src/IronPython/Runtime/MemoryView.cs Outdated

if (flags.HasFlag(BufferFlags.CContiguous) && !_isCContig)
throw PythonOps.BufferError("memoryview: underlying buffer is not C-contiguous");
throw PythonOps.BufferError("memoryview: underlying buffer is not C contiguous");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought Python used "C-contiguous" with a dash?

struct.unpack("H", memoryview(b"AA")[::-1])

BufferError: memoryview: underlying buffer is not C-contiguous

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memoryview does, but ctypes doesn't.

c_short.from_buffer(memoryview(bytearray(b"AA"))[::-1])

TypeError: underlying buffer is not C contiguous

Unfortunately, tests for ctypes check for this form. Will see if I can get it both ways.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a peek at the CPython ctypes implementation and it seems like from_buffer is the odd one:

ctypes.c_int.from_buffer(memoryview(bytearray(b"aaaa"))[::-1])

TypeError: underlying buffer is not C contiguous

but from_buffer_copy gives the memoryview error:

ctypes.c_int.from_buffer_copy(memoryview(bytearray(b"aaaa"))[::-1])

BufferError: memoryview: underlying buffer is not C-contiguous

@slozier slozier merged commit d3f8d64 into IronLanguages:master Apr 21, 2022
@slozier
Copy link
Copy Markdown
Contributor

slozier commented Apr 21, 2022

Thanks!

@BCSharp BCSharp deleted the pythonbuffer_pin branch April 22, 2022 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support pinning of IPythonBuffer

2 participants