pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/pull/124415.patch

{"a": fwd}, ) - self.assertEqual(annotationlib.get_annotations(f2, format=2), {"a": fwd}) + self.assertEqual(annotationlib.get_annotations(f2, format=3), {"a": fwd}) self.assertEqual( annotationlib.get_annotations(f1, format=annotationlib.Format.SOURCE), {"a": "int"}, ) - self.assertEqual(annotationlib.get_annotations(f1, format=3), {"a": "int"}) - - with self.assertRaises(ValueError): - annotationlib.get_annotations(f1, format=0) + self.assertEqual(annotationlib.get_annotations(f1, format=4), {"a": "int"}) with self.assertRaises(ValueError): annotationlib.get_annotations(f1, format=42) @@ -394,7 +394,7 @@ def f2(a: undefined): ValueError, r"The VALUE_WITH_FAKE_GLOBALS format is for internal use only", ): - annotationlib.get_annotations(f1, format=4) + annotationlib.get_annotations(f1, format=2) def test_custom_object_with_annotations(self): class C: @@ -852,7 +852,7 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self): class TestCallEvaluateFunction(unittest.TestCase): def test_evaluation(self): def evaluate(format, exc=NotImplementedError): - if format != 1 and format != 4: + if format > 2: raise exc return undefined diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py index 91082e6b23c04b..da87d88163e568 100644 --- a/Lib/test/test_type_annotations.py +++ b/Lib/test/test_type_annotations.py @@ -316,7 +316,7 @@ def test_module(self): ns = run_code("x: undefined = 1") anno = ns["__annotate__"] with self.assertRaises(NotImplementedError): - anno(2) + anno(3) with self.assertRaises(NameError): anno(1) @@ -376,7 +376,7 @@ class X: annotate(annotationlib.Format.FORWARDREF) with self.assertRaises(NotImplementedError): annotate(annotationlib.Format.SOURCE) - with self.assertRaises(NotImplementedError): + with self.assertRaises(TypeError): annotate(None) self.assertEqual(annotate(annotationlib.Format.VALUE), {"x": int}) diff --git a/Python/codegen.c b/Python/codegen.c index bf29995b9d2121..0fb599c145fc87 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -655,21 +655,15 @@ codegen_setup_annotations_scope(compiler *c, location loc, codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS, key, loc.lineno, NULL, &umd)); - // if .format != 1 and .format != 4: raise NotImplementedError + // if .format > 2: raise NotImplementedError _Py_DECLARE_STR(format, ".format"); + PyObject *two = PyLong_FromLong(2); ADDOP_I(c, loc, LOAD_FAST, 0); - ADDOP_LOAD_CONST(c, loc, _PyLong_GetOne()); - ADDOP_I(c, loc, COMPARE_OP, (Py_NE << 5) | compare_masks[Py_NE]); + ADDOP_LOAD_CONST(c, loc, two); + ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]); NEW_JUMP_TARGET_LABEL(c, body); ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body); - ADDOP_I(c, loc, LOAD_FAST, 0); - PyObject *four = PyLong_FromLong(4); - assert(four != NULL); - ADDOP_LOAD_CONST(c, loc, four); - ADDOP_I(c, loc, COMPARE_OP, (Py_NE << 5) | compare_masks[Py_NE]); - ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body); - ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_NOTIMPLEMENTEDERROR); ADDOP_I(c, loc, RAISE_VARARGS, 1); USE_LABEL(c, body); From 3ba132a9ae3e945ffb1826d72475f67132178c0c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 25 Sep 2024 12:52:57 -0700 Subject: [PATCH 3/6] fix constevaluator --- Objects/typevarobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index d3656155fae330..9d61d943c69e9d 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -168,7 +168,7 @@ constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs) return NULL; } PyObject *value = ((constevaluatorobject *)self)->value; - if (format == 3) { // SOURCE + if (format == 4) { // SOURCE PyUnicodeWriter *writer = PyUnicodeWriter_Create(5); // cannot be <5 if (writer == NULL) { return NULL; From 2012bb477d017ce3bab648da14b960c57ab956e0 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 25 Sep 2024 17:00:55 -0700 Subject: [PATCH 4/6] fix tests --- Lib/annotationlib.py | 2 ++ Lib/test/test_annotationlib.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py index d9db44ff0d7ca5..7849a7f0b54da2 100644 --- a/Lib/annotationlib.py +++ b/Lib/annotationlib.py @@ -697,6 +697,8 @@ def get_annotations( # But if we didn't get it, we use __annotations__ instead. ann = _get_dunder_annotations(obj) return ann + case Format.VALUE_WITH_FAKE_GLOBALS: + raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only") case _: raise ValueError(f"Unsupported format {format!r}") diff --git a/Lib/test/test_annotationlib.py b/Lib/test/test_annotationlib.py index debb2289954977..1549a25a612692 100644 --- a/Lib/test/test_annotationlib.py +++ b/Lib/test/test_annotationlib.py @@ -467,6 +467,8 @@ def foo(a: int, b: str): foo.__annotations__ = {"a": "foo", "b": "str"} for format in annotationlib.Format: + if format is Format.VALUE_WITH_FAKE_GLOBALS: + continue with self.subTest(format=format): self.assertEqual( annotationlib.get_annotations(foo, format=format), @@ -778,6 +780,8 @@ def __annotations__(self): wa = WeirdAnnotations() for format in Format: + if format is Format.VALUE_WITH_FAKE_GLOBALS: + continue with ( self.subTest(format=format), self.assertRaisesRegex( From 4081f8f64a01332f34323db12678a14071cfbedf Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 29 Sep 2024 06:04:16 -0700 Subject: [PATCH 5/6] Add C enum for format --- Include/internal/pycore_object.h | 7 +++++++ Objects/typevarobject.c | 4 ++-- Python/codegen.c | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index 80b588815bc9cf..7df23911920640 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -879,6 +879,13 @@ PyAPI_DATA(int) _Py_SwappedOp[]; extern void _Py_GetConstant_Init(void); +enum _PyAnnotateFormat { + _Py_ANNOTATE_FORMAT_VALUE = 1, + _Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS = 2, + _Py_ANNOTATE_FORMAT_FORWARDREF = 3, + _Py_ANNOTATE_FORMAT_STRING = 4, +}; + #ifdef __cplusplus } #endif diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index c2691463594c44..bfb889ddccabac 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -1,6 +1,6 @@ // TypeVar, TypeVarTuple, and ParamSpec #include "Python.h" -#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK +#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK, PyAnnotateFormat #include "pycore_typevarobject.h" #include "pycore_unionobject.h" // _Py_union_type_or @@ -168,7 +168,7 @@ constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs) return NULL; } PyObject *value = ((constevaluatorobject *)self)->value; - if (format == 4) { // STRING + if (format == _Py_ANNOTATE_FORMAT_STRING) { PyUnicodeWriter *writer = PyUnicodeWriter_Create(5); // cannot be <5 if (writer == NULL) { return NULL; diff --git a/Python/codegen.c b/Python/codegen.c index 0fb599c145fc87..6cca6ef939c007 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -24,6 +24,7 @@ #include "pycore_instruction_sequence.h" // _PyInstructionSequence_NewLabel() #include "pycore_intrinsics.h" #include "pycore_long.h" // _PyLong_GetZero() +#include "pycore_object.h" // _Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS #include "pycore_pystate.h" // _Py_GetConfig() #include "pycore_symtable.h" // PySTEntryObject @@ -655,9 +656,8 @@ codegen_setup_annotations_scope(compiler *c, location loc, codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS, key, loc.lineno, NULL, &umd)); - // if .format > 2: raise NotImplementedError - _Py_DECLARE_STR(format, ".format"); - PyObject *two = PyLong_FromLong(2); + // if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError + PyObject *two = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS); ADDOP_I(c, loc, LOAD_FAST, 0); ADDOP_LOAD_CONST(c, loc, two); ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]); From 718205f02eb42043ef27430cfdda056b04353309 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 26 Nov 2024 07:16:58 -0800 Subject: [PATCH 6/6] Rename poorly named variable --- Python/codegen.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/codegen.c b/Python/codegen.c index 6e4fef58f7bd1c..a5e550cf8c947e 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -674,11 +674,11 @@ codegen_setup_annotations_scope(compiler *c, location loc, key, loc.lineno, NULL, &umd)); // if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError - PyObject *two = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS); + PyObject *value_with_fake_globals = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS); assert(!SYMTABLE_ENTRY(c)->ste_has_docstring); _Py_DECLARE_STR(format, ".format"); ADDOP_I(c, loc, LOAD_FAST, 0); - ADDOP_LOAD_CONST(c, loc, two); + ADDOP_LOAD_CONST(c, loc, value_with_fake_globals); ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]); NEW_JUMP_TARGET_LABEL(c, body); ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body); pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy