Ticket #5897: twisted_python_runtime_3x_compatibility_2.diff

File twisted_python_runtime_3x_compatibility_2.diff, 2.9 KB (added by meissenPlate, 2 years ago)

3x compatibility patch for twisted.python.runtime, with changes to that module's tests included. Ignore the 3rd file.

  • twisted/python/runtime.py

    diff --git a/twisted/python/runtime.py b/twisted/python/runtime.py
    index da078bf..c54a0c3 100644
    a b import sys 
    99import time 
    1010import imp 
    1111 
     12from twisted.python import compat 
     13 
    1214 
    1315 
    1416def shortPythonVersion(): 
    class Platform: 
    7375    def isWinNT(self): 
    7476        """Are we running in Windows NT?""" 
    7577        if self.getType() == 'win32': 
    76             import _winreg 
     78            # Python 3.x changed this module's name.  
     79            winreg = None 
     80            if compat._PY3: 
     81                import winreg as winreg 
     82            else: 
     83                import _winreg as winreg 
    7784            try: 
    78                 k = _winreg.OpenKeyEx( 
    79                         _winreg.HKEY_LOCAL_MACHINE, 
     85                k = winreg.OpenKeyEx( 
     86                        winreg.HKEY_LOCAL_MACHINE, 
    8087                        r'Software\Microsoft\Windows NT\CurrentVersion') 
    81                 _winreg.QueryValueEx(k, 'SystemRoot') 
     88                winreg.QueryValueEx(k, 'SystemRoot') 
    8289                return 1 
    8390            except WindowsError: 
    8491                return 0 
    class Platform: 
    113120        return self._platform.startswith("linux") 
    114121 
    115122 
     123    # Python 3.x changed this module's name.  
     124    _thread_module_name = "_thread" if compat._PY3 else "thread" 
    116125    def supportsThreads(self): 
    117126        """Can threads be created? 
    118127        """ 
    119128        try: 
    120             return imp.find_module('thread')[0] is None 
     129            return imp.find_module(self._thread_module_name)[0] is None 
    121130        except ImportError: 
    122131            return False 
    123132 
  • twisted/python/test/test_runtime.py

    diff --git a/twisted/python/test/test_runtime.py b/twisted/python/test/test_runtime.py
    index 4c94fb5..5646fe4 100644
    a b Tests for runtime checks. 
    66""" 
    77 
    88import sys 
     9from unittest import TestCase 
    910 
    1011from twisted.python.runtime import Platform, shortPythonVersion 
    11 from twisted.trial.unittest import TestCase 
     12## from twisted.trial.unittest import TestCase 
     13 
     14# TODO: Once trial is ported to Python 3.x these tests should start using it  
     15# again. Until then, use the standard Python unittesting framework. 
     16# To do this, uncomment the line above that imports TestCase from trial, and  
     17# delete the line that imports TestCase from unittest. 
     18# Ticket #5919 
    1219 
    1320 
    1421 
    class PlatformTests(TestCase): 
    6471            self.assertTrue(sys.platform.startswith("linux")) 
    6572 
    6673 
     74    def test_isWinNT(self):  
     75        """  
     76        L{Platform.isWinNT}'s can return only C{0} or C{1} and can not 
     77        return C{1} if L{Platform.getType} is not C{"win32"}. 
     78        """  
     79        platform = Platform()  
     80        isWinNT = platform.isWinNT() 
     81        self.assertIn(isWinNT, (0, 1)) 
     82        if platform.getType() != "win32": 
     83            self.assertEqual(isWinNT, 0) 
     84 
     85 
    6786 
    6887class ForeignPlatformTests(TestCase): 
    6988    """