| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -159,6 +159,15 @@ def test_basic(self): | |||
| 159 | 159 | self.assertEqual(TA.__type_params__, ()) | |
| 160 | 160 | self.assertEqual(TA.__module__, __name__) | |
| 161 | 161 | ||
| 162 | + def test_attributes_with_exec(self): | ||
| 163 | + ns = {} | ||
| 164 | + exec("type TA = int", ns, ns) | ||
| 165 | + TA = ns["TA"] | ||
| 166 | + self.assertEqual(TA.__name__, "TA") | ||
| 167 | + self.assertIs(TA.__value__, int) | ||
| 168 | + self.assertEqual(TA.__type_params__, ()) | ||
| 169 | + self.assertIs(TA.__module__, None) | ||
| 170 | + | ||
| 162 | 171 | def test_generic(self): | |
| 163 | 172 | T = TypeVar("T") | |
| 164 | 173 | TA = TypeAliasType("TA", list[T], type_params=(T,)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -373,6 +373,20 @@ def test_basic_plain(self): | |||
| 373 | 373 | self.assertIs(T.__covariant__, False) | |
| 374 | 374 | self.assertIs(T.__contravariant__, False) | |
| 375 | 375 | self.assertIs(T.__infer_variance__, False) | |
| 376 | + self.assertEqual(T.__module__, __name__) | ||
| 377 | + | ||
| 378 | + def test_basic_with_exec(self): | ||
| 379 | + ns = {} | ||
| 380 | + exec('from typing import TypeVar; T = TypeVar("T", bound=float)', ns, ns) | ||
| 381 | + T = ns['T'] | ||
| 382 | + self.assertIsInstance(T, TypeVar) | ||
| 383 | + self.assertEqual(T.__name__, 'T') | ||
| 384 | + self.assertEqual(T.__constraints__, ()) | ||
| 385 | + self.assertIs(T.__bound__, float) | ||
| 386 | + self.assertIs(T.__covariant__, False) | ||
| 387 | + self.assertIs(T.__contravariant__, False) | ||
| 388 | + self.assertIs(T.__infer_variance__, False) | ||
| 389 | + self.assertIs(T.__module__, None) | ||
| 376 | 390 | ||
| 377 | 391 | def test_attributes(self): | |
| 378 | 392 | T_bound = TypeVar('T_bound', bound=int) | |
@@ -939,6 +953,17 @@ def test_name(self): | |||
| 939 | 953 | Ts2 = TypeVarTuple('Ts2') | |
| 940 | 954 | self.assertEqual(Ts2.__name__, 'Ts2') | |
| 941 | 955 | ||
| 956 | + def test_module(self): | ||
| 957 | + Ts = TypeVarTuple('Ts') | ||
| 958 | + self.assertEqual(Ts.__module__, __name__) | ||
| 959 | + | ||
| 960 | + def test_exec(self): | ||
| 961 | + ns = {} | ||
| 962 | + exec('from typing import TypeVarTuple; Ts = TypeVarTuple("Ts")', ns) | ||
| 963 | + Ts = ns['Ts'] | ||
| 964 | + self.assertEqual(Ts.__name__, 'Ts') | ||
| 965 | + self.assertIs(Ts.__module__, None) | ||
| 966 | + | ||
| 942 | 967 | def test_instance_is_equal_to_itself(self): | |
| 943 | 968 | Ts = TypeVarTuple('Ts') | |
| 944 | 969 | self.assertEqual(Ts, Ts) | |
@@ -7985,6 +8010,15 @@ def test_basic_plain(self): | |||
| 7985 | 8010 | self.assertEqual(P, P) | |
| 7986 | 8011 | self.assertIsInstance(P, ParamSpec) | |
| 7987 | 8012 | self.assertEqual(P.__name__, 'P') | |
| 8013 | + self.assertEqual(P.__module__, __name__) | ||
| 8014 | + | ||
| 8015 | + def test_basic_with_exec(self): | ||
| 8016 | + ns = {} | ||
| 8017 | + exec('from typing import ParamSpec; P = ParamSpec("P")', ns, ns) | ||
| 8018 | + P = ns['P'] | ||
| 8019 | + self.assertIsInstance(P, ParamSpec) | ||
| 8020 | + self.assertEqual(P.__name__, 'P') | ||
| 8021 | + self.assertIs(P.__module__, None) | ||
| 7988 | 8022 | ||
| 7989 | 8023 | def test_valid_uses(self): | |
| 7990 | 8024 | P = ParamSpec('P') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Fix crash when accessing the ``__module__`` attribute of type aliases | ||
| 2 | + defined outside a module. Patch by Jelle Zijlstra. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1319,8 +1319,13 @@ typealias_module(PyObject *self, void *unused) | |||
| 1319 | 1319 | return Py_NewRef(ta->module); | |
| 1320 | 1320 | } | |
| 1321 | 1321 | if (ta->compute_value != NULL) { | |
| 1322 | - // PyFunction_GetModule() returns a borrowed reference | ||
| 1323 | - return Py_NewRef(PyFunction_GetModule(ta->compute_value)); | ||
| 1322 | + PyObject* mod = PyFunction_GetModule(ta->compute_value); | ||
| 1323 | + if (mod != NULL) { | ||
| 1324 | + // PyFunction_GetModule() returns a borrowed reference, | ||
| 1325 | + // and it may return NULL (e.g., for functions defined | ||
| 1326 | + // in an exec()'ed block). | ||
| 1327 | + return Py_NewRef(mod); | ||
| 1328 | + } | ||
| 1324 | 1329 | } | |
| 1325 | 1330 | Py_RETURN_NONE; | |
| 1326 | 1331 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments