This serves as a personal development environment to tinker around with PyO3.
I'm mainly focusing on implementing support for Python sub-interpreters at the moment.
This repository consists of two parts, a Python module with bindings to PyO3 and a rather primitive C++ application that uses
playground
is a Python module with bindings to PyO3 that contains functions,
objects, etc. that bind to whatever PyO3 code I'm testing at the moment.
The sources of this module can be found in src
.
This (yet to be named) C++ application uses Python's C API
to play around with the stuff that's in the playground
module.
In the future, this application might support a REPL of some sort, but for the time being it just does whatever I want to experiment with.
Well, initially I thought it would be easier to just use C or C++ to quickly
set up something that allows me to mess around with Python's C API - and it was!
Eventually I decided to use cmake
to "make my life easier" which ended up in
my life being made harder.
Either way, it now builds and it's (more or less) easy to use, so I'm reluctant to rewrite the whole thing.
Note: Open an issue if you're actually interested in using this repository to play around with PyO3, too. These instructions may not be complete; right now, they've only been tested on my machine.
- Be on some Linux distribution of your choice (WSL should probably work too)
- Python, preferably via
pyenv
- GNU Make
cmake
(optionally withctest
)- Rust
- Make sure you've cloned this repository with its submodules:
git clone --recurse-submodules [email protected]:Aequitosh/pyo3-playground.git
- Alternatively, if you've already cloned it, run:
git submodule update --init --recursive
Note: This was tested using Python 3.11. Older or newer Python versions might or might not work. Good luck!
-
Prepare and activate a virtual environment. I highly recommend using pyenv to install your desired Python version in order to not mess with your system's Python installation.
-
Congratulations, you can now build the
playground
module from the repo's directory:make install
Yeah, it's suprisingly easy, I know!
-
To test the module, import it in the
python
REPL:>>> import playground >>> playground.hello_world() 'Hello, World!'
-
After confirming that the module builds and runs correctly, you may also build the C++ application:
make interpreter-debug
make interpreter-release
Or, to build both:
make interpreter
-
Once your desired targets are build successfully, you should be able to run either of them:
./interpreter/build/debug/test-interpreter
./interpreter/build/release/test-interpreter
Once you completed the above steps and everything's built, can also run the (currently not very useful) tests for either build variant. The tests are built automatically for both builds.
- Run the test executable directly:
./build/debug/tests/run_tests
./build/release/tests/run_tests
- You may also run the tests with
ctest
:ctest --test-dir ./build/debug
ctest --test-dir ./build/release
If you're working on the PyO3 sources yourself and want to test your changes
using this repository, you can point cargo
to your local sources:
- Enter the
src/rust/
directorycd src/rust/
- Uncomment the line regarding
pyo3
inCargo.toml
and change thepath
to point to the directory of your localpyo3
sources - Run
cargo update
cargo
should now have updated all PyO3-related dependencies accordingly. You can then rebuild the Python module like above:cd ../..
pip install -e .
Even though this is just a (more or less literal) playground for PyO3, this project is licensed under the AGPL-3.0.
Should this at any point become more relevant (which I doubt) I might adapt the license correspondingly.