Skip to content

Commit

Permalink
fix: UnicodeDecodeError (#490)
Browse files Browse the repository at this point in the history
* fix: utf-8 encoding

Fixes utf-8 encoding

```
- hook id: nbqa-black
- exit code: 1
- files were modified by this hook

Traceback (most recent call last):
  File "c:\users\cees closed\.cache\pre-commit\repo9j8ifsr9\py_env-python3\lib\site-packages\nbqa\__main__.py", line 579, in _run_on_one_root_dir
    replace_source.main(
  File "c:\users\cees closed\.cache\pre-commit\repo9j8ifsr9\py_env-python3\lib\site-packages\nbqa\replace_source.py", line 88, in main
    notebook_json = json.loads(notebook.read_text())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\pathlib.py", line 1236, in read_text
    return f.read()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1184027: character maps to <undefined>
```

* Update save_source.py

fix: utf-8 encoding

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sbrugman and pre-commit-ci[bot] authored Nov 30, 2020
1 parent 99cc047 commit 0e72cfc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions nbqa/replace_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def mutate(python_file: "Path", notebook: "Path", notebook_info: NotebookInfo) -
notebook_info
Information about notebook cells used for processing
"""
notebook_json = json.loads(notebook.read_text())
notebook_json = json.loads(notebook.read_text(encoding="utf-8"))

pycells = _get_pycells(python_file)
for code_cell_number, cell in enumerate(
Expand All @@ -167,7 +167,9 @@ def mutate(python_file: "Path", notebook: "Path", notebook_info: NotebookInfo) -
continue
cell["source"] = _get_new_source(code_cell_number, notebook_info, next(pycells))

notebook.write_text(f"{json.dumps(notebook_json, indent=1, ensure_ascii=False)}\n")
notebook.write_text(
f"{json.dumps(notebook_json, indent=1, ensure_ascii=False)}\n", encoding="utf-8"
)


def _print_diff(code_cell_number: int, cell_diff: Iterator[str]) -> None:
Expand Down Expand Up @@ -211,7 +213,7 @@ def diff(python_file: "Path", notebook: "Path", notebook_info: NotebookInfo) ->
notebook_info
Information about notebook cells used for processing
"""
notebook_json = json.loads(notebook.read_text())
notebook_json = json.loads(notebook.read_text(encoding="utf-8"))

pycells = _get_pycells(python_file)

Expand Down
6 changes: 4 additions & 2 deletions nbqa/save_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def main(
NotebookInfo
"""
cells = json.loads(notebook.read_text())["cells"]
cells = json.loads(notebook.read_text(encoding="utf-8"))["cells"]

result = []
cell_mapping = {0: "cell_0:0"}
Expand Down Expand Up @@ -366,7 +366,9 @@ def main(
result.append(re.sub(r";(\s*)$", "\\1", parsed_cell))
line_number += len(parsed_cell.splitlines())

temp_python_file.write_text("".join(result).rstrip(NEWLINE) + NEWLINE)
temp_python_file.write_text(
"".join(result).rstrip(NEWLINE) + NEWLINE, encoding="utf-8"
)

return NotebookInfo(
cell_mapping, trailing_semicolons, temporary_lines, code_cells_to_ignore
Expand Down

0 comments on commit 0e72cfc

Please sign in to comment.