Apologies for continuing the 'contamination' of this thread (ostensibly about iPad keyboards!)

Below is something I used to convince myself that Pythonista could run 'bytecode binaries' (i.e., .pyc) files even if it didn't produce them. It's a short .pyc file created using a mini-assembler that emits Python bytecode. So in effect, yes -- if someone were sufficiently motivated they could conceivably write another language compiler to emit bytecode files that Pythonista could execute. (Much like how the JVM or CLR allows this as mentioned in my earlier comment). I'm not sure what the point would be from a practical perspective. But some things are fun simply because they're possible, I suppose.

Anyway, apologies again if this isn't exactly news to the experienced Python hackers here. As I've said before, I'm new to this language and cobbling together snippets of test code has always been my preferred way of learning.

Thanks,<br>
Pacco

Reminder: The Pythonista file navigator doesn't display .pyc files. Use shellista or some other tool to see them.

<pre><code>

A proof-of-concept to illustrate that Pythonista can call/run bytecode (.pyc) files that have been precompiled (no surprise) or even created using other tools. Pacco 20150114

import os,base64

A bytecode binary created using the bytecode assembler [https://github.com/pib/papaya] and patched to use Pythonista 1.5's identifier [03f3] in first two bytes. [http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html] This is just the Fibonacci (fib.pya) example included with the Papaya assembler

pycbin=base64.b64decode(
'A/MNCr7JtlRjAAAAAAAAAAABAAAAQAAAAHMNAAAAZAEAhAAAWgAAZAAAUygCAAAATmMBAAAAAQAAAAQA'+
'AABDAAAAcz4AAAB8AABkAQBrAgBvEABkAQBTAXwAAGQCAGsCAG8hAGQCAFMBdAAAfAAAZAIAGIMBAHQA'+
'AHwAAGQDABiDAQAXUygEAAAATmkAAAAAaQEAAABpAgAAACgBAAAAdAMAAABmaWIoAQAAAHQBAAAAbigA'+
'AAAAKAAAAABzBwAAAGZpYi5weWFSAAAAAAQAAABzMgAAAAMBAwEDAQMBAwEBBAEBAwEDAQMBAwEDAQEE'+
'AQEDAQMBAwEBAQMCAwEDAQMBAQEDAQECKAEAAABSAAAAACgAAAAAKAAAAAAoAAAAAHMHAAAAZmliLnB5'+
'YXMIAAAAPG1vZHVsZT4CAAAAcwgAAAADJQMBAwEDAQ=='
)

write it to a .pyc file

pycfile='fib0.pyc'
with open(pycfile,'wb') as f:
f.write(pycbin)

Now load and call a function in the binary

import imp

try:
ourpyc=imp.load_compiled('Test Module',pycfile)
print ourpyc.fib(10) # call a function in our loaded pyc file
except Exception as e:
print "Error:",e

clean up .pyc afterwards to be neat

if os.path.isfile(pycfile): os.remove(pycfile)

</code></pre>