This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: BZ2File objects do not have name attribute
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Hasan Diwan, hrik2001, jcd, jojko.sivek, nadeem.vawda, roysmith, stestagg
Priority: normal Keywords: patch

Created on 2015-05-21 09:16 by jojko.sivek, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
bz2file.tar jojko.sivek, 2015-05-21 09:16 tar ball with test and example of bz2 behaviour
bz2.name.patch Hasan Diwan, 2021-04-27 07:23
Repositories containing patches
https://bitbucket.org/cliff/cpython#python24258
Messages (7)
msg243743 - (view) Author: Jozef Sivek (jojko.sivek) Date: 2015-05-21 09:16
Unlike GzipFile the BZ2File does not have defined name attribute. The reading of this attribute results in: "AttributeError: 'BZ2File' object has no attribute 'name'".

This is truly missing feature and wrong behaviour, compare:
https://hg.python.org/releasing/3.4.3/file/933c3041d548/Lib/gzip.py
and 
https://hg.python.org/releasing/3.4.3/file/933c3041d548/Lib/bz2.py

In addition to that the BZ2File did have name attribute in python 2.7.* .

In attachment there is tar ball with example files, expected results in python 3 and results in python 2.
msg244095 - (view) Author: Cliff Dyer (jcd) Date: 2015-05-26 09:31
I'd be happy to take a look at this one, if no one else is working on it.
msg244622 - (view) Author: Cliff Dyer (jcd) Date: 2015-06-02 00:08
I've got a fix for this now.  In working on it, I've discovered that not all file-like objects have a .name attribute.  io.BytesIO (which is used all over the test suite) does not.  I've written a patch that always creates a .name attribute on BZ2File, but sets it to None if the associated file-like object has no .name.

See the linked branch
msg244623 - (view) Author: Cliff Dyer (jcd) Date: 2015-06-02 00:11
It's probably too late for this to get into 3.5, since we're already in betas.  Pushing back to 3.6.
msg392035 - (view) Author: Hasan Diwan (Hasan Diwan) Date: 2021-04-27 07:23
Patch adds a bz2.BZ2File.name property corresponding to the filename passed in.
msg392059 - (view) Author: Steve Stagg (stestagg) Date: 2021-04-27 12:17
Please may you add a test that uses "io.BytesIO()" as the filename argument.  BytesIO() objects do not have a 'name' attribute
msg392069 - (view) Author: Roy Smith (roysmith) Date: 2021-04-27 13:05
The https://bitbucket.org/cliff/cpython#python24258 URL 404's

Looking at the attached bz2.py diff, I would change:

         if isinstance(filename, (str, bytes, os.PathLike)):
             self._fp = _builtin_open(filename, mode)
+            self.filename = filename
             self._closefp = True
             self._mode = mode_code

to special-case os.PathLike and do:

            self.filename = str(filename)

I would expect BZ2File.name to be a string.   Returning a PathLike means lots of legitimate string methods will not work on it.  Also, it should be "name" as an attribute, not "name()" as a method, to match other existing interfaces.
History
Date User Action Args
2022-04-11 14:58:17adminsetgithub: 68446
2021-04-27 13:05:23roysmithsetmessages: + msg392069
2021-04-27 12:17:05stestaggsetnosy: + stestagg
messages: + msg392059
2021-04-27 07:23:47Hasan Diwansetfiles: + bz2.name.patch
versions: + Python 3.8, Python 3.9, Python 3.10, Python 3.11, - Python 3.6
nosy: + Hasan Diwan

messages: + msg392035

keywords: + patch
2021-04-27 06:24:43hrik2001setnosy: + hrik2001
2021-04-27 04:48:22roysmithsetnosy: + roysmith
2015-06-02 00:11:35jcdsetmessages: + msg244623
versions: + Python 3.6, - Python 3.5
2015-06-02 00:08:23jcdsethgrepos: + hgrepo311
messages: + msg244622
2015-05-26 09:31:03jcdsetnosy: + jcd
messages: + msg244095
2015-05-21 21:23:57ned.deilysetnosy: + nadeem.vawda
2015-05-21 09:16:55jojko.sivekcreate