Skip to content

FileContains does not support binary files #538

@jelmer

Description

@jelmer

Migrated from Launchpad: https://bugs.launchpad.net/bugs/1250483

Original Reporter: Gavin Panella
Date Created: 2013-11-12 14:04:00.284077+00:00
Status on Launchpad: Triaged
Importance: Wishlist


The code in FileContains.match():

       f = open(path)
       try:
           actual_contents = f.read()
           return self.matcher.match(actual_contents)
       finally:
           f.close()

opens a file in text mode on Python 3. From open()'s docstring:

 The default mode is 'rt' (open for reading text)
 ...
 Python distinguishes between files opened in binary and text modes,
 even when the underlying operating system doesn't. Files opened in
 binary mode (appending 'b' to the mode argument) return contents as
 bytes objects without any decoding. In text mode (the default, or when
 't' is appended to the mode argument), the contents of the file are
 returned as strings, the bytes having been first decoded using a
 platform-dependent encoding or using the specified encoding if given.

I think it would be kess surprising to open the file in binary mode.

An optional user-defined encoding might be useful too, if text is what
you need.

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