Page 1 of 1
[SOLVED] 1.9.3 - ValueError: can only save bytecode
Posted: Sun Nov 05, 2017 9:51 pm
by jickster
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?
Re: ValueError: can only save bytecode
Posted: Mon Nov 06, 2017 6:44 am
by Roberthh
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.
Re: ValueError: can only save bytecode
Posted: Mon Nov 06, 2017 11:48 am
by pythoncoder
The clue is in the term "frozen bytecode". The cross compiler emits bytecode, not native machine code.
Re: ValueError: can only save bytecode
Posted: Mon Nov 06, 2017 3:47 pm
by jickster
pythoncoder wrote: ↑Mon Nov 06, 2017 11:48 am
The clue is in the term "frozen bytecode". The cross compiler emits bytecode, not native machine code.
Roberthh wrote: ↑Mon Nov 06, 2017 6:44 am
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.
Is it possible to compile it not at
import time but by me calling a micropython function i.e. "compile this .py NOW"?
Re: ValueError: can only save bytecode
Posted: Mon Nov 06, 2017 4:03 pm
by Roberthh
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
Posted: Mon Nov 06, 2017 5:13 pm
by Roberthh
I tried that on PyBoard. You can have native code compiled by compile() and exec(). The sequence was:
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)
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.