There are 3 problems with latest lxml on PyPy.

(1) Proxy release moved from _Element.__dealloc__ to _Element.__del__
(which Cython emits as tp_finalize and calls via
PyObject_CallFinalizerFromDealloc() from inside tp_dealloc)
to better deal with the free threaded build. The call tries to
reconstruct the object for implementation reasons in PyPy. PyPy errors when
objects are reconstructed during deallocation, it is unsafe. So the PR to move
it also defined CYTHON_USE_TP_FINALIZE=0 on PyPy. This in turn removes the
tp_finalize slot, __del__ never runs, and xmlNode->_private still points to the
proxy after it has been freed. The next _elementFactory() call goes through
getProxy(), which finds an object and hands it back leading to a segfault.

(2) Lxml turned on CYTHON_USE_TYPE_SPECS. This exposed a bug in the
way PyPy's tp_dealloc
for subclasses of builtin-types __ElementUnicodeResultC(str) is handled, we
(incorrectly) call Py_DECREF in the base type's tp_dealloc and then the heap
type subtype (correctly) calls it again. Then when the type's refcount
underflows, it causes type_realize to be called again, which raises an
AssertionError. Honestly, any heaptype subclass of a builtin type would trip
this, lxml just happens to be the first one where we noticed it.

(3) _Element.extend(iter(other)) fails on PyPy with
SystemError: <cyfunction _Element.extend> returned NULL without setting an
error. This is due to a slot lookup returning NULL. PyPy's cpyext
inherit_slots() fails to copy tp_iter/tp_iternext from the parent.
ElementChildIterator hits this bug. In order to trip the bug, it requires a
cdef class inheriting from a cdef base, iterated in a cython for loop.

Here is my plan:
A. I will submit a PR to work around the three problems in lxml for now
B. I will fix these shortcomings in PyPy and add lxml to the set of projects
we test in a weekly CI job to make sure I find out about problems
before they are released

Does that make sense?
Thanks
Matti
_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/lxml.python.org
Member address: [email protected]
  • [lxml] lxml and pypy matti picus via lxml - The Python XML Toolkit

Reply via email to