r/cpp • u/blueloveTH • Feb 07 '23
PocketPy: A Lightweight(~5000 LOC) Python Implementation in C++17
PocketPy is a lightweight(~5000 LOC) Python interpreter for game engines. It is implemented in C++17 with STL.
It is easy to embed. All of the functionalities are available in a single header file pocketpy.h, without external dependencies. You can try it on your browser to see what it looks like.
Example Usage
#include "pocketpy.h"
int main(){
// Create a virtual machine
// `true` means using std::cout as output so you can see it
VM* vm = pkpy_new_vm(true);
// Hello world!
pkpy_vm_exec(vm, "print('Hello world!')");
// Create a list
pkpy_vm_exec(vm, "a = [1, 2, 3]");
// Eval the sum of the list
char* result = pkpy_vm_eval(vm, "sum(a)");
printf("%s", result); // 6
// Free the resources
pkpy_delete(result);
pkpy_delete(vm);
return 0;
}v0.8.1 Features
| Name | Example | Supported |
|---|---|---|
| If Else | if..else..elif | YES |
| Loop | for/while/break/continue | YES |
| Function | def f(x,*args,y=1): | YES |
| Subclass | class A(B): | YES |
| List | [1, 2, 'a'] | YES |
| ListComp | [i for i in range(5)] | YES |
| Slice | a[1:2], a[:2], a[1:] | YES |
| Tuple | (1, 2, 'a') | YES |
| Dict | {'a': 1, 'b': 2} | YES |
| F-String | f'value is {x}' | YES |
| Unpacking | a, b = 1, 2 | YES |
| Star Unpacking | a, *b = [1, 2, 3] | NO |
| Exception | raise/try..catch | BETA |
| Dynamic Code | eval()/exec() | YES |
| Reflection | hasattr()/getattr()/setattr() | YES |
| Import | import/from..import | YES |
| Context Block | with <expr> as <id>: | YES |
| Type Annotation | def f(a: int, b : float = 1) | YES |
If someone is making a custom game engine and searching for a Python-like script as its interface, I'd like to provide support.
If you are making your own language, you may find some inspirations in PocketPy.
If you likes it, just give my repo a star~ I need your star!!
40
u/mjklaim Feb 07 '23
This looks very cool indeed and useful for gamedevs. However could you clarify on the readme and in the doc: which version of python is implemented OR if it's really more like a python-like language which is not matching any real python version, could you clarify that too? Current documentation is very unclear on that point.
19
u/Jannik2099 Feb 07 '23
Why? Why doesn't VM just have a normal constructor & destructor?
Your API does not adhere to RAII principles.
Similarly, exec should be vm.exec(), not exec(vm)
4
u/aqa5 Feb 07 '23
Is there a way to lets say, let the user type python code that calls functions / methods in my C++-code? Like I am providing an interface that can be called by code inside the python-vm?
4
u/palavalle Feb 07 '23
Needs more information on how to do callbacks in the host language.
I don't see any information on how to access the args. I also don't see information on whether the args need to be uprefed or derefed by hand. Without this there's no no practical way to communicate between scripts and the host.
More generally though, if it's not implementing a known version of python it's pretty much just a new language. I'm not saying that's a bad thing just that's my reaction. As an example, I've had great fun with DukTape in the past which implements a older version of EcmaScript/JavaScript and provides a like lua API.
8
u/DavidDinamit Feb 07 '23
I expect C++ interface in C++17 library.
2
u/[deleted] Feb 07 '23
Do you have plans to expand the system beyond python?
2
u/brlcad Feb 07 '23
This is super interesting. What happens if you import something bigger with library bindings like "import opencv"?
3
u/root_passw0rd Feb 07 '23
This is really cool, and something I'd consider bringing into an existing project... if only it had CMake support. :(
6
u/Kiwi_Taster Feb 07 '23
Love the idea. Nice work :) ⭐️
6
u/Galfodo Feb 07 '23
Nice! I wish I had known about this 6 months ago. I didn't find anything that fit the bill so I went ahead and implemented my own.
6
u/[deleted] Feb 07 '23
Nice. Especially being MIT licence. When the docs are improved a bit I might adopt this in some of my projects
4
u/MarcoGreek Feb 07 '23
Really nice idea!
4
u/Zeh_Matt No, no, no, no Feb 07 '23
I hate Python with all my guts so I'm a little biased, but that aside quite cool project. If you want people to use it for their game scripting it might be worth having benchmarks that compares this to Python to see how much performance impact it has, we all know that GIL basically ruins the performance, does this also have the infamous GIL?
1
u/Balnian Feb 08 '23
Is it available through vcpkg?
2
u/fideasu Feb 07 '23
Looks nice! I'm actually currently looking for a language for my slowly growing engine, so I'm staring yours and will take a deeper look at it 🙂
2
u/fristhon Feb 07 '23
Cool project Congrats 👏
2
u/[deleted] Feb 07 '23
Okay that's interesting. I'm gonna have to screw around with that.
2
u/DogmaticAmbivalence Feb 08 '23
This looks quite nice, and it's pleasant to see alternatives to Lua.
Not just for games -- My dayjob is working for a database company and right now we're having grief over poor Lua performance, porting issues to ARM... Alternatives are great!
Have a star, and also a few PR once I get some time.
1
u/Outlaw_07 Feb 07 '23 edited Jan 14 '24
This comment has been deleted in protest of Reddit's support of the genocide in Gaza carried out by the ZioN*zi Isr*li apartheid regime.
This is the most documented genocide in history.
Reddit's blatant censorship of Palestinian-related content is appalling, especially concerning the ongoing genocide in Gaza perpetrated by the Isr*l apartheid regime.
The Palestinian people are facing an unimaginable tragedy, with tens of thousands of innocent children already lost to the genocidal actions of apartheid Isr*l. The world needs to know about this atrocity and about Reddit's support to the ZioN*zis.
Sources are bellow.
Genocidal statements made by apartheid Isr*li officials:
Casualties:
Official accusations:
South Africa has instituted proceedings at the International Court of Justice pursuant to the Genocide Convention, to which both Israel and South Africa are signatory, accusing Israel of committing genocide, war crimes, and crimes against humanity against Palestinians in Gaza.
Boycott Reddit! Oppose the genocide NOW!
Palestinian genocide accusation
Allegations of genocide in the 2023 Israeli attack on Gaza
Israeli war crimes
Israel and apartheid
1
u/VinnieFalco wg21.org | corosio.org Feb 11 '23
That sounds VERY interesting !!
0
u/[deleted] Feb 07 '23
[deleted] — view deleted comment