Skip to content

Commit c67afe4

Browse files
burkovaeevo-andrempenkov
authored
Correctly pass newline parameter to built-in open function (piskvorky#478)
* fix(smart_open_lib): add missing newline parameter to _shortcut_open * fix(smart_open_lib): add missing newline parameter to _shortcut_open * minor fixes to get tests to pass * unskip S3 test * adjust newline unit tests * update CHANGELOG.md * fix flake8 * fix: merge conflicts in changelog Co-authored-by: Andre Burkovski <andre.burkovski@evosoft.com> Co-authored-by: Michael Penkov <m@penkov.dev>
1 parent 93510da commit c67afe4

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Correctly pass `newline` parameter to built-in `open` function (PR [#478](https://github.com/RaRe-Technologies/smart_open/pull/478), [@burkovae](https://github.com/burkovae))
4+
35
# 2.0.0, 27 April 2020, "Python 3"
46

57
- **This version supports Python 3 only** (3.5+).

smart_open/smart_open_lib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def open(
187187
buffering=buffering,
188188
encoding=encoding,
189189
errors=errors,
190+
newline=newline,
190191
)
191192
if fobj is not None:
192193
return fobj
@@ -316,6 +317,7 @@ def _shortcut_open(
316317
buffering=-1,
317318
encoding=None,
318319
errors=None,
320+
newline=None,
319321
):
320322
"""Try to open the URI using the standard library io.open function.
321323
@@ -331,7 +333,6 @@ def _shortcut_open(
331333
332334
:param str uri: A string indicating what to open.
333335
:param str mode: The mode to pass to the open function.
334-
:param dict kw:
335336
:returns: The opened file
336337
:rtype: file
337338
"""
@@ -348,10 +349,11 @@ def _shortcut_open(
348349
return None
349350

350351
open_kwargs = {}
351-
352352
if encoding is not None:
353353
open_kwargs['encoding'] = encoding
354354
mode = mode.replace('b', '')
355+
if newline is not None:
356+
open_kwargs['newline'] = newline
355357

356358
#
357359
# binary mode of the builtin/stdlib open function doesn't take an errors argument

smart_open/tests/test_smart_open.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88

99
import bz2
10+
import csv
1011
import contextlib
1112
import io
1213
import unittest
@@ -911,7 +912,6 @@ def test_s3_read_moto(self):
911912

912913
self.assertEqual(content[14:], smart_open_object.read()) # read the rest
913914

914-
@unittest.skip('seek functionality for S3 currently disabled because of Issue #152')
915915
@mock_s3
916916
def test_s3_seek_moto(self):
917917
"""Does seeking in S3 files work correctly?"""
@@ -1076,6 +1076,29 @@ def test_append_binary_absolute_path(self):
10761076
mock_open.assert_called_with("/some/file.txt", "wb+", buffering=-1)
10771077
fout.write(self.as_bytes)
10781078

1079+
def test_newline(self):
1080+
with mock.patch(_BUILTIN_OPEN, mock.Mock(return_value=self.bytesio)) as mock_open:
1081+
smart_open.smart_open("/some/file.txt", "wb+", newline='\n')
1082+
mock_open.assert_called_with("/some/file.txt", "wb+", buffering=-1, newline='\n')
1083+
1084+
def test_newline_csv(self):
1085+
#
1086+
# See https://github.com/RaRe-Technologies/smart_open/issues/477
1087+
#
1088+
rows = [{'name': 'alice', 'color': 'aqua'}, {'name': 'bob', 'color': 'blue'}]
1089+
expected = 'name,color\nalice,aqua\nbob,blue\n'
1090+
1091+
with named_temporary_file(mode='w') as tmp:
1092+
with smart_open.open(tmp.name, 'w+', newline='\n') as fout:
1093+
out = csv.DictWriter(fout, fieldnames=['name', 'color'])
1094+
out.writeheader()
1095+
out.writerows(rows)
1096+
1097+
with open(tmp.name, 'r') as fin:
1098+
content = fin.read()
1099+
1100+
assert content == expected
1101+
10791102
@mock.patch('boto3.Session')
10801103
def test_s3_mode_mock(self, mock_session):
10811104
"""Are s3:// open modes passed correctly?"""

0 commit comments

Comments
 (0)