Skip to content
Myr

Catch AttributeError in utils.super_len (#5239)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Catch AttributeError in utils.super_len This allows it to handle files obtained via `Tarfile.extractfile()`.

authored by David Hotham · committed by Nate Prewitt · parent e77dd8d · Browse files · 3 files+22 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+22 added−3 removed

  • tests:+13 added−0 removed
  • other:+9 added−3 removed

3 files+22 added−3 removed

modified:requests/utils.py+4 added-1 removed
122 unmodified lines
123124 elif hasattr(o, 'fileno'):125 try:126 fileno = o.fileno()127 except io.UnsupportedOperation:127 except (io.UnsupportedOperation, AttributeError):128 # AttributeError is a surprising exception, seeing as how we've just checked129 # that `hasattr(o, 'fileno')`. It happens for objects obtained via130 # `Tarfile.extractfile()`, per issue 5229.131 pass132 else:133 total_length = os.fstat(fileno).st_size134
914 unmodified lines
modified:tests/test_utils.py+13 added
2 unmodified lines
3import os4import copy5import filecmp6from io import BytesIO7import tarfile8import zipfile9from collections import deque1011import pytest
74 unmodified lines
86 with file_obj.open(mode) as fd:87 assert super_len(fd) == 488 assert len(recwarn) == warnings_num8990 def test_tarfile_member(self, tmpdir):91 file_obj = tmpdir.join('test.txt')92 file_obj.write('Test')9394 tar_obj = str(tmpdir.join('test.tar'))95 with tarfile.open(tar_obj, 'w') as tar:96 tar.add(str(file_obj), arcname='test.txt')9798 with tarfile.open(tar_obj) as tar:99 member = tar.extractfile('test.txt')100 assert super_len(member) == 4101102 def test_super_len_with__len__(self):103 foo = [1,2,3,4]104 len_foo = super_len(foo)105 assert len_foo == 4
695 unmodified lines
modified:HISTORY.md+5 added-2 removed
9 unmodified lines
10 in the library. This gets raised in the `response.json()` method, and is11 backwards compatible as it inherits from previously thrown exceptions.12 Can be caught from `requests.exceptions.RequestException` as well.1314- Catch `AttributeError` when calculating length of files obtained by15 `Tarfile.extractfile()`16172.26.0 (2021-07-13)18-------------------1920**Improvements**
1683 unmodified lines
17041705- Automatic Authentication API Change1706- Smarter Query URL Parameterization1707- Allow file uploads and POST data together1705- 1708-17091710 New Authentication Manager System17111712 : - Simpler Basic HTTP System
10 unmodified lines
172317240.2.3 (2011-02-15)1725------------------17261724- 1727-17281729 New HTTPHandling Methods17301731 : - Response.\_\_nonzero\_\_ (false if bad HTTP Status)
28 unmodified lines