News for hackers. Stuff that matters.
Re:Write Python inline in your Rust code by m-ou-se on Tuesday April 23, 2019 @12:04PM (#19729915)
Author here. Let me know if you have any questions. :)
Re:Write Python inline in your Rust code by dbcurtis on Tuesday April 23, 2019 @01:02PM (#19730554)
How does this interact with Rust memory safety? I am not familiar with the PyToObject trait, so I am guessing that this has some kind of safety wrapper?
Re:Write Python inline in your Rust code by m-ou-se on Tuesday April 23, 2019 @01:49PM (#19731041)
In Python, all data (even simple integers) are all allocated on the heap. ToPyObject makes a deep copy of everything, converting everything to `PyObject` on the heap, so all data is owned.

The numpy crate helps if you need to convert large arrays to Python, as numpy does not store every element separately on the heap.

Re:Write Python inline in your Rust code by maccard on Tuesday April 23, 2019 @07:10PM (#19733935)
> In Python, all data (even simple integers) are all allocated on the heap.

Total sidebar, this leads to one of my favourite programming language "quirks" - you can redefine the value of 1! http://hforsten.com/redefining-the-number-2-in-python.html

Re:Write Python inline in your Rust code by amelius on Tuesday April 23, 2019 @02:43PM (#19731524)
Does every invocation create a new Python instance, and global namespace, etc?
Re:Write Python inline in your Rust code by m-ou-se on Tuesday April 23, 2019 @02:46PM (#19731560)
It uses the same Python interpreter, but each macro invocation is considered its own module. A later version of the crate will have the possibility to keep the context around to be re-used by a later invocation such that, for example, you don't need to import things again.
Re:Write Python inline in your Rust code by AndyMcConachie on Tuesday April 23, 2019 @06:28PM (#19733619)
So this doesn't actually compile any Python, it just launches a Python interpreter when it encounters Python code?

If I have many threads running Python code will it launch a new interpreter for each thread?

Does it use Python 2 or 3?

Thanks! I might actually use this sometime for scrap code that I'm writing to test stuff.

Re:Write Python inline in your Rust code by m-ou-se on Wednesday April 24, 2019 @02:47AM (#19736134)
It doesn't translate Python to Rust or anything like that, it uses CPython both to compile to Python bytecode and to run it. It doesn't launch a separate interpreter, the interpreter is used as a library, so runs in the same process.

Python doesn't really do multithreading: https://wiki.python.org/moin/GlobalInterpreterLock

It uses Python 3 of course. Using Python 2 these days would be a crime.