Skip to content

Commit 307ecc5

Browse files
projectgusdpgeorge
authored andcommitted
docs: Add note about position-only arguments in CPython vs MicroPython.
Required modifying the gen-cpydiff.py code to allow a "preamble" section to be inserted at the top of any of the generated files. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent c8772b7 commit 307ecc5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

docs/differences/modules_preamble.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.. Preamble section inserted into generated output
2+
3+
Positional-only Parameters
4+
--------------------------
5+
6+
To save code size, many functions that accept keyword arguments in CPython only accept positional arguments in MicroPython.
7+
8+
MicroPython marks positional-only parameters in the same way as CPython, by inserting a ``/`` to mark the end of the positional parameters. Any function whose signature ends in ``/`` takes *only* positional arguments. For more details, see `PEP 570 <https://peps.python.org/pep-0570/>`_.
9+
10+
Example
11+
~~~~~~~
12+
13+
For example, in CPython 3.4 this is the signature of the constructor ``socket.socket``::
14+
15+
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
16+
17+
However, the signature documented in :func:`MicroPython<socket.socket>` is::
18+
19+
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
20+
21+
The ``/`` at the end of the parameters indicates that they are all positional-only in MicroPython. The following code works in CPython but not in most MicroPython ports::
22+
23+
import socket
24+
s = socket.socket(type=socket.SOCK_DGRAM)
25+
26+
MicroPython will raise an exception::
27+
28+
TypeError: function doesn't take keyword arguments
29+
30+
The following code will work in both CPython and MicroPython::
31+
32+
import socket
33+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

tools/gen-cpydiff.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747

4848
TESTPATH = "../tests/cpydiff"
4949
DOCPATH = "../docs/genrst"
50-
INDEXTEMPLATE = "../docs/differences/index_template.txt"
50+
SRCDIR = "../docs/differences"
51+
INDEXTEMPLATE = os.path.join(SRCDIR, "index_template.txt")
5152
INDEX = "index.rst"
5253

5354
HEADER = ".. This document was generated by tools/gen-cpydiff.py\n\n"
@@ -219,6 +220,14 @@ def gen_rst(results):
219220
rst.write(section[i] + "\n")
220221
rst.write(RSTCHARS[0] * len(section[i]))
221222
rst.write(time.strftime("\nGenerated %a %d %b %Y %X UTC\n\n", time.gmtime()))
223+
# If a file docs/differences/<filename>_preamble.txt exists
224+
# then its output is inserted after the top-level heading,
225+
# but before any of the generated sections.
226+
preamble_path = os.path.join(SRCDIR, filename + "_preamble.txt")
227+
if os.path.exists(preamble_path):
228+
with open(preamble_path, "r") as f:
229+
rst.write(f.read())
230+
rst.write("\n")
222231
toctree.append(filename)
223232
else:
224233
rst.write(section[i] + "\n")

0 commit comments

Comments
 (0)