FileContains does not support binary files

Bug #1250483 reported by Gavin Panella
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
testtools
Triaged
Wishlist
Unassigned

Bug Description

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.

Tags: matchers
Gavin Panella (allenap)
tags: added: matchers
Revision history for this message
Robert Collins (lifeless) wrote :

I think it's fair to want to be able to match against binary files, but it's not strictly a defect, it's just you want something we haven't written yet :)

Changed in testtools:
status: New → Triaged
importance: Undecided → Wishlist
summary: - FileContains opens files in text mode
+ FileContains does not support binary files
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.