From 4a6eac2e557b34378133ec7bbf0ae20c24297145 Mon Sep 17 00:00:00 2001 From: Graham Dumpleton Date: Wed, 2 Oct 2013 10:29:01 +1000 Subject: [PATCH] Add explicit ObjectProxy.__getattr__() in C implementation so can be called from derived classes. --- docs/changes.rst | 10 ++++++++++ src/_wrappers.c | 20 ++++++++++++++++++++ tests/test_object_proxy.py | 11 +++++++++++ 3 files changed, 41 insertions(+) diff --git a/docs/changes.rst b/docs/changes.rst index 22408df..6f8552a 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -1,6 +1,16 @@ Changes ======= +Version 1.2.0 +------------- + +**Bugs Fixed** + +* When creating a custom proxy by deriving from ObjectProxy and the custom + proxy needed to override __getattr__(), it was not possible to called the + base class ObjectProxy.__getattr__() when the C implementation of + ObjectProxy was being used. + Version 1.1.3 ------------- diff --git a/src/_wrappers.c b/src/_wrappers.c index 08ffe58..84cbe75 100644 --- a/src/_wrappers.c +++ b/src/_wrappers.c @@ -1181,6 +1181,24 @@ static PyObject *WraptObjectProxy_getattro( /* ------------------------------------------------------------------------- */ +static PyObject *WraptObjectProxy_getattr( + WraptObjectProxyObject *self, PyObject *args) +{ + PyObject *name = NULL; + +#if PY_MAJOR_VERSION >= 3 + if (!PyArg_ParseTuple(args, "U:__getattr__", &name)) + return NULL; +#else + if (!PyArg_ParseTuple(args, "S:__getattr__", &name)) + return NULL; +#endif + + return WraptObjectProxy_getattro(self, name); +} + +/* ------------------------------------------------------------------------- */ + static int WraptObjectProxy_setattro( WraptObjectProxyObject *self, PyObject *name, PyObject *value) { @@ -1338,6 +1356,8 @@ static PyMethodDef WraptObjectProxy_methods[] = { METH_VARARGS | METH_KEYWORDS, 0 }, { "__exit__", (PyCFunction)WraptObjectProxy_exit, METH_VARARGS | METH_KEYWORDS, 0 }, + { "__getattr__", (PyCFunction)WraptObjectProxy_getattr, + METH_VARARGS , 0 }, { NULL, NULL }, }; diff --git a/tests/test_object_proxy.py b/tests/test_object_proxy.py index e7ea6c2..5f68491 100644 --- a/tests/test_object_proxy.py +++ b/tests/test_object_proxy.py @@ -1292,6 +1292,17 @@ class TestDerivedClassCreation(unittest.TestCase): class DerivedClassAttributes(unittest.TestCase): + def test_attr_functions(self): + + def function(): + pass + + proxy = wrapt.ObjectProxy(function) + + self.assertTrue(hasattr(proxy, '__getattr__')) + self.assertTrue(hasattr(proxy, '__setattr__')) + self.assertTrue(hasattr(proxy, '__delattr__')) + def test_setup_class_attributes(self): def function():