Ignore:
Timestamp:
Jul 28, 2013, 7:42:28 PM (6 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Convert paths to unicode on entry in public methods.

This fixes many of the failed tests (see previous commit
13b3ce5d06a4).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftputil/host.py

    r1284 r1288  
    6464        self.stat_cache.enable()
    6565        with ftputil.error.ftplib_error_to_ftp_os_error:
    66             self._cached_current_dir = self._session.pwd()
     66            self._cached_current_dir = \
     67              ftputil.tool.as_unicode(self._session.pwd())
    6768        # Associated `FTPHost` objects for data transfer.
    6869        self._children = []
     
    163164        if none is available.
    164165        """
    165         # Fail early if we get a unicode path which can't be encoded.
    166         path = str(path)
     166        path = ftputil.tool.as_unicode(path)
    167167        host = self._available_child()
    168168        if host is None:
     
    442442        text copies, or 'b' for binary copies.
    443443        """
    444         # Fail early if we get a unicode path which can't be encoded.
    445         # Only attempt to convert the remote `target` name to a
    446         # bytestring. Leave it to the local filesystem whether it
    447         # wants to support unicode filenames or not.
    448         target = str(target)
     444        target = ftputil.tool.as_unicode(target)
    449445        source_file, target_file = self._upload_files(source, target, mode)
    450446        ftputil.file_transfer.copy_file(source_file, target_file,
     
    459455        If an upload was necessary, return `True`, else return `False`.
    460456        """
    461         # See comment in `upload`.
    462         target = str(target)
     457        target = ftputil.tool.as_unicode(target)
    463458        source_file, target_file = self._upload_files(source, target, mode)
    464459        return ftputil.file_transfer.copy_file(source_file, target_file,
     
    486481        text copies, or 'b' for binary copies.
    487482        """
    488         # Fail early if we get a unicode path which can't be encoded.
    489         # Only attempt to convert the remote `source` name to a
    490         # bytestring. We leave it to the local filesystem whether it
    491         # wants to support unicode filenames or not.
    492         source = str(source)
     483        source = ftputil.tool.as_unicode(source)
    493484        source_file, target_file = self._download_files(source, target, mode)
    494485        ftputil.file_transfer.copy_file(source_file, target_file,
     
    504495        `False`.
    505496        """
    506         # See comment in `download`.
    507         source = str(source)
     497        source = ftputil.tool.as_unicode(source)
    508498        source_file, target_file = self._download_files(source, target, mode)
    509499        return ftputil.file_transfer.copy_file(source_file, target_file,
     
    576566    def chdir(self, path):
    577567        """Change the directory on the host."""
    578         path = ftputil.tool.as_bytes(path)
     568        path = ftputil.tool.as_unicode(path)
    579569        with ftputil.error.ftplib_error_to_ftp_os_error:
    580570            self._session.cwd(path)
     
    592582        `os.mkdir`.
    593583        """
    594         # Fail early if we get a unicode path which can't be encoded.
    595         path = str(path)
     584        path = ftputil.tool.as_unicode(path)
    596585        def command(self, path):
    597586            """Callback function."""
     
    609598        `os.makedirs` but otherwise ignored.
    610599        """
    611         # Fail early if we get a unicode path which can't be encoded.
    612         path = str(path)
     600        path = ftputil.tool.as_unicode(path)
    613601        path = self.path.abspath(path)
    614602        directories = path.split(self.sep)
     
    638626        is no longer supported.
    639627        """
    640         # Fail early if we get a unicode path which can't be encoded.
    641         path = str(path)
     628        path = ftputil.tool.as_unicode(path)
    642629        path = self.path.abspath(path)
    643630        if self.listdir(path):
     
    654641    def remove(self, path):
    655642        """Remove the given file or link."""
    656         # Fail early if we get a unicode path which can't be encoded.
    657         path = str(path)
     643        path = ftputil.tool.as_unicode(path)
    658644        path = self.path.abspath(path)
    659645        # Though `isfile` includes also links to files, `islink`
     
    682668        on the state of the server (e. g. timeout).
    683669        """
     670        path = ftputil.tool.as_unicode(path)
    684671        self.remove(path)
    685672
     
    707694        in Python 2.4 and adapted to ftputil.
    708695        """
    709         # Fail early if we get a unicode path which can't be encoded.
    710         path = str(path)
     696        path = ftputil.tool.as_unicode(path)
    711697        # The following code is an adapted version of Python 2.4's
    712698        # `shutil.rmtree` function.
     
    750736    def rename(self, source, target):
    751737        """Rename the source on the FTP host to target."""
    752         # Fail early if we get a unicode path which can't be encoded.
    753         source = str(source)
    754         target = str(target)
     738        source = ftputil.tool.as_unicode(source)
     739        target = ftputil.tool.as_unicode(target)
    755740        # The following code is in spirit similar to the code in the
    756741        # method `_robust_ftp_command`, though we do _not_ do
     
    811796        any of the available parsers raise a `ParserError`.
    812797        """
    813         return self._stat._listdir(path)
     798        orig_path = path
     799        path = ftputil.tool.as_unicode(path)
     800        items = self._stat._listdir(path)
     801        return [ftputil.tool.same_string_type_as(orig_path, item)
     802                for item in items]
    814803
    815804    def lstat(self, path, _exception_for_missing_path=True):
     
    825814        _not_ intended for use by ftputil clients.)
    826815        """
     816        path = ftputil.tool.as_unicode(path)
    827817        return self._stat._lstat(path, _exception_for_missing_path)
    828818
     
    840830        _not_ intended for use by ftputil clients.)
    841831        """
     832        path = ftputil.tool.as_unicode(path)
    842833        return self._stat._stat(path, _exception_for_missing_path)
    843834
     
    848839        function (see http://docs.python.org/lib/os-file-dir.html ).
    849840        """
    850         # Fail early if we get a unicode path which can't be encoded.
    851         top = str(top)
     841        top = ftputil.tool.as_unicode(top)
    852842        # The following code is copied from `os.walk` in Python 2.4
    853843        # and adapted to ftputil.
     
    885875        causes a `PermanentError`.
    886876        """
    887         # Fail early if we get a unicode path which can't be encoded.
    888         path = str(path)
     877        path = ftputil.tool.as_unicode(path)
    889878        path = self.path.abspath(path)
    890879        def command(self, path):
Note: See TracChangeset for help on using the changeset viewer.