Repository navigation
0.6.0
Changes
More ordinary Python runs natively under ppy run: functions that print, read input, or call back into Python; functions that read module globals, take the project's objects, or are called with keywords; isinstance, chained comparisons, in, integer powers, and generators passed around as values; and random, math, heapq, bisect, itertools, Counter, defaultdict, and lru_cache. The Python boundary copies containers and objects in C. Without strict mode, unannotated parameters take their types from the project's calls, and ppy run no longer refuses a program CPython runs.
On TheAlgorithms/Python, the functions Python calls natively went from 231 to 806 (846 without strict mode), and 387 of 400 scripts print the same as under CPython, with the other 13 skipped as nondeterministic or slow and none differing.
Effects, globals, and objects
- Functions that print, read input, read and write text files, or call Python functions compile. Output is held per call, so a fallback never prints twice;
input(),flush=True, files, and calls into Python are barriers after which nothing falls back. - A function that reads a module global bound once runs natively. Objects of the project's classes, and dataclass values in containers, cross between Python and native code, with shared objects and cycles kept shared.
More of Python native
isinstance, chained comparisons,inover tuples, sets, andrange, integer**andpow, moreforloops, generators returned or passed on, ande.args.- Keyword arguments and defaults, results from Python functions that change nothing, and nested functions that share nothing with the function around them.
- A native function given an
intfor afloatparameter returns what CPython returns:f(0)is0.
The standard library
random(draw for draw with CPython, module-level andrandom.Randominstances),math,heapq,bisect,itertools,deque,defaultdict,Counter,OrderedDict,functools.cache/lru_cache/reduce/partial/cmp_to_key,operator, andstring.
The boundary
- Containers and objects cross in C: about 8 to 35 times faster than in 0.5.0. A native two-int call costs 29 ns against 30 ns in CPython, and a call with keywords or defaults about 40 ns, bound in C.
- The cost model is measured: a function gets a Python boundary only where the call is cheaper than CPython's.
Unannotated code and the checker
- Without strict mode, parameters take their types from calls, defaults, and doctests, and
ppy explainshows where each came from. A Python caller passing other types runs the Python body. --no-strictturns what the analysis cannot follow (unresolvable imports,eval,globals(), star imports, metaclasses) into warnings, and that code runs on CPython.typing.Self,Generic[T], named tuples,MutableSequence, and thecollectionstypes are accepted.ppy runwithout the LLVM extra runs the program on CPython with one warning.
Fixed
- Cached recursion ran without its cache under
ppy run; a string-keyed map copy asked for 64 GiB; a double free after a native call raised; a segfault past the recursion limit; a nested function reading a lent list crashed;rng.choiceover strings freed its result;d.get(k)without a default crashedppy run; temporaries leaked when a check raised insidetry; several Python-backend folds that hid an exception.
Examples
51_clinic,52_unannotated,53_caches_and_counters, and54_objects_and_grids.
See the changelog for the full list and the known limitations.
Install or upgrade with pip install --upgrade "ppy-lang[llvm]==0.6.0".
The full repository gate passed: 2,463 tests, 49/49 examples on all three paths, 70/70 conversion claims, pylint on every example, and the documentation examples. The emitted C and C++ of the new tests runs clean under AddressSanitizer with leak detection.