[Python-checkins] r68408 - sandbox/trunk/iobench/iobench.py

antoine.pitrou python-checkins at python.org
Thu Jan 8 20:53:19 CET 2009


Author: antoine.pitrou
Date: Thu Jan  8 20:53:19 2009
New Revision: 68408

Log:
Add an option to choose line endings in text tests



Modified:
   sandbox/trunk/iobench/iobench.py

Modified: sandbox/trunk/iobench/iobench.py
==============================================================================
--- sandbox/trunk/iobench/iobench.py	(original)
+++ sandbox/trunk/iobench/iobench.py	Thu Jan  8 20:53:19 2009
@@ -13,6 +13,7 @@
 out = sys.stdout
 
 TEXT_ENCODING = 'utf8'
+NEWLINES = 'lf'
 
 # Compatibility
 try:
@@ -36,7 +37,7 @@
     return ((name + ".bin", size) for name, size in get_file_sizes())
 
 def get_text_files():
-    return ((name + "-" + TEXT_ENCODING + ".txt", size)
+    return (("%s-%s-%s.txt" % (name, TEXT_ENCODING, NEWLINES), size)
         for name, size in get_file_sizes())
 
 def with_open_mode(mode):
@@ -377,9 +378,12 @@
         else:
             raise RuntimeError(
                 "Couldn't find chunk marker in %s !" % __file__)
-        # Change some newlines to exercise newlines translation (in 3.x)
-        newlines = itertools.cycle(["\n", "\r", "\r\n"])
-        chunk = "".join(line.replace("\n", next(newlines)) for line in f)
+        if NEWLINES == "all":
+            it = itertools.cycle(["\n", "\r", "\r\n"])
+        else:
+            it = itertools.repeat(
+                {"cr": "\r", "lf": "\n", "crlf": "\r\n"}[NEWLINES])
+        chunk = "".join(line.replace("\n", next(it)) for line in f)
         if isinstance(chunk, bytes):
             chunk = chunk.decode('utf8')
         chunk = chunk.encode(TEXT_ENCODING)
@@ -400,7 +404,7 @@
             f.write(tail)
 
 def main():
-    global TEXT_ENCODING
+    global TEXT_ENCODING, NEWLINES
 
     usage = "usage: %prog [-h|--help] [options]"
     parser = OptionParser(usage=usage)
@@ -419,9 +423,16 @@
     parser.add_option("-E", "--encoding",
                       action="store", dest="encoding", default=None,
                       help="encoding for text tests (default: %s)" % TEXT_ENCODING)
+    parser.add_option("-N", "--newlines",
+                      action="store", dest="newlines", default='lf',
+                      help="line endings for text tests "
+                           "(one of: {lf (default), cr, crlf, all})")
     options, args = parser.parse_args()
     if args:
         parser.error("unexpected arguments")
+    NEWLINES = options.newlines.lower()
+    if NEWLINES not in ('lf', 'cr', 'crlf', 'all'):
+        parser.error("invalid 'newlines' option: %r" % NEWLINES)
 
     test_options = ""
     if options.read:


More information about the Python-checkins mailing list