Commit f81bb7b7 authored by Marius Wachtler's avatar Marius Wachtler

Add support for weakref.proxy

I had to change 'new_weakref' to use 'tp_alloc' instead of 'PyObject_GC_New'
in order to create a weak ref because 'PyObject_GC_New' allocated memory
is scanned conservatively.
'tp_alloc' points to 'PystonType_GenericAlloc' and is of PYTHON type.
parent 27e24f67
...@@ -35,7 +35,9 @@ new_weakref(PyObject *ob, PyObject *callback) ...@@ -35,7 +35,9 @@ new_weakref(PyObject *ob, PyObject *callback)
{ {
PyWeakReference *result; PyWeakReference *result;
result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType); // Pyston change: We can't use PyObject_GC_New because it will conservatively scan the memory.
// result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);
result = (PyWeakReference*)_PyWeakref_RefType.tp_alloc(&_PyWeakref_RefType, 0);
if (result) { if (result) {
init_weakref(result, ob, callback); init_weakref(result, ob, callback);
PyObject_GC_Track(result); PyObject_GC_Track(result);
...@@ -706,9 +708,8 @@ _PyWeakref_ProxyType = { ...@@ -706,9 +708,8 @@ _PyWeakref_ProxyType = {
0, /* tp_hash */ 0, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
proxy_str, /* tp_str */ proxy_str, /* tp_str */
// Pyston change: proxy_getattr, /* tp_getattro */
0, //proxy_getattr, /* tp_getattro */ (setattrofunc)proxy_setattr, /* tp_setattro */
0, //(setattrofunc)proxy_setattr, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES, /* tp_flags */ | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
...@@ -744,9 +745,8 @@ _PyWeakref_CallableProxyType = { ...@@ -744,9 +745,8 @@ _PyWeakref_CallableProxyType = {
0, /* tp_hash */ 0, /* tp_hash */
proxy_call, /* tp_call */ proxy_call, /* tp_call */
proxy_str, /* tp_str */ proxy_str, /* tp_str */
// Pyston change: proxy_getattr, /* tp_getattro */
0, //proxy_getattr, /* tp_getattro */ (setattrofunc)proxy_setattr, /* tp_setattro */
0, //(setattrofunc)proxy_setattr, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES, /* tp_flags */ | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
......
import weakref
import gc
class C(object):
def foo(self):
print "inside foo()"
def fact(n):
if n <= 1:
return n
return n * fact(n-1)
def getWR():
c = C()
wr = weakref.proxy(c)
wr.attr = "test attr"
print wr.attr, c.attr
wr.foo()
del c
return wr
wr = getWR()
fact(100) # try to clear some memory
gc.collect()
try:
wr.foo()
except ReferenceError as e:
print e
try:
print wr.attr
except ReferenceError as e:
print e
try:
wr.attr = "val2"
except ReferenceError as e:
print e
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