I tried to use mpy-cross with a .py file that used @micropython.native but it gave me this error.
How can I incorporate frozen mpy files that use @micropython.native into my build?
[SOLVED] 1.9.3 - ValueError: can only save bytecode
[SOLVED] 1.9.3 - ValueError: can only save bytecode
Last edited by jickster on Mon Nov 06, 2017 4:28 pm, edited 1 time in total.
Re: ValueError: can only save bytecode
You cannot. It is an TODO item. If you want to put it into flash memory, you can embed it as frozen source code in some ports. Just put these files in a subdirectory called scripts, like esp8266/scripts or stm32/scripts. But it will still be compiled at import time and consume RAM. Typically, that should not hurt, when this variant of coding is used only for small, time-critical sections of the code.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: ValueError: can only save bytecode
The clue is in the term "frozen bytecode". The cross compiler emits bytecode, not native machine code.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: ValueError: can only save bytecode
pythoncoder wrote: ↑Mon Nov 06, 2017 11:48 amThe clue is in the term "frozen bytecode". The cross compiler emits bytecode, not native machine code.
Is it possible to compile it not at import time but by me calling a micropython function i.e. "compile this .py NOW"?Roberthh wrote: ↑Mon Nov 06, 2017 6:44 amYou cannot. It is an TODO item. If you want to put it into flash memory, you can embed it as frozen source code in some ports. Just put these files in a subdirectory called scripts, like esp8266/scripts or stm32/scripts. But it will still be compiled at import time and consume RAM. Typically, that should not hurt, when this variant of coding is used only for small, time-critical sections of the code.
Re: ValueError: can only save bytecode
There are the builtins compile() and exec(), which serve this task. I never tried to uses these with native code. But I do not see the benefit against import. instead of using exec() you can use import. Once compile'd/exec'ed or import'ed, both consume RAM. You could try, if a piece of code, which is assigned to an object via compile(), can be deleted later and release the RAM used by it.
Re: [SOLVED] 1.9.3 - ValueError: can only save bytecode
I tried that on PyBoard. You can have native code compiled by compile() and exec(). The sequence was:
The second argument of compile can be an arbitrary string. Then, test is defined. If after that the objects are deleted by:
memory seems to be released again.
Code: Select all
s='\n@micropython.native\ndef test(a,b):\n a = a + b\n return a*b\n'
o=compile(s, "test", "exec")
exec(o)
Code: Select all
del o, test