[tor-commits] [stem/master] Adding future 'with' import and checking for it

atagar at torproject.org atagar at torproject.org
Tue Jun 12 02:09:41 UTC 2012


commit c52042c44b5a157dceb6013743fdea40bbc3f52c
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Jun 11 10:01:17 2012 -0700

    Adding future 'with' import and checking for it
    
    Stem aims for python 2.5 compatability, but in that version the 'with' keyword
    wasn't available by default and would only function with a 'future' import.
    This has the possability of being a continuing pain in our ass so adding this
    as a check to the check_whitespace module. Admittedly this isn't a whitespace
    issue, but that module is just so damn conveninent for lintian style issues
    too...
    
    While doing this I discovered that we aren't running the checker over the stem
    module (just the tests, it seems), so something else to fix...
---
 test/check_whitespace.py                      |   14 ++++++++++++++
 test/integ/connection/authentication.py       |    2 ++
 test/integ/control/base_controller.py         |    2 ++
 test/integ/control/controller.py              |    2 ++
 test/integ/descriptor/extrainfo_descriptor.py |    2 ++
 test/integ/descriptor/reader.py               |    2 ++
 test/integ/descriptor/server_descriptor.py    |    2 ++
 test/integ/response/protocolinfo.py           |    2 ++
 test/integ/socket/control_message.py          |    2 ++
 test/integ/socket/control_socket.py           |    2 ++
 test/runner.py                                |    2 ++
 11 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/test/check_whitespace.py b/test/check_whitespace.py
index db97130..5aed7fa 100644
--- a/test/check_whitespace.py
+++ b/test/check_whitespace.py
@@ -6,6 +6,12 @@ which are...
 * tabs are the root of all evil and should be shot on sight
 * no trailing whitespace unless the line is empty, in which case it should have
   the same indentation as the surrounding code
+
+This also checks for 2.5 compatability issues (yea, they're not whitespace but
+it's so much easier to do here...):
+
+* checks that anything using the 'with' keyword has...
+  from __future__ import with_statement
 """
 
 import re
@@ -32,10 +38,18 @@ def get_issues(base_path = DEFAULT_TARGET):
   for file_path in _get_python_files(base_path):
     with open(file_path) as f: file_contents = f.read()
     lines, file_issues, prev_indent = file_contents.splitlines(), [], 0
+    has_with_import, given_with_warning = False, False
     
     for i in xrange(len(lines)):
       whitespace, content = re.match("^(\s*)(.*)$", lines[i]).groups()
       
+      if content == "from __future__ import with_statement":
+        has_with_import = True
+      elif content.startswith("with ") and content.endswith(":") \
+        and not has_with_import and not given_with_warning:
+        file_issues.append((i + 1, "missing 'with' import (from __future__ import with_statement)"))
+        given_with_warning = True
+      
       if "\t" in whitespace:
         file_issues.append((i + 1, "indentation has a tab"))
       elif content != content.rstrip():
diff --git a/test/integ/connection/authentication.py b/test/integ/connection/authentication.py
index 4f1d504..31407a3 100644
--- a/test/integ/connection/authentication.py
+++ b/test/integ/connection/authentication.py
@@ -3,6 +3,8 @@ Integration tests for authenticating to the control socket via
 stem.connection.authenticate* functions.
 """
 
+from __future__ import with_statement
+
 import os
 import unittest
 
diff --git a/test/integ/control/base_controller.py b/test/integ/control/base_controller.py
index 32239d2..ab5ded5 100644
--- a/test/integ/control/base_controller.py
+++ b/test/integ/control/base_controller.py
@@ -2,6 +2,8 @@
 Integration tests for the stem.control.BaseController class.
 """
 
+from __future__ import with_statement
+
 import re
 import time
 import unittest
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 1b743e1..b3eb523 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -2,6 +2,8 @@
 Integration tests for the stem.control.Controller class.
 """
 
+from __future__ import with_statement
+
 import unittest
 
 import stem.control
diff --git a/test/integ/descriptor/extrainfo_descriptor.py b/test/integ/descriptor/extrainfo_descriptor.py
index d7244a9..45d1a2b 100644
--- a/test/integ/descriptor/extrainfo_descriptor.py
+++ b/test/integ/descriptor/extrainfo_descriptor.py
@@ -2,6 +2,8 @@
 Integration tests for stem.descriptor.extrainfo_descriptor.
 """
 
+from __future__ import with_statement
+
 import os
 import datetime
 import unittest
diff --git a/test/integ/descriptor/reader.py b/test/integ/descriptor/reader.py
index 0e6c867..bebb917 100644
--- a/test/integ/descriptor/reader.py
+++ b/test/integ/descriptor/reader.py
@@ -2,6 +2,8 @@
 Integration tests for stem.descriptor.reader.
 """
 
+from __future__ import with_statement
+
 import os
 import sys
 import time
diff --git a/test/integ/descriptor/server_descriptor.py b/test/integ/descriptor/server_descriptor.py
index 449fb2f..0af3cdf 100644
--- a/test/integ/descriptor/server_descriptor.py
+++ b/test/integ/descriptor/server_descriptor.py
@@ -2,6 +2,8 @@
 Integration tests for stem.descriptor.server_descriptor.
 """
 
+from __future__ import with_statement
+
 import os
 import datetime
 import unittest
diff --git a/test/integ/response/protocolinfo.py b/test/integ/response/protocolinfo.py
index 6b7e397..a80f406 100644
--- a/test/integ/response/protocolinfo.py
+++ b/test/integ/response/protocolinfo.py
@@ -3,6 +3,8 @@ Integration tests for the stem.response.protocolinfo.ProtocolInfoResponse class
 and related functions.
 """
 
+from __future__ import with_statement
+
 import unittest
 
 import test.runner
diff --git a/test/integ/socket/control_message.py b/test/integ/socket/control_message.py
index c148605..1578917 100644
--- a/test/integ/socket/control_message.py
+++ b/test/integ/socket/control_message.py
@@ -2,6 +2,8 @@
 Integration tests for the stem.response.ControlMessage class.
 """
 
+from __future__ import with_statement
+
 import re
 import unittest
 
diff --git a/test/integ/socket/control_socket.py b/test/integ/socket/control_socket.py
index b00e381..1f06027 100644
--- a/test/integ/socket/control_socket.py
+++ b/test/integ/socket/control_socket.py
@@ -8,6 +8,8 @@ those focus on parsing and correctness of the content these are more concerned
 with the behavior of the socket itself.
 """
 
+from __future__ import with_statement
+
 import unittest
 
 import stem.connection
diff --git a/test/runner.py b/test/runner.py
index 8c01c3e..7d053cb 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -33,6 +33,8 @@ about the tor test instance they're running against.
     +- get_tor_command - provides the command used to start tor
 """
 
+from __future__ import with_statement
+
 import os
 import sys
 import time





More information about the tor-commits mailing list