-
Notifications
You must be signed in to change notification settings - Fork 19
posix: argument binding across the module surface, plus a lone-surrogate abort #1113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8b204d4
posix: give utime's unnamed time UTIME_NOW, and run ftruncate's EINTR…
youknowone b6fb1b3
ci: run the parity_tests step even when check.py failed
youknowone c9ba387
posix: name the function and the argument in path-converter type errors
youknowone 1241d16
posix: retry sendfile's EINTR through the signal gate
youknowone aa7b51e
posix: bind pipe2 and the scheduling-policy calls where the libc has …
youknowone ca010e7
posix: bind the CPU affinity pair and the rest of the <fcntl.h> flag set
youknowone e114db1
posix: read cpu_count from the processor count rather than the thread…
youknowone dde0932
posix: register dup2 with a Signature so its keyword argument binds
youknowone 156b193
posix: bind the arguments of nine entry points that read the raw slice
youknowone 7f25a8d
function: read a name through the surrogate-tolerant accessor
youknowone 6be0835
sys, pyexpat: check the name argument of audit and ParserCreate
youknowone 9b16d0c
posix: answer os.access's three modifiers through faccessat
youknowone ac3df89
pyexpat: distinguish an omitted `ParserCreate` intern from an explici…
youknowone dd756ac
pyexpat, sys: state why the two name unwraps are the strict encode
youknowone 3018a5d
posix: read the pid argument of seven scheduling calls through c_int_w
youknowone 6f793c2
jit: fold the INT_MIN unary negate, pin the unary operand class, and …
youknowone c157a14
sys: widen the addaudithook refusal to Exception and re-wrap the audi…
youknowone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
269 changes: 269 additions & 0 deletions
269
pyre/extra_tests/parity_tests/audit_and_parsercreate_name_argument.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| """`sys.audit` and `pyexpat.ParserCreate` check the name they are handed. | ||
|
|
||
| `sys.audit` was registered as `|_| Ok(w_none())` — a no-op that accepted | ||
| anything, so `sys.audit()` with no event at all, with an `int`, or by keyword | ||
| all returned None. 22 stdlib modules call it (`os.walk`, `glob.glob`, | ||
| `pickle.find_class`, `subprocess`, ...), so a bad event name reached none of the | ||
| checks that upstream's `@unwrap_spec(event="text")` performs. | ||
|
|
||
| `ParserCreate` did check that `encoding` is `str` or `None`, but stored it | ||
| without asking whether it has a UTF-8 spelling — the sibling | ||
| `namespace_separator` arm did ask. A `str` holding a lone surrogate therefore | ||
| built a parser whose stored encoding no `&str` reader can see. The same arm | ||
| also spelled its type name as a literal `int`, so every other type was reported | ||
| as `int`. | ||
|
|
||
| No audit hook can be installed yet (`sys.addaudithook` stores nothing), so | ||
| `sys.audit` still does nothing past the checks below; the hook mechanism is a | ||
| separate gap. The surrogate is built from bytes rather than written as a | ||
| literal because that is how it arrives in practice and because a source file | ||
| cannot carry one. | ||
|
|
||
| Only one surrogate is used for the messages that are compared verbatim: a run | ||
| of adjacent unencodable code points is reported by CPython as one error with a | ||
| range (`characters in position 3-4`) and by pyre as the first one alone, which | ||
| is a separate divergence in the encoder rather than in these entry points. | ||
| """ | ||
|
|
||
| import sys | ||
|
|
||
| import pyexpat | ||
|
|
||
| # b'\xff' has no UTF-8 spelling in any position, so surrogateescape maps it to | ||
| # U+DCFF — what a filesystem name or an argv element yields on a host that does | ||
| # not enforce UTF-8. | ||
| SURR = b"bad\xffname".decode("utf-8", "surrogateescape") | ||
| ENCODE_ERROR = ( | ||
| "'utf-8' codec can't encode character '\\udcff' in position 3: " | ||
| "surrogates not allowed" | ||
| ) | ||
|
|
||
| ERRORS = [] | ||
|
|
||
|
|
||
| def check(cond, what): | ||
| if not cond: | ||
| ERRORS.append(what) | ||
|
|
||
|
|
||
| def raises(what, exc, expected, fn): | ||
| """Assert fn() raises `exc` whose message is exactly `expected`.""" | ||
| try: | ||
| fn() | ||
| except exc as e: | ||
| check( | ||
| str(e) == expected, | ||
| f"{what}: got {str(e)!r}, expected {expected!r}", | ||
| ) | ||
| return | ||
| except BaseException as e: | ||
| ERRORS.append( | ||
| f"{what}: raised {type(e).__name__}({e!r}), expected {exc.__name__}" | ||
| ) | ||
| return | ||
| ERRORS.append(f"{what}: no exception, expected {exc.__name__}") | ||
|
|
||
|
|
||
| # The payload is the point of the test, so verify it before using it. | ||
| check(len(SURR) == 8, f"surrogate payload is {len(SURR)} code points, expected 8") | ||
| check(SURR.encode("utf-8", "surrogateescape") == b"bad\xffname", "payload lost its byte") | ||
|
|
||
|
|
||
| class Odd: | ||
| pass | ||
|
|
||
|
|
||
| # ── sys.audit: the event name is a str that must encode ─────────────────── | ||
| raises( | ||
| "audit with no event", | ||
| TypeError, | ||
| "audit expected at least 1 argument, got 0", | ||
| lambda: sys.audit(), | ||
| ) | ||
| for value, spelling in ( | ||
| (123, "int"), | ||
| (1.5, "float"), | ||
| (None, "None"), | ||
| (b"x", "bytes"), | ||
| (bytearray(b"x"), "bytearray"), | ||
| ((), "tuple"), | ||
| (Odd(), "Odd"), | ||
| ): | ||
| raises( | ||
| f"audit({spelling})", | ||
| TypeError, | ||
| f"audit() argument 1 must be str, not {spelling}", | ||
| lambda value=value: sys.audit(value), | ||
| ) | ||
| raises( | ||
| "audit with a surrogate event", | ||
| UnicodeEncodeError, | ||
| ENCODE_ERROR, | ||
| lambda: sys.audit(SURR), | ||
| ) | ||
| # Every parameter is positional-only, so the event cannot be named. | ||
| raises( | ||
| "audit by keyword", | ||
| TypeError, | ||
| "sys.audit() takes no keyword arguments", | ||
| lambda: sys.audit(event="x"), | ||
| ) | ||
| # ...and the accepting calls still accept, so the checks above are not passing | ||
| # because audit stopped working. | ||
| check(sys.audit("pyre.test") is None, "audit rejected a plain event name") | ||
| check(sys.audit("pyre.test", 1, 2) is None, "audit rejected trailing parameters") | ||
|
|
||
|
|
||
| class SubStr(str): | ||
| pass | ||
|
|
||
|
|
||
| check(sys.audit(SubStr("pyre.test")) is None, "audit rejected a str subclass") | ||
|
|
||
|
|
||
| # ── pyexpat.ParserCreate: both str-or-None parameters report their type ──── | ||
| for value, spelling in ((123, "int"), (1.5, "float"), (b"x", "bytes")): | ||
| raises( | ||
| f"ParserCreate(encoding={spelling})", | ||
| TypeError, | ||
| f"ParserCreate() argument 'encoding' must be str or None, not {spelling}", | ||
| lambda value=value: pyexpat.ParserCreate(value), | ||
| ) | ||
| raises( | ||
| f"ParserCreate(namespace_separator={spelling})", | ||
| TypeError, | ||
| f"ParserCreate() argument 'namespace_separator' must be str or None, " | ||
| f"not {spelling}", | ||
| lambda value=value: pyexpat.ParserCreate(None, value), | ||
| ) | ||
|
|
||
| raises( | ||
| "ParserCreate with a surrogate encoding", | ||
| UnicodeEncodeError, | ||
| ENCODE_ERROR, | ||
| lambda: pyexpat.ParserCreate(SURR), | ||
| ) | ||
| raises( | ||
| "ParserCreate with a surrogate separator", | ||
| UnicodeEncodeError, | ||
| ENCODE_ERROR, | ||
| lambda: pyexpat.ParserCreate(None, SURR), | ||
| ) | ||
| # The encoding check runs before the separator's length check, so a too-long | ||
| # separator carrying a surrogate reports the encoding, not the length. | ||
| raises( | ||
| "ParserCreate with a long surrogate separator", | ||
| UnicodeEncodeError, | ||
| ENCODE_ERROR, | ||
| lambda: pyexpat.ParserCreate(None, SURR + "x"), | ||
| ) | ||
| raises( | ||
| "ParserCreate with a two-character separator", | ||
| ValueError, | ||
| "namespace_separator must be at most one character, omitted, or None", | ||
| lambda: pyexpat.ParserCreate(None, "ab"), | ||
| ) | ||
|
|
||
| # The accepting calls, so the checks above are not passing because | ||
| # ParserCreate stopped building parsers. | ||
| check( | ||
| type(pyexpat.ParserCreate()).__name__ == "xmlparser", | ||
| "ParserCreate() no longer builds a parser", | ||
| ) | ||
| check( | ||
| type(pyexpat.ParserCreate("utf-8")).__name__ == "xmlparser", | ||
| "ParserCreate('utf-8') no longer builds a parser", | ||
| ) | ||
| check( | ||
| type(pyexpat.ParserCreate(encoding="utf-8")).__name__ == "xmlparser", | ||
| "ParserCreate(encoding=) no longer binds", | ||
| ) | ||
| check( | ||
| type(pyexpat.ParserCreate(None, ":")).__name__ == "xmlparser", | ||
| "ParserCreate with a separator no longer builds a parser", | ||
| ) | ||
| check( | ||
| type(pyexpat.ParserCreate(SubStr("utf-8"))).__name__ == "xmlparser", | ||
| "ParserCreate rejected a str subclass encoding", | ||
| ) | ||
|
|
||
|
|
||
| # ── an omitted `intern` is not the same as an explicit None ─────────────── | ||
| # Omitting it asks for a fresh dictionary; passing None asks for no interning | ||
| # at all. A default of `None` cannot tell the two apart, and the parser then | ||
| # interns names the caller asked it not to. | ||
| def interned(parser): | ||
| """The number of names `parser` interned while parsing, or None.""" | ||
| parser.StartElementHandler = lambda name, attrs: None | ||
| parser.Parse("<a><b/></a>", True) | ||
| return None if parser.intern is None else len(parser.intern) | ||
|
|
||
|
|
||
| check( | ||
| isinstance(pyexpat.ParserCreate().intern, dict), | ||
| "an omitted intern did not produce a dictionary", | ||
| ) | ||
| check(interned(pyexpat.ParserCreate()) == 2, "an omitted intern interned nothing") | ||
| check( | ||
| pyexpat.ParserCreate(None, None, None).intern is None, | ||
| "an explicit intern=None was replaced with a dictionary", | ||
| ) | ||
| check( | ||
| interned(pyexpat.ParserCreate(None, None, None)) is None, | ||
| "an explicit intern=None still interned names", | ||
| ) | ||
| # A dictionary of the caller's own is used as given. | ||
| own = {} | ||
| p = pyexpat.ParserCreate(None, None, own) | ||
| check(p.intern is own, "a supplied intern dictionary was not the one used") | ||
| check(interned(p) == 2 and len(own) == 2, "the supplied dictionary was not filled") | ||
|
|
||
| # ── ctypes.CDLL: the library name reaches dlopen in filesystem units ────── | ||
| # Filed alongside the two above as a third disagreement, and already correct: | ||
| # `_ctypes.dlopen` takes the name through `fsencode`, so a surrogate escape | ||
| # folds back to the byte it stands for and dlopen is the thing that fails. | ||
| # Pinned because the earlier reading came from a binary built before that | ||
| # routing landed. A surrogate that is *not* an escape (U+D800 is outside | ||
| # U+DC80..U+DCFF) has no byte to fold to, so it is refused before the call — | ||
| # which is what the second row separates from the first. | ||
| try: | ||
| import ctypes | ||
| except ImportError: | ||
| ctypes = None | ||
| if ctypes is not None: | ||
|
|
||
| def raises_class(what, exc, fn): | ||
| """Assert fn() raises `exc`; the message is the host's dlopen text.""" | ||
| try: | ||
| fn() | ||
| except exc: | ||
| return | ||
| except BaseException as e: | ||
| ERRORS.append( | ||
| f"{what}: raised {type(e).__name__}, expected {exc.__name__}" | ||
| ) | ||
| return | ||
| ERRORS.append(f"{what}: no exception, expected {exc.__name__}") | ||
|
|
||
| raises_class( | ||
| "CDLL with a surrogate-escaped name", | ||
| OSError, | ||
| lambda: ctypes.CDLL(SURR), | ||
| ) | ||
| raises_class( | ||
| "CDLL with a non-escape surrogate", | ||
| UnicodeEncodeError, | ||
| lambda: ctypes.CDLL("bad\ud800name"), | ||
| ) | ||
| raises_class( | ||
| "CDLL with a missing library", | ||
| OSError, | ||
| lambda: ctypes.CDLL("nosuchlib_pyre_parity_test"), | ||
| ) | ||
|
|
||
| if ERRORS: | ||
| for e in ERRORS: | ||
| sys.stderr.write(f"FAIL: {e}\n".encode("utf-8", "backslashreplace").decode()) | ||
| raise AssertionError(f"{len(ERRORS)} divergence(s)") | ||
|
|
||
| print("OK") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Fix the comment sentence.
The sentence is incomplete. Line 111 uses the intended form. Align this one with it.
📝 Proposed wording fix
📝 Committable suggestion
🤖 Prompt for AI Agents