Title: Misleading AttributeError accessing fileno attribute in tarfile
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu.Popa, ZackerySpytz, lars.gustaebel, serhiy.storchaka, xtreak
Priority: normal Keywords: patch

Created on 2019-10-24 06:55 by xtreak, last changed 2020-09-09 20:27 by ZackerySpytz.

Pull Requests
URL Status Linked Edit
PR 17449 closed Claudiu.Popa, 2019-12-03 11:04
PR 22178 open ZackerySpytz, 2020-09-09 20:27
Messages (2)
msg355283 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-24 06:55
This came up during . While fileno returns True as an attribute the actual implementation uses self.raw.fileno where the AttributeError is raised at (Python implementation) . I guess it should be raising UnsupportedOperation when self.raw has no fileno so that the callers only need to catch UnsupportedOperation. Something like below for fileno implementation that will give a better exception.

   def fileno(self):
        if hasattr(self.raw, 'fileno'):
            return self.raw.fileno()


import tarfile
import os

for f in ["foo.txt", "foo.tar"]:
    except OSError:

with open("foo.txt", "w") as f:
    with"foo.tar", "w:gz") as tarfile_obj:

with"foo.tar", "r:gz") as f:
    member = f.extractfile("foo.txt")
    print(f"Has attr : {hasattr(member, 'fileno')}")

# Current exception

python.exe .\
Has attr : True
Traceback (most recent call last):
  File ".\", line 18, in <module>
AttributeError: '_FileInFile' object has no attribute 'fileno'

# Proposed exception (Patching tarfile to use _pyio and modifying fileno as above) 

python.exe .\
Has attr : True
Traceback (most recent call last):
  File ".\", line 11, in <module>
  File "C:\Users\kasingar\AppData\Local\Programs\Python\Python37-32\lib\", line 831, in fileno
  File "C:\Users\kasingar\AppData\Local\Programs\Python\Python37-32\lib\", line 320, in _unsupported
    (self.__class__.__name__, name))
io.UnsupportedOperation: ExFileObject.fileno() not supported
msg357749 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2019-12-03 11:05
Just submitted a PR for this issue, hope I got it right. Curious to see if there are any backwards compatibility concerns with this approach.
Date User Action Args
2020-09-09 20:27:43ZackerySpytzsetnosy: + ZackerySpytz
pull_requests: + pull_request21244
2019-12-03 11:05:44Claudiu.Popasetmessages: + msg357749
2019-12-03 11:04:48Claudiu.Popasetkeywords: + patch
stage: patch review
pull_requests: + pull_request16930
2019-12-03 09:37:44Claudiu.Popasetnosy: + Claudiu.Popa
2019-10-24 06:55:32xtreakcreate