FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-47217: add name attribute to bz2 fileobject by ellaellela · Pull Request #32311 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions Lib/bz2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def __init__(self, filename, mode="r", *, compresslevel=9):
else:
self._pos = 0

if not isinstance(filename, (str, bytes)):
self.name = ''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Could this be some recognizable sentinel (<stream> or something better) rather than empty string?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This seemed the easiest way of doing it without complicating the logic and the tests. Also, I couldn't find a uniform way of dealing with a lack of "name" for BytesIO() objects in other libraries, so I did not want to introduce anything "new and specific".
Do you have an example (or an idea) of a sentinel use that would fit here? In that case, we could do a similar change for gzip, bz2, lzma, ... libraries to have a bit more standardized way of dealing with this situation when we have BytesIO() objects.

else:
self.name = os.fspath(filename)

def close(self):
"""Flush and close the file.

Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_bz2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ def testBadArgs(self):
# compresslevel is keyword-only
self.assertRaises(TypeError, BZ2File, os.devnull, "r", 3)

def testNameFile(self):
self.createTempFile()
with BZ2File(self.filename) as bz2f:
self.assertTrue(hasattr(bz2f, 'name'))
self.assertTrue(bz2f.name == self.filename)

def testNameBytesIO(self):
bz2f = BZ2File(BytesIO(self.DATA))
try:
self.assertTrue(hasattr(bz2f, 'name'))
self.assertTrue(bz2f.name == '')
finally:
bz2f.close()

bz2f = BZ2File(BytesIO(), "w")
try:
self.assertTrue(hasattr(bz2f, 'name'))
self.assertTrue(bz2f.name == '')
finally:
bz2f.close()

def testRead(self):
self.createTempFile()
with BZ2File(self.filename) as bz2f:
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added name attribute for BZ2File (where appropriate and possible).

Back | FazBrowse Home | New Git URL