Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

fix(python): escape carriage returns in dataframe literals

Rudy Celekli00042ff6210f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6646495d..5e835230 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- Preserve carriage returns and CRLF text when importing dataframe rows into the Python engine.
+
- Keep the last active ontology intact when parsing a replacement source, cache, or refresh fails; publish successful replacements in one graph transaction.
- Bound wildcard repository filtering to pattern/filename pairs so repeated stars cannot stall `onto_repo_list`.
- Sanitize quotes and ASCII control bytes in mapped IRI components so valid structured data does not fail RDF ingestion.
diff --git a/python/src/open_ontologies_lite/dataframe.py b/python/src/open_ontologies_lite/dataframe.py
index e99cbc87..41ad0515 100644
--- a/python/src/open_ontologies_lite/dataframe.py
+++ b/python/src/open_ontologies_lite/dataframe.py
@@ -56,7 +56,13 @@ def _literal(value: Any) -> str | None:
return f'"{value}"^^<{XSD}integer>'
if isinstance(value, float):
return f'"{value!r}"^^<{XSD}double>'
- escaped = str(value).replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n")
+ escaped = (
+ str(value)
+ .replace("\\", "\\\\")
+ .replace('"', '\\"')
+ .replace("\n", "\\n")
+ .replace("\r", "\\r")
+ )
return f'"{escaped}"'
diff --git a/python/tests/test_dataframe.py b/python/tests/test_dataframe.py
index fe7faf6b..c486d3f5 100644
--- a/python/tests/test_dataframe.py
+++ b/python/tests/test_dataframe.py
@@ -84,6 +84,35 @@ def test_engine_load_rows_roundtrip():
assert [r["name"] for r in res["rows"]] == ["Bakery", "Dairy"]
+ "note",
+ [
+ pytest.param("left\rright", id="carriage-return"),
+ pytest.param("left\r\nright", id="crlf"),
+ pytest.param('say "hi"\\bye', id="quote-and-backslash"),
+ pytest.param("left\nright", id="newline"),
+ pytest.param("left\tright", id="tab"),
+ pytest.param("ordinary text", id="ordinary"),
+ ],
+)
+def test_engine_load_rows_preserves_string_line_endings(note):
+ engine = OntologyEngine()
+ count = engine.load_rows(
+ [{"note": note, "integer": 7, "double": 1.5, "boolean": True, "absent": None}],
+ base_iri="http://x.org/",
+ )
+ assert count == 4
+ result = engine.query(
+ "SELECT ?note ?integer ?double ?boolean WHERE { "
+ "<http://x.org/0> <http://x.org/note> ?note ; "
+ "<http://x.org/integer> ?integer ; <http://x.org/double> ?double ; "
+ "<http://x.org/boolean> ?boolean }"
+ )
+ assert result["rows"] == [
+ {"note": note, "integer": "7", "double": "1.5", "boolean": "true"}
+ ]
+
+
def test_real_fenic_dataframe_if_installed():
fc = pytest.importorskip("fenic")
session = fc.Session.get_or_create(fc.SessionConfig(app_name="oo_lite_test"))