Ignore:
Timestamp:
Aug 3, 2013, 10:12:37 PM (6 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Made several names local to `_open`.

They're no longer needed at instance level since we "manual" line
ending conversion, where we needed the mode flags, is gone.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftputil/file.py

    r1320 r1321  
    6161        # The file is still closed.
    6262        self.closed = True
    63         # Overwritten later in `_open`.
    64         self._bin_mode = None
    6563        self._conn = None
    66         self._read_mode = None
    67         self._fo = None
     64        self._fobj = None
    6865
    6966    def _open(self, path, mode, buffering=None, encoding=None, errors=None,
     
    8986            # Raise a `ValueError` like Python would.
    9087            raise ValueError("can't have text and binary mode at once")
    91         # Remember convenience variables instead of the mode itself.
    92         self._bin_mode = 'b' in mode
    93         self._read_mode = 'r' in mode
     88        # Convenience variables
     89        is_bin_mode = "b" in mode
     90        is_read_mode = "r" in mode
    9491        # Always use binary mode (see above).
    9592        transfer_type = "I"
     
    9895            self._session.voidcmd(command)
    9996        # Make transfer command.
    100         command_type = ('STOR', 'RETR')[self._read_mode]
     97        command_type = ('STOR', 'RETR')[is_read_mode]
    10198        command = '{0} {1}'.format(command_type, path)
    10299        # Force to binary regardless of transfer type (see above).
     
    110107            self._conn = self._session.transfercmd(command)
    111108        # The actual file object.
    112         self._fo = self._conn.makefile(makefile_mode)
    113         if self._read_mode:
    114             self._fo = io.BufferedReader(self._fo)
     109        fobj = self._conn.makefile(makefile_mode)
     110        if is_read_mode:
     111            fobj = io.BufferedReader(fobj)
    115112        else:
    116             self._fo = io.BufferedWriter(self._fo)
    117         if not self._bin_mode:
    118             self._fo = io.TextIOWrapper(self._fo, encoding=encoding,
     113            fobj = io.BufferedWriter(fobj)
     114        if not is_bin_mode:
     115            fobj = io.TextIOWrapper(fobj, encoding=encoding,
    119116                                        errors=errors, newline=newline)
     117        self._fobj = fobj
    120118        # This comes last so that `close` won't try to close `_FTPFile`
    121119        # objects without `_conn` and `_fo` attributes in case of an error.
     
    165163        delegate the requests to the contained file object.
    166164        """
    167         if attr_name in ("encoding flush isatty fileno read readlines seek "
    168                          "tell truncate name softspace write writelines".
    169                          split()):
    170             return getattr(self._fo, attr_name)
     165        if attr_name in ("encoding flush isatty fileno read readline "
     166                         "readlines seek tell truncate name softspace "
     167                         "write writelines".split()):
     168            return getattr(self._fobj, attr_name)
    171169        raise AttributeError(
    172170              "'FTPFile' object has no attribute '{0}'".format(attr_name))
     
    184182        old_timeout = self._session.sock.gettimeout()
    185183        try:
    186             self._fo.close()
    187             self._fo = None
     184            self._fobj.close()
     185            self._fobj = None
    188186            with ftputil.error.ftplib_error_to_ftp_io_error:
    189187                self._conn.close()
Note: See TracChangeset for help on using the changeset viewer.