Ideas: making it easier to extend Python’s syntax

Code on Github

ideas logo

You have an Excellent Idea ® to change the Python syntax and want to find a way to include your Excellent Idea ® in your Python programs. According to Python Developers Guide, this might be doable if you are willing to follow “a few steps” including:

  1. Get a copy of the CPython’s code repository and all the required compilers for your platform.

  2. Modify the grammar file to add rules for the new syntax.

  3. Modify the AST generation code; this requires a knowledge of C

  4. Compile the AST into bytecode

  5. Recompile the modified Python interpreter

This … can be a rather daunting task. It might get a bit easier if you grab a copy of the currently unpublished book by Anthony Shaw, CPython Internals and read it from cover to cover, but it will still remain a major task. Furthermore, it would not be easy to share your work with others so that they can try it out.

However, there is a simpler way: it is possible to run code with a modified syntax using import hooks. [1]

Important

This entire project is meant as enabling easy experiments with changes to Python’s normal syntax. Some (most?) of the examples are definitely not reliable enough to be included in code used by anyone other than yourself.

To do

Todo

Currently, only one AST example exists and only one AST transformation can be done. A second example should be created and find a way to ensure that both could be applied.

(The original entry is located in C:\Users\Andre\github\ideas\docs\source\guide.rst, line 58.)

Todo

Currently, only one Bytecode example exists and only one Bytecode transformation can be done. A second example should be created and find a way to ensure that both could be applied.

(The original entry is located in C:\Users\Andre\github\ideas\docs\source\guide.rst, line 82.)

Todo

Currently, a patch requires to specify a module name for it to be applied to that module. It might be worthwhile exploring a case where we use "*" to specify that the patch should be applied to all modules.

(The original entry is located in C:\Users\Andre\github\ideas\docs\source\guide.rst, line 105.)

Todo

Try to create a different module object.

(The original entry is located in C:\Users\Andre\github\ideas\docs\source\included\constants.rst, line 123.)

Todo

Write this up.

(The original entry is located in C:\Users\Andre\github\ideas\docs\source\included\patching.rst, line 4.)

Todo

Write an example where we include different locations to search.

(The original entry is located in C:\Users\Andre\github\ideas\docs\source\possible.rst, line 49.)