Skip to content
Merged
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
8 changes: 7 additions & 1 deletion Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ def do(self) -> None:
r = self.reader
text = self.event * r.get_arg()
r.insert(text)
if r.paste_mode:
data = ""
ev = r.console.getpending()
data += ev.data
if data:
r.insert(data)
r.last_refresh_cache.invalidated = True


class insert_nl(EditCommand):
Expand Down Expand Up @@ -484,7 +491,6 @@ def do(self) -> None:
data = ""
start = time.time()
while done not in data:
self.reader.console.wait(100)
ev = self.reader.console.getpending()
data += ev.data
trace(
Expand Down
26 changes: 15 additions & 11 deletions Lib/_pyrepl/windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,7 @@ def _getscrollbacksize(self) -> int:

return info.srWindow.Bottom # type: ignore[no-any-return]

def _read_input(self, block: bool = True) -> INPUT_RECORD | None:
if not block and not self.wait(timeout=0):
return None

def _read_input(self) -> INPUT_RECORD | None:
rec = INPUT_RECORD()
read = DWORD()
if not ReadConsoleInput(InHandle, rec, 1, read):
Expand All @@ -431,14 +428,10 @@ def _read_input(self, block: bool = True) -> INPUT_RECORD | None:
return rec

def _read_input_bulk(
self, block: bool, n: int
self, n: int
) -> tuple[ctypes.Array[INPUT_RECORD], int]:
rec = (n * INPUT_RECORD)()
read = DWORD()

if not block and not self.wait(timeout=0):
return rec, 0

if not ReadConsoleInput(InHandle, rec, n, read):
raise WinError(GetLastError())

Expand All @@ -449,8 +442,11 @@ def get_event(self, block: bool = True) -> Event | None:
and there is no event pending, otherwise waits for the
completion of an event."""

if not block and not self.wait(timeout=0):
return None

while self.event_queue.empty():
rec = self._read_input(block)
rec = self._read_input()
if rec is None:
return None

Expand Down Expand Up @@ -551,12 +547,20 @@ def getpending(self) -> Event:
if e2:
e.data += e2.data

recs, rec_count = self._read_input_bulk(False, 1024)
recs, rec_count = self._read_input_bulk(1024)
for i in range(rec_count):
rec = recs[i]
# In case of a legacy console, we do not only receive a keydown
# event, but also a keyup event - and for uppercase letters
# an additional SHIFT_PRESSED event.
if rec and rec.EventType == KEY_EVENT:
key_event = rec.Event.KeyEvent
if not key_event.bKeyDown:
continue
ch = key_event.uChar.UnicodeChar
if ch == "\x00":
# ignore SHIFT_PRESSED and special keys
continue
if ch == "\r":
ch += "\n"
e.data += ch
Expand Down
19 changes: 16 additions & 3 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,27 @@ def _get_and_call_annotate(obj, format):
return None


_BASE_GET_ANNOTATIONS = type.__dict__["__annotations__"].__get__


def _get_dunder_annotations(obj):
"""Return the annotations for an object, checking that it is a dictionary.

Does not return a fresh dictionary.
"""
ann = getattr(obj, "__annotations__", None)
if ann is None:
return None
# This special case is needed to support types defined under
# from __future__ import annotations, where accessing the __annotations__
# attribute directly might return annotations for the wrong class.
if isinstance(obj, type):
try:
ann = _BASE_GET_ANNOTATIONS(obj)
except AttributeError:
# For static types, the descriptor raises AttributeError.
return None
else:
ann = getattr(obj, "__annotations__", None)
if ann is None:
return None

if not isinstance(ann, dict):
raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None")
Expand Down
66 changes: 65 additions & 1 deletion Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import functools
import itertools
import pickle
from string.templatelib import Interpolation, Template
from string.templatelib import Template
import typing
import unittest
from annotationlib import (
Expand Down Expand Up @@ -815,6 +815,70 @@ def test_stringized_annotations_on_class(self):
{"x": int},
)

def test_stringized_annotation_permutations(self):
def define_class(name, has_future, has_annos, base_text, extra_names=None):
lines = []
if has_future:
lines.append("from __future__ import annotations")
lines.append(f"class {name}({base_text}):")
if has_annos:
lines.append(f" {name}_attr: int")
else:
lines.append(" pass")
code = "\n".join(lines)
ns = support.run_code(code, extra_names=extra_names)
return ns[name]

def check_annotations(cls, has_future, has_annos):
if has_annos:
if has_future:
anno = "int"
else:
anno = int
self.assertEqual(get_annotations(cls), {f"{cls.__name__}_attr": anno})
else:
self.assertEqual(get_annotations(cls), {})

for meta_future, base_future, child_future, meta_has_annos, base_has_annos, child_has_annos in itertools.product(
(False, True),
(False, True),
(False, True),
(False, True),
(False, True),
(False, True),
):
with self.subTest(
meta_future=meta_future,
base_future=base_future,
child_future=child_future,
meta_has_annos=meta_has_annos,
base_has_annos=base_has_annos,
child_has_annos=child_has_annos,
):
meta = define_class(
"Meta",
has_future=meta_future,
has_annos=meta_has_annos,
base_text="type",
)
base = define_class(
"Base",
has_future=base_future,
has_annos=base_has_annos,
base_text="metaclass=Meta",
extra_names={"Meta": meta},
)
child = define_class(
"Child",
has_future=child_future,
has_annos=child_has_annos,
base_text="Base",
extra_names={"Base": base},
)
check_annotations(meta, meta_future, meta_has_annos)
check_annotations(base, base_future, base_has_annos)
check_annotations(child, child_future, child_has_annos)

def test_modify_annotations(self):
def f(x: int):
pass
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
def unix_console(events, **kwargs):
console = UnixConsole()
console.get_event = MagicMock(side_effect=events)
console.getpending = MagicMock(return_value=Event("key", ""))

height = kwargs.get("height", 25)
width = kwargs.get("width", 80)
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_pyrepl/test_windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WindowsConsoleTests(TestCase):
def console(self, events, **kwargs) -> Console:
console = WindowsConsole()
console.get_event = MagicMock(side_effect=events)
console.getpending = MagicMock(return_value=Event("key", ""))
console.wait = MagicMock()
console._scroll = MagicMock()
console._hide_cursor = MagicMock()
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,28 @@ def f(x: int) -> int: pass
self.assertEqual(f.__annotate__(annotationlib.Format.VALUE), annos)
self.assertEqual(f.__annotations__, annos)

def test_set_annotations(self):
function_code = textwrap.dedent("""
def f(x: int):
pass
""")
class_code = textwrap.dedent("""
class f:
x: int
""")
for future in (False, True):
for label, code in (("function", function_code), ("class", class_code)):
with self.subTest(future=future, label=label):
if future:
code = "from __future__ import annotations\n" + code
ns = run_code(code)
f = ns["f"]
anno = "int" if future else int
self.assertEqual(f.__annotations__, {"x": anno})

f.__annotations__ = {"x": str}
self.assertEqual(f.__annotations__, {"x": str})

def test_name_clash_with_format(self):
# this test would fail if __annotate__'s parameter was called "format"
# during symbol table construction
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where assigning to the :attr:`~type.__annotations__` attributes of
classes defined under ``from __future__ import annotations`` had no effect.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speedup pasting in ``PyREPL`` on Windows in a legacy console. Patch by Chris
Eibl.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug where :func:`annotationlib.get_annotations` would return the wrong
result for certain classes that are part of a class hierarchy where ``from
__future__ import annotations`` is used.
45 changes: 36 additions & 9 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2065,19 +2065,46 @@ type_set_annotations(PyObject *tp, PyObject *value, void *Py_UNUSED(closure))
return -1;
}

int result;
PyObject *dict = PyType_GetDict(type);
if (value != NULL) {
/* set */
result = PyDict_SetItem(dict, &_Py_ID(__annotations_cache__), value);
} else {
/* delete */
result = PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL);
if (result == 0) {
PyErr_SetString(PyExc_AttributeError, "__annotations__");
int result = PyDict_ContainsString(dict, "__annotations__");
if (result < 0) {
Py_DECREF(dict);
return -1;
}
if (result) {
// If __annotations__ is currently in the dict, we update it,
if (value != NULL) {
result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
} else {
result = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
if (result == 0) {
// Somebody else just deleted it?
PyErr_SetString(PyExc_AttributeError, "__annotations__");
Py_DECREF(dict);
return -1;
}
}
if (result < 0) {
Py_DECREF(dict);
return -1;
}
// Also clear __annotations_cache__ just in case.
result = PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL);
}
else {
// Else we update only __annotations_cache__.
if (value != NULL) {
/* set */
result = PyDict_SetItem(dict, &_Py_ID(__annotations_cache__), value);
} else {
/* delete */
result = PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL);
if (result == 0) {
PyErr_SetString(PyExc_AttributeError, "__annotations__");
Py_DECREF(dict);
return -1;
}
}
}
if (result < 0) {
Py_DECREF(dict);
Expand Down
Loading