Skip to content

curses.ascii: Fix some is* function ranges and reduce footprint. - #1160

Merged
dpgeorge merged 3 commits into
micropython:masterfrom
agatti:curses-ascii
Oct 12, 2026
Merged

dpgeorge merged 3 commits into
micropython:masterfrom
agatti:curses-ascii

Conversation

@agatti

@agatti agatti commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR updates curses.ascii.isblank, curses.ascii.iscntrl, and curses.ascii.ispunct to use the same ranges as CPython, along with reducing the compiled module size.

For one reason or another, the three functions in question would use either incorrect values or use partial ranges for validation. Maybe CPython's range changed for iscntrl and ispunct from when the module was originally written?

That went unnoticed for a while, probably due to the lack of popularity of this module. However, I've added a test file that compares the output of curses.ascii.is* functions with the off-line output of the same function recorded from CPython 3.14, covering the full 0..255 values range.

Most of the functions in the module have been reworked to take up less space, even after the changes required to make the module behave as CPython. Now the compiled module is 16 bytes shorter.

Testing

The new test file that's part of this PR has been added to CI, so most of the module should be now tested automatically. The test file's behaviour was checked against CPython using its own implementation of curses.ascii.

Generative AI

I did not use generative AI tools when creating this PR.

This commit updates `curses.ascii.isblank`, `curses.ascii.iscntrl`, and
`curses.ascii.ispunct` to use the same ranges as CPython.

For one reason or another, the three functions in question would use
either incorrect values or use partial ranges for validation.  The new
test file that's part of this commit compares the output of
`curses.ascii.is*` functions with the output of the same function from
CPython, covering the full 0..255 values range.

The ranges fix increases the compiled size of this module by 73 bytes.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit reduces the module footprint by changing most functions to
generate shorter bytecode sequences once compiled.

The same few changes were applied to most functions in the module:
type coercions are now done at most once per function body, range
membership checks are converted into a series of comparisons (as a
series of chained equality checks for disjointed sets or as two
numerical comparisons with the range edges otherwise), and using the
inline ternary comparison form for returning function values instead
of a regular if-else construct.

These changes reduce the compiled module size by 89 bytes.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit updates the `curses.ascii` module's manifest version
number, bumping its patch version by one.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>

@dpgeorge dpgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, some good little bug fixes, a nice reduction in code size, and a test! And thanks for keeping the changes in separate, easy to review commits.

@dpgeorge
dpgeorge merged commit 90028be into micropython:master Oct 12, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants