Skip to content

Commit de38423

Browse files
committed
backport changes from mpy-cross-v6 v1.1.0
1 parent 132d1c4 commit de38423

File tree

8 files changed

+49
-45
lines changed

8 files changed

+49
-45
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
platforms: all
3131

3232
- name: Build wheels
33-
uses: pypa/cibuildwheel@v2.22.0
33+
uses: pypa/cibuildwheel@v3.1.3
3434
env:
3535
CIBW_ARCHS_MACOS: x86_64 arm64
3636
CIBW_ARCHS_LINUX: x86_64 aarch64
@@ -69,7 +69,7 @@ jobs:
6969
merge-multiple: true
7070
path: dist
7171

72-
- uses: pypa/[email protected].3
72+
- uses: pypa/[email protected].4
7373
with:
7474
user: __token__
7575
password: ${{ secrets.PYPI_TOKEN }}

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"[python]": {
3-
"editor.defaultFormatter": "ms-python.python",
3+
"editor.defaultFormatter": "ms-python.black-formatter",
44
"editor.formatOnSave": true
55
}
66
}

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@ from [MicroPython](https://github.com/micropython/micropython) via PyPI.
66
There are multiple MPY ABI versions, so you will need to install the package
77
that corresponds to the target MicroPython version.
88

9-
For MicroPython 1.12 to 1.18:
9+
Alternatively, you can install the [mpy-cross-multi](https://pypi.org/project/mpy-cross-multi/)
10+
package to install all versions of `mpy-cross` at once.
1011

11-
pip install mpy-cross-v5
12+
For MicroPython 1.23 and up:
13+
14+
pip install mpy-cross-v6.3
15+
16+
For MicroPython 1.22:
17+
18+
pip install mpy-cross-v6.2
19+
20+
For MicroPython 1.20 to 1.21:
1221

13-
For MicroPython 1.19+:
22+
pip install mpy-cross-v6.1
23+
24+
For MicroPython 1.19:
1425

1526
pip install mpy-cross-v6
27+
28+
For MicroPython 1.12 to 1.18:
29+
30+
pip install mpy-cross-v5

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "mpy-cross-v5"
7-
version = "1.0.2"
7+
version = "1.1.0"
88
description = "Python wrapper for the mpy-cross tool from MicroPython."
99
readme = "README.md"
1010

1111
[project.scripts]
12-
mpy-cross-v5 = "mpy_cross_v5:_run"
12+
"mpy-cross-v5" = "mpy_cross_v5:run"
1313

1414
[tool.setuptools.packages.find]
1515
where = ["src"]

src/mpy_cross_v5/__init__.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from enum import Enum
22
import subprocess
33
import sys
4-
import tempfile
54
import pathlib
65
import platform
76
from typing import List, Optional, Tuple
87

98

10-
MPY_CROSS_PATH = (
9+
MPY_CROSS_PATH = str(
1110
(pathlib.Path(__file__).parent / "mpy-cross")
1211
.with_suffix(".exe" if platform.system() == "Windows" else "")
1312
.absolute()
@@ -42,7 +41,7 @@ def mpy_cross_compile(
4241
emit: Optional[Emitter] = None,
4342
heap_size: Optional[int] = None,
4443
extra_args: Optional[List[str]] = None,
45-
) -> Tuple[subprocess.CompletedProcess, Optional[bytes]]:
44+
) -> Tuple[subprocess.CompletedProcess[bytes], Optional[bytes]]:
4645
"""
4746
Compiles a file using mpy-cross.
4847
@@ -77,47 +76,35 @@ def mpy_cross_compile(
7776
...
7877
7978
"""
80-
with tempfile.TemporaryDirectory() as tmp_dir:
81-
tmp_dir = pathlib.Path(tmp_dir)
79+
args: list[str] = [MPY_CROSS_PATH, "-", "-s", file_name]
8280

83-
with open(tmp_dir / "tmp.py", "w") as in_file:
84-
in_file.write(file_contents)
81+
if optimization_level is not None:
82+
if optimization_level not in range(4):
83+
raise ValueError("optimization_level must be between 0 and 3")
8584

86-
args = [MPY_CROSS_PATH, in_file.name, "-s", file_name]
85+
args.append(f"-O{optimization_level}")
8786

88-
if optimization_level is not None:
89-
if optimization_level not in range(4):
90-
raise ValueError("optimization_level must be between 0 and 3")
87+
if small_number_bits is not None:
88+
args.append(f"-msmall-int-bits={small_number_bits}")
9189

92-
args.append(f"-O{optimization_level}")
90+
if no_unicode:
91+
args.append("-mno-unicode")
9392

94-
if small_number_bits is not None:
95-
args.append(f"-msmall-int-bits={small_number_bits}")
93+
if arch is not None:
94+
args.append(f"-march={arch.value}")
9695

97-
if no_unicode:
98-
args.append("-mno-unicode")
96+
if emit is not None:
97+
args += ["-X", f"emit={emit.value}"]
9998

100-
if arch is not None:
101-
args.append(f"-march={arch.value}")
99+
if heap_size is not None:
100+
args += ["-X", f"heapsize={heap_size}"]
102101

103-
if emit is not None:
104-
args += ["-X", f"emit={emit.value}"]
102+
if extra_args:
103+
args += extra_args
105104

106-
if heap_size is not None:
107-
args += ["-X", f"heapsize={heap_size}"]
105+
process = subprocess.run(args, capture_output=True, input=file_contents.encode())
108106

109-
if extra_args:
110-
args += extra_args
111-
112-
process = subprocess.run(args, capture_output=True)
113-
114-
try:
115-
with open(tmp_dir / "tmp.mpy", "rb") as out_file:
116-
data = out_file.read()
117-
except OSError:
118-
data = None
119-
120-
return process, data
107+
return process, process.stdout if process.returncode == 0 else None
121108

122109

123110
def mpy_cross_version() -> str:
@@ -129,7 +116,7 @@ def mpy_cross_version() -> str:
129116
return proc.stdout.decode().strip()
130117

131118

132-
def _run():
119+
def run() -> None:
133120
"""
134121
Run mpy-cross directly.
135122
"""

src/mpy_cross_v5/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from . import _run
1+
from . import run
22

33
if __name__ == "__main__":
4-
_run()
4+
run()

src/mpy_cross_v5/py.typed

Whitespace-only changes.

tests/test_mpy_cross.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class FeatureFlags(IntFlag):
1111
def test_compile_no_opts():
1212
p, mpy = mpy_cross_compile("test.py", "")
1313
p.check_returncode()
14+
assert mpy is not None
1415

1516
magic, version, flags, small_int_bits = struct.unpack_from("BBBB", mpy)
1617

@@ -35,6 +36,7 @@ def test_compile_opt_no_unicode():
3536
def test_compile_opt_small_int_bits():
3637
p, mpy = mpy_cross_compile("test.py", "", small_number_bits=63)
3738
p.check_returncode()
39+
assert mpy is not None
3840

3941
magic, version, flags, small_int_bits = struct.unpack_from("BBBB", mpy)
4042

0 commit comments

Comments
 (0)