Skip to content

Commit

Permalink
pystache/common.py: test failures in Windows due to CRs
Browse files Browse the repository at this point in the history
* cleaned up from defunkt/pystache#193

Signed-off-by: Stephen L Arnold <[email protected]>
  • Loading branch information
sarnold committed Nov 9, 2020
1 parent fb40760 commit 54a4ae7
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pystache/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@ def read(path):
Return the contents of a text file as a byte string.
"""
import platform
# Opening in binary mode is necessary for compatibility across Python
# 2 and 3. In both Python 2 and 3, open() defaults to opening files in
# text mode. However, in Python 2, open() returns file objects whose
# read() method returns byte strings (strings of type `str` in Python 2),
# whereas in Python 3, the file object returns unicode strings (strings
# of type `str` in Python 3).
f = open(path, 'rb')
# We avoid use of the with keyword for Python 2.4 support.
try:
return f.read()
finally:
f.close()
file_mode = 'r'
code_page = 'utf-8'
if version_info < (3, ):
file_mode = 'rbU'
if platform.system() == "Windows":
code_page = 'utf-16'

with open(path, file_mode) as f:
if version_info < (3, ):
return f.read()
return f.read().encode(code_page)


class MissingTags(object):
Expand Down

0 comments on commit 54a4ae7

Please sign in to comment.