Commit 061558c7 authored by William Stein's avatar William Stein

Peter Johnson (peter@tortall.net) weakref patch

I recently ran into this problem myself (as the current code causes
Python to crash), so I whipped up a quick patch that fixes it for me.
I think it follows all of the weakref guidelines now.

The patch is against the LXML svn pyrex
(http://codespeak.net/svn/lxml/pyrex/).  Is there a different SVN I
should be pointing to?  It patches functions generate_new_function,
generate_dealloc_function, generate_traverse_function, and
generate_clear_function.

The patch just compares the entry.name against "__weakref__"; this
could probably be centralized in the Entry object if so desired.
parent 6f8e8da4
...@@ -542,7 +542,10 @@ class ModuleNode(Node, BlockNode): ...@@ -542,7 +542,10 @@ class ModuleNode(Node, BlockNode):
type.vtabptr_cname)) type.vtabptr_cname))
for entry in scope.var_entries: for entry in scope.var_entries:
if entry.type.is_pyobject: if entry.type.is_pyobject:
code.put_init_var_to_py_none(entry, "p->%s") if entry.name == "__weakref__":
code.putln("p->%s = NULL;" % entry.cname)
else:
code.put_init_var_to_py_none(entry, "p->%s")
entry = scope.lookup_here("__new__") entry = scope.lookup_here("__new__")
if entry: if entry:
code.putln( code.putln(
...@@ -566,7 +569,12 @@ class ModuleNode(Node, BlockNode): ...@@ -566,7 +569,12 @@ class ModuleNode(Node, BlockNode):
self.generate_usr_dealloc_call(scope, code) self.generate_usr_dealloc_call(scope, code)
for entry in scope.var_entries: for entry in scope.var_entries:
if entry.type.is_pyobject: if entry.type.is_pyobject:
code.put_xdecref("p->%s" % entry.cname, entry.type) if entry.name == "__weakref__":
code.putln(
"if (p->%s) PyObject_ClearWeakRefs(o);" %
entry.cname)
else:
code.put_xdecref("p->%s" % entry.cname, entry.type)
if base_type: if base_type:
code.putln( code.putln(
"%s->tp_dealloc(o);" % "%s->tp_dealloc(o);" %
...@@ -614,7 +622,7 @@ class ModuleNode(Node, BlockNode): ...@@ -614,7 +622,7 @@ class ModuleNode(Node, BlockNode):
"e = %s->tp_traverse(o, v, a); if (e) return e;" % "e = %s->tp_traverse(o, v, a); if (e) return e;" %
base_type.typeptr_cname) base_type.typeptr_cname)
for entry in scope.var_entries: for entry in scope.var_entries:
if entry.type.is_pyobject: if entry.type.is_pyobject and entry.name != "__weakref__":
var_code = "p->%s" % entry.cname var_code = "p->%s" % entry.cname
code.putln( code.putln(
"if (%s) {" "if (%s) {"
...@@ -643,7 +651,7 @@ class ModuleNode(Node, BlockNode): ...@@ -643,7 +651,7 @@ class ModuleNode(Node, BlockNode):
"%s->tp_clear(o);" % "%s->tp_clear(o);" %
base_type.typeptr_cname) base_type.typeptr_cname)
for entry in scope.var_entries: for entry in scope.var_entries:
if entry.type.is_pyobject: if entry.type.is_pyobject and entry.name != "__weakref__":
name = "p->%s" % entry.cname name = "p->%s" % entry.cname
code.put_xdecref(name, entry.type) code.put_xdecref(name, entry.type)
#code.put_init_to_py_none(name) #code.put_init_to_py_none(name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment