Ticket #5897: twisted_python_runtime_3x_compatibility.diff

File twisted_python_runtime_3x_compatibility.diff, 2.8 KB (added by meissenPlate, 4 years ago)

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

  • twisted/python/runtime.py

    diff --git a/twisted/python/runtime.py b/twisted/python/runtime.py
    index da078bf..5fa2ea6 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')
    82                 return 1
     88                winreg.QueryValueEx(k, 'SystemRoot')
     89                return True
    8390            except WindowsError:
    84                 return 0
     91                return False
    8592        # not windows NT
    86         return 0
     93        return False
    8794
    8895
    8996    def isWindows(self):
    class Platform: 
    117124        """Can threads be created?
    118125        """
    119126        try:
    120             return imp.find_module('thread')[0] is None
     127            # Python 3.x changed this module's name.
     128            if compat._PY3:
     129                return imp.find_module('_thread')[0] is None
     130            else:
     131                return imp.find_module('thread')[0] is None
    121132        except ImportError:
    122133            return False
    123134
  • twisted/python/test/test_runtime.py

    diff --git a/twisted/python/test/test_runtime.py b/twisted/python/test/test_runtime.py
    index 4c94fb5..7c03ac4 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{True} or C{False}.
     77        """
     78        platform = Platform()
     79        self.assertIn(platform.isWinNT(), (True, False))
     80
     81
    6782
    6883class ForeignPlatformTests(TestCase):
    6984    """