[Zope3-checkins] SVN: zdaemon/branches/py3/src/zdaemon/ Towards Py3K: file.write() return value, close files

Marius Gedminas cvs-admin at zope.org
Thu Feb 14 15:33:38 UTC 2013


Log message for revision 129413:
  Towards Py3K: file.write() return value, close files
  
  Oh, and Python 3 doesn't let you turn off buffering for text files, so
  sprinkle some flush() calls instead.

Changed:
  U   zdaemon/branches/py3/src/zdaemon/README.txt
  U   zdaemon/branches/py3/src/zdaemon/tests/tests.py

-=-
Modified: zdaemon/branches/py3/src/zdaemon/README.txt
===================================================================
--- zdaemon/branches/py3/src/zdaemon/README.txt	2013-02-14 15:33:35 UTC (rev 129412)
+++ zdaemon/branches/py3/src/zdaemon/README.txt	2013-02-14 15:33:37 UTC (rev 129413)
@@ -84,7 +84,7 @@
 .. -> text
 
     >>> with open('conf', 'w') as file:
-    ...     file.write(text)
+    ...     _ = file.write(text)
 
 Now, we can run with the -C option to read the configuration file:
 
@@ -117,7 +117,7 @@
 .. -> text
 
     >>> with open('conf', 'w') as file:
-    ...     file.write(text.replace('/tmp', tmpdir))
+    ...     _ = file.write(text.replace('/tmp', tmpdir))
 
 Now, when we run zdaemon:
 
@@ -152,7 +152,7 @@
 .. -> text
 
     >>> with open('conf', 'w') as file:
-    ...     file.write(text.replace('/tmp', tmpdir))
+    ...     _ = file.write(text.replace('/tmp', tmpdir))
 
 Then we can pass the program argument on the command line:
 
@@ -188,7 +188,7 @@
 .. -> text
 
     >>> with open('conf', 'w') as file:
-    ...     file.write(text.replace('/tmp', tmpdir))
+    ...     _ = file.write(text.replace('/tmp', tmpdir))
 
 Now, when we run the command, we'll see out environment settings reflected:
 
@@ -219,9 +219,9 @@
 Let's look at an example. We'll have a long-running process that
 simple tails a data file:
 
-    >>> f = open('data', 'w', 0)
+    >>> f = open('data', 'w', 1)
     >>> import os
-    >>> f.write('rec 1\n'); os.fsync(f.fileno())
+    >>> _ = f.write('rec 1\n'); f.flush(); os.fsync(f.fileno())
 
 Now, here's out zdaemon configuration::
 
@@ -232,7 +232,8 @@
 
 .. -> text
 
-    >>> open('conf', 'w').write(text)
+    >>> with open('conf', 'w') as file:
+    ...     _ = file.write(text)
 
 Now we'll start:
 
@@ -247,7 +248,8 @@
 
 After waiting a bit, if we look at the log file, it contains the tail output:
 
-    >>> open('log').read()
+    >>> with open('log') as file:
+    ...     file.read()
     'rec 1\n'
 
 We can rotate the transcript log by renaming it and telling zdaemon to
@@ -258,7 +260,7 @@
 
 If we generate more output:
 
-    >>> f.write('rec 2\n'); os.fsync(f.fileno())
+    >>> _ = f.write('rec 2\n'); f.flush(); os.fsync(f.fileno())
 
 .. Wait a little bit to make sure tail has a chance to work
 
@@ -267,7 +269,8 @@
 The output will appear in the old file, because zdaemon still has it
 open:
 
-    >>> open('log.1').read()
+    >>> with open('log.1') as file:
+    ...     file.read()
     'rec 1\nrec 2\n'
 
 Now, if we tell zdaemon to reopen the file:
@@ -276,7 +279,7 @@
 
 and generate some output:
 
-    >>> f.write('rec 3\n'); os.fsync(f.fileno())
+    >>> _ = f.write('rec 3\n'); f.flush(); os.fsync(f.fileno())
 
 .. Wait a little bit to make sure tail has a chance to work
 

Modified: zdaemon/branches/py3/src/zdaemon/tests/tests.py
===================================================================
--- zdaemon/branches/py3/src/zdaemon/tests/tests.py	2013-02-14 15:33:35 UTC (rev 129412)
+++ zdaemon/branches/py3/src/zdaemon/tests/tests.py	2013-02-14 15:33:37 UTC (rev 129413)
@@ -156,7 +156,7 @@
     ... '''
     ... import time
     ... time.sleep(1)
-    ... open('x', 'w')
+    ... open('x', 'w').close()
     ... time.sleep(99)
     ... ''')
 
@@ -189,7 +189,7 @@
     ... '''
     ... import time
     ... time.sleep(1)
-    ... open('x', 'w')
+    ... open('x', 'w').close()
     ... time.sleep(99)
     ... ''')
 
@@ -272,7 +272,7 @@
     True
     """
 
-def nonzeo_exit_on_program_failure():
+def nonzero_exit_on_program_failure():
     """
     >>> write('conf',
     ... '''
@@ -325,15 +325,13 @@
     workspace = tempfile.mkdtemp()
     td.append(lambda : shutil.rmtree(workspace))
     os.chdir(workspace)
-    open('zdaemon', 'w').write(zdaemon_template % dict(
-        python = sys.executable,
-        zdaemon = zdaemon_loc,
-        ZConfig = zconfig_loc,
-        ))
+    write('zdaemon', zdaemon_template % dict(
+        python=sys.executable,
+        zdaemon=zdaemon_loc,
+        ZConfig=zconfig_loc,
+    ))
     os.chmod('zdaemon', 0o755)
-    test.globs.update(dict(
-        system = system
-        ))
+    test.globs['system'] = system
 
 def tearDown(test):
     for f in test.globs['_td']:



More information about the Zope3-Checkins mailing list