Skip to content

Commit

Permalink
fix: Open Snakefile for reading with explicit encoding specified (#1146)
Browse files Browse the repository at this point in the history
* fix: Open Snakefile for reading with explicit encoding specified

* fix merge

* fmt

* only apply encoding if not using binary mode

Co-authored-by: Johannes Köster <johannes.koester@tu-dortmund.de>
Co-authored-by: Johannes Köster <johannes.koester@uni-due.de>
  • Loading branch information
3 people committed Aug 25, 2022
1 parent 10ef7c4 commit ec1d859
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions snakemake/sourcecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ def runtime_cache_path(self):

def open(self, source_file, mode="r"):
cache_entry = self._cache(source_file)
return self._open_local_or_remote(LocalSourceFile(cache_entry), mode)
return self._open_local_or_remote(
LocalSourceFile(cache_entry), mode, encoding="utf-8"
)

def exists(self, source_file):
try:
Expand Down Expand Up @@ -403,21 +405,21 @@ def _do_cache(self, source_file, cache_entry):
# as mtime.
os.utime(cache_entry, times=(mtime, mtime))

def _open_local_or_remote(self, source_file, mode):
def _open_local_or_remote(self, source_file, mode, encoding=None):
from retry.api import retry_call

if source_file.is_local:
return self._open(source_file, mode)
return self._open(source_file, mode, encoding=encoding)
else:
return retry_call(
self._open,
[source_file, mode],
[source_file, mode, encoding],
tries=3,
delay=3,
backoff=2,
)

def _open(self, source_file, mode):
def _open(self, source_file, mode, encoding=None):
from smart_open import open

if isinstance(source_file, LocalGitFile):
Expand All @@ -432,6 +434,6 @@ def _open(self, source_file, mode):
path_or_uri = source_file.get_path_or_uri()

try:
return open(path_or_uri, mode)
return open(path_or_uri, mode, encoding=None if "b" in mode else encoding)
except Exception as e:
raise WorkflowError("Failed to open source file {}".format(path_or_uri), e)

0 comments on commit ec1d859

Please sign in to comment.