X Tutup
Skip to content

Xml elementtree port#64

Open
mcepl wants to merge 7 commits intonima:masterfrom
openSUSE-Python:xml-elementtree-port
Open

Xml elementtree port#64
mcepl wants to merge 7 commits intonima:masterfrom
openSUSE-Python:xml-elementtree-port

Conversation

@mcepl
Copy link

@mcepl mcepl commented Mar 3, 2026

No description provided.

mcepl added 6 commits March 2, 2026 22:58
Replace Python libxml2 XML objects with ElementTree wrappers and make the C extension return serialized XML bytes. Update tests/examples and drop xml2mod linkage and packaging deps as the next step toward removing Python libxml2 bindings.
Update the test mock to accept the simplified positional xmlapi('t', rtype, typeid) call shape used by the port so the integration test reflects the real extension behavior.
Accept legacy xmlapi() call shapes and make Makefile/unit tests resilient to modern Python build directories.
@mcepl mcepl marked this pull request as ready for review March 9, 2026 13:59
@mcepl
Copy link
Author

mcepl commented Mar 9, 2026

Code Review

This is a well-motivated, largely well-executed modernization effort. The key goal — removing the python-libxml2 / xml2mod Python binding dependency — is cleanly achieved. The C layer correctly retains libxml2 for its internal XML work while exposing a clean string-based boundary to Python. The test updates show real thought about the new type contracts.


What the PR does well

  • Clean boundary: The C extension now serializes XML to bytes and Python re-parses it with xml.etree.ElementTree. This completely eliminates the python-libxml2 / xml2mod shared-library dependency on the Python side.
  • src/setup_common.py: Correctly removes the xml2mod force-link. The comment explaining why is appreciated.
  • Makefile: The modern SOABI/cache_tag-based build path detection is a good fix for Python 3.8+.
  • Test suite: _add_build_paths() makes tests resilient across Python version build directory layouts, and the isinstance(..., dmidecode.XmlNode/XmlDoc) assertions correctly reflect the new type contracts.

🔴 Blocking issues

1. XmlNode.__getattr__ / XmlDoc.__getattr__ — infinite recursion risk

If __init__ has not yet finished (e.g. during copy.copy, unpickling, or certain subclassing patterns), accessing self.element inside __getattr__ will call __getattr__ again → infinite recursion → RecursionError. The safe fix is:

def __getattr__(self, name):
    element = object.__getattribute__(self, 'element')
    return getattr(element, name)

Same issue applies to XmlDoc.getattr with self.element_tree.

2. xmlapi keyword-argument compatibility break

The old dmidecode.py called:

xmlapi(query_type='s', result_type=self.restype, section=sectname)

The new code calls:

xmlapi('s', self.restype, sectname)

Any third-party code calling xmlapi with the old keyword-argument form will break. This should be documented as a breaking change, or the C extension should accept both forms.

3. xmlapi return type is undocumented

The C extension now returns PyBytes_FromStringAndSize(...) (bytes), but ET.fromstring() accepts both str and bytes. The contract should be explicit so that future changes don't cause confusing ParseError messages.

🟡 Non-blocking / suggestions

4. Dead sys.path insertion in unit-tests/unit (line 14)

sys.path.insert(0,'../build/lib.%s-%s-%s' % (sysname.lower(), machine, pyver))

This legacy path is now superseded by _add_build_paths() (called later in the same file) but was never removed. It should be deleted to avoid confusion.

5. distutils removed in Python 3.12

src/setup.py uses from distutils.core import setup, Extension and src/setup_common.py uses from distutils.sysconfig import get_python_lib. Both fail on Python 3.12+. Migrating to setuptools and sysconfig.get_path('platlib') would complete the modernisation story.

6. First commit message starts with WIP:

Commit 24861ff is titled "WIP: switch XML API to ElementTree bridge". A squash/rebase before merge would clean up the history.

7. Empty PR description

The PR body is empty. A short description of the motivation, the approach, and any known limitations would help the maintainer evaluate and merge this.

Overall

The architectural decision is sound and the implementation is mostly clean. Address the __getattr__ recursion issue and document (or handle) the xmlapi call-signature change, and this will be in good shape to merge.

Accept xmlapi() keyword arguments again (while keeping positional call forms) and avoid __getattr__ recursion in ElementTree wrapper classes. Update docs/tests and silence cast-function-type warnings for the METH_KEYWORDS entry.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

X Tutup