Handle text-mode file objects in parse_wpc_surface_bulletin#4068
Open
gaoflow wants to merge 1 commit into
Open
Handle text-mode file objects in parse_wpc_surface_bulletin#4068gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
parse_wpc_surface_bulletin documents accepting a file-like object, but it
unconditionally called .decode('utf-8') on the result of file.read(). For a
text-mode object such as io.StringIO, read() returns str, so this raised
AttributeError: 'str' object has no attribute 'decode'.
Only decode when read() returns bytes, so both binary (file paths opened in
'rb', BytesIO) and text (StringIO, files opened in text mode) sources work as
the docstring promises. Add a regression test asserting a StringIO parses
identically to the equivalent BytesIO, and move the misplaced 'year' entry from
the Returns section to Parameters in the docstring.
Closes Unidata#3923
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
parse_wpc_surface_bulletindocuments accepting a "str or file-like object", but it unconditionally calls.decode('utf-8')on the result offile.read().open_as_neededopens filename paths in binary mode, but a file-like object passed in directly is returned untouched — so a text-mode object such asio.StringIOmakesread()returnstr, and the.decode()call raises:Fixes #3923.
Reproduction
Fix
Only decode when
read()returnsbytes:I went with an explicit
isinstance(text, bytes)check rather than a broadtry/except AttributeError(discussed in the issue): it handles exactly the two things the docstring promises — binary sources ('rb'file paths,BytesIO) and text sources (StringIO, text-mode files) — without swallowing unrelatedAttributeErrors or revisiting the Python 2→3 byte-handling story more broadly.Tests / docs
test_parse_wpc_surface_bulletin_text_file_object, asserting aStringIOparses identically (pd.testing.assert_frame_equal) to the equivalentBytesIO.yearentry from the Returns section to Parameters in the docstring (it is a parameter, not a return value).tests/io/test_text.py(all 5), thetext.pydoctest, andruffpass locally.