Skip to content

Commit

Permalink
🚸 Support Python UTF-8 mode on Windows (#130)
Browse files Browse the repository at this point in the history
* 🚸 Support Python UTF-8 mode on Windows

* 🐛 Fix encoding check on Python 2
  • Loading branch information
ddelange authored Nov 23, 2023
1 parent 4600545 commit f7227dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ jobs:
pip install -U pip setuptools wheel
pip install .
- name: Test
- name: Test (ascii)
run: pipgrip -vvv --tree pipgrip

- name: Test (unicode)
env:
PYTHONUTF8: '1'
run: pipgrip -vvv --tree pipgrip

CD:
Expand Down
17 changes: 10 additions & 7 deletions src/pipgrip/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import re
import sys
from collections import OrderedDict
from functools import partial
from json import dumps
Expand Down Expand Up @@ -163,13 +164,15 @@ def build_tree(source, decision_packages):


def render_tree(tree_root, max_depth, tree_ascii=False):
# Windows' cp1252 encoding does not supports anytree's unicode markers.
# Even in Github Actions where the windows runner has PYTHONUTF8 mode enabled,
# `if sys.getfilesystemencoding() == "cp1252":` check is insufficient and the
# error is still raised in CI.
# Hence disabling unicode trees altogether for Windows.
# ref https://github.com/pallets/click/issues/2121#issuecomment-1312773882
if click.utils.WIN: # pragma: no cover
# click.echo on Windows' cp1252 encoding does not supports anytree's unicode markers
# ref https://github.com/pallets/click/issues/2121#issuecomment-1809693939
# so check for UTF-8 mode https://docs.python.org/3/library/os.html#utf8-mode
# PEP 686: Python 3.15 will make Python UTF-8 Mode default
if ( # pragma: no cover
not tree_ascii
and hasattr(sys.stdout, "encoding")
and not sys.stdout.encoding.lower().startswith("utf")
):
tree_ascii = True
style = AsciiStyle() if tree_ascii else ContStyle()
output = []
Expand Down

0 comments on commit f7227dc

Please sign in to comment.