Commits

Stefan Schwarzer committed 002060b Merge

Branch merge.

I shouldn't have so many repositories. ;-)

Comments (0)

Files changed (2)

ftputil/compat.py

+# encoding: utf-8
+# Copyright (C) 2011, Stefan Schwarzer <sschwarzer@sschwarzer.net>
+# See the file LICENSE for licensing terms.
+
+"""
+Some functions to make the code work also on Python 3 after applying
+the 2to3 utility.
+
+The comments given for the Python 2 versions of the helpers apply to
+the Python 3 helpers as well.
+"""
+
+# Note that more imports are to be found in the large `if`
+#  statement below, because they work only in Python 2 or
+#  Python 3.
+import sys
+
+
+if sys.version_info[0] == 2:
+
+    # As a low-level networking library, ftputil mostly works on
+    #  byte strings, so 2to3's approach to turn byte strings into
+    #  unicode strings won't work most of the time.
+    def b(byte_string):
+        return byte_string
+
+    # Similarly for `StringIO` and such.
+    import StringIO
+    byte_string_io = StringIO.StringIO
+
+else:
+
+    def b(byte_string):
+        return bytes(byte_string, encoding="ASCII")
+
+    import io
+    byte_string_io = io.BytesIO

test/test_ftp_stat.py

 def test_stat():
     host = test_base.ftp_host_factory()
     stat = ftp_stat._Stat(host)
-    # use Unix format parser explicitly
+    # Use Unix format parser explicitly.
     stat._parser = ftp_stat.UnixParser()
     return stat
 
             parse_result = parser.parse_line(line)
             stat_result = list(parse_result) + [parse_result._st_name,
                                                 parse_result._st_target]
-            # Convert time tuple to seconds
+            # Convert time tuple to seconds.
             expected_stat_result[8] = \
               stat_tuple_to_seconds(expected_stat_result[8])
-            # Compare both lists
+            # Compare both lists.
             self.assertEqual(stat_result, expected_stat_result)
 
     def _test_invalid_lines(self, parser_class, lines):
         # If in this year it's after Dec 19, 23:11, use the current
         #  year, else use the previous year ...
         now = time.localtime()
-        # We need only month, day, hour and minute
+        # We need only month, day, hour and minute.
         current_time_parts = now[1:5]
         time_parts_in_listing = (12, 19, 23, 11)
         if current_time_parts > time_parts_in_listing:
         to full hours.
         """
         host = test_base.ftp_host_factory()
-        # Explicitly use Unix format parser
+        # Explicitly use Unix format parser here.
         host._stat._parser = ftp_stat.UnixParser()
         host.set_time_shift(supposed_time_shift)
         server_time = time.time() + supposed_time_shift + deviation