Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r89882:0d92840e04ff
Date: 2017-02-01 15:05 +0100
http://bitbucket.org/pypy/pypy/changeset/0d92840e04ff/

Log:    Forbid reading __dict__ or setting random attributes on functions
        that are 'not can_change_code', corresponding to CPython's built-in
        functions

diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py
--- a/pypy/interpreter/function.py
+++ b/pypy/interpreter/function.py
@@ -198,10 +198,16 @@
 
     def getdict(self, space):
         if self.w_func_dict is None:
+            if not self.can_change_code:
+                raise oefmt(space.w_AttributeError,
+                            "cannot set extra attributes on built-in 
functions")
             self.w_func_dict = space.newdict(instance=True)
         return self.w_func_dict
 
     def setdict(self, space, w_dict):
+        if not self.can_change_code:
+            raise oefmt(space.w_AttributeError,
+                        "cannot set __dict__ on built-in functions")
         if not space.isinstance_w(w_dict, space.w_dict):
             raise oefmt(space.w_TypeError,
                         "setting function's dictionary to a non-dict")
@@ -660,7 +666,7 @@
         Function.__init__(self, func.space, func.code, func.w_func_globals,
                           func.defs_w, func.closure, func.name)
         self.w_doc = func.w_doc
-        self.w_func_dict = func.w_func_dict
+        #self.w_func_dict = func.w_func_dict---nowadays, always None
         self.w_module = func.w_module
 
     def descr_builtinfunction__new__(space, w_subtype):
diff --git a/pypy/interpreter/test/test_function.py 
b/pypy/interpreter/test/test_function.py
--- a/pypy/interpreter/test/test_function.py
+++ b/pypy/interpreter/test/test_function.py
@@ -98,6 +98,12 @@
         raises(TypeError, "dir.func_code = f.func_code")
         raises(TypeError, "list.append.im_func.func_code = f.func_code")
 
+    def test_write_extra_attributes_builtin_forbidden(self):
+        raises(AttributeError, "dir.abcd = 5")
+        raises(AttributeError, "list.append.im_func.efgh = 6")
+        raises(AttributeError, "dir.__dict__")
+        raises(AttributeError, "dir.__dict__ = {}")
+
     def test_set_module_to_name_eagerly(self):
         skip("fails on PyPy but works on CPython.  Unsure we want to care")
         exec '''if 1:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to