11from enum import Enum
22import subprocess
33import sys
4- import tempfile
54import pathlib
65import platform
76from 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
123110def 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 """
0 commit comments