Skip to content

Commit e2e0f8f

Browse files
committed
a little more details on implementing builtins
1 parent c2f70ed commit e2e0f8f

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

docs/contributor/CONTRIBUTING.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,48 @@ are:
5151
- `python-unittest` - Run the unittests written in Python, including those for the C extension API
5252
- `python-license` - Check that all files have the correct copyright headers applied to them
5353

54-
###### Builtin modules and classes
54+
###### Built-In modules and classes
5555

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
5757
`com.oracle.graal.python.builtins` package. For each module or class, there's
5858
Java class annoted with `@CoreFunctions`. Each function in a module or a class
5959
is implemented in a Node annotated with `@Builtin`. Take a look at the existing
6060
implementations to get a feel for how this is done. For now, when adding new
6161
classes or modules, they need to be added to the list in
6262
`com.oracle.graal.python.builtins.Python3Core`.
6363

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
6565
files for this are in `graalpython/lib-graalpython`. These files are listed in
6666
the Java `com.oracle.graal.python.builtins.Python3Core` class. Take a look at
6767
these files to see what they do. If a file is called exactly as a built-in
6868
module is, it is executed in the context of that module during startup, so some
6969
of our modules are implemented both in Java and Python. If the name matches no
7070
existing module, the file is executed just for the side-effects.
7171

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+
7296
###### Python C API
7397

7498
The C implementation and headers for our C API are in

0 commit comments

Comments
 (0)