| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -519,6 +519,26 @@ def test_sys_flags(self): | |||
| 519 | 519 | self.assertTrue(repr(sys.flags)) | |
| 520 | 520 | self.assertEqual(len(sys.flags), len(attrs)) | |
| 521 | 521 | ||
| 522 | + def assert_raise_on_new_sys_type(self, sys_attr): | ||
| 523 | + # Users are intentionally prevented from creating new instances of | ||
| 524 | + # sys.flags, sys.version_info, and sys.getwindowsversion. | ||
| 525 | + attr_type = type(sys_attr) | ||
| 526 | + with self.assertRaises(TypeError): | ||
| 527 | + attr_type() | ||
| 528 | + with self.assertRaises(TypeError): | ||
| 529 | + attr_type.__new__(attr_type) | ||
| 530 | + | ||
| 531 | + def test_sys_flags_no_instantiation(self): | ||
| 532 | + self.assert_raise_on_new_sys_type(sys.flags) | ||
| 533 | + | ||
| 534 | + def test_sys_version_info_no_instantiation(self): | ||
| 535 | + self.assert_raise_on_new_sys_type(sys.version_info) | ||
| 536 | + | ||
| 537 | + def test_sys_getwindowsversion_no_instantiation(self): | ||
| 538 | + # Skip if not being run on Windows. | ||
| 539 | + test.support.get_attribute(sys, "getwindowsversion") | ||
| 540 | + self.assert_raise_on_new_sys_type(sys.getwindowsversion()) | ||
| 541 | + | ||
| 522 | 542 | def test_clear_type_cache(self): | |
| 523 | 543 | sys._clear_type_cache() | |
| 524 | 544 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,6 +39,9 @@ Core and Builtins | |||
| 39 | 39 | Library | |
| 40 | 40 | ------- | |
| 41 | 41 | ||
| 42 | + - Issue #13204: Calling sys.flags.__new__ would crash the interpreter, | ||
| 43 | + now it raises a TypeError. | ||
| 44 | + | ||
| 42 | 45 | - Issue #19385: Make operations on a closed dbm.dumb database always raise the | |
| 43 | 46 | same exception. | |
| 44 | 47 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1622,14 +1622,14 @@ PyObject * | |||
| 1622 | 1622 | _PySys_Init(void) | |
| 1623 | 1623 | { | |
| 1624 | 1624 | PyObject *m, *sysdict, *version_info; | |
| 1625 | + int res; | ||
| 1625 | 1626 | ||
| 1626 | 1627 | m = PyModule_Create(&sysmodule); | |
| 1627 | 1628 | if (m == NULL) | |
| 1628 | 1629 | return NULL; | |
| 1629 | 1630 | sysdict = PyModule_GetDict(m); | |
| 1630 | 1631 | #define SET_SYS_FROM_STRING_BORROW(key, value) \ | |
| 1631 | 1632 | do { \ | |
| 1632 | - int res; \ | ||
| 1633 | 1633 | PyObject *v = (value); \ | |
| 1634 | 1634 | if (v == NULL) \ | |
| 1635 | 1635 | return NULL; \ | |
@@ -1640,7 +1640,6 @@ _PySys_Init(void) | |||
| 1640 | 1640 | } while (0) | |
| 1641 | 1641 | #define SET_SYS_FROM_STRING(key, value) \ | |
| 1642 | 1642 | do { \ | |
| 1643 | - int res; \ | ||
| 1644 | 1643 | PyObject *v = (value); \ | |
| 1645 | 1644 | if (v == NULL) \ | |
| 1646 | 1645 | return NULL; \ | |
@@ -1759,6 +1758,9 @@ _PySys_Init(void) | |||
| 1759 | 1758 | /* prevent user from creating new instances */ | |
| 1760 | 1759 | VersionInfoType.tp_init = NULL; | |
| 1761 | 1760 | VersionInfoType.tp_new = NULL; | |
| 1761 | + res = PyDict_DelItemString(VersionInfoType.tp_dict, "__new__"); | ||
| 1762 | + if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) | ||
| 1763 | + PyErr_Clear(); | ||
| 1762 | 1764 | ||
| 1763 | 1765 | /* implementation */ | |
| 1764 | 1766 | SET_SYS_FROM_STRING("implementation", make_impl_info(version_info)); | |
@@ -1772,7 +1774,9 @@ _PySys_Init(void) | |||
| 1772 | 1774 | /* prevent user from creating new instances */ | |
| 1773 | 1775 | FlagsType.tp_init = NULL; | |
| 1774 | 1776 | FlagsType.tp_new = NULL; | |
| 1775 | - | ||
| 1777 | + res = PyDict_DelItemString(FlagsType.tp_dict, "__new__"); | ||
| 1778 | + if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) | ||
| 1779 | + PyErr_Clear(); | ||
| 1776 | 1780 | ||
| 1777 | 1781 | #if defined(MS_WINDOWS) | |
| 1778 | 1782 | /* getwindowsversion */ | |
@@ -1783,6 +1787,9 @@ _PySys_Init(void) | |||
| 1783 | 1787 | /* prevent user from creating new instances */ | |
| 1784 | 1788 | WindowsVersionType.tp_init = NULL; | |
| 1785 | 1789 | WindowsVersionType.tp_new = NULL; | |
| 1790 | + res = PyDict_DelItemString(WindowsVersionType.tp_dict, "__new__"); | ||
| 1791 | + if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) | ||
| 1792 | + PyErr_Clear(); | ||
| 1786 | 1793 | #endif | |
| 1787 | 1794 | ||
| 1788 | 1795 | /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments