Skip to content

Commit

Permalink
Merge pull request #42 from trossi/fix-empty-string
Browse files Browse the repository at this point in the history
Fix parsing ascii file with empty string
  • Loading branch information
vnmabus authored Aug 16, 2024
2 parents fcf0591 + 15137c9 commit 5166258
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 1 addition & 3 deletions rdata/parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,8 @@ def parse_R_object( # noqa: N802, C901, PLR0912, PLR0915

elif info.type == RObjectType.CHAR:
length = self.parse_int()
if length > 0:
if length >= 0:
value = self.parse_string(length=length)
elif length == 0:
value = b""
elif length == -1:
value = None
else:
Expand Down
4 changes: 2 additions & 2 deletions rdata/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ def execute_r_data_source(
with tempfile.NamedTemporaryFile("w") as file:
file.write(source)
file.flush()
subprocess.check_call(
["Rscript", file.name], # noqa: S603, S607
subprocess.check_call( # noqa: S603
["Rscript", file.name], # noqa: S607
)
11 changes: 11 additions & 0 deletions rdata/tests/data/test_ascii_empty_str.rds
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
A
3
262914
197888
5
UTF-8
16
1
262153
0

5 changes: 5 additions & 0 deletions rdata/tests/test_rdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ def test_empty_string(self) -> None:
"test_empty_str": [""],
})

def test_ascii_empty_string(self) -> None:
"""Test that the empty string is parsed correctly from ascii file."""
data = rdata.read_rds(TESTDATA_PATH / "test_ascii_empty_str.rds")
np.testing.assert_equal(data, [""])

def test_na_string(self) -> None:
"""Test that the NA string is parsed correctly."""
data = rdata.read_rda(TESTDATA_PATH / "test_na_string.rda")
Expand Down

0 comments on commit 5166258

Please sign in to comment.