[tor-commits] [nyx/master] Allow our tests to run with python3

atagar at torproject.org atagar at torproject.org
Thu Jun 11 16:56:40 UTC 2015


commit a610441d33f451a4e344cea0658b0273aac81ac1
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Jun 11 09:51:51 2015 -0700

    Allow our tests to run with python3
    
    No, Nyx doesn't fully support python3 yet but a step in the right direction.
    Now our tests pass when run via python3. Oh, and fixed a python3 installation
    issue in the process.
---
 nyx/arguments.py     |    2 +-
 nyx/config_panel.py  |   16 ++++++++--------
 nyx/util/log.py      |    2 +-
 run_tests.py         |   10 +++++-----
 setup.py             |    3 +--
 test/installation.py |   10 +++++-----
 6 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/nyx/arguments.py b/nyx/arguments.py
index 834fea1..4351974 100644
--- a/nyx/arguments.py
+++ b/nyx/arguments.py
@@ -251,4 +251,4 @@ def missing_event_types():
 
   tor_event_types = response.split(' ')
   recognized_types = TOR_EVENT_TYPES.values()
-  return filter(lambda x: x not in recognized_types, tor_event_types)
+  return list(filter(lambda x: x not in recognized_types, tor_event_types))
diff --git a/nyx/config_panel.py b/nyx/config_panel.py
index 170ef46..05a65e0 100644
--- a/nyx/config_panel.py
+++ b/nyx/config_panel.py
@@ -7,7 +7,7 @@ import curses
 import threading
 
 import nyx.controller
-import popups
+import nyx.popups
 
 from nyx.util import panel, tor_config, tor_controller, ui_tools
 
@@ -352,7 +352,7 @@ class ConfigPanel(panel.Panel):
     options = [FIELD_ATTR[field][0] for field in Field]
     old_selection = [FIELD_ATTR[field][0] for field in CONFIG['features.config.order']]
     option_colors = dict([FIELD_ATTR[field] for field in Field])
-    results = popups.show_sort_dialog(title_label, options, old_selection, option_colors)
+    results = nyx.popups.show_sort_dialog(title_label, options, old_selection, option_colors)
 
     if results:
       # converts labels back to enums
@@ -388,7 +388,7 @@ class ConfigPanel(panel.Panel):
 
           prompt_msg = '%s Value (esc to cancel): ' % config_option
           is_prepopulated = CONFIG['features.config.prepopulateEditValues']
-          new_value = popups.input_prompt(prompt_msg, initial_value if is_prepopulated else '')
+          new_value = nyx.popups.input_prompt(prompt_msg, initial_value if is_prepopulated else '')
 
           if new_value is not None and new_value != initial_value:
             try:
@@ -416,7 +416,7 @@ class ConfigPanel(panel.Panel):
 
               self.redraw(True)
             except Exception as exc:
-              popups.show_msg('%s (press any key)' % exc)
+              nyx.popups.show_msg('%s (press any key)' % exc)
       elif key.match('a'):
         self.show_all = not self.show_all
         self.redraw(True)
@@ -438,7 +438,7 @@ class ConfigPanel(panel.Panel):
     # display a popup for saving the current configuration
 
     config_lines = tor_config.get_custom_options(True)
-    popup, width, height = popups.init(len(config_lines) + 2)
+    popup, width, height = nyx.popups.init(len(config_lines) + 2)
 
     if not popup:
       return
@@ -544,7 +544,7 @@ class ConfigPanel(panel.Panel):
 
         if selection == 1:
           # prompts user for a configuration location
-          config_location = popups.input_prompt('Save to (esc to cancel): ', config_location)
+          config_location = nyx.popups.input_prompt('Save to (esc to cancel): ', config_location)
 
           if not config_location:
             prompt_canceled = True
@@ -556,9 +556,9 @@ class ConfigPanel(panel.Panel):
           except IOError as exc:
             msg = 'Unable to save configuration (%s)' % exc.strerror
 
-          popups.show_msg(msg, 2)
+          nyx.popups.show_msg(msg, 2)
     finally:
