Skip to content

Commit d60553a

Browse files
committed
pybricksdev: use explicit text encoding for text files
Ensure that text files are opened with UTF-8 encoding by default to avoid issues with different default encodings. This also requires updating some dependencies that also had issues with text encoding. PYTHONWARNDEFAULTENCODING is set for tests to try to catch any future issues with text encoding. And PYTHONDEVMODE is set for good measure.
1 parent 0142fb1 commit d60553a

File tree

7 files changed

+134
-38
lines changed

7 files changed

+134
-38
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ jobs:
3838
- run: poetry install --only=main --only=test
3939
- run: poetry run pytest
4040
if: matrix.python-version != '3.10'
41+
env:
42+
PYTHONDEVMODE: 1
43+
PYTHONWARNDEFAULTENCODING: 1
4144
- run: poetry run coverage run
4245
if: matrix.python-version == '3.10'
4346
- run: poetry run coverage xml

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Fixed
1313
- Fixed calling `PybricksHub.write()` methods.
14+
- Fixed using default text encoding when opening text files.
1415

1516
### Changed
1617
- Downloading programs without starting them can now be done by

poetry.lock

Lines changed: 122 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pybricksdev/cli/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def add_parser(self, subparsers: argparse._SubParsersAction):
9898
"file",
9999
metavar="<file>",
100100
help="path to a MicroPython script or `-` for stdin",
101-
type=argparse.FileType(),
101+
type=argparse.FileType(encoding="utf-8"),
102102
)
103103
parser.add_argument(
104104
"--abi",
@@ -135,7 +135,7 @@ def add_parser(self, subparsers: argparse._SubParsersAction):
135135
"file",
136136
metavar="<file>",
137137
help="path to a MicroPython script or `-` for stdin",
138-
type=argparse.FileType(),
138+
type=argparse.FileType(encoding="utf-8"),
139139
)
140140
parser.add_argument(
141141
"-n",

pybricksdev/compile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def compile_file(
5555
"""
5656

5757
# Get version info
58-
with open(os.path.join(proj_dir, proj_path), "r") as f:
58+
with open(os.path.join(proj_dir, proj_path), "r", encoding="utf-8") as f:
5959
loop = asyncio.get_running_loop()
6060
script = f.read()
6161

@@ -188,7 +188,7 @@ def save_script(py_string):
188188
py_path = os.path.join(BUILD_DIR, TMP_PY_SCRIPT)
189189

190190
# Write Python command to a file.
191-
with open(py_path, "w") as f:
191+
with open(py_path, "w", encoding="utf-8") as f:
192192
f.write(py_string)
193193
f.write("\n")
194194

pybricksdev/connections/pybricks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def _line_handler(self, line: bytes) -> None:
194194
os.makedirs(dir_path)
195195

196196
logger.info("Saving log to {0}.".format(full_path))
197-
self.log_file = open(full_path, "w")
197+
self.log_file = open(full_path, "w", encoding="utf-8")
198198
return
199199

200200
# The line tells us to close a log file, so do it.

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ include = [
2828
pybricksdev = 'pybricksdev.cli:main'
2929

3030
[tool.poetry.dependencies]
31-
argcomplete = ">=1.11.1"
31+
argcomplete = ">=3.6.2"
3232
bleak = ">=0.22.0"
33-
mpy-cross-v5 = ">=1.0.0"
33+
mpy-cross-v5 = ">=1.1.0"
3434
python = ">=3.10,<3.14"
3535
tqdm = ">=4.62.3"
3636
pyusb = ">=1.0.2"
3737
semver = ">=2.13.0"
3838
appdirs = ">=1.4.4"
3939
prompt-toolkit = ">=3.0.18"
40-
mpy-cross-v6 = ">=1.0.0"
40+
mpy-cross-v6 = ">=1.1.0"
4141
packaging = ">=22"
4242
typing-extensions = ">=4.3.0"
4343
reactivex = ">=4.0.4"

0 commit comments

Comments
 (0)