Amaury Forgeot d'Arc, 18.02.2012 10:08:
> I made some modifications to pypy, cython and lxml,
> and now I can compile and install cython, lxml, and they seem to work!
> 
> For example::
>     html = etree.Element("html")
>     body = etree.SubElement(html, "body")
>     body.text = "TEXT"
>     br = etree.SubElement(body, "br")
>     br.tail = "TAIL"
>     html.xpath("//text()")
> 
> Here are the changes I made, some parts are really hacks and should be
> polished:
> lxml: http://paste.pocoo.org/show/552903/

I attached a reworked but untested patch for lxml. Could you try it?

I couldn't find the PyWeakref_LockObject() function anywhere. That's a
PyPy-only thing, right? I aliased it to (NULL) when compiling for CPython.

I'll go through the Cython changes next, so I haven't got a working Cython
version yet.

Stefan
# HG changeset patch
# Parent 9b80192c1ed232ec7f01bd4b4e4b2563e6d1e922
initial patch for PyPy support

diff -r 9b80192c1ed2 -r 4e41ae7d7dc9 src/lxml/etree_defs.h
--- a/src/lxml/etree_defs.h	Sat Feb 18 12:43:03 2012 +0100
+++ b/src/lxml/etree_defs.h	Sat Feb 18 13:30:55 2012 +0100
@@ -27,6 +27,22 @@
 #  endif
 #endif
 
+#ifdef PYPY_VERSION
+#    define IS_PYPY 1
+#else
+#    define IS_PYPY 0
+#endif
+
+#if PY_VERSION_HEX >= 0x03000000
+#  define IS_PYTHON3 1
+#else
+#  define IS_PYTHON3 0
+#endif
+
+#if !IS_PYPY
+define PyWeakref_LockObject(obj)             (NULL)
+#endif
+
 /* Python 3 doesn't have PyFile_*(), PyString_*(), ... */
 #if PY_VERSION_HEX >= 0x03000000
 #  define PyFile_AsFile(o)                   (NULL)
@@ -36,6 +52,9 @@
 #  define PyString_GET_SIZE(s)               PyBytes_GET_SIZE(s)
 #  define PyString_AS_STRING(s)              PyBytes_AS_STRING(s)
 #else
+#if IS_PYPY
+#  define PyFile_AsFile(o)                   (NULL)
+#endif
 #if PY_VERSION_HEX < 0x02060000
 #  define PyBytes_CheckExact(o)              PyString_CheckExact(o)
 #  define PyBytes_Check(o)                   PyString_Check(o)
@@ -48,12 +67,6 @@
 #endif
 #endif
 
-#if PY_VERSION_HEX >= 0x03000000
-#  define IS_PYTHON3 1
-#else
-#  define IS_PYTHON3 0
-#endif
-
 #ifdef WITHOUT_THREADING
 #  define PyEval_SaveThread() (NULL)
 #  define PyEval_RestoreThread(state)
diff -r 9b80192c1ed2 -r 4e41ae7d7dc9 src/lxml/proxy.pxi
--- a/src/lxml/proxy.pxi	Sat Feb 18 12:43:03 2012 +0100
+++ b/src/lxml/proxy.pxi	Sat Feb 18 13:30:55 2012 +0100
@@ -9,7 +9,10 @@
     """
     #print "getProxy for:", <int>c_node
     if c_node is not NULL and c_node._private is not NULL:
-        return <_Element>c_node._private
+        if python.IS_PYPY:
+            return <_Element>python.PyWeakref_LockObject(<python.PyObject*>c_node._private)
+        else:
+            return <_Element>c_node._private
     else:
         return None
 
@@ -24,7 +27,10 @@
     assert c_node._private is NULL, u"double registering proxy!"
     proxy._doc = doc
     proxy._c_node = c_node
-    c_node._private = <void*>proxy
+    if python.IS_PYPY:
+        c_node._private = <void*>python.PyWeakref_NewRef(proxy, NULL)
+    else:
+        c_node._private = <void*>proxy
     # additional INCREF to make sure _Document is GC-ed LAST!
     proxy._gc_doc = <python.PyObject*>doc
     python.Py_INCREF(doc)
@@ -34,7 +40,10 @@
     """
     cdef xmlNode* c_node
     c_node = proxy._c_node
-    assert c_node._private is <void*>proxy, u"Tried to unregister unknown proxy"
+    if python.IS_PYPY:
+        python.Py_XDECREF(<python.PyObject*>c_node._private)
+    else:
+        assert c_node._private is <void*>proxy, u"Tried to unregister unknown proxy"
     c_node._private = NULL
     return 0
 
@@ -49,7 +58,11 @@
     iff it was replaced (None otherwise).
     """
     cdef _Document old_doc
-    cdef _Element element = <_Element>c_node._private
+    cdef _Element element
+    if python.IS_PYPY:
+        element = <_Element>python.PyWeakref_LockObject(<python.PyObject*>c_node._private)
+    else:
+        element = <_Element>c_node._private
     if element._doc is not doc:
         old_doc = element._doc
         element._doc = doc
diff -r 9b80192c1ed2 -r 4e41ae7d7dc9 src/lxml/python.pxd
--- a/src/lxml/python.pxd	Sat Feb 18 12:43:03 2012 +0100
+++ b/src/lxml/python.pxd	Sat Feb 18 13:30:55 2012 +0100
@@ -81,8 +81,8 @@
     cdef object PyObject_RichCompare(object o1, object o2, int op)
     cdef int PyObject_RichCompareBool(object o1, object o2, int op)
 
-#    object PyWeakref_NewRef(object ob, PyObject* callback)
-#    PyObject* PyWeakref_GET_OBJECT(object ref)
+    PyObject* PyWeakref_NewRef(object ob, PyObject* callback) # used for PyPy only
+    object PyWeakref_LockObject(PyObject* ob) # PyPy only
 
     cdef void* PyMem_Malloc(size_t size)
     cdef void* PyMem_Realloc(void* p, size_t size)
@@ -129,3 +129,4 @@
     cdef char* _fqtypename(object t)
     cdef object PY_NEW(object t)
     cdef bint IS_PYTHON3
+    cdef bint IS_PYPY
_______________________________________________
pypy-dev mailing list
pypy-dev@python.org
http://mail.python.org/mailman/listinfo/pypy-dev

Reply via email to