Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising RuntimeError when trying to get empty body encoding #4481

Merged
merged 3 commits into from
Jan 18, 2020

Conversation

Purusah
Copy link
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
@@ -268,6 +268,7 @@ Vladimir Rutsky
Vladimir Shulyak
Vladimir Zakharov
Vladyslav Bondar
Vladyslav Bohaichuk
Copy link
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

@@ -1305,6 +1305,8 @@ Response object
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
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

@@ -938,6 +938,8 @@ def get_encoding(self) -> str:
if mimetype.type == 'application' and mimetype.subtype == 'json':
# 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
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
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
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

codecov-io commented Jan 6, 2020

Codecov Report

Merging #4481 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4481      +/-   ##
==========================================
+ Coverage   97.51%   97.51%   +<.01%     
==========================================
  Files          43       43              
  Lines        8865     8867       +2     
  Branches     1390     1391       +1     
==========================================
+ Hits         8645     8647       +2     
  Misses         96       96              
  Partials      124      124
Impacted Files Coverage Δ
aiohttp/client_reqrep.py 97% <100%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a249b41...4aba8c0. Read the comment docs.

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

Thanks!

asvetlov pushed a commit that referenced this pull request Jan 18, 2020
)

(cherry picked from commit 63a0d10)

Co-authored-by: Purusah <16886633+Purusah@users.noreply.github.com>
asvetlov added a commit that referenced this pull request Jan 18, 2020
) (#4512)

(cherry picked from commit 63a0d10)

Co-authored-by: Purusah <16886633+Purusah@users.noreply.github.com>

Co-authored-by: Purusah <16886633+Purusah@users.noreply.github.com>
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