Skip to content

Commit a5920a6

Browse files
committed
backport changes from mpy-cross-v6.3 v1.1.0
1 parent 769c8dc commit a5920a6

File tree

9 files changed

+44
-48
lines changed

9 files changed

+44
-48
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_TOKEN4 }}

.vscode/settings.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"[python]": {
3-
"editor.defaultFormatter": "ms-python.python",
3+
"editor.defaultFormatter": "ms-python.black-formatter",
44
"editor.formatOnSave": true
5-
},
6-
"python.formatting.provider": "black"
5+
}
76
}

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
recursive-include micropython py/** mpy-cross/** shared/** tools/**
22
include micropython/docs/conf.py
3+
include micropython/extmod/modplatform.h
34
prune micropython/mpy-cross/build*
45
prune micropython/ports*
56
exclude src/mpy_cross_v6.2/mpy-cross*

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +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:
1213

13-
For MicroPython 1.19:
14+
pip install mpy-cross-v6.3
1415

15-
pip install mpy-cross-v6
16+
For MicroPython 1.22:
17+
18+
pip install mpy-cross-v6.2
1619

1720
For MicroPython 1.20 to 1.21:
1821

1922
pip install mpy-cross-v6.1
2023

21-
For MicroPython 1.22:
24+
For MicroPython 1.19:
2225

23-
pip install mpy-cross-v6.2
26+
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-v6.2"
7-
version = "1.0.0"
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-v6.2" = "mpy_cross_v6_2:_run"
12+
"mpy-cross-v6.2" = "mpy_cross_v6_2:run"
1313

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

src/mpy_cross_v6_2/__init__.py

Lines changed: 20 additions & 33 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
@@ -75,44 +74,32 @@ def mpy_cross_compile(
7574
...
7675
7776
"""
78-
with tempfile.TemporaryDirectory() as tmp_dir:
79-
tmp_dir = pathlib.Path(tmp_dir)
77+
args: list[str] = [MPY_CROSS_PATH, "-", "-s", file_name]
8078

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

84-
args = [MPY_CROSS_PATH, in_file.name, "-s", file_name]
83+
args.append(f"-O{optimization_level}")
8584

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

90-
args.append(f"-O{optimization_level}")
88+
if arch is not None:
89+
args.append(f"-march={arch.value}")
9190

92-
if small_number_bits is not None:
93-
args.append(f"-msmall-int-bits={small_number_bits}")
91+
if emit is not None:
92+
args += ["-X", f"emit={emit.value}"]
9493

95-
if arch is not None:
96-
args.append(f"-march={arch.value}")
94+
if heap_size is not None:
95+
args += ["-X", f"heapsize={heap_size}"]
9796

98-
if emit is not None:
99-
args += ["-X", f"emit={emit.value}"]
97+
if extra_args:
98+
args += extra_args
10099

101-
if heap_size is not None:
102-
args += ["-X", f"heapsize={heap_size}"]
100+
process = subprocess.run(args, capture_output=True, input=file_contents.encode())
103101

104-
if extra_args:
105-
args += extra_args
106-
107-
process = subprocess.run(args, capture_output=True)
108-
109-
try:
110-
with open(tmp_dir / "tmp.mpy", "rb") as out_file:
111-
data = out_file.read()
112-
except OSError:
113-
data = None
114-
115-
return process, data
102+
return process, process.stdout if process.returncode == 0 else None
116103

117104

118105
def mpy_cross_version() -> str:
@@ -124,7 +111,7 @@ def mpy_cross_version() -> str:
124111
return proc.stdout.decode().strip()
125112

126113

127-
def _run():
114+
def run() -> None:
128115
"""
129116
Run mpy-cross directly.
130117
"""

src/mpy_cross_v6_2/__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_v6_2/py.typed

Whitespace-only changes.

tests/test_mpy_cross.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
def test_compile_no_opts():
77
p, mpy = mpy_cross_compile("test.py", "")
88
p.check_returncode()
9+
assert mpy is not None
910

1011
magic, version, flags, small_int_bits = struct.unpack_from("BBBB", mpy)
1112

@@ -18,6 +19,7 @@ def test_compile_no_opts():
1819
def test_compile_opt_small_int_bits():
1920
p, mpy = mpy_cross_compile("test.py", "", small_number_bits=63)
2021
p.check_returncode()
22+
assert mpy is not None
2123

2224
magic, version, flags, small_int_bits = struct.unpack_from("BBBB", mpy)
2325

0 commit comments

Comments
 (0)