-      popups.finalize()
+      nyx.popups.finalize()
 
   def get_help(self):
     return [
diff --git a/nyx/util/log.py b/nyx/util/log.py
index c2e1a4c..3bd0d40 100644
--- a/nyx/util/log.py
+++ b/nyx/util/log.py
@@ -481,7 +481,7 @@ def read_tor_log(path, read_limit = None):
       timestamp_comp = list(time.strptime(timestamp_str, '%Y %b %d %H:%M:%S'))
       timestamp_comp[8] = isdst
 
-      timestamp = int(time.mktime(timestamp_comp))  # converts local to unix time
+      timestamp = int(time.mktime(tuple(timestamp_comp)))  # converts local to unix time
 
       if timestamp > time.time():
         # log entry is from before a year boundary
diff --git a/run_tests.py b/run_tests.py
index 6e62169..aea9d01 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -34,13 +34,13 @@ def main():
   orphaned_pyc = stem.util.test_tools.clean_orphaned_pyc(NYX_BASE)
 
   for path in orphaned_pyc:
-    print 'Deleted orphaned pyc file: %s' % path
+    print('Deleted orphaned pyc file: %s' % path)
 
   tests = unittest.defaultTestLoader.discover('test', pattern='*.py')
   test_runner = unittest.TextTestRunner()
   test_runner.run(tests)
 
-  print
+  print('')
 
   static_check_issues = {}
 
@@ -66,13 +66,13 @@ def main():
         static_check_issues.setdefault(path, []).append(issue)
 
   if static_check_issues:
-    print 'STATIC CHECKS'
+    print('STATIC CHECKS')
 
     for file_path in static_check_issues:
-      print '* %s' % file_path
+      print('* %s' % file_path)
 
       for issue in static_check_issues[file_path]:
-        print '  line %-4s - %-40s %s' % (issue.line_number, issue.message, issue.line)
+        print('  line %-4s - %-40s %s' % (issue.line_number, issue.message, issue.line))
 
       print
 
diff --git a/setup.py b/setup.py
index 43d7a97..f5399ef 100644
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,6 @@
 
 import gzip
 import os
-import shutil
 import stat
 import sysconfig
 
@@ -64,7 +63,7 @@ class NyxInstaller(install):
         new_shebang = '#!%s\n' % os.path.join(sysconfig.get_config_var('BINDIR'), python_cmd)
 
         log.info("adjusting bin script's shebang line '%s' -> '%s'" % (orig_shebang.strip(), new_shebang.strip()))
-        dest_file.write(new_shebang)
+        dest_file.write(str.encode(new_shebang))
         dest_file.write(source_file.read())
 
     mode = ((os.stat(dest)[stat.ST_MODE]) | 0o555) & 0o7777
diff --git a/test/installation.py b/test/installation.py
index e233d67..3095e16 100644
--- a/test/installation.py
+++ b/test/installation.py
@@ -20,20 +20,20 @@ class TestInstallation(unittest.TestCase):
 
     try:
       os.chdir(base_directory)
-      stem.util.system.call('python setup.py install --prefix /tmp/nyx_test --man-page /tmp/nyx_test/nyx.1.gz --sample-path /tmp/nyx_test/nyxrc.sample')
-      stem.util.system.call('python setup.py clean --all')  # tidy up the build directory
+      stem.util.system.call(sys.executable + ' setup.py install --prefix /tmp/nyx_test --man-page /tmp/nyx_test/nyx.1.gz --sample-path /tmp/nyx_test/nyxrc.sample')
+      stem.util.system.call(sys.executable + ' setup.py clean --all')  # tidy up the build directory
       site_packages_paths = glob.glob('/tmp/nyx_test/lib*/*/site-packages')
 
       if len(site_packages_paths) != 1:
         self.fail('We should only have a single site-packages directory, but instead had: %s' % site_packages_paths)
 
-      self.assertEqual(nyx.__version__, stem.util.system.call(['python', '-c', "import sys;sys.path.insert(0, '%s');import nyx;print(nyx.__version__)" % site_packages_paths[0]])[0])
+      self.assertEqual(nyx.__version__, stem.util.system.call([sys.executable, '-c', "import sys;sys.path.insert(0, '%s');import nyx;print(nyx.__version__)" % site_packages_paths[0]])[0])
 
-      process_path = sys.path + ['/tmp/nyx_test/lib/python2.7/site-packages']
+      process_path = [site_packages_paths[0]] + sys.path
       process = subprocess.Popen(['/tmp/nyx_test/bin/nyx', '--help'], stdout = subprocess.PIPE, env = {'PYTHONPATH': ':'.join(process_path)})
       stdout = process.communicate()[0]
 
-      self.assertTrue(stdout.startswith('Usage nyx [OPTION]'))
+      self.assertTrue(stdout.startswith(b'Usage nyx [OPTION]'))
     finally:
       shutil.rmtree('/tmp/nyx_test')
       os.chdir(original_cwd)



More information about the tor-commits mailing list