summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_inspect.py
diff options
context:
space:
mode:
authorMichael Foord <michael@python.org>2011-03-15 23:20:44 (GMT)
committerMichael Foord <michael@python.org>2011-03-15 23:20:44 (GMT)
commitdcebe0f2dca40a22654c5f09ce1bd141419ea8d3 (patch)
treecb76f16fb999a3ac559cdfdf996ad3eef90ac8df /Lib/test/test_inspect.py
parentc867239a3121a51102d99c9bc1dbdf30566b0cbe (diff)
downloadcpython-dcebe0f2dca40a22654c5f09ce1bd141419ea8d3.zip
cpython-dcebe0f2dca40a22654c5f09ce1bd141419ea8d3.tar.gz
cpython-dcebe0f2dca40a22654c5f09ce1bd141419ea8d3.tar.bz2
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r--Lib/test/test_inspect.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index ccfcaba..331d247 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -906,6 +906,53 @@ class TestGetattrStatic(unittest.TestCase):
self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
+ def test_dict_as_property(self):
+ test = self
+ test.called = False
+
+ class Foo(dict):
+ a = 3
+ @property
+ def __dict__(self):
+ test.called = True
+ return {}
+
+ foo = Foo()
+ foo.a = 4
+ self.assertEqual(inspect.getattr_static(foo, 'a'), 3)
+ self.assertFalse(test.called)
+
+ def test_custom_object_dict(self):
+ test = self
+ test.called = False
+
+ class Custom(dict):
+ def get(self, key, default=None):
+ test.called = True
+ super().get(key, default)
+
+ class Foo(object):
+ a = 3
+ foo = Foo()
+ foo.__dict__ = Custom()
+ self.assertEqual(inspect.getattr_static(foo, 'a'), 3)
+ self.assertFalse(test.called)
+
+ def test_metaclass_dict_as_property(self):
+ class Meta(type):
+ @property
+ def __dict__(self):
+ self.executed = True
+
+ class Thing(metaclass=Meta):
+ executed = False
+
+ def __init__(self):
+ self.spam = 42
+
+ instance = Thing()
+ self.assertEqual(inspect.getattr_static(instance, "spam"), 42)
+ self.assertFalse(Thing.executed)
class TestGetGeneratorState(unittest.TestCase):