Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions python-stdlib/curses.ascii/curses/ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@


def _ctoi(c):
if type(c) == type(""):
return ord(c)
else:
return c
return ord(c) if type(c) is str else c


def isalnum(c):
Expand All @@ -94,45 +91,58 @@ def isascii(c):


def isblank(c):
return _ctoi(c) in (8, 32)
ch = _ctoi(c)
return ch == 9 or ch == 32


def iscntrl(c):
return _ctoi(c) <= 31
ch = _ctoi(c)
return ch <= 31 or ch == 127


def isdigit(c):
return _ctoi(c) >= 48 and _ctoi(c) <= 57
ch = _ctoi(c)
return ch >= 48 and ch <= 57


def isgraph(c):
return _ctoi(c) >= 33 and _ctoi(c) <= 126
ch = _ctoi(c)
return ch >= 33 and ch <= 126


def islower(c):
return _ctoi(c) >= 97 and _ctoi(c) <= 122
ch = _ctoi(c)
return ch >= 97 and ch <= 122


def isprint(c):
return _ctoi(c) >= 32 and _ctoi(c) <= 126
ch = _ctoi(c)
return ch >= 32 and ch <= 126


def ispunct(c):
return _ctoi(c) != 32 and not isalnum(c)
ch = _ctoi(c)
return (
(ch >= 33 and ch <= 47)
or (ch >= 58 and ch <= 64)
or (ch >= 91 and ch <= 96)
or (ch >= 123 and ch <= 126)
)


def isspace(c):
return _ctoi(c) in (9, 10, 11, 12, 13, 32)
ch = _ctoi(c)
return isblank(c) or (ch >= 10 and ch <= 13)


def isupper(c):
return _ctoi(c) >= 65 and _ctoi(c) <= 90
ch = _ctoi(c)
return ch >= 65 and ch <= 90


def isxdigit(c):
return (
isdigit(c) or (_ctoi(c) >= 65 and _ctoi(c) <= 70) or (_ctoi(c) >= 97 and _ctoi(c) <= 102)
)
ch = _ctoi(c)
return isdigit(c) or (ch >= 65 and ch <= 70) or (ch >= 97 and ch <= 102)


def isctrl(c):
Expand All @@ -144,24 +154,18 @@ def ismeta(c):


def ascii(c):
if type(c) == type(""):
return chr(_ctoi(c) & 0x7F)
else:
return _ctoi(c) & 0x7F
ch = _ctoi(c) & 0x7F
return chr(ch) if type(c) is str else ch


def ctrl(c):
if type(c) == type(""):
return chr(_ctoi(c) & 0x1F)
else:
return _ctoi(c) & 0x1F
ch = _ctoi(c) & 0x1F
return chr(ch) if type(c) is str else ch


def alt(c):
if type(c) == type(""):
return chr(_ctoi(c) | 0x80)
else:
return _ctoi(c) | 0x80
ch = _ctoi(c) | 0x80
return chr(ch) if type(c) is str else ch


def unctrl(c):
Expand All @@ -172,6 +176,4 @@ def unctrl(c):
rep = chr(bits & 0x7F)
else:
rep = "^" + chr(((bits & 0x7F) | 0x20) + 0x20)
if bits & 0x80:
return "!" + rep
return rep
return "!" + rep if bits & 0x80 else rep
2 changes: 1 addition & 1 deletion python-stdlib/curses.ascii/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="3.4.3")
metadata(version="3.4.4")

package("curses")
122 changes: 122 additions & 0 deletions python-stdlib/curses.ascii/test_curses_ascii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import curses.ascii


def _array_pack(function, char):
out = bytearray(256 // 8)
for octet in range(256 // 8):
byte = 0
for bit in range(8):
arg = (octet * 8) + bit
if char:
value = function(chr(arg))
else:
value = function(arg)
byte |= int(value) << bit
out[octet] = byte
return bytes(out)


isalnum = (
b"\x00\x00\x00\x00\x00\x00\xff\x03\xfe\xff\xff\x07\xfe\xff\xff\x07"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isalnum, False) == isalnum
assert _array_pack(curses.ascii.isalnum, True) == isalnum

isalpha = (
b"\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\x07\xfe\xff\xff\x07"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isalpha, False) == isalpha
assert _array_pack(curses.ascii.isalpha, True) == isalpha

isascii = (
b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isascii, False) == isascii
assert _array_pack(curses.ascii.isascii, True) == isascii

isblank = (
b"\x00\x02\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isblank, False) == isblank
assert _array_pack(curses.ascii.isblank, True) == isblank

iscntrl = (
b"\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.iscntrl, False) == iscntrl
assert _array_pack(curses.ascii.iscntrl, True) == iscntrl

isdigit = (
b"\x00\x00\x00\x00\x00\x00\xff\x03\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isdigit, False) == isdigit
assert _array_pack(curses.ascii.isdigit, True) == isdigit

isgraph = (
b"\x00\x00\x00\x00\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isgraph, False) == isgraph
assert _array_pack(curses.ascii.isgraph, True) == isgraph

islower = (
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\x07"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.islower, False) == islower
assert _array_pack(curses.ascii.islower, True) == islower

isprint = (
b"\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isprint, False) == isprint
assert _array_pack(curses.ascii.isprint, True) == isprint

ispunct = (
b"\x00\x00\x00\x00\xfe\xff\x00\xfc\x01\x00\x00\xf8\x01\x00\x00\x78"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.ispunct, False) == ispunct
assert _array_pack(curses.ascii.ispunct, True) == ispunct

isspace = (
b"\x00\x3e\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isspace, False) == isspace
assert _array_pack(curses.ascii.isspace, True) == isspace

isupper = (
b"\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\x07\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isupper, False) == isupper
assert _array_pack(curses.ascii.isupper, True) == isupper

isxdigit = (
b"\x00\x00\x00\x00\x00\x00\xff\x03\x7e\x00\x00\x00\x7e\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isxdigit, False) == isxdigit
assert _array_pack(curses.ascii.isxdigit, True) == isxdigit

isctrl = (
b"\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
)
assert _array_pack(curses.ascii.isctrl, False) == isctrl
assert _array_pack(curses.ascii.isctrl, True) == isctrl

ismeta = (
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
)
assert _array_pack(curses.ascii.ismeta, False) == ismeta
assert _array_pack(curses.ascii.ismeta, True) == ismeta
1 change: 1 addition & 0 deletions tools/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function ci_package_tests_run {
python-stdlib/base64/test_base64.py \
python-stdlib/binascii/test_binascii.py \
python-stdlib/collections-defaultdict/test_defaultdict.py \
python-stdlib/curses.ascii/test_curses_ascii.py \
python-stdlib/functools/test_partial.py \
python-stdlib/functools/test_reduce.py \
python-stdlib/heapq/test_heapq.py \
Expand Down
Loading