Repository: jazzband/tablib
Pull request #518 (2022-02-21 16:00:02)

Add newline to StringIO
This is a fix for #517.
Adding the [newline](https://docs.python.org/3/glossary.html#term-universal-newlines) kwarg to `StringIO` handles interpreting the LF char.

https://docs.python.org/3/library/functions.html#open-newline-parameter

codecov[bot]: # [Codecov](https://codecov.io/gh/jazzband/tablib/pull/518?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) Report
> Merging [#518](https://codecov.io/gh/jazzband/tablib/pull/518?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) (c9f3060) into [master](https://codecov.io/gh/jazzband/tablib/commit/d9a4a9790a4d94185f8cac022572480bac593319?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) (d9a4a97) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head c9f3060 differs from pull request most recent head 12aace0. Consider uploading reports for the commit 12aace0 to get more accurate results

[![Impacted file tree graph](https://codecov.io/gh/jazzband/tablib/pull/518/graphs/tree.svg?width=650&height=150&src=pr&token=ns7MUP4H8t&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband)](https://codecov.io/gh/jazzband/tablib/pull/518?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband)

```diff
@@            Coverage Diff             @@
##           master     #518      +/-   ##
==========================================
+ Coverage   90.82%   90.84%   +0.01%     
==========================================
  Files          28       28              
  Lines        2660     2664       +4     
==========================================
+ Hits         2416     2420       +4     
  Misses        244      244              
```


| [Impacted Files](https://codecov.io/gh/jazzband/tablib/pull/518?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) | Coverage Δ | |
|---|---|---|
| [src/tablib/utils.py](https://codecov.io/gh/jazzband/tablib/pull/518/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband#diff-c3JjL3RhYmxpYi91dGlscy5weQ==) | `100.00% <100.00%> (ø)` | |
| [tests/test\_tablib.py](https://codecov.io/gh/jazzband/tablib/pull/518/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband#diff-dGVzdHMvdGVzdF90YWJsaWIucHk=) | `98.62% <100.00%> (+<0.01%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/jazzband/tablib/pull/518?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/jazzband/tablib/pull/518?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband). Last update [d9a4a97...12aace0](https://codecov.io/gh/jazzband/tablib/pull/518?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband).

claudep: Thanks! May I ask you to also add a line in `HISTORY.md`?
matthewhegarty: Have updated HISTORY as requested.

Patch:
diff --git a/AUTHORS b/AUTHORS
index a3d356d..2ca69cc 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -25,6 +25,7 @@ Here is a list of passed and present much-appreciated contributors:
     Mark Rogers
     Mark Walling
     Mathias Loesch
+    Matthew Hegarty
     Mike Waldner
     Peyman Salehi
     Rabin Nankhwa
diff --git a/HISTORY.md b/HISTORY.md
index bf22c9c..23cb133 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -1,5 +1,11 @@
 # History
 
+## 3.2.1 (unreleased)
+
+### Bugfixes
+
+- Support solo CR in text input imports (#518).
+
 ## 3.2.0 (2022-01-27)
 
 ### Changes
diff --git a/src/tablib/utils.py b/src/tablib/utils.py
index 39de8ce..97bae72 100644
--- a/src/tablib/utils.py
+++ b/src/tablib/utils.py
@@ -7,7 +7,7 @@ def normalize_input(stream):
     file-like object.
     """
     if isinstance(stream, str):
-        return StringIO(stream)
+        return StringIO(stream, newline='')
     elif isinstance(stream, bytes):
         return BytesIO(stream)
     return stream


Test patch:
diff --git a/tests/test_tablib.py b/tests/test_tablib.py
index 0ee4e86..3821cc0 100755
--- a/tests/test_tablib.py
+++ b/tests/test_tablib.py
@@ -824,6 +824,14 @@ class CSVTests(BaseTestCase):
         dataset = tablib.import_set(csv_text, format="csv", skip_lines=2)
         self.assertEqual(dataset.headers, ['id', 'name','description'])
 
+    def test_csv_import_mac_os_lf(self):
+        csv_text = (
+            'id,name,description\r'
+            '12,Smith,rounded\r'
+        )
+        dataset = tablib.import_set(csv_text, format="csv")
+        self.assertEqual('id,name,description\r\n12,Smith,rounded\r\n', dataset.csv)
+
     def test_csv_export(self):
         """Verify exporting dataset object as CSV."""