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

Catch AttributeError in utils.super_len #5239

Merged
merged 2 commits into from Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions HISTORY.md
Expand Up @@ -11,6 +11,9 @@ dev
backwards compatible as it inherits from previously thrown exceptions.
Can be caught from `requests.exceptions.RequestException` as well.

- Catch `AttributeError` when calculating length of files obtained by
`Tarfile.extractfile()`

2.26.0 (2021-07-13)
-------------------

Expand Down Expand Up @@ -1702,7 +1705,7 @@ This is not a backwards compatible change.
- Automatic Authentication API Change
- Smarter Query URL Parameterization
- Allow file uploads and POST data together
-
-

New Authentication Manager System

Expand All @@ -1721,7 +1724,7 @@ This is not a backwards compatible change.
0.2.3 (2011-02-15)
------------------

-
-

New HTTPHandling Methods

Expand Down
5 changes: 4 additions & 1 deletion requests/utils.py
Expand Up @@ -124,7 +124,10 @@ def super_len(o):
elif hasattr(o, 'fileno'):
try:
fileno = o.fileno()
except io.UnsupportedOperation:
except (io.UnsupportedOperation, AttributeError):
# AttributeError is a surprising exception, seeing as how we've just checked
# that `hasattr(o, 'fileno')`. It happens for objects obtained via
# `Tarfile.extractfile()`, per issue 5229.
pass
else:
total_length = os.fstat(fileno).st_size
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Expand Up @@ -4,6 +4,7 @@
import copy
import filecmp
from io import BytesIO
import tarfile
import zipfile
from collections import deque

Expand Down Expand Up @@ -86,6 +87,18 @@ def test_file(self, tmpdir, mode, warnings_num, recwarn):
assert super_len(fd) == 4
assert len(recwarn) == warnings_num

def test_tarfile_member(self, tmpdir):
file_obj = tmpdir.join('test.txt')
file_obj.write('Test')

tar_obj = str(tmpdir.join('test.tar'))
with tarfile.open(tar_obj, 'w') as tar:
tar.add(str(file_obj), arcname='test.txt')

with tarfile.open(tar_obj) as tar:
member = tar.extractfile('test.txt')
assert super_len(member) == 4

def test_super_len_with__len__(self):
foo = [1,2,3,4]
len_foo = super_len(foo)
Expand Down