Skip to content

Commit 72162ce

Browse files
authored
Add A "CPython Builtins" Page to Docs (#400)
Adds a page to the docs that illustrates which built-ins from CPython are currently implemented and which are not. Inspired by `setattr` and `getattr` getting added.
2 parents e5c9b59 + 90c9a80 commit 72162ce

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

docs/mkdocs.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ markdown_extensions:
4242
- pymdownx.blocks.admonition
4343
- pymdownx.tabbed:
4444
alternate_style: true
45+
- toc:
46+
permalink: true
47+
- def_list
4548

4649
nav:
4750
- Home: index.md
4851
- contributing.md
52+
- API Reference:
53+
- api_reference/python_builtins.md
4954
- Unsorted notes:
5055
- Low-level memory model: llmem.md
5156

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
title: CPython-Like Builtins
2+
---
3+
4+
<style>
5+
h3 {
6+
font-family: "Lucida Console", "Courier New", monospace;
7+
}
8+
</style>
9+
10+
## Implemented CPython-Like Built-ins
11+
12+
The following built-in functions work similarly to their equivalents in CPython; see the specific functions below for notes
13+
14+
### __abs__(object) { data-toc-label='abs()' }
15+
16+
: Currently only implemented for int32's or objects convertible to int32's. The `__abs__` attribute is not currently supported.
17+
18+
### __breakpoint()__
19+
20+
: Drops the user into an interactive SPy debugging session via `spdb`, a `pdb-like` debugging interface for SPy.
21+
22+
### __dict__\[keytype, valuetype\]() { data-toc-label='dict()' }
23+
: In SPy, `dict` must always be fully typed and used as `dict[keytype, valuetype]`., The syntax `dict[keytype, valuetype]()` can be used to create a new empty dict of the given types. The simpler syntax `d: dict[keytype, valuetype] = {}` can also be used.
24+
25+
: Unlike CPython, this does not (currently) accept an Iterable to create a new dict from.
26+
27+
: The implementation (in SPy) of `dict` can be [viewed here](https://github.com/spylang/spy/blob/main/stdlib/_dict.spy).
28+
29+
### __dir__(object) {data-toc-label='dir()'}
30+
31+
: Returns a list of object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes. `dir(type)` is not currently implemented.
32+
33+
: The no-argument form of `dir()` (i.e. print local variables) is not currently implemented. Custom `__dir__` methods on objects are not currently supported.
34+
35+
### __float__(object) { data-toc-label='float()' }
36+
37+
: Converts `object` to a float if able. `float` is an alias for the `f64` type.
38+
39+
### __getattr__(obj, name: str) { data-toc-label='getattr()' }
40+
41+
: Return the value of the named attribute of object. `attr` must be blue
42+
43+
### __hash__(object) { data-toc-label='hash()' }
44+
: Currently implemented for types: `i8`,`i32`, `u8`, `bool`, `str`.
45+
46+
: By default, instances of SPy structs are not hashable. As a planned future feature, structs will have auto-generated `__hash__` by default, but this is awaiting implementation. Currently, users can implement the `__hash__` function to permit hashing.
47+
48+
### __int__(object) { data-toc-label='int()' }
49+
50+
: Converts `object` to an int if able. Works for number types, as well as strings.
51+
52+
: The `int` type is currently an alias to `i32`. In the future, `int` will alias preferred individual types for specific platforms, but currently it is always `i32`.
53+
54+
### __len__(object) { data-toc-label='len()' }
55+
56+
: Return the length (the number of items) in a container
57+
58+
### __list__\[type\]() { data-toc-label='list()' }
59+
60+
: The syntax `list[type]()` can be used to create a new empty list of the given type. The simpler syntax `l: list[membertype] = []` can also be used. Unlike CPython, this does not (currently) accept an Iterable to create a new list from.
61+
62+
: The implementation (in SPy) of `list` can be [viewed here](https://github.com/spylang/spy/blob/main/stdlib/_list.spy).
63+
64+
### __max__(x: i32, y: i32) { data-toc-label='max()' }
65+
66+
: Currently only implemented for int32's or objects convertible to int32's.
67+
68+
### __min__(x: i32, y: i32) { data-toc-label='min()' }
69+
70+
: Currently only implemented for int32's or objects convertible to int32's.
71+
72+
### __object__
73+
74+
: `object` is implemented as a type, and can be used as a parameter or return type. "Plain" objects (i.e. `x = object()`) are not supported.
75+
76+
### __print__(obj) { data-toc-label='print()' }
77+
78+
: The print function is currently not variadic, in the sense that it only accepts a single argument. The built-in types are special-cased, and SPy can always print blue objects by pre-computing their string representation
79+
80+
### __range__(stop) { data-toc-label='range()' }
81+
<h3> <b>range</b>(start, stop, step)</h3> <!-- An HTML label to hide this in the TOC -->
82+
83+
: Creates an iterable set of indices between `start` and `stop`, jumping over `step` indices between each.
84+
85+
: The implementation (in SPy) of `range` can be [viewed here](https://github.com/spylang/spy/blob/main/stdlib/_range.spy).
86+
87+
### __repr__(object) { data-toc-label='repr()' }
88+
89+
: Returns string containing a printable representation of an object.
90+
91+
### __setattr__(object, name: str, value: obj) { data-toc-label='setattr()' }
92+
93+
: Assigns `value` to the attribute of `object` named by `name`. `attr` must be blue.
94+
95+
### __slice__(stop) { data-toc-label='slice()' }
96+
<h3><b>slice</b>(start, stop, step=None)</h3> <!-- An HTML label to hide this in the TOC -->
97+
98+
: Return a slice object representing the items reached when iterating over range(start, stop, step). The start and step arguments default to None.
99+
100+
### __str__(object) { data-toc-label='str()' }
101+
102+
: Returns a string version of the object. Selecting an encoding is not currently implemented.
103+
104+
### __tuple__()
105+
106+
: The syntax `tuple[t1, t2, ...](val1, val2 ...)` can be used to create a new tuple, with `t1` as the type of `val1`, etc. unlike CPython, this does not (currently) accept an Iterable to create a new tuple from.
107+
108+
: The implementation (in SPy) of `tuple` can be [viewed here](https://github.com/spylang/spy/blob/main/stdlib/_tuple.spy).
109+
110+
### __type__(object) { data-toc-label='type()' }
111+
112+
: Returns the type (i.e. the dynamic type at runtime) of an object
113+
114+
## Not-Implemented CPython Built-ins
115+
116+
The following CPython built-ins are not currently implemented in SPy. Each category has a brief note about the current state of that category of object or function - some require additional internal mechanics, others are simply lower priority that other facets of the language to this point.
117+
118+
### Async
119+
120+
SPy does not currently have an async story.
121+
122+
: aiter(), anext()
123+
124+
### Iterables and Iterators
125+
126+
Iterables and collections are very much an area of active developmen; as their API solidifies, these types of builtins should become more straightforward to implement.
127+
128+
Generators are not currently supported in SPy.
129+
130+
: all(), any(), enumerate(), filter(), iter(), map(), next(), reversed(), sorted(), sum(), zip()
131+
132+
### Math
133+
134+
Number types beyond int and float are in active development; some of the math functions below are also in active development.
135+
136+
: divmod(), bin(), hex(), oct(), pow(), round()
137+
138+
### Function Types, Introspection and Metaprogramming
139+
140+
The internals of SPy are significantly different from CPython; as such, the road to (and need for) some of these built-ins is less straightforward. Some are also waiting on internal details to solidify prior to implementation.
141+
142+
: callable(), classmethod(), compile(), delattr(), eval(), exec(), globals(), hasattr(), help(), id(), isinstance(), issubclass(), locals(), property(), super(), vars(). \_\_import\_\_()
143+
144+
### Type Conversion
145+
146+
Many of these types are not implemented yet; others are in active development.
147+
148+
: ascii(), bool(), bytearray(), bytes(), chr(), complex(), format(), frozenset(), memoryview(), ord() set()
149+
150+
### I/O
151+
152+
The I/O story is currently a high priority and is in active development.
153+
154+
: input(), open()

0 commit comments

Comments
 (0)