Commit 0230bff6 authored by Chris Toshok's avatar Chris Toshok

Merge pull request #496 from undingen/weakref_proxy

Add support for weakref.proxy
parents 12abc42b 1be43d90
......@@ -35,7 +35,9 @@ new_weakref(PyObject *ob, PyObject *callback)
{
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) {
init_weakref(result, ob, callback);
PyObject_GC_Track(result);
......@@ -706,9 +708,8 @@ _PyWeakref_ProxyType = {
0, /* tp_hash */
0, /* tp_call */
proxy_str, /* tp_str */
// Pyston change:
0, //proxy_getattr, /* tp_getattro */
0, //(setattrofunc)proxy_setattr, /* tp_setattro */
proxy_getattr, /* tp_getattro */
(setattrofunc)proxy_setattr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES, /* tp_flags */
......@@ -744,9 +745,8 @@ _PyWeakref_CallableProxyType = {
0, /* tp_hash */
proxy_call, /* tp_call */
proxy_str, /* tp_str */
// Pyston change:
0, //proxy_getattr, /* tp_getattro */
0, //(setattrofunc)proxy_setattr, /* tp_setattro */
proxy_getattr, /* tp_getattro */
(setattrofunc)proxy_setattr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES, /* tp_flags */
......
......@@ -431,8 +431,28 @@ extern "C" int PyObject_GenericSetAttr(PyObject* obj, PyObject* name, PyObject*
return 0;
}
extern "C" int PyObject_SetAttr(PyObject* v, PyObject* name, PyObject* value) noexcept {
Py_FatalError("unimplemented");
extern "C" int PyObject_SetAttr(PyObject* obj, PyObject* name, PyObject* value) noexcept {
if (!PyString_Check(name)) {
if (PyUnicode_Check(name)) {
name = PyUnicode_AsEncodedString(name, NULL, NULL);
if (name == NULL)
return -1;
} else {
PyErr_Format(PyExc_TypeError, "attribute name must be string, not '%.200s'", Py_TYPE(name)->tp_name);
return -1;
}
}
try {
if (value == NULL)
delattr(obj, static_cast<BoxedString*>(name)->s.data());
else
setattr(obj, static_cast<BoxedString*>(name)->s.data(), value);
} catch (ExcInfo e) {
setCAPIException(e);
return -1;
}
return 0;
}
extern "C" int PyObject_SetAttrString(PyObject* v, const char* name, PyObject* w) noexcept {
......
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