Skip to content

Wrong type interpretation in path.write #262

@ahsimb

Description

@ahsimb

Here is the offending piece of code

The write method is supposed to take data in one of three forms:

  • A byte-stream-like object, e.g. returned by open()
  • ByteString
  • An Iterable of ByteStrings.

The code is faulty in two ways.

  1. The test not isinstance(data, IOBase) and isinstance(data, Iterable) does not rule out a stream-like object. For example tempfile.NamedTemporaryFile passes the test.
  2. By iterating over the "chunks" in the test the iterator can be exhausted, so the next line - b"".join(tmp_file) - will return an empty ByteString.

The bug is first of all in the interface. The Iterable option was unnecessary. Since it is already out there, a reasonable compromise is changing the type from Iterable to Sequence. Then change the test to

if isinstance(tmp_file, Sequence):
   # optionally
   if not all(isinstance(chunk, ByteString) for chunk in tmp_file):
      raise ValueError(...)
   ...

It should not effect the user code, since an Iterable, if it's actually an Iterable and not a Sequence, wouldn't work anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions