From 8154a68f83b33b3b6e14c3d1fc05c3b6c2b12376 Mon Sep 17 00:00:00 2001 From: miute Date: Sat, 27 Jun 2026 18:55:21 +0900 Subject: [PATCH] Replace py::enum_ with py::native_enum --- CHANGELOG.md | 2 ++ src/main.cpp | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6cece..c33fac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Add `pugixml.pugi.XMLNode.ensure_attribute(name: str)` - Add `pugixml.pugi.XMLNode.ensure_child(name: str)` - Bump pybind11 from 2.13.6 to 3.0.4 ([#141], [#159], [#198]) +- Replace the base class of all enums from `pybind11_object` to `enum.IntEnum` ([#202]) ### Added @@ -142,3 +143,4 @@ Initial release. [#198]: https://github.com/miute/pugixml-python/pull/198 [#200]: https://github.com/miute/pugixml-python/pull/200 [#201]: https://github.com/miute/pugixml-python/pull/201 +[#202]: https://github.com/miute/pugixml-python/pull/202 diff --git a/src/main.cpp b/src/main.cpp index 62ab44f..da26a4b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -179,7 +180,7 @@ PYBIND11_MODULE(MODULE_NAME, m) { m.attr("DEFAULT_DOUBLE_PRECISION") = default_double_precision; m.attr("DEFAULT_FLOAT_PRECISION") = default_float_precision; - py::enum_(m, "XMLNodeType", "Tree node types.") + py::native_enum(m, "XMLNodeType", "enum.IntEnum", "Tree node types.") .value("NODE_NULL", node_null, "Empty (null) node handle.") .value("NODE_DOCUMENT", node_document, "A document tree's absolute root.") .value("NODE_ELEMENT", node_element, "Element tag, i.e. ''") @@ -189,9 +190,11 @@ PYBIND11_MODULE(MODULE_NAME, m) { .value("NODE_PI", node_pi, "Processing instruction, i.e. ''") .value("NODE_DECLARATION", node_declaration, "Document declaration, i.e. ''") .value("NODE_DOCTYPE", node_doctype, "Document type declaration, i.e. ''") - .export_values(); + .export_values() + .finalize(); - py::enum_(m, "XMLEncoding", "These flags determine the encoding of input/output data for XML document.") + py::native_enum(m, "XMLEncoding", "enum.IntEnum", + "These flags determine the encoding of input/output data for XML document.") .value("ENCODING_AUTO", encoding_auto, "Auto-detect input encoding using BOM or '<' / '(m, "XMLParseStatus", - "Parsing status, returned as part of :class:`XMLParseResult` object.") + py::native_enum(m, "XMLParseStatus", "enum.IntEnum", + "Parsing status, returned as part of :class:`XMLParseResult` object.") .value("STATUS_OK", status_ok, "No error.") .value("STATUS_FILE_NOT_FOUND", status_file_not_found, "File was not found during XMLDocument.load_file().") .value("STATUS_IO_ERROR", status_io_error, "Error reading from file/stream.") @@ -232,15 +236,17 @@ PYBIND11_MODULE(MODULE_NAME, m) { "XMLNode.append_buffer()).") .value("STATUS_NO_DOCUMENT_ELEMENT", status_no_document_element, "Parsing resulted in a document without element nodes.") - .export_values(); + .export_values() + .finalize(); - py::enum_(m, "XPathValueType", "XPath query return type.") + py::native_enum(m, "XPathValueType", "enum.IntEnum", "XPath query return type.") .value("XPATH_TYPE_NONE", xpath_type_none, "Unknown type (query failed to compile).") .value("XPATH_TYPE_NODE_SET", xpath_type_node_set, "Node set (XPathNodeSet).") .value("XPATH_TYPE_NUMBER", xpath_type_number, "Number.") .value("XPATH_TYPE_STRING", xpath_type_string, "String.") .value("XPATH_TYPE_BOOLEAN", xpath_type_boolean, "Boolean.") - .export_values(); + .export_values() + .finalize(); py::class_ atst(m, "XMLAttributeStruct", R"doc( The internal object of the attribute. @@ -2750,11 +2756,12 @@ PYBIND11_MODULE(MODULE_NAME, m) { // // pugi::xpath_node_set // - py::enum_(xpns, "Type", "Collection type.") + py::native_enum(xpns, "Type", "enum.IntEnum", "Collection type.") .value("TYPE_UNSORTED", xpath_node_set::type_unsorted, "Not ordered.") .value("TYPE_SORTED", xpath_node_set::type_sorted, "Sorted by document order (ascending).") .value("TYPE_SORTED_REVERSE", xpath_node_set::type_sorted_reverse, "Sorted by document order (descending).") - .export_values(); + .export_values() + .finalize(); xpns.def(py::init<>(), "\tInitialize ``XPathNodeSet`` as an empty collection.") .def(py::init(), py::arg("other"),