Changeset 1205:0eba346a99e5


Ignore:
Timestamp:
Mar 31, 2013, 6:49:04 PM (7 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Processed tests and `setup.py` with `2to3` and fixed a few things.

When run under Python 2.7, most tests pass. However, there are still
unicode-related error messages in `test_real_ftp.py`. Moreover,
returning the correct type (i. e. the same type as the argument) from
several functions/methods doesn't work yet. Mostly, there aren't unit
tests for this yet, so this problem might give an inappropriate
confidence.

There a still quite a few test failures when running under Python 3.2,
but I didn't expect anything else.
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • ftputil/compat.py

    r1199 r1205  
    11# encoding: utf-8
    2 # Copyright (C) 2011, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     2# Copyright (C) 2011-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    33# See the file LICENSE for licensing terms.
    44
     
    1111"""
    1212
    13 # Note that more imports are to be found in the large `if`
    14 # statement below, because they work only in Python 2 or
    15 # Python 3.
    1613import sys
    1714
     
    2724    int_types = (int, long)
    2825
     26    unicode_type = unicode
     27    bytes_type = str
     28
     29    # Non-evaluating input
     30    input = raw_input
     31
    2932else:
    3033
     
    3336
    3437    int_types = (int,)
     38
     39    unicode_type = str
     40    bytes_type = bytes
     41
     42    input = input
  • ftputil/stat.py

    r1199 r1205  
    466466        Return a list of directories, files etc. in the directory
    467467        named `path`.
     468
     469        Like `os.listdir` the returned list elements have the type
     470        of the argument.
    468471
    469472        If the directory listing from the server can't be parsed
  • setup.py

    r1197 r1205  
    66setup.py - installation script for Python distutils
    77"""
     8
     9from __future__ import print_function
    810
    911import os
     
    2729
    2830if "install" in sys.argv[1:] and not doc_files_are_present:
    29     print "One or more of the HTML documentation files are missing."
    30     print "Please generate them with `make docs`."
     31    print("One or more of the HTML documentation files are missing.")
     32    print("Please generate them with `make docs`.")
    3133    sys.exit(1)
    3234
  • test/mock_ftplib.py

    r1196 r1205  
    1010"""
    1111
     12from __future__ import unicode_literals
     13
     14import io
     15import collections
    1216import ftplib
    1317import posixpath
    14 import StringIO
    1518
    1619DEBUG = 0
     
    2528
    2629
    27 class MockFile(StringIO.StringIO, object):
     30class MockFile(io.BytesIO, object):
    2831    """
    2932    Mock class for the file objects _contained in_ `_FTPFile` objects
     
    3336    be queried for their contents after they have been closed.
    3437    """
    35     def __init__(self, path, content=''):
     38    def __init__(self, path, content=b''):
    3639        global mock_files
    3740        mock_files[path] = self
     
    5760    def __init__(self, path, mock_file_content=''):
    5861        if DEBUG:
    59             print "File content: *{0}*".format(mock_file_content)
     62            print("File content: *{0}*".format(mock_file_content))
    6063        self.file_path = path
    6164        self.mock_file_content = mock_file_content
     
    8992
    9093    # File content to be used (indirectly) with `transfercmd`.
    91     mock_file_content = ''
     94    mock_file_content = b''
    9295
    9396    def __init__(self, host='', user='', password=''):
     
    101104    def voidcmd(self, cmd):
    102105        if DEBUG:
    103             print cmd
     106            print(cmd)
    104107        if cmd == 'STAT':
    105108            return 'MockSession server awaiting your commands ;-)'
     
    129132        a directory listing. Return nothing.
    130133        """
    131         if callable(args[-1]):
     134        if isinstance(args[-1], collections.Callable):
    132135            callback = args[-1]
    133136            args = args[:-1]
     
    137140        path = args[-1]
    138141        if DEBUG:
    139             print "dir: {0}".format(path)
     142            print("dir: {0}".format(path))
    140143        path = self._transform_path(path)
    141         if not self.dir_contents.has_key(path):
     144        if path not in self.dir_contents:
    142145            raise ftplib.error_perm
    143146        dir_lines = self.dir_contents[path].split('\n')
    144147        for line in dir_lines:
    145148            if callback is None:
    146                 print line
     149                print(line)
    147150            else:
    148151                callback(line)
     
    159162        """
    160163        if DEBUG:
    161             print cmd
     164            print(cmd)
    162165        # Fail if attempting to read from/write to a directory
    163166        cmd, path = cmd.split()
    164167        path = self._remove_trailing_slash(path)
    165         if self.dir_contents.has_key(path):
     168        if path in self.dir_contents:
    166169            raise ftplib.error_perm
    167170        # Fail if path isn't available (this name is hard-coded here
  • test/test_error.py

    r1194 r1205  
    1717    def test_unicode_argument(self):
    1818        # An umlaut as unicode character
    19         io_error = ftputil.error.FTPIOError(u"\xe4")
     19        io_error = ftputil.error.FTPIOError("\xe4")
    2020
    2121
  • test/test_file.py

    r1187 r1205  
    192192        input_ = host.file('dummy')
    193193        input_iterator = iter(input_)
    194         self.assertEqual(input_iterator.next(), "line 1\n")
    195         self.assertEqual(input_iterator.next(), "another line\n")
    196         self.assertEqual(input_iterator.next(), "yet another line")
    197         self.assertRaises(StopIteration, input_iterator.next)
     194        self.assertEqual(next(input_iterator), "line 1\n")
     195        self.assertEqual(next(input_iterator), "another line\n")
     196        self.assertEqual(next(input_iterator), "yet another line")
     197        self.assertRaises(StopIteration, input_iterator.__next__)
    198198        input_.close()
    199199
     
    203203        input_ = host.file('dummy', 'rb')
    204204        input_iterator = iter(input_)
    205         self.assertEqual(input_iterator.next(), "line 1\r\n")
    206         self.assertEqual(input_iterator.next(), "another line\r\n")
    207         self.assertEqual(input_iterator.next(), "yet another line")
    208         self.assertRaises(StopIteration, input_iterator.next)
     205        self.assertEqual(next(input_iterator), "line 1\r\n")
     206        self.assertEqual(next(input_iterator), "another line\r\n")
     207        self.assertEqual(next(input_iterator), "yet another line")
     208        self.assertRaises(StopIteration, input_iterator.__next__)
    209209        input_.close()
    210210
  • test/test_file_transfer.py

    r1136 r1205  
    22# See the file LICENSE for licensing terms.
    33
     4import io
    45import random
    5 import StringIO
    66import unittest
    77
    8 from ftputil import file_transfer
     8import ftputil.file_transfer
    99
    1010
     
    5959            source_file = MockFile(source_mtime, source_mtime_precision)
    6060            target_file = MockFile(target_mtime, target_mtime_precision)
    61             result = file_transfer.source_is_newer_than_target(source_file,
    62                                                                target_file)
     61            result = ftputil.file_transfer.source_is_newer_than_target(
     62                       source_file, target_file)
    6363            self.assertEqual(result, expected_result)
    6464
    6565
    66 class FailingStringIO(StringIO.StringIO):
     66class FailingStringIO(io.BytesIO):
    6767    """Mock class to test whether exceptions are passed on."""
    6868
     
    7777
    7878    def random_string(self, count):
    79         """Return a `StringIO` object containing `count` "random" bytes."""
    80         ints = (random.randint(0, 255) for i in xrange(count))
    81         return "".join((chr(i) for i in ints))
     79        """Return a `BytesIO` object containing `count` "random" bytes."""
     80        ints = (random.randint(0, 255) for i in range(count))
     81        return b"".join((chr(i) for i in ints))
    8282
    8383    def test_chunkwise_transfer_without_remainder(self):
    8484        """Check if we get four chunks with 256 Bytes each."""
    8585        data = self.random_string(1024)
    86         fobj = StringIO.StringIO(data)
    87         chunks = list(file_transfer.chunks(fobj, 256))
     86        fobj = io.BytesIO(data)
     87        chunks = list(ftputil.file_transfer.chunks(fobj, 256))
    8888        self.assertEqual(len(chunks), 4)
    8989        self.assertEqual(chunks[0], data[:256])
     
    9595        """Check if we get three chunks with 256 Bytes and one with 253."""
    9696        data = self.random_string(1021)
    97         fobj = StringIO.StringIO(data)
    98         chunks = list(file_transfer.chunks(fobj, 256))
     97        fobj = io.BytesIO(data)
     98        chunks = list(ftputil.file_transfer.chunks(fobj, 256))
    9999        self.assertEqual(len(chunks), 4)
    100100        self.assertEqual(chunks[0], data[:256])
     
    107107        data = self.random_string(1024)
    108108        fobj = FailingStringIO(data)
    109         iterator = file_transfer.chunks(fobj, 256)
    110         self.assertRaises(FailingStringIO.expected_exception,
    111                           iterator.next)
     109        iterator = ftputil.file_transfer.chunks(fobj, 256)
     110        self.assertRaises(FailingStringIO.expected_exception, next, iterator)
    112111
    113112
    114113if __name__ == '__main__':
    115114    unittest.main()
    116 
  • test/test_ftputil.py

    r1190 r1205  
    3535def ascii_data():
    3636    """Return an ASCII character string."""
    37     pool = range(32, 128)
     37    pool = list(range(32, 128))
    3838    pool.append(ord('\n'))
    3939    return random_data(pool)
     
    4242def binary_data():
    4343    """Return a binary character string."""
    44     pool = range(0, 256)
     44    pool = list(range(0, 256))
    4545    return random_data(pool)
    4646
     
    226226        host = test_base.ftp_host_factory()
    227227        self.assertRaises(ftputil.error.PermanentError, host.chmod,
    228                           "nonexistent", 0644)
     228                          "nonexistent", 0o644)
    229229        # `CommandNotImplementedError` is a subclass of `PermanentError`
    230230        self.assertRaises(ftputil.error.CommandNotImplementedError,
    231                           host.chmod, "nonexistent", 0644)
     231                          host.chmod, "nonexistent", 0o644)
    232232
    233233
  • test/test_path.py

    r1187 r1205  
    11# Copyright (C) 2003-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# See the file LICENSE for licensing terms.
     3
     4from __future__ import unicode_literals
    35
    46import ftplib
     
    68
    79import ftputil
     10import ftputil.compat
    811import ftputil.error
    912
  • test/test_public_servers.py

    r1196 r1205  
    168168                initial_directory = initial_directory.replace(
    169169                                      "DIR", actual_initial_directory)
    170                 print server, initial_directory
     170                print(server, initial_directory)
    171171                self.inner_test_server(server, initial_directory, paths)
    172172
  • test/test_real_ftp.py

    r1196 r1205  
    77
    88from __future__ import absolute_import
     9from __future__ import unicode_literals
    910
    1011import ftplib
     12import functools
    1113import gc
    1214import getpass
     
    1921
    2022import ftputil
     23import ftputil.compat
    2124import ftputil.error
    2225import ftputil.file_transfer
     
    621624          stat.S_IRWXG, stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP,
    622625          stat.S_IRWXO, stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH]
    623         allowed_mask = reduce(operator.or_, allowed_flags)
     626        allowed_mask = functools.reduce(operator.or_, allowed_flags)
    624627        mode = full_mode & allowed_mask
    625628        self.assertEqual(mode, expected_mode,
     
    631634        self.cleaner.add_dir("_test dir_")
    632635        # Set/get mode of a directory
    633         host.chmod("_test dir_", 0757)
    634         self.assert_mode("_test dir_", 0757)
     636        host.chmod("_test dir_", 0o757)
     637        self.assert_mode("_test dir_", 0o757)
    635638        # Set/get mode in nested directory
    636639        host.mkdir("_test dir_/nested_dir")
    637640        self.cleaner.add_dir("_test dir_/nested_dir")
    638641        # Set/get mode of a directory
    639         host.chmod("_test dir_/nested_dir", 0757)
    640         self.assert_mode("_test dir_/nested_dir", 0757)
     642        host.chmod("_test dir_/nested_dir", 0o757)
     643        self.assert_mode("_test dir_/nested_dir", 0o757)
    641644
    642645    def test_chmod_existing_file(self):
     
    647650        file_name = host.path.join("_test dir_", "_testfile_")
    648651        self.make_file(file_name)
    649         host.chmod(file_name, 0646)
    650         self.assert_mode(file_name, 0646)
     652        host.chmod(file_name, 0o646)
     653        self.assert_mode(file_name, 0o646)
    651654
    652655    def test_chmod_nonexistent_path(self):
    653656        # Set/get mode of a directory
    654657        self.assertRaises(ftputil.error.PermanentError, self.host.chmod,
    655                           "nonexistent", 0757)
     658                          "nonexistent", 0o757)
    656659
    657660    def test_cache_invalidation(self):
     
    662665        unused_stat_result = host.stat("_test dir_")
    663666        # Set/get mode of a directory
    664         host.chmod("_test dir_", 0757)
    665         self.assert_mode("_test dir_", 0757)
     667        host.chmod("_test dir_", 0o757)
     668        self.assert_mode("_test dir_", 0o757)
    666669        # Set/get mode on a file
    667670        file_name = host.path.join("_test dir_", "_testfile_")
     
    669672        # Make sure the mode is in the cache
    670673        unused_stat_result = host.stat(file_name)
    671         host.chmod(file_name, 0646)
    672         self.assert_mode(file_name, 0646)
     674        host.chmod(file_name, 0o646)
     675        self.assert_mode(file_name, 0o646)
    673676
    674677
     
    688691 
    689692    def assert_non_unicode(self, s):
    690         self.assertFalse(isinstance(s, unicode))
     693        self.assertFalse(isinstance(s, ftputil.compat.unicode_type))
    691694
    692695    def assert_unicode_error(self, function, *args):
     
    702705        finally:
    703706            fobj.close()
    704         fobj = host.file(u"CONTENTS")
     707        fobj = host.file("CONTENTS")
    705708        try:
    706709            self.assert_non_unicode(fobj.name)
     
    708711            fobj.close()
    709712        # Check if non-encodable unicode strings are refused.
    710         self.assert_unicode_error(host.file, u"ä")
     713        self.assert_unicode_error(host.file, "ä")
    711714
    712715    def test_upload(self):
    713         self.assert_unicode_error(self.host.upload, "ftputil.py", u"ä")
     716        self.assert_unicode_error(self.host.upload, "ftputil.py", "ä")
    714717
    715718    def test_upload_if_newer(self):
    716719        self.assert_unicode_error(self.host.upload_if_newer,
    717                                   "ftputil.py", u"ä")
     720                                  "ftputil.py", "ä")
    718721
    719722    def test_download(self):
    720         self.assert_unicode_error(self.host.download, u"ä", "ok")
     723        self.assert_unicode_error(self.host.download, "ä", "ok")
    721724
    722725    def test_download_if_newer(self):
    723         self.assert_unicode_error(self.host.download_if_newer, u"ä", "ok")
     726        self.assert_unicode_error(self.host.download_if_newer, "ä", "ok")
    724727
    725728    def test_chdir(self):
     
    728731        host.chdir(".")
    729732        self.assert_non_unicode(host.getcwd())
    730         host.chdir(u".")
     733        host.chdir(".")
    731734        self.assert_non_unicode(host.getcwd())
    732735        # Fail early if string can't be encoded to ASCII.
    733         self.assert_unicode_error(host.chdir, u"ä")
     736        self.assert_unicode_error(host.chdir, "ä")
    734737
    735738    def test_rename(self):
    736         self.assert_unicode_error(self.host.rename, u"ä", "b")
    737         self.assert_unicode_error(self.host.rename, "b", u"ä")
     739        self.assert_unicode_error(self.host.rename, "ä", "b")
     740        self.assert_unicode_error(self.host.rename, "b", "ä")
    738741
    739742    def test_walk(self):
    740743        # The string test is only executed when the first item is
    741744        #  requested from the generator.
    742         iterator = self.host.walk(u"ä")
    743         self.assert_unicode_error(iterator.next)
     745        iterator = self.host.walk("ä")
     746        self.assert_unicode_error(next, iterator)
    744747
    745748    def test_chmod(self):
    746         self.assert_unicode_error(self.host.chmod, u"ä", 0644)
     749        self.assert_unicode_error(self.host.chmod, "ä", 0o644)
    747750
    748751    def test_single_path_methods(self):
     
    752755          "mkdir makedirs rmdir remove rmtree listdir lstat stat".split():
    753756            method = getattr(self.host, method_name)
    754             self.assert_unicode_error(method, u"ä")
     757            self.assert_unicode_error(method, "ä")
    755758
    756759    def test_path(self):
     
    758761          "abspath exists getmtime getsize isfile isdir islink".split():
    759762            method = getattr(self.host.path, method_name)
    760             self.assert_unicode_error(method, u"ä")
     763            self.assert_unicode_error(method, "ä")
    761764
    762765    def test_path_walk(self):
    763766        def noop():
    764767            pass
    765         self.assert_unicode_error(self.host.path.walk, u"ä", noop, None)
     768        self.assert_unicode_error(self.host.path.walk, "ä", noop, None)
    766769
    767770
     
    819822
    820823    def _make_objects_to_be_garbage_collected(self):
    821         for i in xrange(10):
     824        for i in range(10):
    822825            with ftputil.FTPHost(server, user, password) as host:
    823                 for j in xrange(10):
     826                for j in range(10):
    824827                    stat_result = host.stat("CONTENTS")
    825828                    with host.file("CONTENTS") as fobj:
     
    837840
    838841if __name__ == '__main__':
    839     print """\
     842    print("""\
    840843Test real FTP access.
    841844
     
    847850You'll need write access in the login directory. This test can take a few
    848851minutes because it has to wait to test the timezone calculation.
    849 """
     852""")
    850853    try:
    851         raw_input("[Return] to continue, or [Ctrl-C] to skip test. ")
     854        ftputil.compat.input(
     855          "[Return] to continue, or [Ctrl-C] to skip test. ")
    852856    except KeyboardInterrupt:
    853         print "\nTest aborted."
     857        print("\nTest aborted.")
    854858        sys.exit()
    855859    # Get login data only once, not for each test
  • test/test_stat.py

    r1196 r1205  
    226226            self.assertFalse(abs_difference > 60.0)
    227227        except AssertionError:
    228             print "Difference is", abs_difference, "seconds"
     228            print("Difference is", abs_difference, "seconds")
    229229            raise
    230230
  • test/test_stat_cache.py

    r1196 r1205  
    4848    def test_resize(self):
    4949        self.cache.resize(100)
    50         for i in xrange(150):
     50        for i in range(150):
    5151            self.cache["/{0:d}".format(i)] = i
    5252        self.assertEqual(len(self.cache), 100)
  • test/test_sync.py

    r1187 r1205  
    22# See the file LICENSE for licensing terms.
    33
     4from __future__ import unicode_literals
     5
     6import io
    47import ntpath
    58import os
    69import shutil
    7 import StringIO
    810import sys
    911import unittest
     
    5557    def __init__(self):
    5658        self.path = LocalWindowsHostPath()
    57         self.sep = u"\\"
     59        self.sep = "\\"
    5860
    5961    def open(self, path, mode):
    6062        # Just return a dummy file object.
    61         return StringIO.StringIO(u"")
     63        return io.StringIO("")
    6264
    6365    def walk(self, root):
     
    7678        join = ntpath.join
    7779        return [(root,
    78                  [join(root, u"dir1")],
     80                 [join(root, "dir1")],
    7981                 []),
    80                 (join(root, u"dir1"),
    81                  [u"dir11"],
    82                  [u"file1", u"file2"])
     82                (join(root, "dir1"),
     83                 ["dir11"],
     84                 ["file1", "file2"])
    8385                ]
    8486
     
    8789
    8890    def pwd(self):
    89         return u"/"
     91        return "/"
    9092
    9193    def dir(self, *args):
     
    101103
    102104    def isdir(self, path):
    103         return path[:-1].endswith(u"dir")
     105        return path[:-1].endswith("dir")
    104106
    105107    def isfile(self, path):
    106         return path[:-1].endswith(u"file")
     108        return path[:-1].endswith("file")
    107109
    108110
     
    117119
    118120    def mkdir(self, path):
    119         assert u"\\" not in path
     121        assert "\\" not in path
    120122
    121123    def open(self, path, mode):
    122         assert u"\\" not in path
    123         return StringIO.StringIO(u"")
     124        assert "\\" not in path
     125        return io.StringIO("")
    124126
    125127
     
    129131        source = LocalWindowsHost()
    130132        target = ArgumentCheckingFTPHost()
    131         local_root = ntpath.join(u"some", u"directory")
     133        local_root = ntpath.join("some", "directory")
    132134        syncer = ftputil.sync.Syncer(source, target)
    133135        # If the following call raises any `AssertionError`s, the
    134136        # `unittest` framework will catch them and show them.
    135         syncer.sync(local_root, u"not_used_by_ArgumentCheckingFTPHost")
     137        syncer.sync(local_root, "not_used_by_ArgumentCheckingFTPHost")
    136138
    137139
  • test/test_with_statement.py

    r1187 r1205  
    22# Copyright (C) 2008-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    33# See the file LICENSE for licensing terms.
    4 
    5 from __future__ import with_statement
    64
    75import unittest
Note: See TracChangeset for help on using the changeset viewer.