Skip to content

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

Open
@d-chris

Description

@d-chris

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions