[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Fix static checks



commit d4998ded0018302c6c36b186fe4de4f19270e8d8
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Thu Mar 26 16:18:17 2020 -0700

    Fix static checks
    
    I didn't install pycodestyle and pyflakes when upgrading to python 3.7 so quite
    a few regressions slipped in.
---
 stem/exit_policy.py                                |  2 --
 stem/response/events.py                            |  1 -
 stem/util/ed25519.py                               | 25 +++-------------------
 stem/util/enum.py                                  |  2 --
 stem/util/str_tools.py                             | 22 +++++++------------
 test/integ/control/controller.py                   |  1 -
 test/settings.cfg                                  | 11 +++++++++-
 test/unit/client/size.py                           |  1 -
 test/unit/descriptor/extrainfo_descriptor.py       |  1 -
 test/unit/descriptor/hidden_service_v3.py          |  2 --
 test/unit/descriptor/microdescriptor.py            |  1 -
 .../networkstatus/directory_authority.py           |  2 --
 test/unit/descriptor/networkstatus/document_v2.py  |  2 --
 .../descriptor/networkstatus/key_certificate.py    |  1 -
 test/unit/descriptor/remote.py                     |  1 -
 test/unit/descriptor/router_status_entry.py        |  1 -
 test/unit/doctest.py                               |  1 -
 test/unit/exit_policy/policy.py                    |  2 --
 test/unit/tutorial_examples.py                     |  3 ---
 19 files changed, 21 insertions(+), 61 deletions(-)

diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index f67e9f82..ddcd7dfd 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -64,8 +64,6 @@ exiting to a destination is permissible or not. For instance...
 """
 
 import functools
-import re
-import socket
 import zlib
 
 import stem.util
diff --git a/stem/response/events.py b/stem/response/events.py
index 542c0f22..fdd17a25 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -766,7 +766,6 @@ class NewConsensusEvent(Event):
     :returns: **list** of :class:`~stem.descriptor.router_status_entry.RouterStatusEntryV3`
     """
 
-
     if self._parsed is None:
       self._parsed = list(stem.descriptor.router_status_entry._parse_file(
         io.BytesIO(str_tools._to_bytes(self.consensus_content)),
diff --git a/stem/util/ed25519.py b/stem/util/ed25519.py
index 67b2db3c..7c0161dc 100644
--- a/stem/util/ed25519.py
+++ b/stem/util/ed25519.py
@@ -41,33 +41,14 @@ arithmetic, so we cannot handle secrets without risking their disclosure.
 
 import hashlib
 import operator
-import sys
 
 
 __version__ = "1.0.dev0"
 
-
-# Useful for very coarse version differentiation.
-PY3 = sys.version_info[0] == 3
-
-if PY3:
-    indexbytes = operator.getitem
-    intlist2bytes = bytes
-    int2byte = operator.methodcaller("to_bytes", 1, "big")
-else:
-    int2byte = chr
-    range = xrange
-
-    def indexbytes(buf, i):
-        return ord(buf[i])
-
-    def intlist2bytes(l):
-        return b"".join(chr(c) for c in l)
-
-
 b = 256
 q = 2 ** 255 - 19
 l = 2 ** 252 + 27742317777372353535851937790883648493
+int2byte = operator.methodcaller("to_bytes", 1, "big")
 
 
 def H(m):
@@ -223,7 +204,7 @@ def encodepoint(P):
 
 
 def bit(h, i):
-    return (indexbytes(h, i // 8) >> (i % 8)) & 1
+    return (operator.getitem(h, i // 8) >> (i % 8)) & 1
 
 
 def publickey_unsafe(sk):
@@ -252,7 +233,7 @@ def signature_unsafe(m, sk, pk):
     h = H(sk)
     a = 2 ** (b - 2) + sum(2 ** i * bit(h, i) for i in range(3, b - 2))
     r = Hint(
-        intlist2bytes([indexbytes(h, j) for j in range(b // 8, b // 4)]) + m
+        bytes([operator.getitem(h, j) for j in range(b // 8, b // 4)]) + m
     )
     R = scalarmult_B(r)
     S = (r + Hint(encodepoint(R) + pk + m) * a) % l
diff --git a/stem/util/enum.py b/stem/util/enum.py
index abaf2490..56bf119d 100644
--- a/stem/util/enum.py
+++ b/stem/util/enum.py
@@ -40,8 +40,6 @@ constructed as simple type listings...
     +- __iter__ - iterator over our enum keys
 """
 
-import stem.util
-
 
 def UppercaseEnum(*args):
   """
diff --git a/stem/util/str_tools.py b/stem/util/str_tools.py
index 8ce22dc9..c1285626 100644
--- a/stem/util/str_tools.py
+++ b/stem/util/str_tools.py
@@ -56,18 +56,6 @@ TIME_UNITS = (
 
 _timestamp_re = re.compile(r'(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})')
 
-def _to_bytes_impl(msg):
-  if isinstance(msg, str):
-    return codecs.latin_1_encode(msg, 'replace')[0]
-  else:
-    return msg
-
-def _to_unicode_impl(msg):
-  if msg is not None and not isinstance(msg, str):
-    return msg.decode('utf-8', 'replace')
-  else:
-    return msg
-
 
 def _to_bytes(msg):
   """
@@ -82,7 +70,10 @@ def _to_bytes(msg):
   :returns: ASCII bytes for string
   """
 
-  return _to_bytes_impl(msg)
+  if isinstance(msg, str):
+    return codecs.latin_1_encode(msg, 'replace')[0]
+  else:
+    return msg
 
 
 def _to_unicode(msg):
@@ -96,7 +87,10 @@ def _to_unicode(msg):
   :returns: unicode conversion
   """
 
-  return _to_unicode_impl(msg)
+  if msg is not None and not isinstance(msg, str):
+    return msg.decode('utf-8', 'replace')
+  else:
+    return msg
 
 
 def _decode_b64(msg):
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 5e92bfd7..8b8b3205 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -26,7 +26,6 @@ import test.runner
 from stem import Flag, Signal
 from stem.control import EventType, Listener, State
 from stem.exit_policy import ExitPolicy
-from stem.version import Requirement
 
 # Router status entry for a relay with a nickname other than 'Unnamed'. This is
 # used for a few tests that need to look up a relay.
diff --git a/test/settings.cfg b/test/settings.cfg
index d796993a..38a37ef9 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -205,10 +205,19 @@ pyflakes.ignore stem/util/__init__.py => undefined name 'unicode'
 pyflakes.ignore stem/util/conf.py => undefined name 'unicode'
 pyflakes.ignore stem/util/test_tools.py => 'pyflakes' imported but unused
 pyflakes.ignore stem/util/test_tools.py => 'pycodestyle' imported but unused
+pyflakes.ignore test/__init__.py => undefined name 'test'
+pyflakes.ignore test/require.py => 'cryptography.utils.int_from_bytes' imported but unused
+pyflakes.ignore test/require.py => 'cryptography.utils.int_to_bytes' imported but unused
+pyflakes.ignore test/require.py => 'cryptography.hazmat.backends.default_backend' imported but unused
+pyflakes.ignore test/require.py => 'cryptography.hazmat.primitives.ciphers.algorithms' imported but unused
+pyflakes.ignore test/require.py => 'cryptography.hazmat.primitives.ciphers.Cipher' imported but unused
+pyflakes.ignore test/require.py => 'cryptography.hazmat.primitives.ciphers.modes' imported but unused
+pyflakes.ignore test/require.py => 'cryptography.hazmat.primitives.serialization.load_der_public_key' imported but unused
+pyflakes.ignore test/require.py => 'cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PublicKey' imported but unused
 pyflakes.ignore test/unit/response/events.py => 'from stem import *' used; unable to detect undefined names
 pyflakes.ignore test/unit/response/events.py => *may be undefined, or defined from star imports: stem
 pyflakes.ignore stem/util/str_tools.py => undefined name 'unicode'
-pyflakes.ignore test/__init__.py => undefined name 'test'
+pyflakes.ignore test/integ/interpreter.py => 'readline' imported but unused
 
 # Test modules we want to run. Modules are roughly ordered by the dependencies
 # so the lowest level tests come first. This is because a problem in say,
diff --git a/test/unit/client/size.py b/test/unit/client/size.py
index d3bb4a4a..ae171501 100644
--- a/test/unit/client/size.py
+++ b/test/unit/client/size.py
@@ -2,7 +2,6 @@
 Unit tests for stem.client.Size.
 """
 
-import re
 import unittest
 
 from stem.client.datatype import Size
diff --git a/test/unit/descriptor/extrainfo_descriptor.py b/test/unit/descriptor/extrainfo_descriptor.py
index 2dae4de7..793a8129 100644
--- a/test/unit/descriptor/extrainfo_descriptor.py
+++ b/test/unit/descriptor/extrainfo_descriptor.py
@@ -7,7 +7,6 @@ import functools
 import unittest
 
 import stem.descriptor
-import test.require
 
 from stem.descriptor.extrainfo_descriptor import (
   RelayExtraInfoDescriptor,
diff --git a/test/unit/descriptor/hidden_service_v3.py b/test/unit/descriptor/hidden_service_v3.py
index c24eb1ad..33c4da21 100644
--- a/test/unit/descriptor/hidden_service_v3.py
+++ b/test/unit/descriptor/hidden_service_v3.py
@@ -13,8 +13,6 @@ import stem.descriptor.hidden_service
 
 import test.require
 
-from unittest.mock import patch, Mock
-
 from stem.descriptor.hidden_service import (
   IntroductionPointV3,
   HiddenServiceDescriptorV3,
diff --git a/test/unit/descriptor/microdescriptor.py b/test/unit/descriptor/microdescriptor.py
index b0b8ebaf..e70b7307 100644
--- a/test/unit/descriptor/microdescriptor.py
+++ b/test/unit/descriptor/microdescriptor.py
@@ -6,7 +6,6 @@ import unittest
 
 import stem.descriptor
 import stem.exit_policy
-import test.require
 
 from stem.descriptor.microdescriptor import Microdescriptor
 from test.unit.descriptor import get_resource
diff --git a/test/unit/descriptor/networkstatus/directory_authority.py b/test/unit/descriptor/networkstatus/directory_authority.py
index 6153aa64..d4bb9905 100644
--- a/test/unit/descriptor/networkstatus/directory_authority.py
+++ b/test/unit/descriptor/networkstatus/directory_authority.py
@@ -4,8 +4,6 @@ Unit tests for the DirectoryAuthority of stem.descriptor.networkstatus.
 
 import unittest
 
-import test.require
-
 from stem.descriptor.networkstatus import (
   DirectoryAuthority,
   KeyCertificate,
diff --git a/test/unit/descriptor/networkstatus/document_v2.py b/test/unit/descriptor/networkstatus/document_v2.py
index 7fb96328..6ab0606b 100644
--- a/test/unit/descriptor/networkstatus/document_v2.py
+++ b/test/unit/descriptor/networkstatus/document_v2.py
@@ -5,8 +5,6 @@ Unit tests for the NetworkStatusDocumentV2 of stem.descriptor.networkstatus.
 import datetime
 import unittest
 
-import test.require
-
 from stem.descriptor.networkstatus import NetworkStatusDocumentV2
 from test.unit.descriptor import get_resource
 
diff --git a/test/unit/descriptor/networkstatus/key_certificate.py b/test/unit/descriptor/networkstatus/key_certificate.py
index ee5cd92c..21453a36 100644
--- a/test/unit/descriptor/networkstatus/key_certificate.py
+++ b/test/unit/descriptor/networkstatus/key_certificate.py
@@ -6,7 +6,6 @@ import datetime
 import unittest
 
 import stem.descriptor
-import test.require
 
 from stem.descriptor.networkstatus import KeyCertificate
 from test.unit.descriptor import get_resource
diff --git a/test/unit/descriptor/remote.py b/test/unit/descriptor/remote.py
index d3f43995..e57da92b 100644
--- a/test/unit/descriptor/remote.py
+++ b/test/unit/descriptor/remote.py
@@ -3,7 +3,6 @@ Unit tests for stem.descriptor.remote.
 """
 
 import http.client
-import io
 import socket
 import time
 import unittest
diff --git a/test/unit/descriptor/router_status_entry.py b/test/unit/descriptor/router_status_entry.py
index 4b2837b8..1fafd2c8 100644
--- a/test/unit/descriptor/router_status_entry.py
+++ b/test/unit/descriptor/router_status_entry.py
@@ -8,7 +8,6 @@ import functools
 import unittest
 
 import stem.descriptor
-import test.require
 
 from stem import Flag
 from stem.exit_policy import MicroExitPolicy
diff --git a/test/unit/doctest.py b/test/unit/doctest.py
index 267d2ca5..40791cf3 100644
--- a/test/unit/doctest.py
+++ b/test/unit/doctest.py
@@ -119,7 +119,6 @@ class TestDocumentation(unittest.TestCase):
           test_run = doctest.testfile(path, **args)
       elif path.endswith('/stem/manual.py'):
         try:
-          import sqlite3
           test_run = doctest.testfile(path, **args)
         except ImportError:
           pass  # manual module requires sqlite3
diff --git a/test/unit/exit_policy/policy.py b/test/unit/exit_policy/policy.py
index f1e74aeb..fede80ac 100644
--- a/test/unit/exit_policy/policy.py
+++ b/test/unit/exit_policy/policy.py
@@ -5,8 +5,6 @@ Unit tests for the stem.exit_policy.ExitPolicy class.
 import pickle
 import unittest
 
-from unittest.mock import Mock, patch
-
 from stem.exit_policy import (
   DEFAULT_POLICY_RULES,
   ExitPolicy,
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index fccdba57..5ccf1ce2 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -7,9 +7,6 @@ import itertools
 import os
 import unittest
 
-import stem.response
-import stem.descriptor.remote
-
 from unittest.mock import Mock, patch
 
 from stem.control import Controller

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits