From 1957ffb7359a6c4968fdd29b25f3fc825291b382 Mon Sep 17 00:00:00 2001 From: Marcin Wojdyr Date: Mon, 6 Jan 2025 20:44:33 +0100 Subject: [PATCH] python: replace Ddl.read_ddl_file(path) with Ddl.read_ddl(doc) --- include/gemmi/ddl.hpp | 1 + python/cif.cpp | 7 ++++--- tests/test_cif.py | 8 +++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/gemmi/ddl.hpp b/include/gemmi/ddl.hpp index a6a82034..36d974e0 100644 --- a/include/gemmi/ddl.hpp +++ b/include/gemmi/ddl.hpp @@ -37,6 +37,7 @@ struct GEMMI_DLL Ddl { Ddl(Ddl const&) = delete; Ddl& operator=(Ddl const&) = delete; + /// it moves doc to ddl_docs_ to control lifetime and prevent modifications void read_ddl(cif::Document&& doc); bool validate_cif(const cif::Document& doc) const; diff --git a/python/cif.cpp b/python/cif.cpp index e23c86aa..a1f6e963 100644 --- a/python/cif.cpp +++ b/python/cif.cpp @@ -462,9 +462,10 @@ void add_cif(nb::module_& cif) { nb::arg("use_context")=true, nb::arg("use_linked_groups")=true, nb::arg("use_mandatory")=true, nb::arg("use_unique_keys")=true) .def("set_logger", [](Ddl& self, gemmi::Logger&& logger) { self.logger = std::move(logger); }) - .def("read_ddl_file", [](Ddl& self, const std::string& path) { - self.read_ddl(gemmi::read_cif_gz(path)); - }, nb::arg("path")) + .def("read_ddl", [](Ddl& self, Document& doc) { + self.read_ddl(std::move(doc)); + doc.clear(); + }, nb::arg("doc")) .def("validate_cif", &Ddl::validate_cif) ; } diff --git a/tests/test_cif.py b/tests/test_cif.py index ad53d4b7..6a1cdbd7 100755 --- a/tests/test_cif.py +++ b/tests/test_cif.py @@ -390,9 +390,11 @@ def test_validation(self): """) msg_list = [] ddl = cif.Ddl(logger=(lambda msg: msg_list.append(msg), 6)) - ddl.read_ddl_file(full_path('mmcif_pdbx_v50_frag.dic')) - self.assertTrue(len(msg_list) == 1) - self.assertTrue(msg_list[0].startswith("Bad DDL2: can't parse regex")) + ddl.read_ddl(cif.read(full_path('mmcif_pdbx_v50_frag.dic'))) + if msg_list: + # regex for type binary may result in regex_error + self.assertEqual(len(msg_list), 1) + self.assertTrue(msg_list[0].startswith("Bad DDL2: can't parse regex")) msg_list = [] ddl.validate_cif(doc) self.assertEqual(msg_list,