Skip to content

Commit cc247c5

Browse files
piskvorkympenkov
andauthored
Bring back filename parameter to compression_wrapper (piskvorky#497)
* bring back filename to compression_wrapper * fix docstring * move fallback inside try-catch * fix flake8 failures (unrelated to PR) * Update smart_open/compression.py Co-authored-by: Michael Penkov <m@penkov.dev> Co-authored-by: Michael Penkov <m@penkov.dev>
1 parent c67afe4 commit cc247c5

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

smart_open/compression.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,22 @@ def _handle_gzip(file_obj, mode):
6262
return gzip.GzipFile(fileobj=file_obj, mode=mode)
6363

6464

65-
def compression_wrapper(file_obj, mode):
65+
def compression_wrapper(file_obj, mode, filename=None):
6666
"""
6767
This function will wrap the file_obj with an appropriate
6868
[de]compression mechanism based on the extension of the filename.
6969
7070
file_obj must either be a filehandle object, or a class which behaves
71-
like one. It must have a .name attribute.
71+
like one. It must have a .name attribute unless ``filename`` is given.
7272
7373
If the filename extension isn't recognized, will simply return the original
7474
file_obj.
75-
"""
7675
76+
"""
7777
try:
78-
_, ext = os.path.splitext(file_obj.name)
78+
if filename is None:
79+
filename = file_obj.name
80+
_, ext = os.path.splitext(filename)
7981
except (AttributeError, TypeError):
8082
logger.warning(
8183
'unable to transparently decompress %r because it '

smart_open/doctools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def extract_examples_from_readme_rst(indent=' '):
163163
start = lines.index('.. _doctools_before_examples:\n')
164164
end = lines.index(".. _doctools_after_examples:\n")
165165
lines = lines[start+4:end-2]
166-
return ''.join([indent + re.sub('^ ', '', l) for l in lines])
166+
return ''.join([indent + re.sub('^ ', '', line) for line in lines])
167167
except Exception:
168168
return indent + 'See README.rst'
169169

smart_open/tests/test_smart_open.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ def test_readline_iter(self):
721721

722722
reader = smart_open.smart_open("s3://mybucket/mykey", "rb")
723723

724-
actual_lines = [l.decode("utf-8") for l in reader]
724+
actual_lines = [line.decode("utf-8") for line in reader]
725725
self.assertEqual(2, len(actual_lines))
726726
self.assertEqual(lines[0], actual_lines[0])
727727
self.assertEqual(lines[1], actual_lines[1])
@@ -1273,9 +1273,7 @@ def write_callback(request):
12731273

12741274

12751275
class CompressionFormatTest(unittest.TestCase):
1276-
"""
1277-
Test that compression
1278-
"""
1276+
"""Test transparent (de)compression."""
12791277

12801278
def write_read_assertion(self, suffix):
12811279
test_file = make_buffer(name='file' + suffix)

smart_open/tests/test_smart_open_old.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def test_readline_iter(self):
218218

219219
reader = smart_open.smart_open("s3://mybucket/mykey", "rb")
220220

221-
actual_lines = [l.decode("utf-8") for l in reader]
221+
actual_lines = [line.decode("utf-8") for line in reader]
222222
self.assertEqual(2, len(actual_lines))
223223
self.assertEqual(lines[0], actual_lines[0])
224224
self.assertEqual(lines[1], actual_lines[1])

0 commit comments

Comments
 (0)