Commits

Lenard Lindstrom committed 9f76686

fix unit tests bug for Python 3.1 on Windows

  • Participants
  • Parent commits 64baf57

Comments (0)

Files changed (2)

 # BREAK = change breaks existing code
 # BUG    = fixed a bug that was (or could have been) crashing
 
+[SVN 2517] July 17, 2009
+    [BUG] unit test framework fails for Python 3.1 on Windows due
+          to str/bytes confusion
+
 [SVN 2516] July 16, 2009
     Pygame dependency build now replaces the configure
     generated SDL_config.py, which MSVC can not use,  with 

File test/test_utils/async_sub.py

     return sys.exc_info()[1]
 
 if subprocess.mswindows:
+    if sys.version_info >= (3,):
+        # Test date should be in ascii.
+        def encode(s):
+            return s.encode('ascii')
+        
+        def decode(b):
+            return b.decode('ascii')
+    else:
+        # Strings only; do nothing
+        def encode(s):
+            return s
+        
+        def decode(b):
+            return b
+        
     try:
         import ctypes
         from ctypes.wintypes import DWORD
         TerminateProcess = ctypes.windll.kernel32.TerminateProcess
         def WriteFile(handle, data, ol = None):
             c_written = DWORD()
-            success = ctypes.windll.kernel32.WriteFile(handle, ctypes.create_string_buffer(data), len(data), ctypes.byref(c_written), ol)
+            success = ctypes.windll.kernel32.WriteFile(handle, ctypes.create_string_buffer(encode(data)), len(data), ctypes.byref(c_written), ol)
             return ctypes.windll.kernel32.GetLastError(), c_written.value
         def ReadFile(handle, desired_bytes, ol = None):
             c_read = DWORD()
             buffer = ctypes.create_string_buffer(desired_bytes+1)
             success = ctypes.windll.kernel32.ReadFile(handle, buffer, desired_bytes, ctypes.byref(c_read), ol)
             buffer[c_read.value] = '\0'
-            return ctypes.windll.kernel32.GetLastError(), buffer.value
+            return ctypes.windll.kernel32.GetLastError(), decode(buffer.value)
         def PeekNamedPipe(handle, desired_bytes):
             c_avail = DWORD()
             c_message = DWORD()
                 buffer = ctypes.create_string_buffer(desired_bytes+1)
                 success = ctypes.windll.kernel32.PeekNamedPipe(handle, buffer, desired_bytes, ctypes.byref(c_read), ctypes.byref(c_avail), ctypes.byref(c_message))
                 buffer[c_read.value] = '\0'
-                return buffer.value, c_avail.value, c_message.value
+                return decode(buffer.value), c_avail.value, c_message.value
             else:
                 success = ctypes.windll.kernel32.PeekNamedPipe(handle, None, desired_bytes, None, ctypes.byref(c_avail), ctypes.byref(c_message))
                 return "", c_avail.value, c_message.value
                     nAvail = maxsize
                 if nAvail > 0:
                     (errCode, read) = ReadFile(x, nAvail, None)
+                if isinstance(read, bytes):
+                    read = read.decode('ascii')
             except ValueError:
                 return self._close(which)
             except (subprocess.pywintypes.error, Exception):