Author: florinpapa
Branch: resource_warning
Changeset: r83539:d09f1ccc1094
Date: 2016-04-06 15:08 +0300
http://bitbucket.org/pypy/pypy/changeset/d09f1ccc1094/

Log:    (florin, antocuni) Add resource warning test for file descriptors

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1740,6 +1740,13 @@
             _warnings.warn(msg, warningcls, stacklevel=stacklevel)
         """)
 
+    def resource_warning(self, msg):
+        w_msg = self.wrap(msg)
+        self.appexec([w_msg],
+                     """(msg):
+            import sys
+            print >> sys.stderr, msg
+        """)
 
 class AppExecCache(SpaceCache):
     def build(cache, source):
diff --git a/pypy/module/_file/interp_file.py b/pypy/module/_file/interp_file.py
--- a/pypy/module/_file/interp_file.py
+++ b/pypy/module/_file/interp_file.py
@@ -49,6 +49,10 @@
         # thread that runs __del__, so no race condition should be possible
         self.clear_all_weakrefs()
         if self.stream is not None:
+            if self.space.sys.resource_warning_enabled:
+                w_repr = self.space.repr(self)
+                str_repr = self.space.str_w(w_repr)
+                self.space.resource_warning("WARNING: unclosed file: " + 
str_repr)
             self.enqueue_for_destruction(self.space, W_File.destructor,
                                          'close() method of ')
 
diff --git a/pypy/module/_file/test/test_file.py 
b/pypy/module/_file/test/test_file.py
--- a/pypy/module/_file/test/test_file.py
+++ b/pypy/module/_file/test/test_file.py
@@ -254,6 +254,31 @@
         if '__pypy__' in sys.builtin_module_names:
             assert repr(self.temppath) in g.getvalue()
 
+    def test_resource_warning(self):
+        import os, gc, sys, cStringIO
+        if '__pypy__' not in sys.builtin_module_names:
+            skip("pypy specific test")
+        def fn():
+            f = self.file(self.temppath, 'w')
+            g = cStringIO.StringIO()
+            preverr = sys.stderr
+            try:
+                sys.stderr = g
+                del f
+                gc.collect() # force __del__ to be called
+            finally:
+                sys.stderr = preverr
+            return g.getvalue()
+
+        try:
+            sys.pypy_set_resource_warning(False)
+            assert fn() == ""
+            sys.pypy_set_resource_warning(True)
+            msg = fn()
+            assert msg.startswith("WARNING: unclosed file: <open file ")
+        finally:
+            sys.pypy_set_resource_warning(False)
+
     def test_truncate(self):
         f = self.file(self.temppath, "w")
         f.write("foo")
diff --git a/pypy/module/sys/__init__.py b/pypy/module/sys/__init__.py
--- a/pypy/module/sys/__init__.py
+++ b/pypy/module/sys/__init__.py
@@ -19,6 +19,7 @@
         self.defaultencoding = "ascii"
         self.filesystemencoding = None
         self.debug = True
+        self.resource_warning_enabled = False
 
     interpleveldefs = {
         '__name__'              : '(space.wrap("sys"))',
@@ -53,6 +54,7 @@
         '_current_frames'       : 'currentframes._current_frames',
         'setrecursionlimit'     : 'vm.setrecursionlimit',
         'getrecursionlimit'     : 'vm.getrecursionlimit',
+        'pypy_set_resource_warning' : 'vm.set_resource_warning',
         'setcheckinterval'      : 'vm.setcheckinterval',
         'getcheckinterval'      : 'vm.getcheckinterval',
         'exc_info'              : 'vm.exc_info',
diff --git a/pypy/module/sys/vm.py b/pypy/module/sys/vm.py
--- a/pypy/module/sys/vm.py
+++ b/pypy/module/sys/vm.py
@@ -64,6 +64,10 @@
     """
     return space.wrap(space.sys.recursionlimit)
 
+@unwrap_spec(flag=bool)
+def set_resource_warning(space, flag):
+    space.sys.resource_warning_enabled = flag
+
 @unwrap_spec(interval=int)
 def setcheckinterval(space, interval):
     """Tell the Python interpreter to check for asynchronous events every
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to