pytest-doctestplus icon indicating copy to clipboard operation
pytest-doctestplus copied to clipboard

`pytest --doctest-plus-generate-diff=overwrite` crashes on windows with utf-8 files

Open d-chris opened this issue 8 months ago • 2 comments

running pytest --doctest-plus-generate-diff=overwrite on a windows machine raise an an fatal error and source files will be empty afterwards.

test

content of a utf-8 encoded file

def test_win_fail():
    """
    fatal failure on windows when file contains utf-8 charaters, due default encoding is "cp1252" and not "utf-8".

    reason: `open() is used without possibility to specify encoding`

    `pytest --doctest-plus-generate-diff=overwrite`

    >>> print("✅")
    💥
    """

    assert True

Exception

  File "D:\doctestplus\.venv\src\pytest-doctestplus\pytest_doctestplus\plugin.py", line 950, in write_modified_file
    f.write("".join(text))
    ~~~~~~~^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\u2705' in position 357: character maps to <undefined>

solution

introduce new cli or ini option to specify file encoding

workaround

none

d-chris avatar Mar 18 '25 19:03 d-chris

issue fixed in PR #284

d-chris avatar Mar 24 '25 19:03 d-chris

@d-chris - we'll autoclose the issue when merging the PR, it just needed to be properly cross referenced.

bsipocz avatar Mar 24 '25 19:03 bsipocz