Skip to content

Default encoding issue on windows #21

@alxkocic

Description

@alxkocic

Describe the bug:

On a very simple type annotated function, I get:

Writing api.md.
Failed to generate docs for module api: UnicodeEncodeError('charmap', '<!-- markdownlint-disable -->\r\n\r\n# <kbd>module</kbd> `api`\r\n\r\n\r\n\r\n\r\n\r\n---\r\n\r\n## <kbd>function</kbd> `to_string`\r\n\r\n```python\r\nto_string(value: int) → str\r\n```\r\n\r\nSimple function. \r\n\r\n\r\n\r\n**Args:**\r\n \r\n - <b>`value`</b>:  integer value \r\n\r\n\r\n\r\n**Returns:**\r\n same value as string \r\n\r\n\r\n', 148, 149, 'character maps to <undefined>')

Expected behaviour:

Should generate markdown file.

Steps to reproduce the issue:

  1. On windows make sure your local default encoding is not utf8 (windows console defaults to cp1252)
  2. Run lazydocs generation on any code that has type annotations.

Technical details:

  • Host Machine OS (Windows/Linux/Mac): Windows 10
  • Browser (Chrome/Firefox/Safari): Firefox

Possible Fix:

There's a hacky solution to make sure python is defaulting to 'utf8' when opening files:

if os.name == "nt":
    import _locale
    _locale._gdl_bak = _locale._getdefaultlocale
    _locale._getdefaultlocale = (lambda *args: (_locale._gdl_bak()[0], 'utf8'))

However, I'd advise against it as it may not be exactly future proof. Instead, it is better to be explicit when opening a file:

    with open(os.path.join(out_path, md_file), "w", encoding="utf-8") as f:

The line in question is here:

with open(os.path.join(out_path, md_file), "w") as f:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions