-
Notifications
You must be signed in to change notification settings - Fork 19
objspace, typedef, frame, dict, imp: a user __getattribute__ on any layout, object's remaining text signatures, the collection frame.clear() forced, popitem on the strategy, and create_builtin's pre-filled import metadata
#1094
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
12 commits
Select commit
Hold shift + click to select a range
08720b0
objspace: dispatch a user __getattribute__ on any receiver layout
youknowone 440bc0f
typedef: give object's remaining 19 callables their __text_signature__
youknowone 4abc38d
frame: stop `clear()` forcing an old-generation collection
youknowone 11c02f0
dict: route popitem through the strategy instead of items().last()
youknowone 2f5be4c
imp: stop create_builtin pre-filling __loader__ and __spec__
youknowone a375040
dict: finish the two popitem strategy arms
youknowone 8b9e007
pyrex: die by SIGINT on an uncaught KeyboardInterrupt
youknowone 08691e3
imp: add _imp._override_multi_interp_extensions_check
youknowone bc59c51
cpython_tests: give test_runpy its dotted identity, record three modu…
youknowone b4b2bac
address review: root popitem's results, trace w_dict_popitem, widen t…
youknowone 8e1d30f
compile: name the unlexable character in the SyntaxError message
youknowone 4af5443
address review: assert __exit__ receives the stored traceback, not th…
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
33 changes: 33 additions & 0 deletions
33
pyre/extra_tests/parity_tests/builtin_module_loader_spec.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,33 @@ | ||
| import _imp | ||
| import sys | ||
| from test.support import import_helper | ||
|
|
||
|
|
||
| name = "errno" | ||
| sys.modules.pop(name, None) | ||
|
|
||
| spec = type("Spec", (), {"name": name})() | ||
| module = _imp.create_builtin(spec) | ||
| assert module.__loader__ is None | ||
| assert module.__spec__ is None | ||
|
|
||
| sys.modules.pop(name, None) | ||
| bootstrap = import_helper.import_fresh_module( | ||
| "importlib._bootstrap", | ||
| fresh=("importlib",), | ||
| blocked=("_frozen_importlib", "_frozen_importlib_external"), | ||
| ) | ||
| loader = bootstrap.BuiltinImporter | ||
| module = loader.load_module(name) | ||
| assert module.__loader__ is loader | ||
| assert module.__spec__.loader is loader | ||
|
|
||
| import errno | ||
|
|
||
| assert errno.__loader__ is errno.__spec__.loader | ||
| assert errno.__loader__ is loader | ||
| assert errno.__name__ == name | ||
| assert errno.__package__ == "" | ||
| assert sys.modules[name] is errno | ||
|
|
||
| print("OK") |
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,97 @@ | ||
| import types | ||
|
|
||
|
|
||
| def assert_empty_popitem(d): | ||
| try: | ||
| d.popitem() | ||
| except KeyError as exc: | ||
| assert exc.args == ("popitem(): dictionary is empty",) | ||
| else: | ||
| raise AssertionError("popitem on empty dict did not raise") | ||
|
|
||
|
|
||
| def assert_lifo(label, d, expected): | ||
| out = [] | ||
| while d: | ||
| out.append(d.popitem()) | ||
| assert out == expected, (label, out, expected) | ||
| assert_empty_popitem(d) | ||
|
|
||
|
|
||
| def make_kwargs(**kwargs): | ||
| return kwargs | ||
|
|
||
|
|
||
| class DictSubclass(dict): | ||
| pass | ||
|
|
||
|
|
||
| class Carrier: | ||
| pass | ||
|
|
||
|
|
||
| assert_empty_popitem({}) | ||
|
|
||
| single = {"only": 1} | ||
| assert single.popitem() == ("only", 1) | ||
| assert single == {} | ||
|
|
||
| reused = {1: "a", 2: "b"} | ||
| assert reused.popitem() == (2, "b") | ||
| reused[3] = "c" | ||
| assert reused.popitem() == (3, "c") | ||
| assert reused.popitem() == (1, "a") | ||
| assert_empty_popitem(reused) | ||
|
|
||
| assert_lifo("int", {1: "a", 2: "b", 3: "c"}, [(3, "c"), (2, "b"), (1, "a")]) | ||
| assert_lifo("str", {"a": 1, "b": 2, "c": 3}, [("c", 3), ("b", 2), ("a", 1)]) | ||
| # A bytes-only dict reaches its own strategy arm, which rebuilds the key from | ||
| # the stored bytes rather than handing back a stored object. | ||
| assert_lifo("bytes", {b"a": 1, b"b": 2, b"c": 3}, [(b"c", 3), (b"b", 2), (b"a", 1)]) | ||
| assert_lifo("object", {1: "a", "b": 2, (3,): "c"}, [((3,), "c"), ("b", 2), (1, "a")]) | ||
|
|
||
| kw = make_kwargs(a=1, b=2, c=3) | ||
| assert_lifo("kwargs", kw, [("c", 3), ("b", 2), ("a", 1)]) | ||
|
|
||
| module_ns = globals() | ||
| module_ns["popitem_strategy_mod_a"] = 1 | ||
| module_ns["popitem_strategy_mod_b"] = 2 | ||
| assert module_ns.popitem() == ("popitem_strategy_mod_b", 2) | ||
| assert module_ns.popitem() == ("popitem_strategy_mod_a", 1) | ||
|
|
||
| obj = Carrier() | ||
| obj.first = 1 | ||
| obj.second = 2 | ||
| surrogate = "\ud800" | ||
| setattr(obj, surrogate, 3) | ||
| assert obj.__dict__.popitem() == (surrogate, 3) | ||
| assert obj.__dict__.popitem() == ("second", 2) | ||
| assert obj.__dict__.popitem() == ("first", 1) | ||
| assert_empty_popitem(obj.__dict__) | ||
|
|
||
| sub = DictSubclass() | ||
| sub["x"] = 1 | ||
| sub["y"] = 2 | ||
| assert_lifo("subclass", sub, [("y", 2), ("x", 1)]) | ||
|
|
||
| # A non-str key moves a module dict off cell storage; popitem has a separate | ||
| # arm for that storage half, and a reader of a popped global must stop seeing it. | ||
| switched = types.ModuleType("popitem_strategy_switched") | ||
| exec("def read_g():\n return g\n", switched.__dict__) | ||
| read_g = switched.read_g | ||
| switched.__dict__[42] = "not a str key" | ||
| switched.__dict__["g"] = "before" | ||
| assert read_g() == "before" | ||
| assert switched.__dict__.popitem() == ("g", "before") | ||
| try: | ||
| read_g() | ||
| except NameError: | ||
| pass | ||
| else: | ||
| raise AssertionError("read_g must not see the popped global") | ||
| switched.__dict__["g"] = "after" | ||
| assert read_g() == "after" | ||
| assert switched.__dict__.popitem() == ("g", "after") | ||
| assert switched.__dict__.popitem() == (42, "not a str key") | ||
|
|
||
| print("OK") | ||
130 changes: 130 additions & 0 deletions
130
pyre/extra_tests/parity_tests/exception_getattribute_override.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,130 @@ | ||
| SENTINEL = object() | ||
|
|
||
|
|
||
| class E(Exception): | ||
| calls = 0 | ||
| getattr_calls = 0 | ||
|
|
||
| def __getattribute__(self, name): | ||
| type(self).calls += 1 | ||
| if name == "marker": | ||
| return SENTINEL | ||
| if name == "args": | ||
| return ("overridden-args",) | ||
| if name == "__traceback__": | ||
| return "overridden-traceback" | ||
| return super().__getattribute__(name) | ||
|
|
||
| def __getattr__(self, name): | ||
| type(self).getattr_calls += 1 | ||
| if name == "fallback": | ||
| return "fallback-value" | ||
| raise AttributeError(name) | ||
|
|
||
|
|
||
| e = E("real-args") | ||
| start = E.calls | ||
| assert e.marker is SENTINEL | ||
| assert E.calls == start + 1 | ||
|
|
||
| start = E.calls | ||
| for _ in range(4000): | ||
| assert e.marker is SENTINEL | ||
| assert E.calls == start + 4000 | ||
|
|
||
| assert e.args == ("overridden-args",) | ||
| assert e.__traceback__ == "overridden-traceback" | ||
| assert e.fallback == "fallback-value" | ||
| assert E.getattr_calls == 1 | ||
| before = E.getattr_calls | ||
| assert e.marker is SENTINEL | ||
| assert E.getattr_calls == before | ||
|
|
||
|
|
||
| class L(list): | ||
| calls = 0 | ||
|
|
||
| def __getattribute__(self, name): | ||
| type(self).calls += 1 | ||
| if name == "marker": | ||
| return SENTINEL | ||
| return super().__getattribute__(name) | ||
|
|
||
|
|
||
| list_subclass = L([1, 2, 3]) | ||
| assert list_subclass.marker is SENTINEL | ||
| assert L.calls == 1 | ||
|
|
||
| try: | ||
| raise E("raised") | ||
| except E as raised: | ||
| assert raised.marker is SENTINEL | ||
| assert raised.args == ("overridden-args",) | ||
| assert raised.__traceback__ == "overridden-traceback" | ||
|
|
||
|
|
||
| # `__exit__` is handed the traceback the interpreter stored, not whatever the | ||
| # override answers for `__traceback__` — the unwinder reads the slot directly. | ||
| class Recorder: | ||
| seen = None | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc, tb): | ||
| type(self).seen = (exc_type, exc, tb) | ||
| return True | ||
|
|
||
|
|
||
| with Recorder() as recorder: | ||
| raise E("in-with") | ||
| seen_type, seen_exc, seen_tb = Recorder.seen | ||
| assert seen_type is E | ||
| assert isinstance(seen_exc, E) | ||
| assert seen_tb is not None | ||
| assert seen_tb != "overridden-traceback" | ||
| assert type(seen_tb).__name__ == "traceback" | ||
| # The override is still what attribute access answers on the same object. | ||
| assert seen_exc.__traceback__ == "overridden-traceback" | ||
|
|
||
|
|
||
| # Receivers whose builtin type carries a `__getattribute__` of its own read | ||
| # their attributes unchanged: super proxies, bound methods, unions, generic | ||
| # aliases, weak proxies and struct objects. | ||
| class Q: | ||
| attr = "q-attr" | ||
|
|
||
| def f(self): | ||
| return "q-f" | ||
|
|
||
|
|
||
| q = Q() | ||
| bound = q.f | ||
| assert bound.__func__ is Q.f | ||
| assert bound.__self__ is q | ||
| assert bound() == "q-f" | ||
|
|
||
| assert (int | str).__args__ == (int, str) | ||
| assert list[int].__origin__ is list | ||
|
|
||
| import struct | ||
| import weakref | ||
|
|
||
| assert struct.Struct("i").size == struct.calcsize("i") | ||
| proxy = weakref.proxy(q) | ||
| assert proxy.attr == "q-attr" | ||
|
|
||
|
|
||
| class Base: | ||
| def who(self): | ||
| return "base" | ||
|
|
||
|
|
||
| class Derived(Base): | ||
| def who(self): | ||
| return "derived:" + super().who() | ||
|
|
||
|
|
||
| assert Derived().who() == "derived:base" | ||
|
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.