Skip to content

Keep caller-owned binary streams open after reading ASCII PLY - #92

Open
xixihaha-ha wants to merge 1 commit into
dranjan:masterfrom
xixihaha-ha:fix/ascii-stream-ownership
Open

xixihaha-ha wants to merge 1 commit into
dranjan:masterfrom
xixihaha-ha:fix/ascii-stream-ownership

Conversation

@xixihaha-ha

@xixihaha-ha xixihaha-ha commented Nov 3, 2026 •

Copy link
Copy Markdown

Reading ASCII PLY from a caller-supplied binary stream creates a TextIOWrapper that closes the underlying stream when destroyed. Consequently BytesIO.getvalue() or reusing the file after PlyData.read() raises ValueError. This also occurs after a parsing error, whereas text streams and binary PLY streams remain open.

Detach the internally created text wrapper in finally, leaving externally owned streams open while retaining the existing closure of files opened from paths. Add success and parse-error regression cases.

Validation: both new cases fail on unchanged code because the stream is closed. After the fix the complete test directory passes (123 passed), on Python 3.11 with NumPy 2.

AI assistance was used to investigate, implement and test this change.


📚 Documentation preview 📚: https://python-plyfile--92.org.readthedocs.build/en/92/

@dranjan

dranjan commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Huh...I thought I replied two weeks ago, but apparently I forgot to submit the comment. Sorry about that.

Thanks for reporting the bug, although this project doesn't accept AI-generated submissions, so this pull request can't be merged as-is. The .detach workaround is so trivial that the ultimate solution might end up looking very similar to what you submitted, although I also have some alternative approaches in mind.

Here's some more discussion and info on the problematic TextIOWrapper behavior: https://discuss.python.org/t/textiowrapper-probably-shouldnt-close-its-stream-when-gced/84346 I don't fully understand the justification given, but I need to think about it more. It seems even some CPython core developers find the behavior unintuitive. One of the justifications was to avoid this: python/cpython#105808

I can't give a firm ETA for applying this fix due to events in my personal life, but the most likely possibility is the first half of December. (I definitely won't be able to release the fix before December, whether or not it gets merged into master.)

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.

2 participants