10 ms·
A lightweight (~5000 LOC) Python interpreter for game engines
- blondin 4y agoimpressive work! i suppose it implements a subset and not the full language? would be nice to see some benchmarks. everyone claims super fast nowadays. i have no doubt though, since it's cpp.
- re 4y agoThey say "The speed of PocketPy is approximately 50% of CPython" so apparently the focus is a small implementation rather than performance. https://pocketpy.dev/performance/ https://pocketpy.dev/performance/
- asicsp 4y agoThere's a table in the README saying "Star Unpacking" isn't supported.
- deathanatos 4y agoThat's not all that's not supported. Through poking at the live demo, I've found that at least slots, kw-only args, some basic syntax elements, decorators, generators, generator comprehensions and async are additionally not. Exceptions are marked as supported … but `raise Exception()` is a syntax error? (Re-raise and binding exceptions also fails to parse.)
- jsjohnst 4y agoHow is this on the homepage twice with no comments and less than 10 votes?
- childintime 4y agoBecause the world rotates and it was a hit while the other half had their eyes closed?
- jsjohnst 4y agoSee my comment here for more context: https://news.ycombinator.com/item?id=34707361 https://news.ycombinator.com/item?id=34707361
- versteegen 4y agoI've been waiting a long time for MicroPython to become usable as an embedded script interpreter -- it can't be run asynchronously [1] so scripts can't do waits (aside from that, MicroPython is amazingly full-featured). CPython can't either, which is why the Stackless Python fork was created, which is used in games such as EVE Online. But that's far too heavy for my tastes. So I was hoping that PocketPy could do that, but after looking around I can't find any suggestion it's possible, and the API looks very sparse. I'm very happy to see a new Python interpreter built for embedding, but I can't use it. Maybe it can be added easily enough. Keep it up! If you want to see a scripting language designed from the ground up for embedding, Lua and Squirrel IMO have the most complete APIs (though not the most efficient or easy to use) [2][3], and LuaJIT has even better lua<->C bridging. I've looked at the APIs of many other small scripting languages, and haven't found any others that can do everything that these can. [1] https://github.com/micropython/micropython/issues/3619 https://github.com/micropython/micropython/issues/3619 [2] In particular https://www.lua.org/manual/5.4/manual.html#4.5 https://www.lua.org/manual/5.4/manual.html#4.5 [3] http://squirrel-lang.org/squirreldoc/reference/embedding_squirrel.html http://squirrel-lang.org/squirreldoc/reference/embedding_squ...
- Gibbon1 4y agoAfter 40 years of dicking around the C21 has an #embed preprocessor feature. I think which allows you to write stuff like const char python_script_foo [] = { #embed <foo.py.wav> }; Would be useful for embedded scripting. Especially for firmware.
- JonChesterfield 4y agoThis was of course available though inline assembly or code generators from the beginning. So C21 is more complicated for the extra looks-like-prepreprocessor-but-probably-isn't syntax with no increase in expressivity.
- dotancohen 4y agoHow could that not be a preprocessor? Or are you suggesting that the compiled binary will now include a Python interpreter?
- nattaylor 4y agoI'd like to give the WASM build a try
- bragr 4y agoWhat's the performance like compared to Lua? Since Lua is the standard here, this feels like nonstarter unless the performance is similar, even with the friendlier python syntax.
- pasquinelli 4y agoto my mind the differences in syntax between python and lua are like two white, long-sleeved, button-up, collared shirts, but they have different buttons.
- deleted 4y ago[deleted]
- 130e13a 4y agoand also the buttons on the Lua shirt are all shifted by one
- aa-jv 4y ago.. while the python shirt has a huge gap that shows a nipple, and if you try to cover that nipple you're going to fall over.
- orangetuba 4y agoHAHA,good one!
- qbasic_forever 4y agoThe python shirt has a combo of buttons, zippers, Velcro, and drawstrings in different places and with a long history about why it's there and why it made sense at the time but no one would do it that way again.
- BlackFingolfin 4y agoDoesn't this apply to every mature and widely used language after a few years of evolution?
- voxelghost 4y agoIf the author comes around, first of all congratulations on what seems like a really cool project. Second, I am curious as to whether the lack of 'star unpack' is due to any real blocker, or more of a 'not done yet'/'not prioritized'?
- simonw 4y agoThe demo is fun to play with: https://blueloveth.github.io/pocketpy/ https://blueloveth.github.io/pocketpy/ It uses https://blueloveth.github.io/pocketpy/lib/pocketpy.wasm https://blueloveth.github.io/pocketpy/lib/pocketpy.wasm which is 766.66 kB - transferred compressed as 240.72 kB
- app4soft 4y ago> The demo is fun to play with: https://blueloveth.github.io/pocketpy/ https://blueloveth.github.io/pocketpy/ Type `exit()` and press [Enter].
- peterbmarks 4y agoI wonder how it compares with MicroPython?
- sitkack 4y agoMicroPython is 286kloc of C.
- unwind 4y agoThat is great information, but perhaps it would be even more interesting with a comparison focusing on supported features, standard library support, and so on. MicroPython [1] is designed for "real" embedding, i.e. in the "embedded development" sense, on microcontrollers and so on and seems quite successful in that niche. Those are often a smaller target than a game running on either a phone/tablet or PC, which would seem to kind of invert things. [1]: https://micropython.org/ https://micropython.org/
- askvictor 4y agoThe other comparison would be memory and disk/ROM usage. Also, micropython, in its hundreds of thousands of lines of code, supports quite a number of microcontrollers and boards; I suspect the core language is smaller, or counting lines used for a single build target would drop it considerably.
- tredre3 4y agoYou are correct, the micropython vm/interpreter + the unix target (one of the simplest) combined are ~50k lines of C. Note that to run correctly micropython also needs its python stdlib which is about 15k lines of Python.
- qbasic_forever 4y agoMicropython is made to run on dozens of boards, each with a nontrivial board support package (the platform-specific glue code to talk to hardware). A better comparison would be looking exclusively at micropython's unix/x86 platform specifically.
- winter_blue 4y agoHmm, wish this had been built as a PocketPy -> LLVM compiler. While the compiler might not have been lightweight, the final emitted binaries would have been superfast.
- cmovq 4y agoThat kind of defeats the purpose of having an embedded scripting language
- sitkack 4y agohttps://github.com/Chocopy-LLVM/chocopy-llvm https://github.com/Chocopy-LLVM/chocopy-llvm
- AnthonBerg 4y agoYour wish is my command: Do it!
- stefncb 4y agoYou can't just put LLVM on it and expect it to go fast, and I can't put a turbo on a 2CV and expect to use it on the motorway now. It's _a lot_ of work to make a language like python "superfast", and most of it is really the dynamic typing part. LLVM doesn't do that for you.
- fransje26 4y ago> and I can't put a turbo on a 2CV and expect to use it on the motorway now. Well, actually, I remember being passed on the autobahn by 2CV going well over 200 km/h. That was quite a shocker.
- Haga 4y ago[dead]
- awelxtr 4y agoI thought that "weight" generally referred to less resource usage (RAM primarly, cpu second) and loc weren't part of those resources. Was I wrong?
- TickleSteve 4y agoweight == resources (both in terms of RAM, code space, maintenance overhead, etc).
- fsloth 4y agoThere is no "standard" terminology but I would say small line count is a definite factor here. Small LOC count implies low complexity of integration and low risk. Risk? Yes, the smaller the codebase, the easier it is to take your own fork and maintain it, even if the original project is no longer maintaining the codebase. Of these two low complexity is probably the main selling point. Small also implies generally "an elegant engineering solution to a problem that generally may involve layers of accidental complexity" and hence also incites a sense of beauty. Aesthetics can be important for people that are hand crafting their codebases.
- 314 4y agoNice idea, as python is much easier to write code for than lua so should fit game scripting well. But, the examples are not the kinds of things that I would care about before trying to put this into a game engine. It needs a different set of examples. * How hard is it to call into a C function from a hook in the py code? * How hard is it to wrap / unwrap values from their py representation to C? If you want people to embed the interpreter into an engine then show that it is clean/easy to do the embedding, rather than showing that the interpreter can execute code. I would assume that it executes python, but need to know how well it embeds.
- benob 4y agoHere is what I came up with for calling a c function after poking around a little bit: #include "pocketpy.h" // builtin function that can be called PyVar test(VM* vm, const pkpy::Args& args) { printf("called!\n"); return vm->PyStr("hi there"); } int main(){ // Create a virtual machine VM* vm = pkpy_new_vm(true); vm->bind_builtin_func<1>("test", test); // Hello world! pkpy_vm_exec(vm, "print(test('Hello world!'))"); // Free the resources pkpy_delete(vm); return 0; }
- diego_moita 4y ago> as python is much easier to write code for than Lua No, it isn't. And it isn't harder, too. They're just different.
- traverseda 4y agoIs assembly harder to write than lua? How about lisp? I mean you must have some kind of rough ranking of language difficulty in your head. Sure, some of it will be cultural, in a world where middle schoolers are taught s expressions and reverse polish notation lisp might be a lot easier. There certainly is some subjectivity involved. Still, I think it's reasonable to say that python is easier to write code for.
- HarHarVeryFunny 4y ago
- avinassh 4y agoWow, this is excellent! I love the fact that it is just in 5K lines. I have a few questions! 1. What were your motivations for building this? 2. Did you eval any similar projects before writing this one? 3. Can this be compiled to WASM and used in the browser? edit: I see that it already supports WASM! 4. I didn't see this on the README, but does it support IO (either disk or network)? 5. I noticed you linked to byterun. Does this also implement a VM similar to Python? Can I run the bytecode produced from CPython in the pocketpy VM? If so, are there any reasons for that? I thought writing an independent implementation would be faster/easier than a compatible one. 6. Any plans on adding type annotations?
- benob 4y agoHow does this compare to tinypy? [1] http://www.tinypy.org/ http://www.tinypy.org/
- forgotpwd16 4y agoTinyPy is a bit larger at 10k LOC, seems not as easily embeddable since source split among numerous files, not really suitable for embedding since it's partially bootstrapped (parser and compiler written in tinypy; although pretty cool), it's dead and stuck at py2.
- scrawl 4y agowhy does 1. source split among numerous files, and 2. partial bootstrapping affect embeddability? i'm asking in earnest. i thought to embed we (typically) just need a shared lib and to #include a header api.
- forgotpwd16 4y agoThe issue for the first is you either have to download the shared lib precompiled or compile yourself and link as extra step. PocketPy is entirely in header file(s) that can be included in the project directly. That's easiness. (Of course header-only libs have their bads too.) It isn't really about the difference in the result but the process. Having part written in the language can contribute to previous but can also impact speed and memory use. That's suitability.
- scrawl 4y agoi learned something. thanks for responding.
- deleted 4y ago[deleted]
- dividedbyzero 4y agoThis is neat! I assume this is Python 3? Still looking for a replacement for the excellent Jython project [1] on the JVM, which has been fantastic for adding scripting to Java/Scala based projects, but sadly stuck on Python 2.7. I wonder if this could be bolted onto or run inside a JVM somehow (perhaps the WebAssembly version?) 1: https://www.jython.org/ https://www.jython.org/
- londons_explore 4y agoI think it is python-like rather than perfectly python compatible. Therefore the exact version is indeterminate...
- ZoomZoomZoom 4y agoThere's not only the question of why use this over Lua, but why use this over many compile-to-lua languages, providing a great range of syntax and typing features.
- quectophoton 4y agoI can confirm that, for example, Fennel[1] is nice if you're looking for something lisp-y. It's a bit unfortunate that it assumes that you are using Lua's standard library (so you can't use it in cases where you only want to embed Lua-the-language with your own restricted APIs), but other than that it's real nice. [1]: https://fennel-lang.org/ https://fennel-lang.org/
- qikInNdOutReply 4y agoWhy not lua though?
- vkazanov 4y agoSo is this a clean reimplementation? Given how small this is it probably is. Which means this is not Python but a language that is syntax-compatible ("Python-like"). To prove compatibility something like a (very) extensive set of compatibility tests would have to be written and mentioned in the README. Otherwise it's a minefield of subtle semantic differences.
- tyingq 4y agoI don't know if they plan to expand it, but it's a pretty small subset of python now. No bytes or bytearray types, for example. Only string escapes like "\t" are supported, so "\0" raises an error. Trying somestring[1:2:3] raises an error. It's missing list.reverse(), list.sort(), list(some_dictionary) raises an error, and so on. Most areas, if I dug in a little, were missing things. Still impressive for 5kLOC, of course.
- vkazanov 4y ago...and there's no official way of finding these limitations. It's amazing for a personal project but not good enough for a serious option.
- forgotpwd16 4y ago*interpreter for a small Python subset Should be expected from the size of codebase but should also be clearly mentioned. And this isn't about just lacking Python's modules. Plain language features are also not there. For example reversing a string with "abcd"[-1::-1] doesn't work. Now there's a features list on README, but not sure whether this is meant to be the comprehensive list this subset has or just a TODO.
- BlackLotus89 4y agoHmm duplicate of https://news.ycombinator.com/item?id=34702386 https://news.ycombinator.com/item?id=34702386 maybe I should text @dang to merge...
- brunovcosta 4y agoGreat project!! I was trying to build my own python interpreter to my project and this saved me a lot of work! super nice-to-have ask: could you replace the is_prime test on the README from range(2,x) to range(2,int(math.sqrt(x)))?
- pipeline_peak 4y agoan i for an i is an i in range