From f37a973f5f65668bd17a192eb37c13ec19970ec6 Mon Sep 17 00:00:00 2001 From: Kashiko Koibumi Date: Sat, 25 May 2024 09:06:02 +0900 Subject: [PATCH] use six.PY2 and six.PY3 --- setup.py | 3 ++- src/depends.py | 3 ++- src/fallback/umsgpack/umsgpack.py | 7 ++++--- src/l10n.py | 6 +++--- src/pyelliptic/openssl.py | 3 ++- src/tests/common.py | 4 ++-- src/tests/partial.py | 3 ++- src/tests/test_api_thread.py | 4 ++-- src/tests/test_l10n.py | 4 ++-- src/tests/test_log.py | 4 ++-- src/tests/test_protocol.py | 4 ++-- tests.py | 3 ++- 12 files changed, 27 insertions(+), 21 deletions(-) diff --git a/setup.py b/setup.py index 30436bec..0daf5d74 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ import os import platform import shutil import sys +import six from importlib import import_module from setuptools import setup, Extension @@ -83,7 +84,7 @@ if __name__ == "__main__": 'images/kivy/text_images*.png' ]} - if sys.version_info[0] == 3: + if six.PY3: packages.extend( [ 'pybitmessage.bitmessagekivy', diff --git a/src/depends.py b/src/depends.py index d966d5fe..bc754440 100755 --- a/src/depends.py +++ b/src/depends.py @@ -6,6 +6,7 @@ and suggest how it may be installed import os import re import sys +import six # Only really old versions of Python don't have sys.hexversion. We don't # support them. The logging module was introduced in Python 2.3 @@ -438,7 +439,7 @@ def check_dependencies(verbose=False, optional=False): 'PyBitmessage requires Python 2.7.4 or greater' ' (but not Python 3+)') has_all_dependencies = False - if sys.hexversion >= 0x3000000: + if six.PY3: logger.error( 'PyBitmessage does not support Python 3+. Python 2.7.4' ' or greater is required. Python 2.7.18 is recommended.') diff --git a/src/fallback/umsgpack/umsgpack.py b/src/fallback/umsgpack/umsgpack.py index 34938614..d4c347ca 100644 --- a/src/fallback/umsgpack/umsgpack.py +++ b/src/fallback/umsgpack/umsgpack.py @@ -53,6 +53,7 @@ import collections import io import struct import sys +import six __version__ = "2.4.1" "Module version string" @@ -99,9 +100,9 @@ class Ext: # pylint: disable=old-style-class if not isinstance(type, int) or not (type >= 0 and type <= 127): raise TypeError("ext type out of range") # Check data is type bytes - elif sys.version_info[0] == 3 and not isinstance(data, bytes): + elif six.PY3 and not isinstance(data, bytes): raise TypeError("ext data is not type \'bytes\'") - elif sys.version_info[0] == 2 and not isinstance(data, str): + elif six.PY2 and not isinstance(data, str): raise TypeError("ext data is not type \'str\'") self.type = type self.data = data @@ -990,7 +991,7 @@ def __init(): _float_precision = "single" # Map packb and unpackb to the appropriate version - if sys.version_info[0] == 3: + if six.PY3: pack = _pack3 packb = _packb3 dump = _pack3 diff --git a/src/l10n.py b/src/l10n.py index fe02d3f4..927937d7 100644 --- a/src/l10n.py +++ b/src/l10n.py @@ -3,8 +3,8 @@ import logging import os import re -import sys import time +import six from six.moves import range @@ -61,7 +61,7 @@ if not re.search(r'\d', time.strftime(time_format)): # It seems some systems lie about the encoding they use # so we perform comprehensive decoding tests -elif sys.version_info[0] == 2: +elif six.PY2: try: # Check day names for i in range(7): @@ -118,7 +118,7 @@ def formatTimestamp(timestamp=None): except ValueError: timestring = time.strftime(time_format) - if sys.version_info[0] == 2: + if six.PY2: return timestring.decode(encoding) return timestring diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index deb81644..42c2946d 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -8,6 +8,7 @@ needed openssl functionality in class _OpenSSL. """ import ctypes import sys +import six # pylint: disable=protected-access @@ -745,7 +746,7 @@ class _OpenSSL(object): """ buffer_ = None if data != 0: - if sys.version_info.major == 3 and isinstance(data, type('')): + if six.PY3 and isinstance(data, type('')): data = data.encode() buffer_ = self.create_string_buffer(data, size) else: diff --git a/src/tests/common.py b/src/tests/common.py index 2d60c716..0cd84d2f 100644 --- a/src/tests/common.py +++ b/src/tests/common.py @@ -1,7 +1,7 @@ import os -import sys import time import unittest +import six _files = ( @@ -33,7 +33,7 @@ def checkup(): def skip_python3(): """Raise unittest.SkipTest() if detected python3""" - if sys.hexversion >= 0x3000000: + if six.PY3: raise unittest.SkipTest('Module is not ported to python3') diff --git a/src/tests/partial.py b/src/tests/partial.py index 870f6626..b5543b71 100644 --- a/src/tests/partial.py +++ b/src/tests/partial.py @@ -3,6 +3,7 @@ import os import sys import unittest +import six from pybitmessage import pathmagic @@ -22,7 +23,7 @@ class TestPartialRun(unittest.TestCase): import state from debug import logger # noqa:F401 pylint: disable=unused-variable - if sys.hexversion >= 0x3000000: + if six.PY3: # pylint: disable=no-name-in-module,relative-import from mockbm import network as network_mock import network diff --git a/src/tests/test_api_thread.py b/src/tests/test_api_thread.py index 6e453b19..6fc3b66f 100644 --- a/src/tests/test_api_thread.py +++ b/src/tests/test_api_thread.py @@ -1,9 +1,9 @@ """TestAPIThread class definition""" -import sys import time from binascii import hexlify, unhexlify from struct import pack +import six from six.moves import queue, xmlrpc_client @@ -68,7 +68,7 @@ class TestAPIThread(TestPartialRun): def test_client_status(self): """Ensure the reply of clientStatus corresponds to mock""" status = self.api.clientStatus() - if sys.hexversion >= 0x3000000: + if six.PY3: self.assertEqual(status["networkConnections"], 4) self.assertEqual(status["pendingDownload"], 0) diff --git a/src/tests/test_l10n.py b/src/tests/test_l10n.py index c6988827..a61f2a99 100644 --- a/src/tests/test_l10n.py +++ b/src/tests/test_l10n.py @@ -1,9 +1,9 @@ """Tests for l10n module""" import re -import sys import time import unittest +import six from pybitmessage import l10n @@ -16,7 +16,7 @@ class TestL10n(unittest.TestCase): self.assertFalse(re.search(r'\d', time.strftime("wrong"))) timestring_type = type(time.strftime(l10n.DEFAULT_TIME_FORMAT)) self.assertEqual(timestring_type, str) - if sys.version_info[0] == 2: + if six.PY2: self.assertEqual(timestring_type, bytes) def test_getWindowsLocale(self): diff --git a/src/tests/test_log.py b/src/tests/test_log.py index 4e74e50d..0d05fcb7 100644 --- a/src/tests/test_log.py +++ b/src/tests/test_log.py @@ -1,8 +1,8 @@ """Tests for logging""" import subprocess -import sys import unittest +import six from pybitmessage import proofofwork @@ -11,7 +11,7 @@ class TestLog(unittest.TestCase): """A test case for logging""" @unittest.skipIf( - sys.hexversion < 0x3000000, 'assertLogs is new in version 3.4') + six.PY2, 'assertLogs is new in version 3.4') def test_LogOutput(self): """Use proofofwork.LogOutput to log output of a shell command""" with self.assertLogs('default') as cm: # pylint: disable=no-member diff --git a/src/tests/test_protocol.py b/src/tests/test_protocol.py index e3137b25..6a7f1ee5 100644 --- a/src/tests/test_protocol.py +++ b/src/tests/test_protocol.py @@ -2,8 +2,8 @@ Tests for common protocol functions """ -import sys import unittest +import six from pybitmessage import protocol, state from pybitmessage.helper_startup import fixSocket @@ -79,7 +79,7 @@ class TestProtocol(TestSocketInet): self.assertEqual(protocol.checkIPAddress(globalhost), '8.8.8.8') @unittest.skipIf( - sys.hexversion >= 0x3000000, 'this is still not working with python3') + six.PY3, 'this is still not working with python3') def test_check_local_socks(self): """The SOCKS part of the local check""" self.assertTrue( diff --git a/tests.py b/tests.py index 713b25ef..0e3adaf1 100644 --- a/tests.py +++ b/tests.py @@ -3,11 +3,12 @@ import random # noseq import sys import unittest +import six def unittest_discover(): """Explicit test suite creation""" - if sys.hexversion >= 0x3000000: + if six.PY3: from pybitmessage import pathmagic pathmagic.setup() loader = unittest.defaultTestLoader