[SOLVED] 1.9.3 - ValueError: can only save bytecode

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

[SOLVED] 1.9.3 - ValueError: can only save bytecode

Post by jickster » Sun Nov 05, 2017 9:51 pm

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?
Last edited by jickster on Mon Nov 06, 2017 4:28 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 1634
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ValueError: can only save bytecode

Post by Roberthh » 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.

User avatar
pythoncoder
Posts: 3598
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ValueError: can only save bytecode

Post by pythoncoder » Mon Nov 06, 2017 11:48 am

The clue is in the term "frozen bytecode". The cross compiler emits bytecode, not native machine code.
Peter Hinch

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: ValueError: can only save bytecode

Post by jickster » Mon Nov 06, 2017 3:47 pm

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"?

User avatar
Roberthh
Posts: 1634
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ValueError: can only save bytecode

Post by Roberthh » Mon Nov 06, 2017 4:03 pm

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.

User avatar
Roberthh
Posts: 1634
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: [SOLVED] 1.9.3 - ValueError: can only save bytecode

Post by Roberthh » Mon Nov 06, 2017 5:13 pm

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:

Code: Select all

del o, test
memory seems to be released again.

Post Reply