Source

CherryPy / cherrypy / test / logtest.py

Diff from to

cherrypy/test/logtest.py

 import time
 
 import cherrypy
+from cherrypy._cpcompat import basestring, ntob, unicodestr
 
 
 try:
     
     logfile = None
     lastmarker = None
-    markerPrefix = "test suite marker: "
+    markerPrefix = ntob("test suite marker: ")
     
     def _handleLogError(self, msg, data, marker, pattern):
         print("")
             raise self.failureException(msg)
         
         p = "    Show: [L]og [M]arker [P]attern; [I]gnore, [R]aise, or sys.e[X]it >> "
-        print p,
+        sys.stdout.write(p + ' ')
         # ARGH
         sys.stdout.flush()
         while True:
                 for x, line in enumerate(data):
                     if (x + 1) % self.console_height == 0:
                         # The \r and comma should make the next line overwrite
-                        print "<-- More -->\r",
+                        sys.stdout.write("<-- More -->\r ")
                         m = getchar().lower()
                         # Erase our "More" prompt
-                        print "            \r",
+                        sys.stdout.write("            \r ")
                         if m == "q":
                             break
                     print(line.rstrip())
                 raise self.failureException(msg)
             elif i == "X":
                 self.exit()
-            print p,
+            sys.stdout.write(p + ' ')
     
     def exit(self):
         sys.exit()
             key = str(time.time())
         self.lastmarker = key
         
-        open(self.logfile, 'ab+').write("%s%s\n" % (self.markerPrefix, key))
+        open(self.logfile, 'ab+').write(ntob("%s%s\n" % (self.markerPrefix, key),"utf-8"))
     
     def _read_marked_region(self, marker=None):
         """Return lines from self.logfile in the marked region.
         if marker is None:
             return open(logfile, 'rb').readlines()
         
+        if isinstance(marker, unicodestr):
+            marker = marker.encode('utf-8')
         data = []
         in_region = False
         for line in open(logfile, 'rb'):
             # Single arg. Use __getitem__ and allow lines to be str or list.
             if isinstance(lines, (tuple, list)):
                 lines = lines[0]
+            if isinstance(lines, unicodestr):
+                lines = lines.encode('utf-8')
             if lines not in data[sliceargs]:
                 msg = "%r not found on log line %r" % (lines, sliceargs)
-                self._handleLogError(msg, [data[sliceargs]], marker, lines)
+                self._handleLogError(msg, [data[sliceargs],"--EXTRA CONTEXT--"] + data[sliceargs+1:sliceargs+6], marker, lines)
         else:
             # Multiple args. Use __getslice__ and require lines to be list.
             if isinstance(lines, tuple):
             
             start, stop = sliceargs
             for line, logline in zip(lines, data[start:stop]):
+                if isinstance(line, unicodestr):
+                    line = line.encode('utf-8')
                 if line not in logline:
                     msg = "%r not found in log" % line
                     self._handleLogError(msg, data[start:stop], marker, line)