Skip to content

Raise PlyHeaderParseError instead of UnicodeDecodeError on a non-ASCII header - #91

Merged
dranjan merged 1 commit into
dranjan:masterfrom
Erfouni:header-non-ascii-parse-error
Oct 15, 2026
Merged

dranjan merged 1 commit into
dranjan:masterfrom
Erfouni:header-non-ascii-parse-error

Conversation

@Erfouni

@Erfouni Erfouni commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

If you pass PlyData.read a file that is not a PLY file and whose first bytes are not ASCII (for example a PNG, or any binary file by mistake), it fails with a bare UnicodeDecodeError:

import io
from plyfile import PlyData
PlyData.read(io.BytesIO(b'\x89PNG\r\n\x1a\n'))
# UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 ...

A non-PLY ASCII file already gives a clean PlyHeaderParseError ("expected 'ply'"), so this makes the binary case behave the same way. _PlyHeaderLines._decode does a strict s.decode('ascii'); I wrapped it to raise PlyHeaderParseError on UnicodeDecodeError. Since _decode is also used while reading the rest of the header, this covers non-ASCII bytes mid-header too. Valid files are unaffected. Added a test.


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

_PlyHeaderLines._decode did a strict s.decode('ascii'), so reading a file whose
first bytes are not ASCII (e.g. a PNG or other binary passed by mistake) raised
a bare UnicodeDecodeError instead of the PlyHeaderParseError that a non-PLY
ASCII file already gives. Catch UnicodeDecodeError in _decode and raise
PlyHeaderParseError, which also covers non-ASCII bytes later in the header.
Added a test.
@dranjan
dranjan self-requested a review October 8, 2026 22:19
@dranjan

dranjan commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Hey @Erfouni, thanks for the contribution! This shouldn't take long to review. From a cursory glance, I didn't see anything concerning. Also thanks for adding the test.

@dranjan dranjan left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks good to me. It occurred to me that we're not using the raise from idiom, but that's separate from this pull request.

@dranjan
dranjan merged commit 9191fcc into dranjan:master Oct 15, 2026
8 checks passed
@Erfouni
Erfouni deleted the header-non-ascii-parse-error branch October 16, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants