Skip to content

Raising RuntimeError when trying to get empty body encoding#4481

Merged
asvetlov merged 3 commits intoaio-libs:masterfrom
Purusah:bugfix/4214
Jan 18, 2020
Merged

Raising RuntimeError when trying to get empty body encoding#4481
asvetlov merged 3 commits intoaio-libs:masterfrom
Purusah:bugfix/4214

Conversation

@Purusah
Copy link
Copy Markdown
Contributor

@Purusah Purusah commented Jan 3, 2020

What do these changes do?

Function get_encoding raise RuntimeError when user is trying to read not present body and header doesn't provide info about encoding

Are there changes in behavior for the user?

Make error more verbose

Related issue number

Fixes #4214

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jan 3, 2020
Vladimir Shulyak
Vladimir Zakharov
Vladyslav Bondar
Vladyslav Bohaichuk
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this should be one line up

decode a response. Some encodings detected by cchardet are not known by
Python (e.g. VISCII).

:raise RuntimeError: if body hasn't been read for :term:`cchardet` usage
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
:raise RuntimeError: if body hasn't been read for :term:`cchardet` usage
:raise RuntimeError: if called before the body has been read, for :term:`cchardet` usage

# RFC 7159 states that the default encoding is UTF-8.
encoding = 'utf-8'
elif self._body is None:
raise RuntimeError("Trying to get encoding of not read body")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
raise RuntimeError("Trying to get encoding of not read body")
raise RuntimeError('Cannot guess the encoding of a not yet read body')

content = response.content = mock.Mock()
content.read.side_effect = side_effect

with pytest.raises(RuntimeError):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
with pytest.raises(RuntimeError):
with pytest.raises(
RuntimeError,
match='^Cannot guess the encoding of a not yet read body$',
):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated PR, thank's for review
Does it always make sense to check exception message?

@codecov-io
Copy link
Copy Markdown

codecov-io commented Jan 6, 2020

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.51%. Comparing base (a249b41) to head (4aba8c0).
⚠️ Report is 5593 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4481   +/-   ##
=======================================
  Coverage   97.51%   97.51%           
=======================================
  Files          43       43           
  Lines        8865     8867    +2     
  Branches     1390     1391    +1     
=======================================
+ Hits         8645     8647    +2     
  Misses         96       96           
  Partials      124      124           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@asvetlov asvetlov merged commit 63a0d10 into aio-libs:master Jan 18, 2020
@asvetlov
Copy link
Copy Markdown
Member

Thanks!

asvetlov pushed a commit that referenced this pull request Jan 18, 2020
asvetlov added a commit that referenced this pull request Jan 18, 2020
) (#4512)

(cherry picked from commit 63a0d10)

Co-authored-by: Purusah <[email protected]>

Co-authored-by: Purusah <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ClientResponse.get_encoding() throws TypeError if it hasn't been read

4 participants