|
51 | 51 | - `python-unittest` - Run the unittests written in Python, including those for the C extension API
|
52 | 52 | - `python-license` - Check that all files have the correct copyright headers applied to them
|
53 | 53 |
|
54 |
| -###### Builtin modules and classes |
| 54 | +###### Built-In modules and classes |
55 | 55 |
|
56 |
| -For the most part, builtin modules and classes are implemented in the |
| 56 | +For the most part, built-in modules and classes are implemented in the |
57 | 57 | `com.oracle.graal.python.builtins` package. For each module or class, there's
|
58 | 58 | Java class annoted with `@CoreFunctions`. Each function in a module or a class
|
59 | 59 | is implemented in a Node annotated with `@Builtin`. Take a look at the existing
|
60 | 60 | implementations to get a feel for how this is done. For now, when adding new
|
61 | 61 | classes or modules, they need to be added to the list in
|
62 | 62 | `com.oracle.graal.python.builtins.Python3Core`.
|
63 | 63 |
|
64 |
| -Some builtin functions, modules, and classes are implemented in pure Python. The |
| 64 | +Some built-in functions, modules, and classes are implemented in pure Python. The |
65 | 65 | files for this are in `graalpython/lib-graalpython`. These files are listed in
|
66 | 66 | the Java `com.oracle.graal.python.builtins.Python3Core` class. Take a look at
|
67 | 67 | these files to see what they do. If a file is called exactly as a built-in
|
68 | 68 | module is, it is executed in the context of that module during startup, so some
|
69 | 69 | of our modules are implemented both in Java and Python. If the name matches no
|
70 | 70 | existing module, the file is executed just for the side-effects.
|
71 | 71 |
|
| 72 | +When implementing a new (or fixing an existing) built-in, take a look at the |
| 73 | +CPython source. The layout and naming of modules and types is kept similar to |
| 74 | +the CPython source so it should be relatively easy to find the right piece of |
| 75 | +code. For some special dunder methods (`__add__`, `__getitem__`, |
| 76 | +`__getattribute__`, ...) you may have to figure out the C API slot names for |
| 77 | +them to find the right piece of code (`nb_add`, `sq_item`, `tp_getattr`, ...). |
| 78 | + |
| 79 | +You will find that often there are specific C API methods that are called to |
| 80 | +convert or coerce arguments, to look up methods either starting on the object or |
| 81 | +only on the class, to call a callable object or invoke a method, and more. In |
| 82 | +general, most of these methods should have equivalents in our |
| 83 | +`PythonObjectLibrary`. See the |
| 84 | +[IMPLEMENTATION_DETAILS.md](./IMPLEMENTATION_DETAILS.md) file for details on |
| 85 | +that library. If something is missing that is commonly used, we probably have |
| 86 | +some Node for it, but it may be a good idea to add it to the |
| 87 | +`PythonObjectLibrary` for easier discovery. |
| 88 | + |
| 89 | +Sometimes, you will not easily find what exactly happens for a given piece of |
| 90 | +code when that involves more than just a simple built-in call. The `dis` module |
| 91 | +on CPython can often help get an angle on what a particular piece of code is |
| 92 | +doing. You can call `dis.dis` on any Python function and it will print you |
| 93 | +details of the bytecode and associated data, which can be a good starting point |
| 94 | +to browse through the CPython source. |
| 95 | + |
72 | 96 | ###### Python C API
|
73 | 97 |
|
74 | 98 | The C implementation and headers for our C API are in
|
|
0 commit comments