Skip to content

BufferedFile TypeError on write with buffer API objects #967

Closed

Description

@bz2

Unlike normal files, the paramiko BufferedFile.write() method throws a TypeError if passed an object that implements the buffer API, such as the builtin memoryview type.

Note this a Python 3 flavoured version of #285 from porting that code away from the old buffer type.

Traceback (most recent call last):
  ...
  File ".../osutils.py", line 753, in pump_string_file
    write(view[offset:offset+segment_size])
  File ".../paramiko/file.py", line 375, in write
    data = b(data)
  File ".../paramiko/py3compat.py", line 47, in b
    raise TypeError("Expected unicode or bytes, got %r" % s)
TypeError: Expected unicode or bytes, got <memory at 0x7f0ce4d08510>

Paramiko should not care about the types it is passed, provided they are len-able and can be written to the underlying (BytesIO here) object.

Activity

  1. added 2 commits that reference this issue on May 25, 2017
    0dd2fa7
    2f74a9f
  2. bz2 commented on May 25, 2017

    @bz2
    ContributorAuthor

    Missed the full path through SFTPFIle with the first pass fix, there's a secondary failure:

    Traceback (most recent call last):
      ...
      File ".../breezy/osutils.py", line 753, in pump_string_file
        write(view[offset:offset+segment_size])
      File ".../paramiko/file.py", line 339, in write
        self._write_all(data)
      File ".../paramiko/file.py", line 456, in _write_all
        count = self._write(data)
      File ".../paramiko/sftp_file.py", line 176, in _write
        self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), data[:chunk]))
      File ".../paramiko/sftp_client.py", line 748, in _async_request
        raise Exception('unknown type for %r type %r' % (item, type(item)))
    Exception: unknown type for <memory at 0xb5613b70> type <type 'memoryview'>
    

    Looks like _async_request should just try treat args as string by default.

  3. added a commit that references this issue on May 26, 2017
    102ed5d
  4. bitprophet commented on May 31, 2017

    @bitprophet
    Member

    Rolling into the final PR. Thanks!

  5. added 2 commits that reference this issue on Jun 9, 2017
    e66899a
    17231b9
  6. added 2 commits that reference this issue on Jun 7, 2018
    b6bf28c
    df710f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions