Python-checkins
Threads by month
- ----- 2024 -----
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2003 -----
- December
- November
- October
- September
- August
July 2021
- 1 participants
- 501 discussions
bpo-41486: zlib uses an UINT32_MAX sliding window for the output buffer (GH-26143)
by gpshead 05 Jul '21
by gpshead 05 Jul '21
05 Jul '21
https://github.com/python/cpython/commit/a9a69bb3ea1e6cf54513717212aaeae0d6…
commit: a9a69bb3ea1e6cf54513717212aaeae0d61b24ee
branch: main
author: Ma Lin <animalize(a)users.noreply.github.com>
committer: gpshead <greg(a)krypto.org>
date: 2021-07-04T18:10:44-07:00
summary:
bpo-41486: zlib uses an UINT32_MAX sliding window for the output buffer (GH-26143)
* zlib uses an UINT32_MAX sliding window for the output buffer
These funtions have an initial output buffer size parameter:
- zlib.decompress(data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)
- zlib.Decompress.flush([length])
If the initial size > UINT32_MAX, use an UINT32_MAX sliding window, instead of clamping to UINT32_MAX.
Speed up when (the initial size == the actual size).
This fixes a memory consumption and copying performance regression in earlier 3.10 beta releases if someone used an output buffer larger than 4GiB with zlib.decompress.
Reviewed-by: Gregory P. Smith
files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-04-17-41-47.bpo-41486.DiM24a.rst
M Modules/zlibmodule.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-04-17-41-47.bpo-41486.DiM24a.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-17-41-47.bpo-41486.DiM24a.rst
new file mode 100644
index 0000000000000..6a373f67f62f9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-17-41-47.bpo-41486.DiM24a.rst
@@ -0,0 +1,3 @@
+Fix a memory consumption and copying performance regression in earlier 3.10
+beta releases if someone used an output buffer larger than 4GiB with
+zlib.decompress on input data that expands that large.
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index fe06094b0027a..3efb24a679caa 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -30,28 +30,6 @@ OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
return allocated;
}
-/* On success, return value >= 0
- On failure, return -1 */
-static inline Py_ssize_t
-OutputBuffer_InitWithSize(_BlocksOutputBuffer *buffer, Py_ssize_t init_size,
- Bytef **next_out, uint32_t *avail_out)
-{
- Py_ssize_t allocated;
-
- if (init_size >= 0 && // ensure (size_t) cast is safe
- (size_t)init_size > UINT32_MAX)
- {
- /* In 32-bit build, never reach this conditional branch.
- The maximum block size accepted by zlib is UINT32_MAX. */
- init_size = UINT32_MAX;
- }
-
- allocated = _BlocksOutputBuffer_InitWithSize(
- buffer, init_size, (void**) next_out);
- *avail_out = (uint32_t) allocated;
- return allocated;
-}
-
/* On success, return value >= 0
On failure, return -1 */
static inline Py_ssize_t
@@ -84,6 +62,106 @@ OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
_BlocksOutputBuffer_OnError(buffer);
}
+/* The max buffer size accepted by zlib is UINT32_MAX, the initial buffer size
+ `init_size` may > it in 64-bit build. These wrapper functions maintain an
+ UINT32_MAX sliding window for the first block:
+ 1. OutputBuffer_WindowInitWithSize()
+ 2. OutputBuffer_WindowGrow()
+ 3. OutputBuffer_WindowFinish()
+ 4. OutputBuffer_WindowOnError()
+
+ ==== is the sliding window:
+ 1. ====------
+ ^ next_posi, left_bytes is 6
+ 2. ----====--
+ ^ next_posi, left_bytes is 2
+ 3. --------==
+ ^ next_posi, left_bytes is 0 */
+typedef struct {
+ Py_ssize_t left_bytes;
+ Bytef *next_posi;
+} _Uint32Window;
+
+/* Initialize the buffer with an inital buffer size.
+
+ On success, return value >= 0
+ On failure, return value < 0 */
+static inline Py_ssize_t
+OutputBuffer_WindowInitWithSize(_BlocksOutputBuffer *buffer, _Uint32Window *window,
+ Py_ssize_t init_size,
+ Bytef **next_out, uint32_t *avail_out)
+{
+ Py_ssize_t allocated = _BlocksOutputBuffer_InitWithSize(
+ buffer, init_size, (void**) next_out);
+
+ if (allocated >= 0) {
+ // the UINT32_MAX sliding window
+ Py_ssize_t window_size = Py_MIN((size_t)allocated, UINT32_MAX);
+ *avail_out = (uint32_t) window_size;
+
+ window->left_bytes = allocated - window_size;
+ window->next_posi = *next_out + window_size;
+ }
+ return allocated;
+}
+
+/* Grow the buffer.
+
+ On success, return value >= 0
+ On failure, return value < 0 */
+static inline Py_ssize_t
+OutputBuffer_WindowGrow(_BlocksOutputBuffer *buffer, _Uint32Window *window,
+ Bytef **next_out, uint32_t *avail_out)
+{
+ Py_ssize_t allocated;
+
+ /* ensure no gaps in the data.
+ if inlined, this check could be optimized away.*/
+ if (*avail_out != 0) {
+ PyErr_SetString(PyExc_SystemError,
+ "*avail_out != 0 in OutputBuffer_WindowGrow().");
+ return -1;
+ }
+
+ // slide the UINT32_MAX sliding window
+ if (window->left_bytes > 0) {
+ Py_ssize_t window_size = Py_MIN((size_t)window->left_bytes, UINT32_MAX);
+
+ *next_out = window->next_posi;
+ *avail_out = (uint32_t) window_size;
+
+ window->left_bytes -= window_size;
+ window->next_posi += window_size;
+
+ return window_size;
+ }
+ assert(window->left_bytes == 0);
+
+ // only the first block may > UINT32_MAX
+ allocated = _BlocksOutputBuffer_Grow(
+ buffer, (void**) next_out, (Py_ssize_t) *avail_out);
+ *avail_out = (uint32_t) allocated;
+ return allocated;
+}
+
+/* Finish the buffer.
+
+ On success, return a bytes object
+ On failure, return NULL */
+static inline PyObject *
+OutputBuffer_WindowFinish(_BlocksOutputBuffer *buffer, _Uint32Window *window,
+ uint32_t avail_out)
+{
+ Py_ssize_t real_avail_out = (Py_ssize_t) avail_out + window->left_bytes;
+ return _BlocksOutputBuffer_Finish(buffer, real_avail_out);
+}
+
+static inline void
+OutputBuffer_WindowOnError(_BlocksOutputBuffer *buffer, _Uint32Window *window)
+{
+ _BlocksOutputBuffer_OnError(buffer);
+}
+
#define ENTER_ZLIB(obj) do { \
if (!PyThread_acquire_lock((obj)->lock, 0)) { \
@@ -344,6 +422,7 @@ zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
int err, flush;
z_stream zst;
_BlocksOutputBuffer buffer = {.list = NULL};
+ _Uint32Window window; // output buffer's UINT32_MAX sliding window
zlibstate *state = get_zlib_state(module);
@@ -354,7 +433,8 @@ zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
bufsize = 1;
}
- if (OutputBuffer_InitWithSize(&buffer, bufsize, &zst.next_out, &zst.avail_out) < 0) {
+ if (OutputBuffer_WindowInitWithSize(&buffer, &window, bufsize,
+ &zst.next_out, &zst.avail_out) < 0) {
goto error;
}
@@ -387,7 +467,8 @@ zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
do {
if (zst.avail_out == 0) {
- if (OutputBuffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
+ if (OutputBuffer_WindowGrow(&buffer, &window,
+ &zst.next_out, &zst.avail_out) < 0) {
inflateEnd(&zst);
goto error;
}
@@ -430,13 +511,13 @@ zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
goto error;
}
- RetVal = OutputBuffer_Finish(&buffer, zst.avail_out);
+ RetVal = OutputBuffer_WindowFinish(&buffer, &window, zst.avail_out);
if (RetVal != NULL) {
return RetVal;
}
error:
- OutputBuffer_OnError(&buffer);
+ OutputBuffer_WindowOnError(&buffer, &window);
return NULL;
}
@@ -1171,6 +1252,7 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
PyObject *RetVal;
Py_ssize_t ibuflen;
_BlocksOutputBuffer buffer = {.list = NULL};
+ _Uint32Window window; // output buffer's UINT32_MAX sliding window
PyObject *module = PyType_GetModule(cls);
if (module == NULL) {
@@ -1193,7 +1275,8 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
self->zst.next_in = data.buf;
ibuflen = data.len;
- if (OutputBuffer_InitWithSize(&buffer, length, &self->zst.next_out, &self->zst.avail_out) < 0) {
+ if (OutputBuffer_WindowInitWithSize(&buffer, &window, length,
+ &self->zst.next_out, &self->zst.avail_out) < 0) {
goto abort;
}
@@ -1203,7 +1286,8 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
do {
if (self->zst.avail_out == 0) {
- if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
+ if (OutputBuffer_WindowGrow(&buffer, &window,
+ &self->zst.next_out, &self->zst.avail_out) < 0) {
goto abort;
}
}
@@ -1248,13 +1332,13 @@ zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
}
}
- RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
+ RetVal = OutputBuffer_WindowFinish(&buffer, &window, self->zst.avail_out);
if (RetVal != NULL) {
goto success;
}
abort:
- OutputBuffer_OnError(&buffer);
+ OutputBuffer_WindowOnError(&buffer, &window);
RetVal = NULL;
success:
PyBuffer_Release(&data);
1
0
https://github.com/python/cpython/commit/5644c7b3ffd49bed58dc095be6e6148e0b…
commit: 5644c7b3ffd49bed58dc095be6e6148e0bb4431e
branch: main
author: Ammar Askar <ammar(a)ammaraskar.com>
committer: pablogsal <Pablogsal(a)gmail.com>
date: 2021-07-05T00:14:33+01:00
summary:
bpo-43950: Print columns in tracebacks (PEP 657) (GH-26958)
The traceback.c and traceback.py mechanisms now utilize the newly added code.co_positions and PyCode_Addr2Location
to print carets on the specific expressions involved in a traceback.
Co-authored-by: Pablo Galindo <Pablogsal(a)gmail.com>
Co-authored-by: Ammar Askar <ammar(a)ammaraskar.com>
Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya(a)gmail.com>
files:
M Doc/library/traceback.rst
M Include/cpython/traceback.h
M Lib/idlelib/idle_test/test_run.py
M Lib/test/test_cmd_line_script.py
M Lib/test/test_doctest.py
M Lib/test/test_traceback.py
M Lib/test/test_zipimport.py
M Lib/traceback.py
M Parser/pegen.c
M Parser/pegen.h
M Python/_warnings.c
M Python/traceback.c
diff --git a/Doc/library/traceback.rst b/Doc/library/traceback.rst
index bd53bc066becc..1961b9a435bd3 100644
--- a/Doc/library/traceback.rst
+++ b/Doc/library/traceback.rst
@@ -447,37 +447,42 @@ The output for the example would look similar to this:
*** print_tb:
File "<doctest...>", line 10, in <module>
lumberjack()
+ ^^^^^^^^^^^^
*** print_exception:
Traceback (most recent call last):
File "<doctest...>", line 10, in <module>
lumberjack()
+ ^^^^^^^^^^^^
File "<doctest...>", line 4, in lumberjack
bright_side_of_death()
+ ^^^^^^^^^^^^^^^^^^^^^^
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
File "<doctest...>", line 10, in <module>
lumberjack()
+ ^^^^^^^^^^^^
File "<doctest...>", line 4, in lumberjack
bright_side_of_death()
+ ^^^^^^^^^^^^^^^^^^^^^^
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
- ' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
- ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
- ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n',
+ ' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
+ ' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
+ ' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ^^^^^^^^^^\n',
'IndexError: tuple index out of range\n']
*** extract_tb:
[<FrameSummary file <doctest...>, line 10 in <module>>,
<FrameSummary file <doctest...>, line 4 in lumberjack>,
<FrameSummary file <doctest...>, line 7 in bright_side_of_death>]
*** format_tb:
- [' File "<doctest...>", line 10, in <module>\n lumberjack()\n',
- ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n',
- ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n']
+ [' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
+ ' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
+ ' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ^^^^^^^^^^\n']
*** tb_lineno: 10
diff --git a/Include/cpython/traceback.h b/Include/cpython/traceback.h
index aac5b42c344d3..d0dde335cfee5 100644
--- a/Include/cpython/traceback.h
+++ b/Include/cpython/traceback.h
@@ -10,5 +10,5 @@ typedef struct _traceback {
int tb_lineno;
} PyTracebackObject;
-PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int);
+PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int, int *, PyObject **);
PyAPI_FUNC(void) _PyTraceback_Add(const char *, const char *, int);
diff --git a/Lib/idlelib/idle_test/test_run.py b/Lib/idlelib/idle_test/test_run.py
index ec4637c5ca617..b289fa7cbd648 100644
--- a/Lib/idlelib/idle_test/test_run.py
+++ b/Lib/idlelib/idle_test/test_run.py
@@ -33,9 +33,9 @@ def __eq__(self, other):
run.print_exception()
tb = output.getvalue().strip().splitlines()
- self.assertEqual(11, len(tb))
- self.assertIn('UnhashableException: ex2', tb[3])
- self.assertIn('UnhashableException: ex1', tb[10])
+ self.assertEqual(13, len(tb))
+ self.assertIn('UnhashableException: ex2', tb[4])
+ self.assertIn('UnhashableException: ex1', tb[12])
data = (('1/0', ZeroDivisionError, "division by zero\n"),
('abc', NameError, "name 'abc' is not defined. "
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 6ffec918ebbd5..e50c992579991 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -548,10 +548,10 @@ def test_pep_409_verbiage(self):
script_name = _make_test_script(script_dir, 'script', script)
exitcode, stdout, stderr = assert_python_failure(script_name)
text = stderr.decode('ascii').split('\n')
- self.assertEqual(len(text), 5)
+ self.assertEqual(len(text), 6)
self.assertTrue(text[0].startswith('Traceback'))
self.assertTrue(text[1].startswith(' File '))
- self.assertTrue(text[3].startswith('NameError'))
+ self.assertTrue(text[4].startswith('NameError'))
def test_non_ascii(self):
# Mac OS X denies the creation of a file with an invalid UTF-8 name.
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index 828a0ff56763a..06d9d5d4ade83 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -2835,6 +2835,7 @@ def test_unicode(): """
exec(compile(example.source, filename, "single",
File "<doctest foo-bär@baz[0]>", line 1, in <module>
raise Exception('clé')
+ ^^^^^^^^^^^^^^^^^^^^^^
Exception: clé
TestResults(failed=1, attempted=1)
"""
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 78b2851d38494..54f592a8ea500 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -17,8 +17,9 @@
test_code = namedtuple('code', ['co_filename', 'co_name'])
+test_code.co_positions = lambda _: iter([(6, 6, 0, 0)])
test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals'])
-test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next'])
+test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next', 'tb_lasti'])
class TracebackCases(unittest.TestCase):
@@ -154,9 +155,9 @@ def do_test(firstlines, message, charset, lineno):
self.assertTrue(stdout[2].endswith(err_line),
"Invalid traceback line: {0!r} instead of {1!r}".format(
stdout[2], err_line))
- self.assertTrue(stdout[3] == err_msg,
+ self.assertTrue(stdout[4] == err_msg,
"Invalid error message: {0!r} instead of {1!r}".format(
- stdout[3], err_msg))
+ stdout[4], err_msg))
do_test("", "foo", "ascii", 3)
for charset in ("ascii", "iso-8859-1", "utf-8", "GBK"):
@@ -272,6 +273,114 @@ def test_signatures(self):
'(exc, /, value=<implicit>)')
+class TracebackErrorLocationCaretTests(unittest.TestCase):
+ """
+ Tests for printing code error expressions as part of PEP 657
+ """
+ def get_exception(self, callable):
+ try:
+ callable()
+ self.fail("No exception thrown.")
+ except:
+ return traceback.format_exc().splitlines()[:-1]
+
+ callable_line = get_exception.__code__.co_firstlineno + 2
+
+ def test_basic_caret(self):
+ def f():
+ raise ValueError("basic caret tests")
+
+ lineno_f = f.__code__.co_firstlineno
+ expected_f = (
+ 'Traceback (most recent call last):\n'
+ f' File "{__file__}", line {self.callable_line}, in get_exception\n'
+ ' callable()\n'
+ ' ^^^^^^^^^^\n'
+ f' File "{__file__}", line {lineno_f+1}, in f\n'
+ ' raise ValueError("basic caret tests")\n'
+ ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
+ )
+ result_lines = self.get_exception(f)
+ self.assertEqual(result_lines, expected_f.splitlines())
+
+ def test_line_with_unicode(self):
+ # Make sure that even if a line contains multi-byte unicode characters
+ # the correct carets are printed.
+ def f_with_unicode():
+ raise ValueError("Ĥellö Wörld")
+
+ lineno_f = f_with_unicode.__code__.co_firstlineno
+ expected_f = (
+ 'Traceback (most recent call last):\n'
+ f' File "{__file__}", line {self.callable_line}, in get_exception\n'
+ ' callable()\n'
+ ' ^^^^^^^^^^\n'
+ f' File "{__file__}", line {lineno_f+1}, in f_with_unicode\n'
+ ' raise ValueError("Ĥellö Wörld")\n'
+ ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
+ )
+ result_lines = self.get_exception(f_with_unicode)
+ self.assertEqual(result_lines, expected_f.splitlines())
+
+ def test_caret_in_type_annotation(self):
+ def f_with_type():
+ def foo(a: THIS_DOES_NOT_EXIST ) -> int:
+ return 0
+
+ lineno_f = f_with_type.__code__.co_firstlineno
+ expected_f = (
+ 'Traceback (most recent call last):\n'
+ f' File "{__file__}", line {self.callable_line}, in get_exception\n'
+ ' callable()\n'
+ ' ^^^^^^^^^^\n'
+ f' File "{__file__}", line {lineno_f+1}, in f_with_type\n'
+ ' def foo(a: THIS_DOES_NOT_EXIST ) -> int:\n'
+ ' ^^^^^^^^^^^^^^^^^^^\n'
+ )
+ result_lines = self.get_exception(f_with_type)
+ self.assertEqual(result_lines, expected_f.splitlines())
+
+ def test_caret_multiline_expression(self):
+ # Make sure no carets are printed for expressions spanning multiple
+ # lines.
+ def f_with_multiline():
+ raise ValueError(
+ "error over multiple lines"
+ )
+
+ lineno_f = f_with_multiline.__code__.co_firstlineno
+ expected_f = (
+ 'Traceback (most recent call last):\n'
+ f' File "{__file__}", line {self.callable_line}, in get_exception\n'
+ ' callable()\n'
+ ' ^^^^^^^^^^\n'
+ f' File "{__file__}", line {lineno_f+1}, in f_with_multiline\n'
+ ' raise ValueError(\n'
+ )
+ result_lines = self.get_exception(f_with_multiline)
+ self.assertEqual(result_lines, expected_f.splitlines())
+
+
+@cpython_only
+class CPythonTracebackErrorCaretTests(TracebackErrorLocationCaretTests):
+ """
+ Same set of tests as above but with Python's internal traceback printing.
+ """
+ def get_exception(self, callable):
+ from _testcapi import traceback_print
+ try:
+ callable()
+ self.fail("No exception thrown.")
+ except:
+ type_, value, tb = sys.exc_info()
+
+ file_ = StringIO()
+ traceback_print(tb, file_)
+ return file_.getvalue().splitlines()
+
+ callable_line = get_exception.__code__.co_firstlineno + 3
+
+
class TracebackFormatTests(unittest.TestCase):
def some_exception(self):
@@ -315,9 +424,9 @@ def check_traceback_format(self, cleanup_func=None):
# Make sure that the traceback is properly indented.
tb_lines = python_fmt.splitlines()
- self.assertEqual(len(tb_lines), 5)
+ self.assertEqual(len(tb_lines), 7)
banner = tb_lines[0]
- location, source_line = tb_lines[-2:]
+ location, source_line = tb_lines[-3], tb_lines[-2]
self.assertTrue(banner.startswith('Traceback'))
self.assertTrue(location.startswith(' File'))
self.assertTrue(source_line.startswith(' raise'))
@@ -381,12 +490,16 @@ def f():
'Traceback (most recent call last):\n'
f' File "{__file__}", line {lineno_f+5}, in _check_recursive_traceback_display\n'
' f()\n'
+ ' ^^^\n'
f' File "{__file__}", line {lineno_f+1}, in f\n'
' f()\n'
+ ' ^^^\n'
f' File "{__file__}", line {lineno_f+1}, in f\n'
' f()\n'
+ ' ^^^\n'
f' File "{__file__}", line {lineno_f+1}, in f\n'
' f()\n'
+ ' ^^^\n'
# XXX: The following line changes depending on whether the tests
# are run through the interactive interpreter or with -m
# It also varies depending on the platform (stack size)
@@ -427,19 +540,24 @@ def g(count=10):
result_g = (
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
' [Previous line repeated 7 more times]\n'
f' File "{__file__}", line {lineno_g+3}, in g\n'
' raise ValueError\n'
+ ' ^^^^^^^^^^^^^^^^\n'
'ValueError\n'
)
tb_line = (
'Traceback (most recent call last):\n'
f' File "{__file__}", line {lineno_g+7}, in _check_recursive_traceback_display\n'
' g()\n'
+ ' ^^^\n'
)
expected = (tb_line + result_g).splitlines()
actual = stderr_g.getvalue().splitlines()
@@ -464,15 +582,20 @@ def h(count=10):
'Traceback (most recent call last):\n'
f' File "{__file__}", line {lineno_h+7}, in _check_recursive_traceback_display\n'
' h()\n'
+ ' ^^^\n'
f' File "{__file__}", line {lineno_h+2}, in h\n'
' return h(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_h+2}, in h\n'
' return h(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_h+2}, in h\n'
' return h(count-1)\n'
+ ' ^^^^^^^^^^\n'
' [Previous line repeated 7 more times]\n'
f' File "{__file__}", line {lineno_h+3}, in h\n'
' g()\n'
+ ' ^^^\n'
)
expected = (result_h + result_g).splitlines()
actual = stderr_h.getvalue().splitlines()
@@ -489,18 +612,23 @@ def h(count=10):
result_g = (
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_g+3}, in g\n'
' raise ValueError\n'
+ ' ^^^^^^^^^^^^^^^^\n'
'ValueError\n'
)
tb_line = (
'Traceback (most recent call last):\n'
- f' File "{__file__}", line {lineno_g+71}, in _check_recursive_traceback_display\n'
+ f' File "{__file__}", line {lineno_g+81}, in _check_recursive_traceback_display\n'
' g(traceback._RECURSIVE_CUTOFF)\n'
+ ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
)
expected = (tb_line + result_g).splitlines()
actual = stderr_g.getvalue().splitlines()
@@ -517,19 +645,24 @@ def h(count=10):
result_g = (
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
f' File "{__file__}", line {lineno_g+2}, in g\n'
' return g(count-1)\n'
+ ' ^^^^^^^^^^\n'
' [Previous line repeated 1 more time]\n'
f' File "{__file__}", line {lineno_g+3}, in g\n'
' raise ValueError\n'
+ ' ^^^^^^^^^^^^^^^^\n'
'ValueError\n'
)
tb_line = (
'Traceback (most recent call last):\n'
- f' File "{__file__}", line {lineno_g+99}, in _check_recursive_traceback_display\n'
+ f' File "{__file__}", line {lineno_g+114}, in _check_recursive_traceback_display\n'
' g(traceback._RECURSIVE_CUTOFF + 1)\n'
+ ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
)
expected = (tb_line + result_g).splitlines()
actual = stderr_g.getvalue().splitlines()
@@ -580,10 +713,10 @@ def __eq__(self, other):
exception_print(exc_val)
tb = stderr_f.getvalue().strip().splitlines()
- self.assertEqual(11, len(tb))
- self.assertEqual(context_message.strip(), tb[5])
- self.assertIn('UnhashableException: ex2', tb[3])
- self.assertIn('UnhashableException: ex1', tb[10])
+ self.assertEqual(13, len(tb))
+ self.assertEqual(context_message.strip(), tb[6])
+ self.assertIn('UnhashableException: ex2', tb[4])
+ self.assertIn('UnhashableException: ex1', tb[12])
cause_message = (
@@ -613,8 +746,8 @@ def zero_div(self):
def check_zero_div(self, msg):
lines = msg.splitlines()
- self.assertTrue(lines[-3].startswith(' File'))
- self.assertIn('1/0 # In zero_div', lines[-2])
+ self.assertTrue(lines[-4].startswith(' File'))
+ self.assertIn('1/0 # In zero_div', lines[-3])
self.assertTrue(lines[-1].startswith('ZeroDivisionError'), lines[-1])
def test_simple(self):
@@ -623,11 +756,11 @@ def test_simple(self):
except ZeroDivisionError as _:
e = _
lines = self.get_report(e).splitlines()
- self.assertEqual(len(lines), 4)
+ self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].startswith('Traceback'))
self.assertTrue(lines[1].startswith(' File'))
self.assertIn('1/0 # Marker', lines[2])
- self.assertTrue(lines[3].startswith('ZeroDivisionError'))
+ self.assertTrue(lines[4].startswith('ZeroDivisionError'))
def test_cause(self):
def inner_raise():
@@ -666,11 +799,11 @@ def test_context_suppression(self):
except ZeroDivisionError as _:
e = _
lines = self.get_report(e).splitlines()
- self.assertEqual(len(lines), 4)
+ self.assertEqual(len(lines), 5)
self.assertTrue(lines[0].startswith('Traceback'))
self.assertTrue(lines[1].startswith(' File'))
self.assertIn('ZeroDivisionError from None', lines[2])
- self.assertTrue(lines[3].startswith('ZeroDivisionError'))
+ self.assertTrue(lines[4].startswith('ZeroDivisionError'))
def test_cause_and_context(self):
# When both a cause and a context are set, only the cause should be
@@ -1362,7 +1495,7 @@ def test_lookup_lines(self):
e = Exception("uh oh")
c = test_code('/foo.py', 'method')
f = test_frame(c, None, None)
- tb = test_tb(f, 6, None)
+ tb = test_tb(f, 6, None, 0)
exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False)
self.assertEqual(linecache.cache, {})
linecache.updatecache('/foo.py', globals())
@@ -1373,7 +1506,7 @@ def test_locals(self):
e = Exception("uh oh")
c = test_code('/foo.py', 'method')
f = test_frame(c, globals(), {'something': 1, 'other': 'string'})
- tb = test_tb(f, 6, None)
+ tb = test_tb(f, 6, None, 0)
exc = traceback.TracebackException(
Exception, e, tb, capture_locals=True)
self.assertEqual(
@@ -1384,7 +1517,7 @@ def test_no_locals(self):
e = Exception("uh oh")
c = test_code('/foo.py', 'method')
f = test_frame(c, globals(), {'something': 1})
- tb = test_tb(f, 6, None)
+ tb = test_tb(f, 6, None, 0)
exc = traceback.TracebackException(Exception, e, tb)
self.assertEqual(exc.stack[0].locals, None)
@@ -1405,8 +1538,9 @@ def f():
output = StringIO()
exc.print(file=output)
self.assertEqual(
- output.getvalue().split('\n')[-4:],
+ output.getvalue().split('\n')[-5:],
[' x/0',
+ ' ^^^',
' x = 12',
'ZeroDivisionError: division by zero',
''])
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index dfc4a060ec6cc..861ebe347b3ee 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -716,7 +716,10 @@ def doTraceback(self, module):
s = io.StringIO()
print_tb(tb, 1, s)
- self.assertTrue(s.getvalue().endswith(raise_src))
+ self.assertTrue(s.getvalue().endswith(
+ ' def do_raise(): raise TypeError\n'
+ ' ^^^^^^^^^^^^^^^\n'
+ ))
else:
raise AssertionError("This ought to be impossible")
diff --git a/Lib/traceback.py b/Lib/traceback.py
index b4c7641addec7..cf1ba2a1a49ac 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -69,7 +69,8 @@ def extract_tb(tb, limit=None):
trace. The line is a string with leading and trailing
whitespace stripped; if the source is not available it is None.
"""
- return StackSummary.extract(walk_tb(tb), limit=limit)
+ return StackSummary._extract_from_extended_frame_gen(
+ _walk_tb_with_full_positions(tb), limit=limit)
#
# Exception formatting and output.
@@ -251,10 +252,12 @@ class FrameSummary:
mapping the name to the repr() of the variable.
"""
- __slots__ = ('filename', 'lineno', 'name', '_line', 'locals')
+ __slots__ = ('filename', 'lineno', 'end_lineno', 'colno', 'end_colno',
+ 'name', '_line', 'locals')
def __init__(self, filename, lineno, name, *, lookup_line=True,
- locals=None, line=None):
+ locals=None, line=None,
+ end_lineno=None, colno=None, end_colno=None):
"""Construct a FrameSummary.
:param lookup_line: If True, `linecache` is consulted for the source
@@ -271,6 +274,9 @@ def __init__(self, filename, lineno, name, *, lookup_line=True,
if lookup_line:
self.line
self.locals = {k: repr(v) for k, v in locals.items()} if locals else None
+ self.end_lineno = end_lineno
+ self.colno = colno
+ self.end_colno = end_colno
def __eq__(self, other):
if isinstance(other, FrameSummary):
@@ -295,11 +301,17 @@ def __repr__(self):
def __len__(self):
return 4
+ @property
+ def _original_line(self):
+ # Returns the line as-is from the source, without modifying whitespace.
+ self.line
+ return self._line
+
@property
def line(self):
if self._line is None:
- self._line = linecache.getline(self.filename, self.lineno).strip()
- return self._line
+ self._line = linecache.getline(self.filename, self.lineno)
+ return self._line.strip()
def walk_stack(f):
@@ -309,7 +321,7 @@ def walk_stack(f):
current stack is used. Usually used with StackSummary.extract.
"""
if f is None:
- f = sys._getframe().f_back.f_back
+ f = sys._getframe().f_back.f_back.f_back.f_back
while f is not None:
yield f, f.f_lineno
f = f.f_back
@@ -326,6 +338,27 @@ def walk_tb(tb):
tb = tb.tb_next
+def _walk_tb_with_full_positions(tb):
+ # Internal version of walk_tb that yields full code positions including
+ # end line and column information.
+ while tb is not None:
+ positions = _get_code_position(tb.tb_frame.f_code, tb.tb_lasti)
+ # Yield tb_lineno when co_positions does not have a line number to
+ # maintain behavior with walk_tb.
+ if positions[0] is None:
+ yield tb.tb_frame, (tb.tb_lineno, ) + positions[1:]
+ else:
+ yield tb.tb_frame, positions
+ tb = tb.tb_next
+
+
+def _get_code_position(code, instruction_index):
+ if instruction_index < 0:
+ return (None, None, None, None)
+ positions_gen = code.co_positions()
+ return next(itertools.islice(positions_gen, instruction_index // 2, None))
+
+
_RECURSIVE_CUTOFF = 3 # Also hardcoded in traceback.c.
class StackSummary(list):
@@ -345,6 +378,21 @@ def extract(klass, frame_gen, *, limit=None, lookup_lines=True,
:param capture_locals: If True, the local variables from each frame will
be captured as object representations into the FrameSummary.
"""
+ def extended_frame_gen():
+ for f, lineno in frame_gen:
+ yield f, (lineno, None, None, None)
+
+ return klass._extract_from_extended_frame_gen(
+ extended_frame_gen(), limit=limit, lookup_lines=lookup_lines,
+ capture_locals=capture_locals)
+
+ @classmethod
+ def _extract_from_extended_frame_gen(klass, frame_gen, *, limit=None,
+ lookup_lines=True, capture_locals=False):
+ # Same as extract but operates on a frame generator that yields
+ # (frame, (lineno, end_lineno, colno, end_colno)) in the stack.
+ # Only lineno is required, the remaining fields can be empty if the
+ # information is not available.
if limit is None:
limit = getattr(sys, 'tracebacklimit', None)
if limit is not None and limit < 0:
@@ -357,7 +405,7 @@ def extract(klass, frame_gen, *, limit=None, lookup_lines=True,
result = klass()
fnames = set()
- for f, lineno in frame_gen:
+ for f, (lineno, end_lineno, colno, end_colno) in frame_gen:
co = f.f_code
filename = co.co_filename
name = co.co_name
@@ -370,7 +418,8 @@ def extract(klass, frame_gen, *, limit=None, lookup_lines=True,
else:
f_locals = None
result.append(FrameSummary(
- filename, lineno, name, lookup_line=False, locals=f_locals))
+ filename, lineno, name, lookup_line=False, locals=f_locals,
+ end_lineno=end_lineno, colno=colno, end_colno=end_colno))
for filename in fnames:
linecache.checkcache(filename)
# If immediate lookup was desired, trigger lookups now.
@@ -437,6 +486,17 @@ def format(self):
frame.filename, frame.lineno, frame.name))
if frame.line:
row.append(' {}\n'.format(frame.line.strip()))
+
+ stripped_characters = len(frame._original_line) - len(frame.line.lstrip())
+ if frame.end_lineno == frame.lineno and frame.end_colno != 0:
+ colno = _byte_offset_to_character_offset(frame._original_line, frame.colno)
+ end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno)
+
+ row.append(' ')
+ row.append(' ' * (colno - stripped_characters))
+ row.append('^' * (end_colno - colno))
+ row.append('\n')
+
if frame.locals:
for name, value in sorted(frame.locals.items()):
row.append(' {name} = {value}\n'.format(name=name, value=value))
@@ -450,6 +510,14 @@ def format(self):
return result
+def _byte_offset_to_character_offset(str, offset):
+ as_utf8 = str.encode('utf-8')
+ if offset > len(as_utf8):
+ offset = len(as_utf8)
+
+ return len(as_utf8[:offset + 1].decode("utf-8"))
+
+
class TracebackException:
"""An exception ready for rendering.
@@ -491,8 +559,9 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
_seen.add(id(exc_value))
# TODO: locals.
- self.stack = StackSummary.extract(
- walk_tb(exc_traceback), limit=limit, lookup_lines=lookup_lines,
+ self.stack = StackSummary._extract_from_extended_frame_gen(
+ _walk_tb_with_full_positions(exc_traceback),
+ limit=limit, lookup_lines=lookup_lines,
capture_locals=capture_locals)
self.exc_type = exc_type
# Capture now to permit freeing resources: only complication is in the
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 3472d489e067d..3e8ddfbf53cf7 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -139,27 +139,6 @@ _create_dummy_identifier(Parser *p)
return _PyPegen_new_identifier(p, "");
}
-static inline Py_ssize_t
-byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset)
-{
- const char *str = PyUnicode_AsUTF8(line);
- if (!str) {
- return 0;
- }
- Py_ssize_t len = strlen(str);
- if (col_offset > len + 1) {
- col_offset = len + 1;
- }
- assert(col_offset >= 0);
- PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
- if (!text) {
- return 0;
- }
- Py_ssize_t size = PyUnicode_GET_LENGTH(text);
- Py_DECREF(text);
- return size;
-}
-
const char *
_PyPegen_get_expr_name(expr_ty e)
{
@@ -418,6 +397,27 @@ get_error_line(Parser *p, Py_ssize_t lineno)
return PyUnicode_DecodeUTF8(cur_line, next_newline - cur_line, "replace");
}
+Py_ssize_t
+_PyPegen_byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset)
+{
+ const char *str = PyUnicode_AsUTF8(line);
+ if (!str) {
+ return 0;
+ }
+ Py_ssize_t len = strlen(str);
+ if (col_offset > len + 1) {
+ col_offset = len + 1;
+ }
+ assert(col_offset >= 0);
+ PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
+ if (!text) {
+ return 0;
+ }
+ Py_ssize_t size = PyUnicode_GET_LENGTH(text);
+ Py_DECREF(text);
+ return size;
+}
+
void *
_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_ssize_t lineno, Py_ssize_t col_offset,
@@ -498,9 +498,9 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_ssize_t end_col_number = end_col_offset;
if (p->tok->encoding != NULL) {
- col_number = byte_offset_to_character_offset(error_line, col_offset);
+ col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
end_col_number = end_col_number > 0 ?
- byte_offset_to_character_offset(error_line, end_col_offset) :
+ _PyPegen_byte_offset_to_character_offset(error_line, end_col_offset) :
end_col_number;
}
tmp = Py_BuildValue("(OiiNii)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);
diff --git a/Parser/pegen.h b/Parser/pegen.h
index eac73bba151bc..c09b4a2927562 100644
--- a/Parser/pegen.h
+++ b/Parser/pegen.h
@@ -139,6 +139,7 @@ expr_ty _PyPegen_name_token(Parser *p);
expr_ty _PyPegen_number_token(Parser *p);
void *_PyPegen_string_token(Parser *p);
const char *_PyPegen_get_expr_name(expr_ty);
+Py_ssize_t _PyPegen_byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset);
void *_PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...);
void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_ssize_t lineno, Py_ssize_t col_offset,
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 9c8815c1a3e20..9f68da208731e 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -544,7 +544,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text,
PyFile_WriteString("\n", f_stderr);
}
else {
- _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
+ _Py_DisplaySourceLine(f_stderr, filename, lineno, 2, NULL, NULL);
}
error:
diff --git a/Python/traceback.c b/Python/traceback.c
index f7dc5ad686476..a60f991642433 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -3,9 +3,11 @@
#include "Python.h"
-#include "code.h"
+#include "code.h" // PyCode_Addr2Line etc
#include "pycore_interp.h" // PyInterpreterState.gc
#include "frameobject.h" // PyFrame_GetBack()
+#include "pycore_frame.h" // _PyFrame_GetCode()
+#include "../Parser/pegen.h" // _PyPegen_byte_offset_to_character_offset()
#include "structmember.h" // PyMemberDef
#include "osdefs.h" // SEP
#ifdef HAVE_FCNTL_H
@@ -370,7 +372,7 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *
}
int
-_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
+_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent, int *truncation, PyObject **line)
{
int err = 0;
int fd;
@@ -461,6 +463,11 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
return err;
}
+ if (line) {
+ Py_INCREF(lineobj);
+ *line = lineobj;
+ }
+
/* remove the indentation of the line */
kind = PyUnicode_KIND(lineobj);
data = PyUnicode_DATA(lineobj);
@@ -480,6 +487,10 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
}
}
+ if (truncation != NULL) {
+ *truncation = i - indent;
+ }
+
/* Write some spaces before the line */
strcpy(buf, " ");
assert (strlen(buf) == 10);
@@ -501,8 +512,11 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
return err;
}
+#define _TRACEBACK_SOURCE_LINE_INDENT 4
+
static int
-tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
+tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int lineno,
+ PyFrameObject *frame, PyObject *name)
{
int err;
PyObject *line;
@@ -517,9 +531,56 @@ tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Py_DECREF(line);
if (err != 0)
return err;
+ int truncation = _TRACEBACK_SOURCE_LINE_INDENT;
+ PyObject* source_line = NULL;
/* ignore errors since we can't report them, can we? */
- if (_Py_DisplaySourceLine(f, filename, lineno, 4))
+ if (!_Py_DisplaySourceLine(f, filename, lineno, _TRACEBACK_SOURCE_LINE_INDENT,
+ &truncation, &source_line)) {
+ int code_offset = tb->tb_lasti;
+ PyCodeObject* code = _PyFrame_GetCode(frame);
+
+ int start_line;
+ int end_line;
+ int start_col_byte_offset;
+ int end_col_byte_offset;
+ if (!PyCode_Addr2Location(code, code_offset, &start_line, &start_col_byte_offset,
+ &end_line, &end_col_byte_offset)) {
+ goto done;
+ }
+ if (start_line != end_line) {
+ goto done;
+ }
+
+ if (start_col_byte_offset < 0 || end_col_byte_offset < 0) {
+ goto done;
+ }
+ // Convert the utf-8 byte offset to the actual character offset so we
+ // print the right number of carets.
+ Py_ssize_t start_offset = _PyPegen_byte_offset_to_character_offset(source_line, start_col_byte_offset);
+ Py_ssize_t end_offset = _PyPegen_byte_offset_to_character_offset(source_line, end_col_byte_offset);
+
+ char offset = truncation;
+ while (++offset <= start_offset) {
+ err = PyFile_WriteString(" ", f);
+ if (err < 0) {
+ goto done;
+ }
+ }
+ while (++offset <= end_offset + 1) {
+ err = PyFile_WriteString("^", f);
+ if (err < 0) {
+ goto done;
+ }
+ }
+ err = PyFile_WriteString("\n", f);
+ }
+
+ else {
PyErr_Clear();
+ }
+
+done:
+ Py_XDECREF(source_line);
return err;
}
@@ -576,8 +637,8 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
}
cnt++;
if (err == 0 && cnt <= TB_RECURSIVE_CUTOFF) {
- err = tb_displayline(f, code->co_filename, tb->tb_lineno,
- code->co_name);
+ err = tb_displayline(tb, f, code->co_filename, tb->tb_lineno,
+ tb->tb_frame, code->co_name);
if (err == 0) {
err = PyErr_CheckSignals();
}
1
0
Revert "bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016) (GH-27018)" (GH-27022)
by pablogsal 04 Jul '21
by pablogsal 04 Jul '21
04 Jul '21
https://github.com/python/cpython/commit/fe847a62852c3baaec6c97a5e2e7b2e667…
commit: fe847a62852c3baaec6c97a5e2e7b2e66732bdb8
branch: 3.9
author: Pablo Galindo <Pablogsal(a)gmail.com>
committer: pablogsal <Pablogsal(a)gmail.com>
date: 2021-07-04T20:26:34+01:00
summary:
Revert "bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016) (GH-27018)" (GH-27022)
This reverts commit 4684a34c8d2a2ffac7b779edb4ba57f043783478.
files:
D Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
M Objects/genericaliasobject.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
deleted file mode 100644
index 2fc65bcfdeef4..0000000000000
--- a/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
+++ /dev/null
@@ -1,2 +0,0 @@
-Remove uses of :c:func:`PyObject_GC_Del` in error path when initializing
-:class:`types.GenericAlias`.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index bb5fa095d3573..945d20593c7c9 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -602,7 +602,7 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
if (!setup_ga(self, origin, arguments)) {
- Py_DECREF(self);
+ type->tp_free((PyObject *)self);
return NULL;
}
return (PyObject *)self;
@@ -644,10 +644,10 @@ Py_GenericAlias(PyObject *origin, PyObject *args)
if (alias == NULL) {
return NULL;
}
- _PyObject_GC_TRACK(alias);
if (!setup_ga(alias, origin, args)) {
- Py_DECREF(alias);
+ PyObject_GC_Del((PyObject *)alias);
return NULL;
}
+ _PyObject_GC_TRACK(alias);
return (PyObject *)alias;
}
1
0
https://github.com/python/cpython/commit/693cec0e2dcafa393ed5cdaa606f8dc8e3…
commit: 693cec0e2dcafa393ed5cdaa606f8dc8e3876adf
branch: main
author: Batuhan Taskaya <batuhan(a)python.org>
committer: miss-islington <31488909+miss-islington(a)users.noreply.github.com>
date: 2021-07-04T12:05:05-07:00
summary:
bpo-43950: include position in dis.Instruction (GH-27015)
Automerge-Triggered-By: GH:isidentical
files:
M Lib/dis.py
M Lib/test/test_dis.py
diff --git a/Lib/dis.py b/Lib/dis.py
index 48a6ab8e41df3..3cacd9a44a97e 100644
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -191,8 +191,32 @@ def show_code(co, *, file=None):
"""
print(code_info(co), file=file)
-_Instruction = collections.namedtuple("_Instruction",
- "opname opcode arg argval argrepr offset starts_line is_jump_target")
+Positions = collections.namedtuple(
+ 'Positions',
+ [
+ 'lineno',
+ 'end_lineno',
+ 'col_offset',
+ 'end_col_offset',
+ ],
+ defaults=[None] * 4
+)
+
+_Instruction = collections.namedtuple(
+ "_Instruction",
+ [
+ 'opname',
+ 'opcode',
+ 'arg',
+ 'argval',
+ 'argrepr',
+ 'offset',
+ 'starts_line',
+ 'is_jump_target',
+ 'positions'
+ ],
+ defaults=[None]
+)
_Instruction.opname.__doc__ = "Human readable name for operation"
_Instruction.opcode.__doc__ = "Numeric code for operation"
@@ -202,6 +226,7 @@ def show_code(co, *, file=None):
_Instruction.offset.__doc__ = "Start index of operation within bytecode sequence"
_Instruction.starts_line.__doc__ = "Line started by this opcode (if any), otherwise None"
_Instruction.is_jump_target.__doc__ = "True if other code jumps to here, otherwise False"
+_Instruction.positions.__doc__ = "dis.Positions object holding the span of source code covered by this instruction"
_ExceptionTableEntry = collections.namedtuple("_ExceptionTableEntry",
"start end target depth lasti")
@@ -221,6 +246,8 @@ class Instruction(_Instruction):
offset - start index of operation within bytecode sequence
starts_line - line started by this opcode (if any), otherwise None
is_jump_target - True if other code jumps to here, otherwise False
+ positions - Optional dis.Positions object holding the span of source code
+ covered by this instruction
"""
def _disassemble(self, lineno_width=3, mark_as_current=False, offset_width=4):
@@ -281,7 +308,7 @@ def get_instructions(x, *, first_line=None):
return _get_instructions_bytes(co.co_code,
co._varname_from_oparg,
co.co_names, co.co_consts,
- linestarts, line_offset)
+ linestarts, line_offset, co_positions=co.co_positions())
def _get_const_info(const_index, const_list):
"""Helper to get optional details about const references
@@ -339,7 +366,7 @@ def parse_exception_table(code):
def _get_instructions_bytes(code, varname_from_oparg=None,
names=None, constants=None,
linestarts=None, line_offset=0,
- exception_entries=()):
+ exception_entries=(), co_positions=None):
"""Iterate over the instructions in a bytecode string.
Generates a sequence of Instruction namedtuples giving the details of each
@@ -348,6 +375,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
arguments.
"""
+ co_positions = co_positions or iter(())
get_name = None if names is None else names.__getitem__
labels = set(findlabels(code))
for start, end, target, _, _ in exception_entries:
@@ -362,6 +390,10 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
is_jump_target = offset in labels
argval = None
argrepr = ''
+ try:
+ positions = next(co_positions)
+ except StopIteration:
+ positions = None
if arg is not None:
# Set argval to the dereferenced value of the argument when
# available, and argrepr to the string representation of argval.
@@ -395,7 +427,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
if arg & (1<<i))
yield Instruction(opname[op], op,
arg, argval, argrepr,
- offset, starts_line, is_jump_target)
+ offset, starts_line, is_jump_target, positions)
def disassemble(co, lasti=-1, *, file=None):
"""Disassemble a code object."""
@@ -404,7 +436,7 @@ def disassemble(co, lasti=-1, *, file=None):
_disassemble_bytes(co.co_code, lasti,
co._varname_from_oparg,
co.co_names, co.co_consts, linestarts, file=file,
- exception_entries=exception_entries)
+ exception_entries=exception_entries, co_positions=co.co_positions())
def _disassemble_recursive(co, *, file=None, depth=None):
disassemble(co, file=file)
@@ -419,7 +451,8 @@ def _disassemble_recursive(co, *, file=None, depth=None):
def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
names=None, constants=None, linestarts=None,
- *, file=None, line_offset=0, exception_entries=()):
+ *, file=None, line_offset=0, exception_entries=(),
+ co_positions=None):
# Omit the line number column entirely if we have no line number info
show_lineno = bool(linestarts)
if show_lineno:
@@ -437,7 +470,8 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
offset_width = 4
for instr in _get_instructions_bytes(code, varname_from_oparg, names,
constants, linestarts,
- line_offset=line_offset, exception_entries=exception_entries):
+ line_offset=line_offset, exception_entries=exception_entries,
+ co_positions=co_positions):
new_source_line = (show_lineno and
instr.starts_line is not None and
instr.offset > 0)
@@ -562,7 +596,8 @@ def dis(self):
line_offset=self._line_offset,
file=output,
lasti=offset,
- exception_entries=self.exception_entries)
+ exception_entries=self.exception_entries,
+ co_positions=co.co_positions())
return output.getvalue()
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index f2daa17acd1fc..a7bd593602e26 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -949,196 +949,204 @@ def jumpy():
# After regeneration, update the references to code_object_f and
# code_object_inner before rerunning the tests
-#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
-#print('expected_opinfo_outer = [\n ',
- #',\n '.join(map(str, _instructions)), ',\n]', sep='')
-#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
-#print('expected_opinfo_f = [\n ',
- #',\n '.join(map(str, _instructions)), ',\n]', sep='')
-#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
-#print('expected_opinfo_inner = [\n ',
- #',\n '.join(map(str, _instructions)), ',\n]', sep='')
-#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
-#print('expected_opinfo_jumpy = [\n ',
- #',\n '.join(map(str, _instructions)), ',\n]', sep='')
-
-#dis.dis(outer)
+def _stringify_instruction(instr):
+ # Since line numbers and other offsets change a lot for these
+ # test cases, ignore them.
+ return str(instr._replace(positions=None))
+
+def _prepare_test_cases():
+ _instructions = dis.get_instructions(outer, first_line=expected_outer_line)
+ print('expected_opinfo_outer = [\n ',
+ ',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='')
+ _instructions = dis.get_instructions(outer(), first_line=expected_f_line)
+ print('expected_opinfo_f = [\n ',
+ ',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='')
+ _instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
+ print('expected_opinfo_inner = [\n ',
+ ',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='')
+ _instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
+ print('expected_opinfo_jumpy = [\n ',
+ ',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='')
+ dis.dis(outer)
+
+#_prepare_test_cases()
Instruction = dis.Instruction
expected_opinfo_outer = [
- Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='a', argrepr='a', offset=0, starts_line=None, is_jump_target=False),
- Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='b', argrepr='b', offset=2, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=4, starts_line=2, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='b', argrepr='b', offset=8, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=10, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=12, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=14, starts_line=None, is_jump_target=False),
- Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=16, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=7, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=26, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=28, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=30, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=32, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=34, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=36, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=40, starts_line=8, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=42, starts_line=None, is_jump_target=False),
+ Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='a', argrepr='a', offset=0, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='b', argrepr='b', offset=2, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=4, starts_line=2, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='b', argrepr='b', offset=8, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=10, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=12, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=14, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=16, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=7, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=26, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=28, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=30, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=32, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=34, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=36, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=40, starts_line=8, is_jump_target=False, positions=None),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=42, starts_line=None, is_jump_target=False, positions=None),
]
+
expected_opinfo_f = [
- Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='c', argrepr='c', offset=0, starts_line=None, is_jump_target=False),
- Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='d', argrepr='d', offset=2, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=4, starts_line=3, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=3, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=4, argval='b', argrepr='b', offset=8, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='c', argrepr='c', offset=10, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
- Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=14, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=16, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=18, starts_line=None, is_jump_target=False),
- Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=20, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=22, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=24, starts_line=5, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='a', argrepr='a', offset=26, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='b', argrepr='b', offset=28, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='c', argrepr='c', offset=30, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='d', argrepr='d', offset=32, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=34, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=38, starts_line=6, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=40, starts_line=None, is_jump_target=False),
+ Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='c', argrepr='c', offset=0, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='d', argrepr='d', offset=2, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=4, starts_line=3, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=3, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=4, argval='b', argrepr='b', offset=8, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='c', argrepr='c', offset=10, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=14, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=16, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=18, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=20, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=22, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=24, starts_line=5, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='a', argrepr='a', offset=26, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='b', argrepr='b', offset=28, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='c', argrepr='c', offset=30, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='d', argrepr='d', offset=32, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=34, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=38, starts_line=6, is_jump_target=False, positions=None),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=40, starts_line=None, is_jump_target=False, positions=None),
]
expected_opinfo_inner = [
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=5, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=5, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False, positions=None),
]
expected_opinfo_jumpy = [
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
- Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False),
- Instruction(opname='FOR_ITER', opcode=93, arg=17, argval=44, argrepr='to 44', offset=8, starts_line=None, is_jump_target=True),
- Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=15, argval=30, argrepr='to 30', offset=26, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=28, starts_line=6, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=21, argval=42, argrepr='to 42', offset=36, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=42, starts_line=7, is_jump_target=True),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=48, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, starts_line=11, is_jump_target=True),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=48, argval=96, argrepr='to 96', offset=54, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=56, starts_line=12, is_jump_target=True),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=58, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=60, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=62, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=64, starts_line=13, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=66, starts_line=None, is_jump_target=False),
- Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=68, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=70, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=72, starts_line=14, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=74, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=76, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=41, argval=82, argrepr='to 82', offset=78, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=80, starts_line=15, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=82, starts_line=16, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
- Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=46, argval=92, argrepr='to 92', offset=88, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=92, starts_line=11, is_jump_target=True),
- Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=28, argval=56, argrepr='to 56', offset=94, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=96, starts_line=19, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=98, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=100, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
- Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=104, starts_line=20, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=106, starts_line=21, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=108, starts_line=None, is_jump_target=False),
- Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=14, argval=144, argrepr='to 144', offset=114, starts_line=None, is_jump_target=False),
- Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False),
- Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=109, argval=218, argrepr='to 218', offset=122, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=130, starts_line=23, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=132, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=134, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=25, argval=192, argrepr='to 192', offset=140, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=142, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, starts_line=25, is_jump_target=True),
- Instruction(opname='BEFORE_WITH', opcode=53, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False),
- Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=148, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=150, starts_line=26, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=152, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=158, starts_line=25, is_jump_target=False),
- Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=None, is_jump_target=False),
- Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=164, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
- Instruction(opname='JUMP_FORWARD', opcode=110, arg=11, argval=192, argrepr='to 192', offset=168, starts_line=None, is_jump_target=False),
- Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False),
- Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=90, argval=180, argrepr='to 180', offset=174, starts_line=None, is_jump_target=False),
- Instruction(opname='RERAISE', opcode=119, arg=4, argval=4, argrepr='', offset=176, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=180, starts_line=None, is_jump_target=True),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=190, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=192, starts_line=28, is_jump_target=True),
- Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=194, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=196, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=198, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=200, starts_line=None, is_jump_target=False),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=202, starts_line=None, is_jump_target=False),
- Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=206, starts_line=None, is_jump_target=False),
- Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=208, starts_line=None, is_jump_target=False),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=210, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False),
- Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=214, starts_line=None, is_jump_target=False),
- Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=216, starts_line=None, is_jump_target=False),
- Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=218, starts_line=22, is_jump_target=True),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='FOR_ITER', opcode=93, arg=17, argval=44, argrepr='to 44', offset=8, starts_line=None, is_jump_target=True, positions=None),
+ Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=15, argval=30, argrepr='to 30', offset=26, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=28, starts_line=6, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=21, argval=42, argrepr='to 42', offset=36, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=42, starts_line=7, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=48, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, starts_line=11, is_jump_target=True, positions=None),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=48, argval=96, argrepr='to 96', offset=54, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=56, starts_line=12, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=58, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=60, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=62, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=64, starts_line=13, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=66, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=68, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=70, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=72, starts_line=14, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=74, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=76, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=41, argval=82, argrepr='to 82', offset=78, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=80, starts_line=15, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=82, starts_line=16, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=46, argval=92, argrepr='to 92', offset=88, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=92, starts_line=11, is_jump_target=True, positions=None),
+ Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=28, argval=56, argrepr='to 56', offset=94, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=96, starts_line=19, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=98, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=100, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=104, starts_line=20, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=106, starts_line=21, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=108, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_FORWARD', opcode=110, arg=14, argval=144, argrepr='to 144', offset=114, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=109, argval=218, argrepr='to 218', offset=122, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=130, starts_line=23, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=132, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=134, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_FORWARD', opcode=110, arg=25, argval=192, argrepr='to 192', offset=140, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=142, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, starts_line=25, is_jump_target=True, positions=None),
+ Instruction(opname='BEFORE_WITH', opcode=53, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=148, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=150, starts_line=26, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=152, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=158, starts_line=25, is_jump_target=False, positions=None),
+ Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=164, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_FORWARD', opcode=110, arg=11, argval=192, argrepr='to 192', offset=168, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=90, argval=180, argrepr='to 180', offset=174, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='RERAISE', opcode=119, arg=4, argval=4, argrepr='', offset=176, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=180, starts_line=None, is_jump_target=True, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=190, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=192, starts_line=28, is_jump_target=True, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=194, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=196, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=198, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=200, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=202, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=206, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=208, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=210, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=214, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_EXCEPT_AND_RERAISE', opcode=37, arg=None, argval=None, argrepr='', offset=216, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=218, starts_line=22, is_jump_target=True, positions=None),
]
# One last piece of inspect fodder to check the default line number handling
@@ -1149,7 +1157,14 @@ def simple(): pass
]
-class InstructionTests(BytecodeTestCase):
+class InstructionTestCase(BytecodeTestCase):
+
+ def assertInstructionsEqual(self, instrs_1, instrs_2, /):
+ instrs_1 = [instr_1._replace(positions=None) for instr_1 in instrs_1]
+ instrs_2 = [instr_2._replace(positions=None) for instr_2 in instrs_2]
+ self.assertEqual(instrs_1, instrs_2)
+
+class InstructionTests(InstructionTestCase):
def __init__(self, *args):
super().__init__(*args)
@@ -1157,35 +1172,75 @@ def __init__(self, *args):
def test_default_first_line(self):
actual = dis.get_instructions(simple)
- self.assertEqual(list(actual), expected_opinfo_simple)
+ self.assertInstructionsEqual(list(actual), expected_opinfo_simple)
def test_first_line_set_to_None(self):
actual = dis.get_instructions(simple, first_line=None)
- self.assertEqual(list(actual), expected_opinfo_simple)
+ self.assertInstructionsEqual(list(actual), expected_opinfo_simple)
def test_outer(self):
actual = dis.get_instructions(outer, first_line=expected_outer_line)
- self.assertEqual(list(actual), expected_opinfo_outer)
+ self.assertInstructionsEqual(list(actual), expected_opinfo_outer)
def test_nested(self):
with captured_stdout():
f = outer()
actual = dis.get_instructions(f, first_line=expected_f_line)
- self.assertEqual(list(actual), expected_opinfo_f)
+ self.assertInstructionsEqual(list(actual), expected_opinfo_f)
def test_doubly_nested(self):
with captured_stdout():
inner = outer()()
actual = dis.get_instructions(inner, first_line=expected_inner_line)
- self.assertEqual(list(actual), expected_opinfo_inner)
+ self.assertInstructionsEqual(list(actual), expected_opinfo_inner)
def test_jumpy(self):
actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
- self.assertEqual(list(actual), expected_opinfo_jumpy)
+ self.assertInstructionsEqual(list(actual), expected_opinfo_jumpy)
+
+ def test_co_positions(self):
+ code = compile('f(\n x, y, z\n)', '<test>', 'exec')
+ positions = [
+ instr.positions
+ for instr in dis.get_instructions(code)
+ ]
+ expected = [
+ (1, 1, 0, 1),
+ (2, 2, 2, 3),
+ (2, 2, 5, 6),
+ (2, 2, 8, 9),
+ (1, 3, 0, 1),
+ (1, 3, 0, 1),
+ (1, 3, 0, 1),
+ (1, 3, 0, 1)
+ ]
+ self.assertEqual(positions, expected)
+
+ def test_co_positions_missing_info(self):
+ code = compile('x, y, z', '<test>', 'exec')
+ code_without_column_table = code.replace(co_columntable=b'')
+ actual = dis.get_instructions(code_without_column_table)
+ for instruction in actual:
+ with self.subTest(instruction=instruction):
+ start_line, end_line, start_offset, end_offset = instruction.positions
+ assert start_line == 1
+ assert end_line == 1
+ assert start_offset is None
+ assert end_offset is None
+
+ code_without_endline_table = code.replace(co_endlinetable=b'')
+ actual = dis.get_instructions(code_without_endline_table)
+ for instruction in actual:
+ with self.subTest(instruction=instruction):
+ start_line, end_line, start_offset, end_offset = instruction.positions
+ assert start_line == 1
+ assert end_line is None
+ assert start_offset is not None
+ assert end_offset is not None
# get_instructions has its own tests above, so can rely on it to validate
# the object oriented API
-class BytecodeTests(unittest.TestCase):
+class BytecodeTests(InstructionTestCase):
def test_instantiation(self):
# Test with function, method, code string and code object
@@ -1201,11 +1256,11 @@ def test_iteration(self):
with self.subTest(obj=obj):
via_object = list(dis.Bytecode(obj))
via_generator = list(dis.get_instructions(obj))
- self.assertEqual(via_object, via_generator)
+ self.assertInstructionsEqual(via_object, via_generator)
def test_explicit_first_line(self):
actual = dis.Bytecode(outer, first_line=expected_outer_line)
- self.assertEqual(list(actual), expected_opinfo_outer)
+ self.assertInstructionsEqual(list(actual), expected_opinfo_outer)
def test_source_line_in_disassembly(self):
# Use the line in the source code
1
0
bpo-43950: use 0-indexed column offsets for bytecode positions (GH-27011)
by miss-islington 04 Jul '21
by miss-islington 04 Jul '21
04 Jul '21
https://github.com/python/cpython/commit/44f91fc802a2b71312e9abd9e8e9dcbf02…
commit: 44f91fc802a2b71312e9abd9e8e9dcbf02e8216d
branch: main
author: Batuhan Taskaya <batuhan(a)python.org>
committer: miss-islington <31488909+miss-islington(a)users.noreply.github.com>
date: 2021-07-04T11:02:16-07:00
summary:
bpo-43950: use 0-indexed column offsets for bytecode positions (GH-27011)
files:
M Lib/test/test_code.py
M Lib/test/test_compile.py
M Objects/codeobject.c
diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py
index 55ba30a9f936a..24d27c0d24046 100644
--- a/Lib/test/test_code.py
+++ b/Lib/test/test_code.py
@@ -344,7 +344,7 @@ def test_co_positions_artificial_instructions(self):
# get assigned the first_lineno but they don't have other positions.
# There is no easy way of inferring them at that stage, so for now
# we don't support it.
- self.assertTrue(all(positions) or not any(positions))
+ self.assertTrue(positions.count(None) in [0, 4])
if not any(positions):
artificial_instructions.append(instr)
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 47deda09f9010..bc8c57d9e1d2f 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -1006,8 +1006,8 @@ def generic_visit(self, node):
return
lines.add(node.lineno)
end_lines.add(node.end_lineno)
- columns.add(node.col_offset + 1)
- end_columns.add(node.end_col_offset + 1)
+ columns.add(node.col_offset)
+ end_columns.add(node.end_col_offset)
SourceOffsetVisitor().visit(ast_tree)
@@ -1058,10 +1058,10 @@ def test_compiles_to_extended_op_arg(self):
self.assertOpcodeSourcePositionIs(compiled_code, 'INPLACE_SUBTRACT',
line=10_000 + 2, end_line=10_000 + 2,
- column=3, end_column=9)
+ column=2, end_column=8)
self.assertOpcodeSourcePositionIs(compiled_code, 'INPLACE_ADD',
line=10_000 + 4, end_line=10_000 + 4,
- column=3, end_column=10)
+ column=2, end_column=9)
def test_multiline_expression(self):
snippet = """\
@@ -1071,7 +1071,7 @@ def test_multiline_expression(self):
"""
compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL_FUNCTION',
- line=1, end_line=3, column=1, end_column=2)
+ line=1, end_line=3, column=0, end_column=1)
def test_very_long_line_end_offset(self):
# Make sure we get None for when the column offset is too large to
@@ -1088,15 +1088,15 @@ def test_complex_single_line_expression(self):
compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_SUBSCR',
- line=1, end_line=1, column=14, end_column=22)
+ line=1, end_line=1, column=13, end_column=21)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_MULTIPLY',
- line=1, end_line=1, column=10, end_column=22)
+ line=1, end_line=1, column=9, end_column=21)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_ADD',
- line=1, end_line=1, column=10, end_column=27)
+ line=1, end_line=1, column=9, end_column=26)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_MATRIX_MULTIPLY',
- line=1, end_line=1, column=5, end_column=28)
+ line=1, end_line=1, column=4, end_column=27)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_SUBTRACT',
- line=1, end_line=1, column=1, end_column=28)
+ line=1, end_line=1, column=0, end_column=27)
class TestExpressionStackSize(unittest.TestCase):
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 678958891a0bc..aa36175aa5ad5 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -610,9 +610,6 @@ PyCode_Addr2Location(PyCodeObject *co, int addrq,
int *end_line, int *end_column)
{
*start_line = PyCode_Addr2Line(co, addrq);
- if (*start_line == -1) {
- *start_line = 0;
- }
*start_column = _PyCode_Addr2Offset(co, addrq);
*end_line = _PyCode_Addr2EndLine(co, addrq);
*end_column = _PyCode_Addr2EndOffset(co, addrq);
@@ -626,7 +623,7 @@ _PyCode_Addr2EndLine(PyCodeObject* co, int addrq)
return co->co_firstlineno;
}
else if (co->co_endlinetable == Py_None) {
- return 0;
+ return -1;
}
assert(addrq >= 0 && addrq < PyBytes_GET_SIZE(co->co_code));
@@ -639,34 +636,34 @@ int
_PyCode_Addr2Offset(PyCodeObject* co, int addrq)
{
if (co->co_columntable == Py_None || addrq < 0) {
- return 0;
+ return -1;
}
if (addrq % 2 == 1) {
--addrq;
}
if (addrq >= PyBytes_GET_SIZE(co->co_columntable)) {
- return 0;
+ return -1;
}
unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
- return bytes[addrq];
+ return bytes[addrq] - 1;
}
int
_PyCode_Addr2EndOffset(PyCodeObject* co, int addrq)
{
if (co->co_columntable == Py_None || addrq < 0) {
- return 0;
+ return -1;
}
if (addrq % 2 == 0) {
++addrq;
}
if (addrq >= PyBytes_GET_SIZE(co->co_columntable)) {
- return 0;
+ return -1;
}
unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
- return bytes[addrq];
+ return bytes[addrq] - 1;
}
void
@@ -980,7 +977,7 @@ positionsiter_dealloc(positionsiterator* pi)
static PyObject*
_source_offset_converter(int* value) {
- if (*value <= 0) {
+ if (*value == -1) {
Py_RETURN_NONE;
}
return PyLong_FromLong(*value);
1
0
bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016) (GH-27018)
by pablogsal 04 Jul '21
by pablogsal 04 Jul '21
04 Jul '21
https://github.com/python/cpython/commit/4684a34c8d2a2ffac7b779edb4ba57f043…
commit: 4684a34c8d2a2ffac7b779edb4ba57f043783478
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: pablogsal <Pablogsal(a)gmail.com>
date: 2021-07-04T18:55:43+01:00
summary:
bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016) (GH-27018)
(cherry picked from commit d33943a6c368c2184e238019c63ac7a267da5594)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner(a)users.noreply.github.com>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner(a)users.noreply.github.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
M Objects/genericaliasobject.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
new file mode 100644
index 0000000000000..2fc65bcfdeef4
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
@@ -0,0 +1,2 @@
+Remove uses of :c:func:`PyObject_GC_Del` in error path when initializing
+:class:`types.GenericAlias`.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index 945d20593c7c9..bb5fa095d3573 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -602,7 +602,7 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
if (!setup_ga(self, origin, arguments)) {
- type->tp_free((PyObject *)self);
+ Py_DECREF(self);
return NULL;
}
return (PyObject *)self;
@@ -644,10 +644,10 @@ Py_GenericAlias(PyObject *origin, PyObject *args)
if (alias == NULL) {
return NULL;
}
+ _PyObject_GC_TRACK(alias);
if (!setup_ga(alias, origin, args)) {
- PyObject_GC_Del((PyObject *)alias);
+ Py_DECREF(alias);
return NULL;
}
- _PyObject_GC_TRACK(alias);
return (PyObject *)alias;
}
1
0
bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016)
by miss-islington 04 Jul '21
by miss-islington 04 Jul '21
04 Jul '21
https://github.com/python/cpython/commit/68330b681a4b63cedad58fcfd1d9573209…
commit: 68330b681a4b63cedad58fcfd1d9573209bad21d
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: miss-islington <31488909+miss-islington(a)users.noreply.github.com>
date: 2021-07-04T10:55:35-07:00
summary:
bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016)
(cherry picked from commit d33943a6c368c2184e238019c63ac7a267da5594)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner(a)users.noreply.github.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
M Objects/genericaliasobject.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
new file mode 100644
index 0000000000000..2fc65bcfdeef4
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
@@ -0,0 +1,2 @@
+Remove uses of :c:func:`PyObject_GC_Del` in error path when initializing
+:class:`types.GenericAlias`.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index 756a7ce474aee..48a8be1c45875 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -603,7 +603,7 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
if (!setup_ga(self, origin, arguments)) {
- type->tp_free((PyObject *)self);
+ Py_DECREF(self);
return NULL;
}
return (PyObject *)self;
@@ -650,10 +650,10 @@ Py_GenericAlias(PyObject *origin, PyObject *args)
if (alias == NULL) {
return NULL;
}
+ _PyObject_GC_TRACK(alias);
if (!setup_ga(alias, origin, args)) {
- PyObject_GC_Del((PyObject *)alias);
+ Py_DECREF(alias);
return NULL;
}
- _PyObject_GC_TRACK(alias);
return (PyObject *)alias;
}
1
0
bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016)
by pablogsal 04 Jul '21
by pablogsal 04 Jul '21
04 Jul '21
https://github.com/python/cpython/commit/d33943a6c368c2184e238019c63ac7a267…
commit: d33943a6c368c2184e238019c63ac7a267da5594
branch: main
author: Ken Jin <28750310+Fidget-Spinner(a)users.noreply.github.com>
committer: pablogsal <Pablogsal(a)gmail.com>
date: 2021-07-04T17:47:38+01:00
summary:
bpo-44562: Remove invalid PyObject_GC_Del from error path of types.GenericAlias … (GH-27016)
files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
M Objects/genericaliasobject.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
new file mode 100644
index 0000000000000..2fc65bcfdeef4
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-04-23-38-51.bpo-44562.QdeRQo.rst
@@ -0,0 +1,2 @@
+Remove uses of :c:func:`PyObject_GC_Del` in error path when initializing
+:class:`types.GenericAlias`.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index 756a7ce474aee..48a8be1c45875 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -603,7 +603,7 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
if (!setup_ga(self, origin, arguments)) {
- type->tp_free((PyObject *)self);
+ Py_DECREF(self);
return NULL;
}
return (PyObject *)self;
@@ -650,10 +650,10 @@ Py_GenericAlias(PyObject *origin, PyObject *args)
if (alias == NULL) {
return NULL;
}
+ _PyObject_GC_TRACK(alias);
if (!setup_ga(alias, origin, args)) {
- PyObject_GC_Del((PyObject *)alias);
+ Py_DECREF(alias);
return NULL;
}
- _PyObject_GC_TRACK(alias);
return (PyObject *)alias;
}
1
0
https://github.com/python/cpython/commit/9bf7c2d638a582af2444bc864feba13ab8…
commit: 9bf7c2d638a582af2444bc864feba13ab8957b68
branch: 3.10
author: Ethan Furman <ethan(a)stoneleaf.us>
committer: ethanfurman <ethan(a)stoneleaf.us>
date: 2021-07-03T21:08:42-07:00
summary:
[3.10] bpo-44559: [Enum] revert enum module to 3.9 (GH-27010)
* [Enum] revert enum module to 3.9
files:
D Doc/howto/enum.rst
M Doc/howto/index.rst
M Doc/library/enum.rst
M Doc/library/http.rst
M Doc/library/socket.rst
M Doc/library/ssl.rst
M Doc/whatsnew/3.10.rst
M Lib/ast.py
M Lib/enum.py
M Lib/http/__init__.py
M Lib/inspect.py
M Lib/plistlib.py
M Lib/pstats.py
M Lib/re.py
M Lib/ssl.py
M Lib/test/test_ast.py
M Lib/test/test_enum.py
M Lib/test/test_httplib.py
M Lib/test/test_pstats.py
M Lib/test/test_pydoc.py
M Lib/test/test_re.py
M Lib/test/test_signal.py
M Lib/test/test_socket.py
M Lib/test/test_ssl.py
M Lib/test/test_unicode.py
M Lib/test/test_uuid.py
M Lib/tkinter/__init__.py
M Lib/tkinter/test/test_tkinter/test_misc.py
M Lib/uuid.py
diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst
deleted file mode 100644
index b0eea5560afce..0000000000000
--- a/Doc/howto/enum.rst
+++ /dev/null
@@ -1,1416 +0,0 @@
-==========
-Enum HOWTO
-==========
-
-:Author: Ethan Furman <ethan at stoneleaf dot us>
-
-.. _enum-basic-tutorial:
-
-.. currentmodule:: enum
-
-Basic Enum Tutorial
--------------------
-
-An :class:`Enum` is a set of symbolic names bound to unique values. They are
-similar to global variables, but they offer a more useful :func:`repr()`,
-grouping, type-safety, and a few other features.
-
-They are most useful when you have a variable that can take one of a limited
-selection of values. For example, the days of the week::
-
- >>> from enum import Enum
- >>> class Weekday(Enum):
- ... MONDAY = 1
- ... TUESDAY = 2
- ... WEDNESDAY = 3
- ... THURSDAY = 4
- ... FRIDAY = 5
- ... SATURDAY = 6
- ... SUNDAY = 7
-
-As you can see, creating an :class:`Enum` is as simple as writing a class that
-inherits from :class:`Enum` itself.
-
-.. note:: Case of Enum Members
-
- Because Enums are used to represent constants we recommend using
- UPPER_CASE names for members, and will be using that style in our examples.
-
-Depending on the nature of the enum a member's value may or may not be
-important, but either way that value can be used to get the corresponding
-member::
-
- >>> Weekday(3)
- Weekday.WEDNESDAY
-
-As you can see, the ``repr()`` of a member shows the enum name and the
-member name. The ``str()`` on a member shows only its name::
-
- >>> print(Weekday.THURSDAY)
- THURSDAY
-
-The *type* of an enumeration member is the enum it belongs to::
-
- >>> type(Weekday.MONDAY)
- <enum 'Weekday'>
- >>> isinstance(Weekday.FRIDAY, Weekday)
- True
-
-Enum members have an attribute that contains just their :attr:`name`::
-
- >>> print(Weekday.TUESDAY.name)
- TUESDAY
-
-Likewise, they have an attribute for their :attr:`value`::
-
-
- >>> Weekday.WEDNESDAY.value
- 3
-
-Unlike many languages that treat enumerations solely as name/value pairs,
-Python Enums can have behavior added. For example, :class:`datetime.date`
-has two methods for returning the weekday: :meth:`weekday` and :meth:`isoweekday`.
-The difference is that one of them counts from 0-6 and the other from 1-7.
-Rather than keep track of that ourselves we can add a method to the :class:`Weekday`
-enum to extract the day from the :class:`date` instance and return the matching
-enum member::
-
- @classmethod
- def from_date(cls, date):
- return cls(date.isoweekday())
-
-The complete :class:`Weekday` enum now looks like this::
-
- >>> class Weekday(Enum):
- ... MONDAY = 1
- ... TUESDAY = 2
- ... WEDNESDAY = 3
- ... THURSDAY = 4
- ... FRIDAY = 5
- ... SATURDAY = 6
- ... SUNDAY = 7
- ... #
- ... @classmethod
- ... def from_date(cls, date):
- ... return cls(date.isoweekday())
-
-Now we can find out what today is! Observe::
-
- >>> from datetime import date
- >>> Weekday.from_date(date.today())
- Weekday.TUESDAY
-
-Of course, if you're reading this on some other day, you'll see that day instead.
-
-This :class:`Weekday` enum is great if our variable only needs one day, but
-what if we need several? Maybe we're writing a function to plot chores during
-a week, and don't want to use a :class:`list` -- we could use a different type
-of :class:`Enum`::
-
- >>> from enum import Flag
- >>> class Weekday(Flag):
- ... MONDAY = 1
- ... TUESDAY = 2
- ... WEDNESDAY = 4
- ... THURSDAY = 8
- ... FRIDAY = 16
- ... SATURDAY = 32
- ... SUNDAY = 64
-
-We've changed two things: we're inherited from :class:`Flag`, and the values are
-all powers of 2.
-
-Just like the original :class:`Weekday` enum above, we can have a single selection::
-
- >>> first_week_day = Weekday.MONDAY
- >>> first_week_day
- Weekday.MONDAY
-
-But :class:`Flag` also allows us to combine several members into a single
-variable::
-
- >>> weekend = Weekday.SATURDAY | Weekday.SUNDAY
- >>> weekend
- Weekday.SATURDAY|Weekday.SUNDAY
-
-You can even iterate over a :class:`Flag` variable::
-
- >>> for day in weekend:
- ... print(day)
- SATURDAY
- SUNDAY
-
-Okay, let's get some chores set up::
-
- >>> chores_for_ethan = {
- ... 'feed the cat': Weekday.MONDAY | Weekday.WEDNESDAY | Weekday.FRIDAY,
- ... 'do the dishes': Weekday.TUESDAY | Weekday.THURSDAY,
- ... 'answer SO questions': Weekday.SATURDAY,
- ... }
-
-And a function to display the chores for a given day::
-
- >>> def show_chores(chores, day):
- ... for chore, days in chores.items():
- ... if day in days:
- ... print(chore)
- >>> show_chores(chores_for_ethan, Weekday.SATURDAY)
- answer SO questions
-
-In cases where the actual values of the members do not matter, you can save
-yourself some work and use :func:`auto()` for the values::
-
- >>> from enum import auto
- >>> class Weekday(Flag):
- ... MONDAY = auto()
- ... TUESDAY = auto()
- ... WEDNESDAY = auto()
- ... THURSDAY = auto()
- ... FRIDAY = auto()
- ... SATURDAY = auto()
- ... SUNDAY = auto()
-
-
-.. _enum-advanced-tutorial:
-
-Programmatic access to enumeration members and their attributes
----------------------------------------------------------------
-
-Sometimes it's useful to access members in enumerations programmatically (i.e.
-situations where ``Color.RED`` won't do because the exact color is not known
-at program-writing time). ``Enum`` allows such access::
-
- >>> Color(1)
- Color.RED
- >>> Color(3)
- Color.BLUE
-
-If you want to access enum members by *name*, use item access::
-
- >>> Color['RED']
- Color.RED
- >>> Color['GREEN']
- Color.GREEN
-
-If you have an enum member and need its :attr:`name` or :attr:`value`::
-
- >>> member = Color.RED
- >>> member.name
- 'RED'
- >>> member.value
- 1
-
-
-Duplicating enum members and values
------------------------------------
-
-Having two enum members with the same name is invalid::
-
- >>> class Shape(Enum):
- ... SQUARE = 2
- ... SQUARE = 3
- ...
- Traceback (most recent call last):
- ...
- TypeError: 'SQUARE' already defined as: 2
-
-However, an enum member can have other names associated with it. Given two
-entries ``A`` and ``B`` with the same value (and ``A`` defined first), ``B``
-is an alias for the member ``A``. By-value lookup of the value of ``A`` will
-return the member ``A``. By-name lookup of ``A`` will return the member ``A``.
-By-name lookup of ``B`` will also return the member ``A``::
-
- >>> class Shape(Enum):
- ... SQUARE = 2
- ... DIAMOND = 1
- ... CIRCLE = 3
- ... ALIAS_FOR_SQUARE = 2
- ...
- >>> Shape.SQUARE
- Shape.SQUARE
- >>> Shape.ALIAS_FOR_SQUARE
- Shape.SQUARE
- >>> Shape(2)
- Shape.SQUARE
-
-.. note::
-
- Attempting to create a member with the same name as an already
- defined attribute (another member, a method, etc.) or attempting to create
- an attribute with the same name as a member is not allowed.
-
-
-Ensuring unique enumeration values
-----------------------------------
-
-By default, enumerations allow multiple names as aliases for the same value.
-When this behavior isn't desired, you can use the :func:`unique` decorator::
-
- >>> from enum import Enum, unique
- >>> @unique
- ... class Mistake(Enum):
- ... ONE = 1
- ... TWO = 2
- ... THREE = 3
- ... FOUR = 3
- ...
- Traceback (most recent call last):
- ...
- ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
-
-
-Using automatic values
-----------------------
-
-If the exact value is unimportant you can use :class:`auto`::
-
- >>> from enum import Enum, auto
- >>> class Color(Enum):
- ... RED = auto()
- ... BLUE = auto()
- ... GREEN = auto()
- ...
- >>> [member.value for member in Color]
- [1, 2, 3]
-
-The values are chosen by :func:`_generate_next_value_`, which can be
-overridden::
-
- >>> class AutoName(Enum):
- ... def _generate_next_value_(name, start, count, last_values):
- ... return name
- ...
- >>> class Ordinal(AutoName):
- ... NORTH = auto()
- ... SOUTH = auto()
- ... EAST = auto()
- ... WEST = auto()
- ...
- >>> [member.value for member in Ordinal]
- ['NORTH', 'SOUTH', 'EAST', 'WEST']
-
-.. note::
-
- The :meth:`_generate_next_value_` method must be defined before any members.
-
-Iteration
----------
-
-Iterating over the members of an enum does not provide the aliases::
-
- >>> list(Shape)
- [Shape.SQUARE, Shape.DIAMOND, Shape.CIRCLE]
-
-The special attribute ``__members__`` is a read-only ordered mapping of names
-to members. It includes all names defined in the enumeration, including the
-aliases::
-
- >>> for name, member in Shape.__members__.items():
- ... name, member
- ...
- ('SQUARE', Shape.SQUARE)
- ('DIAMOND', Shape.DIAMOND)
- ('CIRCLE', Shape.CIRCLE)
- ('ALIAS_FOR_SQUARE', Shape.SQUARE)
-
-The ``__members__`` attribute can be used for detailed programmatic access to
-the enumeration members. For example, finding all the aliases::
-
- >>> [name for name, member in Shape.__members__.items() if member.name != name]
- ['ALIAS_FOR_SQUARE']
-
-
-Comparisons
------------
-
-Enumeration members are compared by identity::
-
- >>> Color.RED is Color.RED
- True
- >>> Color.RED is Color.BLUE
- False
- >>> Color.RED is not Color.BLUE
- True
-
-Ordered comparisons between enumeration values are *not* supported. Enum
-members are not integers (but see `IntEnum`_ below)::
-
- >>> Color.RED < Color.BLUE
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: '<' not supported between instances of 'Color' and 'Color'
-
-Equality comparisons are defined though::
-
- >>> Color.BLUE == Color.RED
- False
- >>> Color.BLUE != Color.RED
- True
- >>> Color.BLUE == Color.BLUE
- True
-
-Comparisons against non-enumeration values will always compare not equal
-(again, :class:`IntEnum` was explicitly designed to behave differently, see
-below)::
-
- >>> Color.BLUE == 2
- False
-
-
-Allowed members and attributes of enumerations
-----------------------------------------------
-
-Most of the examples above use integers for enumeration values. Using integers is
-short and handy (and provided by default by the `Functional API`_), but not
-strictly enforced. In the vast majority of use-cases, one doesn't care what
-the actual value of an enumeration is. But if the value *is* important,
-enumerations can have arbitrary values.
-
-Enumerations are Python classes, and can have methods and special methods as
-usual. If we have this enumeration::
-
- >>> class Mood(Enum):
- ... FUNKY = 1
- ... HAPPY = 3
- ...
- ... def describe(self):
- ... # self is the member here
- ... return self.name, self.value
- ...
- ... def __str__(self):
- ... return 'my custom str! {0}'.format(self.value)
- ...
- ... @classmethod
- ... def favorite_mood(cls):
- ... # cls here is the enumeration
- ... return cls.HAPPY
- ...
-
-Then::
-
- >>> Mood.favorite_mood()
- Mood.HAPPY
- >>> Mood.HAPPY.describe()
- ('HAPPY', 3)
- >>> str(Mood.FUNKY)
- 'my custom str! 1'
-
-The rules for what is allowed are as follows: names that start and end with
-a single underscore are reserved by enum and cannot be used; all other
-attributes defined within an enumeration will become members of this
-enumeration, with the exception of special methods (:meth:`__str__`,
-:meth:`__add__`, etc.), descriptors (methods are also descriptors), and
-variable names listed in :attr:`_ignore_`.
-
-Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then
-any value(s) given to the enum member will be passed into those methods.
-See `Planet`_ for an example.
-
-
-Restricted Enum subclassing
----------------------------
-
-A new :class:`Enum` class must have one base enum class, up to one concrete
-data type, and as many :class:`object`-based mixin classes as needed. The
-order of these base classes is::
-
- class EnumName([mix-in, ...,] [data-type,] base-enum):
- pass
-
-Also, subclassing an enumeration is allowed only if the enumeration does not define
-any members. So this is forbidden::
-
- >>> class MoreColor(Color):
- ... PINK = 17
- ...
- Traceback (most recent call last):
- ...
- TypeError: MoreColor: cannot extend enumeration 'Color'
-
-But this is allowed::
-
- >>> class Foo(Enum):
- ... def some_behavior(self):
- ... pass
- ...
- >>> class Bar(Foo):
- ... HAPPY = 1
- ... SAD = 2
- ...
-
-Allowing subclassing of enums that define members would lead to a violation of
-some important invariants of types and instances. On the other hand, it makes
-sense to allow sharing some common behavior between a group of enumerations.
-(See `OrderedEnum`_ for an example.)
-
-
-Pickling
---------
-
-Enumerations can be pickled and unpickled::
-
- >>> from test.test_enum import Fruit
- >>> from pickle import dumps, loads
- >>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO))
- True
-
-The usual restrictions for pickling apply: picklable enums must be defined in
-the top level of a module, since unpickling requires them to be importable
-from that module.
-
-.. note::
-
- With pickle protocol version 4 it is possible to easily pickle enums
- nested in other classes.
-
-It is possible to modify how enum members are pickled/unpickled by defining
-:meth:`__reduce_ex__` in the enumeration class.
-
-
-Functional API
---------------
-
-The :class:`Enum` class is callable, providing the following functional API::
-
- >>> Animal = Enum('Animal', 'ANT BEE CAT DOG')
- >>> Animal
- <enum 'Animal'>
- >>> Animal.ANT
- Animal.ANT
- >>> Animal.ANT.value
- 1
- >>> list(Animal)
- [Animal.ANT, Animal.BEE, Animal.CAT, Animal.DOG]
-
-The semantics of this API resemble :class:`~collections.namedtuple`. The first
-argument of the call to :class:`Enum` is the name of the enumeration.
-
-The second argument is the *source* of enumeration member names. It can be a
-whitespace-separated string of names, a sequence of names, a sequence of
-2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to
-values. The last two options enable assigning arbitrary values to
-enumerations; the others auto-assign increasing integers starting with 1 (use
-the ``start`` parameter to specify a different starting value). A
-new class derived from :class:`Enum` is returned. In other words, the above
-assignment to :class:`Animal` is equivalent to::
-
- >>> class Animal(Enum):
- ... ANT = 1
- ... BEE = 2
- ... CAT = 3
- ... DOG = 4
- ...
-
-The reason for defaulting to ``1`` as the starting number and not ``0`` is
-that ``0`` is ``False`` in a boolean sense, but by default enum members all
-evaluate to ``True``.
-
-Pickling enums created with the functional API can be tricky as frame stack
-implementation details are used to try and figure out which module the
-enumeration is being created in (e.g. it will fail if you use a utility
-function in separate module, and also may not work on IronPython or Jython).
-The solution is to specify the module name explicitly as follows::
-
- >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__)
-
-.. warning::
-
- If ``module`` is not supplied, and Enum cannot determine what it is,
- the new Enum members will not be unpicklable; to keep errors closer to
- the source, pickling will be disabled.
-
-The new pickle protocol 4 also, in some circumstances, relies on
-:attr:`~definition.__qualname__` being set to the location where pickle will be able
-to find the class. For example, if the class was made available in class
-SomeData in the global scope::
-
- >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal')
-
-The complete signature is::
-
- Enum(
- value='NewEnumName',
- names=<...>,
- *,
- module='...',
- qualname='...',
- type=<mixed-in class>,
- start=1,
- )
-
-:value: What the new enum class will record as its name.
-
-:names: The enum members. This can be a whitespace or comma separated string
- (values will start at 1 unless otherwise specified)::
-
- 'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE'
-
- or an iterator of names::
-
- ['RED', 'GREEN', 'BLUE']
-
- or an iterator of (name, value) pairs::
-
- [('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)]
-
- or a mapping::
-
- {'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42}
-
-:module: name of module where new enum class can be found.
-
-:qualname: where in module new enum class can be found.
-
-:type: type to mix in to new enum class.
-
-:start: number to start counting at if only names are passed in.
-
-.. versionchanged:: 3.5
- The *start* parameter was added.
-
-
-Derived Enumerations
---------------------
-
-IntEnum
-^^^^^^^
-
-The first variation of :class:`Enum` that is provided is also a subclass of
-:class:`int`. Members of an :class:`IntEnum` can be compared to integers;
-by extension, integer enumerations of different types can also be compared
-to each other::
-
- >>> from enum import IntEnum
- >>> class Shape(IntEnum):
- ... CIRCLE = 1
- ... SQUARE = 2
- ...
- >>> class Request(IntEnum):
- ... POST = 1
- ... GET = 2
- ...
- >>> Shape == 1
- False
- >>> Shape.CIRCLE == 1
- True
- >>> Shape.CIRCLE == Request.POST
- True
-
-However, they still can't be compared to standard :class:`Enum` enumerations::
-
- >>> class Shape(IntEnum):
- ... CIRCLE = 1
- ... SQUARE = 2
- ...
- >>> class Color(Enum):
- ... RED = 1
- ... GREEN = 2
- ...
- >>> Shape.CIRCLE == Color.RED
- False
-
-:class:`IntEnum` values behave like integers in other ways you'd expect::
-
- >>> int(Shape.CIRCLE)
- 1
- >>> ['a', 'b', 'c'][Shape.CIRCLE]
- 'b'
- >>> [i for i in range(Shape.SQUARE)]
- [0, 1]
-
-
-StrEnum
-^^^^^^^
-
-The second variation of :class:`Enum` that is provided is also a subclass of
-:class:`str`. Members of a :class:`StrEnum` can be compared to strings;
-by extension, string enumerations of different types can also be compared
-to each other. :class:`StrEnum` exists to help avoid the problem of getting
-an incorrect member::
-
- >>> from enum import StrEnum
- >>> class Directions(StrEnum):
- ... NORTH = 'north', # notice the trailing comma
- ... SOUTH = 'south'
-
-Before :class:`StrEnum`, ``Directions.NORTH`` would have been the :class:`tuple`
-``('north',)``.
-
-.. versionadded:: 3.10
-
-
-IntFlag
-^^^^^^^
-
-The next variation of :class:`Enum` provided, :class:`IntFlag`, is also based
-on :class:`int`. The difference being :class:`IntFlag` members can be combined
-using the bitwise operators (&, \|, ^, ~) and the result is still an
-:class:`IntFlag` member, if possible. However, as the name implies, :class:`IntFlag`
-members also subclass :class:`int` and can be used wherever an :class:`int` is
-used.
-
-.. note::
-
- Any operation on an :class:`IntFlag` member besides the bit-wise operations will
- lose the :class:`IntFlag` membership.
-
- Bit-wise operations that result in invalid :class:`IntFlag` values will lose the
- :class:`IntFlag` membership. See :class:`FlagBoundary` for
- details.
-
-.. versionadded:: 3.6
-.. versionchanged:: 3.10
-
-Sample :class:`IntFlag` class::
-
- >>> from enum import IntFlag
- >>> class Perm(IntFlag):
- ... R = 4
- ... W = 2
- ... X = 1
- ...
- >>> Perm.R | Perm.W
- Perm.R|Perm.W
- >>> Perm.R + Perm.W
- 6
- >>> RW = Perm.R | Perm.W
- >>> Perm.R in RW
- True
-
-It is also possible to name the combinations::
-
- >>> class Perm(IntFlag):
- ... R = 4
- ... W = 2
- ... X = 1
- ... RWX = 7
- >>> Perm.RWX
- Perm.RWX
- >>> ~Perm.RWX
- Perm(0)
- >>> Perm(7)
- Perm.RWX
-
-.. note::
-
- Named combinations are considered aliases. Aliases do not show up during
- iteration, but can be returned from by-value lookups.
-
-.. versionchanged:: 3.10
-
-Another important difference between :class:`IntFlag` and :class:`Enum` is that
-if no flags are set (the value is 0), its boolean evaluation is :data:`False`::
-
- >>> Perm.R & Perm.X
- Perm(0)
- >>> bool(Perm.R & Perm.X)
- False
-
-Because :class:`IntFlag` members are also subclasses of :class:`int` they can
-be combined with them (but may lose :class:`IntFlag` membership::
-
- >>> Perm.X | 4
- Perm.R|Perm.X
-
- >>> Perm.X | 8
- 9
-
-.. note::
-
- The negation operator, ``~``, always returns an :class:`IntFlag` member with a
- positive value::
-
- >>> (~Perm.X).value == (Perm.R|Perm.W).value == 6
- True
-
-:class:`IntFlag` members can also be iterated over::
-
- >>> list(RW)
- [Perm.R, Perm.W]
-
-.. versionadded:: 3.10
-
-
-Flag
-^^^^
-
-The last variation is :class:`Flag`. Like :class:`IntFlag`, :class:`Flag`
-members can be combined using the bitwise operators (&, \|, ^, ~). Unlike
-:class:`IntFlag`, they cannot be combined with, nor compared against, any
-other :class:`Flag` enumeration, nor :class:`int`. While it is possible to
-specify the values directly it is recommended to use :class:`auto` as the
-value and let :class:`Flag` select an appropriate value.
-
-.. versionadded:: 3.6
-
-Like :class:`IntFlag`, if a combination of :class:`Flag` members results in no
-flags being set, the boolean evaluation is :data:`False`::
-
- >>> from enum import Flag, auto
- >>> class Color(Flag):
- ... RED = auto()
- ... BLUE = auto()
- ... GREEN = auto()
- ...
- >>> Color.RED & Color.GREEN
- Color(0)
- >>> bool(Color.RED & Color.GREEN)
- False
-
-Individual flags should have values that are powers of two (1, 2, 4, 8, ...),
-while combinations of flags won't::
-
- >>> class Color(Flag):
- ... RED = auto()
- ... BLUE = auto()
- ... GREEN = auto()
- ... WHITE = RED | BLUE | GREEN
- ...
- >>> Color.WHITE
- Color.WHITE
-
-Giving a name to the "no flags set" condition does not change its boolean
-value::
-
- >>> class Color(Flag):
- ... BLACK = 0
- ... RED = auto()
- ... BLUE = auto()
- ... GREEN = auto()
- ...
- >>> Color.BLACK
- Color.BLACK
- >>> bool(Color.BLACK)
- False
-
-:class:`Flag` members can also be iterated over::
-
- >>> purple = Color.RED | Color.BLUE
- >>> list(purple)
- [Color.RED, Color.BLUE]
-
-.. versionadded:: 3.10
-
-.. note::
-
- For the majority of new code, :class:`Enum` and :class:`Flag` are strongly
- recommended, since :class:`IntEnum` and :class:`IntFlag` break some
- semantic promises of an enumeration (by being comparable to integers, and
- thus by transitivity to other unrelated enumerations). :class:`IntEnum`
- and :class:`IntFlag` should be used only in cases where :class:`Enum` and
- :class:`Flag` will not do; for example, when integer constants are replaced
- with enumerations, or for interoperability with other systems.
-
-
-Others
-^^^^^^
-
-While :class:`IntEnum` is part of the :mod:`enum` module, it would be very
-simple to implement independently::
-
- class IntEnum(int, Enum):
- pass
-
-This demonstrates how similar derived enumerations can be defined; for example
-a :class:`StrEnum` that mixes in :class:`str` instead of :class:`int`.
-
-Some rules:
-
-1. When subclassing :class:`Enum`, mix-in types must appear before
- :class:`Enum` itself in the sequence of bases, as in the :class:`IntEnum`
- example above.
-2. While :class:`Enum` can have members of any type, once you mix in an
- additional type, all the members must have values of that type, e.g.
- :class:`int` above. This restriction does not apply to mix-ins which only
- add methods and don't specify another type.
-3. When another data type is mixed in, the :attr:`value` attribute is *not the
- same* as the enum member itself, although it is equivalent and will compare
- equal.
-4. %-style formatting: `%s` and `%r` call the :class:`Enum` class's
- :meth:`__str__` and :meth:`__repr__` respectively; other codes (such as
- `%i` or `%h` for IntEnum) treat the enum member as its mixed-in type.
-5. :ref:`Formatted string literals <f-strings>`, :meth:`str.format`,
- and :func:`format` will use the mixed-in type's :meth:`__format__`
- unless :meth:`__str__` or :meth:`__format__` is overridden in the subclass,
- in which case the overridden methods or :class:`Enum` methods will be used.
- Use the !s and !r format codes to force usage of the :class:`Enum` class's
- :meth:`__str__` and :meth:`__repr__` methods.
-
-When to use :meth:`__new__` vs. :meth:`__init__`
-------------------------------------------------
-
-:meth:`__new__` must be used whenever you want to customize the actual value of
-the :class:`Enum` member. Any other modifications may go in either
-:meth:`__new__` or :meth:`__init__`, with :meth:`__init__` being preferred.
-
-For example, if you want to pass several items to the constructor, but only
-want one of them to be the value::
-
- >>> class Coordinate(bytes, Enum):
- ... """
- ... Coordinate with binary codes that can be indexed by the int code.
- ... """
- ... def __new__(cls, value, label, unit):
- ... obj = bytes.__new__(cls, [value])
- ... obj._value_ = value
- ... obj.label = label
- ... obj.unit = unit
- ... return obj
- ... PX = (0, 'P.X', 'km')
- ... PY = (1, 'P.Y', 'km')
- ... VX = (2, 'V.X', 'km/s')
- ... VY = (3, 'V.Y', 'km/s')
- ...
-
- >>> print(Coordinate['PY'])
- PY
-
- >>> print(Coordinate(3))
- VY
-
-
-Finer Points
-^^^^^^^^^^^^
-
-Supported ``__dunder__`` names
-""""""""""""""""""""""""""""""
-
-:attr:`__members__` is a read-only ordered mapping of ``member_name``:``member``
-items. It is only available on the class.
-
-:meth:`__new__`, if specified, must create and return the enum members; it is
-also a very good idea to set the member's :attr:`_value_` appropriately. Once
-all the members are created it is no longer used.
-
-
-Supported ``_sunder_`` names
-""""""""""""""""""""""""""""
-
-- ``_name_`` -- name of the member
-- ``_value_`` -- value of the member; can be set / modified in ``__new__``
-
-- ``_missing_`` -- a lookup function used when a value is not found; may be
- overridden
-- ``_ignore_`` -- a list of names, either as a :class:`list` or a :class:`str`,
- that will not be transformed into members, and will be removed from the final
- class
-- ``_order_`` -- used in Python 2/3 code to ensure member order is consistent
- (class attribute, removed during class creation)
-- ``_generate_next_value_`` -- used by the `Functional API`_ and by
- :class:`auto` to get an appropriate value for an enum member; may be
- overridden
-
-.. note::
-
- For standard :class:`Enum` classes the next value chosen is the last value seen
- incremented by one.
-
- For :class:`Flag` classes the next value chosen will be the next highest
- power-of-two, regardless of the last value seen.
-
-.. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_``
-.. versionadded:: 3.7 ``_ignore_``
-
-To help keep Python 2 / Python 3 code in sync an :attr:`_order_` attribute can
-be provided. It will be checked against the actual order of the enumeration
-and raise an error if the two do not match::
-
- >>> class Color(Enum):
- ... _order_ = 'RED GREEN BLUE'
- ... RED = 1
- ... BLUE = 3
- ... GREEN = 2
- ...
- Traceback (most recent call last):
- ...
- TypeError: member order does not match _order_:
- ['RED', 'BLUE', 'GREEN']
- ['RED', 'GREEN', 'BLUE']
-
-.. note::
-
- In Python 2 code the :attr:`_order_` attribute is necessary as definition
- order is lost before it can be recorded.
-
-
-_Private__names
-"""""""""""""""
-
-Private names are not converted to enum members, but remain normal attributes.
-
-.. versionchanged:: 3.10
-
-
-``Enum`` member type
-""""""""""""""""""""
-
-Enum members are instances of their enum class, and are normally accessed as
-``EnumClass.member``. In Python versions ``3.5`` to ``3.9`` you could access
-members from other members -- this practice was discouraged, and in ``3.12``
-:class:`Enum` will return to not allowing it, while in ``3.10`` and ``3.11``
-it will raise a :exc:`DeprecationWarning`::
-
- >>> class FieldTypes(Enum):
- ... name = 0
- ... value = 1
- ... size = 2
- ...
- >>> FieldTypes.value.size # doctest: +SKIP
- DeprecationWarning: accessing one member from another is not supported,
- and will be disabled in 3.12
- <FieldTypes.size: 2>
-
-.. versionchanged:: 3.5
-.. versionchanged:: 3.10
-
-
-Creating members that are mixed with other data types
-"""""""""""""""""""""""""""""""""""""""""""""""""""""
-
-When subclassing other data types, such as :class:`int` or :class:`str`, with
-an :class:`Enum`, all values after the `=` are passed to that data type's
-constructor. For example::
-
- >>> class MyEnum(IntEnum):
- ... example = '11', 16 # '11' will be interpreted as a hexadecimal
- ... # number
- >>> MyEnum.example.value
- 17
-
-
-Boolean value of ``Enum`` classes and members
-"""""""""""""""""""""""""""""""""""""""""""""
-
-Enum classes that are mixed with non-:class:`Enum` types (such as
-:class:`int`, :class:`str`, etc.) are evaluated according to the mixed-in
-type's rules; otherwise, all members evaluate as :data:`True`. To make your
-own enum's boolean evaluation depend on the member's value add the following to
-your class::
-
- def __bool__(self):
- return bool(self.value)
-
-Plain :class:`Enum` classes always evaluate as :data:`True`.
-
-
-``Enum`` classes with methods
-"""""""""""""""""""""""""""""
-
-If you give your enum subclass extra methods, like the `Planet`_
-class above, those methods will show up in a :func:`dir` of the member,
-but not of the class::
-
- >>> dir(Planet)
- ['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__']
- >>> dir(Planet.EARTH)
- ['__class__', '__doc__', '__module__', 'mass', 'name', 'radius', 'surface_gravity', 'value']
-
-
-Combining members of ``Flag``
-"""""""""""""""""""""""""""""
-
-Iterating over a combination of :class:`Flag` members will only return the members that
-are comprised of a single bit::
-
- >>> class Color(Flag):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- ... MAGENTA = RED | BLUE
- ... YELLOW = RED | GREEN
- ... CYAN = GREEN | BLUE
- ...
- >>> Color(3) # named combination
- Color.YELLOW
- >>> Color(7) # not named combination
- Color.RED|Color.GREEN|Color.BLUE
-
-``StrEnum`` and :meth:`str.__str__`
-"""""""""""""""""""""""""""""""""""
-
-An important difference between :class:`StrEnum` and other Enums is the
-:meth:`__str__` method; because :class:`StrEnum` members are strings, some
-parts of Python will read the string data directly, while others will call
-:meth:`str()`. To make those two operations have the same result,
-:meth:`StrEnum.__str__` will be the same as :meth:`str.__str__` so that
-``str(StrEnum.member) == StrEnum.member`` is true.
-
-``Flag`` and ``IntFlag`` minutia
-""""""""""""""""""""""""""""""""
-
-Using the following snippet for our examples::
-
- >>> class Color(IntFlag):
- ... BLACK = 0
- ... RED = 1
- ... GREEN = 2
- ... BLUE = 4
- ... PURPLE = RED | BLUE
- ... WHITE = RED | GREEN | BLUE
- ...
-
-the following are true:
-
-- single-bit flags are canonical
-- multi-bit and zero-bit flags are aliases
-- only canonical flags are returned during iteration::
-
- >>> list(Color.WHITE)
- [Color.RED, Color.GREEN, Color.BLUE]
-
-- negating a flag or flag set returns a new flag/flag set with the
- corresponding positive integer value::
-
- >>> Color.BLUE
- Color.BLUE
-
- >>> ~Color.BLUE
- Color.RED|Color.GREEN
-
-- names of pseudo-flags are constructed from their members' names::
-
- >>> (Color.RED | Color.GREEN).name
- 'RED|GREEN'
-
-- multi-bit flags, aka aliases, can be returned from operations::
-
- >>> Color.RED | Color.BLUE
- Color.PURPLE
-
- >>> Color(7) # or Color(-1)
- Color.WHITE
-
- >>> Color(0)
- Color.BLACK
-
-- membership / containment checking has changed slightly -- zero valued flags
- are never considered to be contained::
-
- >>> Color.BLACK in Color.WHITE
- False
-
- otherwise, if all bits of one flag are in the other flag, True is returned::
-
- >>> Color.PURPLE in Color.WHITE
- True
-
-There is a new boundary mechanism that controls how out-of-range / invalid
-bits are handled: ``STRICT``, ``CONFORM``, ``EJECT``, and ``KEEP``:
-
- * STRICT --> raises an exception when presented with invalid values
- * CONFORM --> discards any invalid bits
- * EJECT --> lose Flag status and become a normal int with the given value
- * KEEP --> keep the extra bits
- - keeps Flag status and extra bits
- - extra bits do not show up in iteration
- - extra bits do show up in repr() and str()
-
-The default for Flag is ``STRICT``, the default for ``IntFlag`` is ``EJECT``,
-and the default for ``_convert_`` is ``KEEP`` (see ``ssl.Options`` for an
-example of when ``KEEP`` is needed).
-
-
-.. _enum-class-differences:
-
-How are Enums different?
-------------------------
-
-Enums have a custom metaclass that affects many aspects of both derived :class:`Enum`
-classes and their instances (members).
-
-
-Enum Classes
-^^^^^^^^^^^^
-
-The :class:`EnumType` metaclass is responsible for providing the
-:meth:`__contains__`, :meth:`__dir__`, :meth:`__iter__` and other methods that
-allow one to do things with an :class:`Enum` class that fail on a typical
-class, such as `list(Color)` or `some_enum_var in Color`. :class:`EnumType` is
-responsible for ensuring that various other methods on the final :class:`Enum`
-class are correct (such as :meth:`__new__`, :meth:`__getnewargs__`,
-:meth:`__str__` and :meth:`__repr__`).
-
-
-Enum Members (aka instances)
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
-The most interesting thing about enum members is that they are singletons.
-:class:`EnumType` creates them all while it is creating the enum class itself,
-and then puts a custom :meth:`__new__` in place to ensure that no new ones are
-ever instantiated by returning only the existing member instances.
-
-
-.. _enum-cookbook:
-
-
-While :class:`Enum`, :class:`IntEnum`, :class:`StrEnum`, :class:`Flag`, and
-:class:`IntFlag` are expected to cover the majority of use-cases, they cannot
-cover them all. Here are recipes for some different types of enumerations
-that can be used directly, or as examples for creating one's own.
-
-
-Omitting values
-^^^^^^^^^^^^^^^
-
-In many use-cases one doesn't care what the actual value of an enumeration
-is. There are several ways to define this type of simple enumeration:
-
-- use instances of :class:`auto` for the value
-- use instances of :class:`object` as the value
-- use a descriptive string as the value
-- use a tuple as the value and a custom :meth:`__new__` to replace the
- tuple with an :class:`int` value
-
-Using any of these methods signifies to the user that these values are not
-important, and also enables one to add, remove, or reorder members without
-having to renumber the remaining members.
-
-
-Using :class:`auto`
-"""""""""""""""""""
-
-Using :class:`auto` would look like::
-
- >>> class Color(Enum):
- ... RED = auto()
- ... BLUE = auto()
- ... GREEN = auto()
- ...
- >>> Color.GREEN
- <Color.GREEN>
-
-
-Using :class:`object`
-"""""""""""""""""""""
-
-Using :class:`object` would look like::
-
- >>> class Color(Enum):
- ... RED = object()
- ... GREEN = object()
- ... BLUE = object()
- ...
- >>> Color.GREEN
- <Color.GREEN>
-
-
-Using a descriptive string
-""""""""""""""""""""""""""
-
-Using a string as the value would look like::
-
- >>> class Color(Enum):
- ... RED = 'stop'
- ... GREEN = 'go'
- ... BLUE = 'too fast!'
- ...
- >>> Color.GREEN
- <Color.GREEN>
- >>> Color.GREEN.value
- 'go'
-
-
-Using a custom :meth:`__new__`
-""""""""""""""""""""""""""""""
-
-Using an auto-numbering :meth:`__new__` would look like::
-
- >>> class AutoNumber(Enum):
- ... def __new__(cls):
- ... value = len(cls.__members__) + 1
- ... obj = object.__new__(cls)
- ... obj._value_ = value
- ... return obj
- ...
- >>> class Color(AutoNumber):
- ... RED = ()
- ... GREEN = ()
- ... BLUE = ()
- ...
- >>> Color.GREEN
- <Color.GREEN>
- >>> Color.GREEN.value
- 2
-
-To make a more general purpose ``AutoNumber``, add ``*args`` to the signature::
-
- >>> class AutoNumber(Enum):
- ... def __new__(cls, *args): # this is the only change from above
- ... value = len(cls.__members__) + 1
- ... obj = object.__new__(cls)
- ... obj._value_ = value
- ... return obj
- ...
-
-Then when you inherit from ``AutoNumber`` you can write your own ``__init__``
-to handle any extra arguments::
-
- >>> class Swatch(AutoNumber):
- ... def __init__(self, pantone='unknown'):
- ... self.pantone = pantone
- ... AUBURN = '3497'
- ... SEA_GREEN = '1246'
- ... BLEACHED_CORAL = () # New color, no Pantone code yet!
- ...
- >>> Swatch.SEA_GREEN
- <Swatch.SEA_GREEN>
- >>> Swatch.SEA_GREEN.pantone
- '1246'
- >>> Swatch.BLEACHED_CORAL.pantone
- 'unknown'
-
-.. note::
-
- The :meth:`__new__` method, if defined, is used during creation of the Enum
- members; it is then replaced by Enum's :meth:`__new__` which is used after
- class creation for lookup of existing members.
-
-
-OrderedEnum
-^^^^^^^^^^^
-
-An ordered enumeration that is not based on :class:`IntEnum` and so maintains
-the normal :class:`Enum` invariants (such as not being comparable to other
-enumerations)::
-
- >>> class OrderedEnum(Enum):
- ... def __ge__(self, other):
- ... if self.__class__ is other.__class__:
- ... return self.value >= other.value
- ... return NotImplemented
- ... def __gt__(self, other):
- ... if self.__class__ is other.__class__:
- ... return self.value > other.value
- ... return NotImplemented
- ... def __le__(self, other):
- ... if self.__class__ is other.__class__:
- ... return self.value <= other.value
- ... return NotImplemented
- ... def __lt__(self, other):
- ... if self.__class__ is other.__class__:
- ... return self.value < other.value
- ... return NotImplemented
- ...
- >>> class Grade(OrderedEnum):
- ... A = 5
- ... B = 4
- ... C = 3
- ... D = 2
- ... F = 1
- ...
- >>> Grade.C < Grade.A
- True
-
-
-DuplicateFreeEnum
-^^^^^^^^^^^^^^^^^
-
-Raises an error if a duplicate member name is found instead of creating an
-alias::
-
- >>> class DuplicateFreeEnum(Enum):
- ... def __init__(self, *args):
- ... cls = self.__class__
- ... if any(self.value == e.value for e in cls):
- ... a = self.name
- ... e = cls(self.value).name
- ... raise ValueError(
- ... "aliases not allowed in DuplicateFreeEnum: %r --> %r"
- ... % (a, e))
- ...
- >>> class Color(DuplicateFreeEnum):
- ... RED = 1
- ... GREEN = 2
- ... BLUE = 3
- ... GRENE = 2
- ...
- Traceback (most recent call last):
- ...
- ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN'
-
-.. note::
-
- This is a useful example for subclassing Enum to add or change other
- behaviors as well as disallowing aliases. If the only desired change is
- disallowing aliases, the :func:`unique` decorator can be used instead.
-
-
-Planet
-^^^^^^
-
-If :meth:`__new__` or :meth:`__init__` is defined the value of the enum member
-will be passed to those methods::
-
- >>> class Planet(Enum):
- ... MERCURY = (3.303e+23, 2.4397e6)
- ... VENUS = (4.869e+24, 6.0518e6)
- ... EARTH = (5.976e+24, 6.37814e6)
- ... MARS = (6.421e+23, 3.3972e6)
- ... JUPITER = (1.9e+27, 7.1492e7)
- ... SATURN = (5.688e+26, 6.0268e7)
- ... URANUS = (8.686e+25, 2.5559e7)
- ... NEPTUNE = (1.024e+26, 2.4746e7)
- ... def __init__(self, mass, radius):
- ... self.mass = mass # in kilograms
- ... self.radius = radius # in meters
- ... @property
- ... def surface_gravity(self):
- ... # universal gravitational constant (m3 kg-1 s-2)
- ... G = 6.67300E-11
- ... return G * self.mass / (self.radius * self.radius)
- ...
- >>> Planet.EARTH.value
- (5.976e+24, 6378140.0)
- >>> Planet.EARTH.surface_gravity
- 9.802652743337129
-
-.. _enum-time-period:
-
-TimePeriod
-^^^^^^^^^^
-
-An example to show the :attr:`_ignore_` attribute in use::
-
- >>> from datetime import timedelta
- >>> class Period(timedelta, Enum):
- ... "different lengths of time"
- ... _ignore_ = 'Period i'
- ... Period = vars()
- ... for i in range(367):
- ... Period['day_%d' % i] = i
- ...
- >>> list(Period)[:2]
- [Period.day_0, Period.day_1]
- >>> list(Period)[-2:]
- [Period.day_365, Period.day_366]
-
-
-Conforming input to Flag
-^^^^^^^^^^^^^^^^^^^^^^^^
-
-Creating a :class:`Flag` enum that is more resilient out-of-bounds results to
-mathematical operations, you can use the :attr:`FlagBoundary.CONFORM` setting::
-
- >>> from enum import Flag, CONFORM, auto
- >>> class Weekday(Flag, boundary=CONFORM):
- ... MONDAY = auto()
- ... TUESDAY = auto()
- ... WEDNESDAY = auto()
- ... THURSDAY = auto()
- ... FRIDAY = auto()
- ... SATURDAY = auto()
- ... SUNDAY = auto()
- >>> today = Weekday.TUESDAY
- >>> Weekday(today + 22) # what day is three weeks from tomorrow?
- >>> Weekday.WEDNESDAY
-
-
-.. _enumtype-examples:
-
-Subclassing EnumType
---------------------
-
-While most enum needs can be met by customizing :class:`Enum` subclasses,
-either with class decorators or custom functions, :class:`EnumType` can be
-subclassed to provide a different Enum experience.
-
diff --git a/Doc/howto/index.rst b/Doc/howto/index.rst
index eae8f143ee206..01a78a556591c 100644
--- a/Doc/howto/index.rst
+++ b/Doc/howto/index.rst
@@ -17,7 +17,6 @@ Currently, the HOWTOs are:
cporting.rst
curses.rst
descriptor.rst
- enum.rst
functional.rst
logging.rst
logging-cookbook.rst
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst
index d3df151b71ea9..e8e49425ab90a 100644
--- a/Doc/library/enum.rst
+++ b/Doc/library/enum.rst
@@ -13,722 +13,1190 @@
**Source code:** :source:`Lib/enum.py`
-.. sidebar:: Important
+----------------
- This page contains the API reference information. For tutorial
- information and discussion of more advanced topics, see
+An enumeration is a set of symbolic names (members) bound to unique,
+constant values. Within an enumeration, the members can be compared
+by identity, and the enumeration itself can be iterated over.
- * :ref:`Basic Tutorial <enum-basic-tutorial>`
- * :ref:`Advanced Tutorial <enum-advanced-tutorial>`
- * :ref:`Enum Cookbook <enum-cookbook>`
+.. note:: Case of Enum Members
----------------
-
-An enumeration:
-
-* is a set of symbolic names (members) bound to unique values
-* can be iterated over to return its members in definition order
-* uses *call* syntax to return members by value
-* uses *index* syntax to return members by name
-
-Enumerations are created either by using the :keyword:`class` syntax, or by
-using function-call syntax::
-
- >>> from enum import Enum
-
- >>> # class syntax
- >>> class Color(Enum):
- ... RED = 1
- ... GREEN = 2
- ... BLUE = 3
-
- >>> # functional syntax
- >>> Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])
-
-Even though we can use the :keyword:`class` syntax to create Enums, Enums
-are not normal Python classes. See
-:ref:`How are Enums different? <enum-class-differences>` for more details.
+ Because Enums are used to represent constants we recommend using
+ UPPER_CASE names for enum members, and will be using that style
+ in our examples.
-.. note:: Nomenclature
-
- - The class :class:`Color` is an *enumeration* (or *enum*)
- - The attributes :attr:`Color.RED`, :attr:`Color.GREEN`, etc., are
- *enumeration members* (or *enum members*) and are functionally constants.
- - The enum members have *names* and *values* (the name of
- :attr:`Color.RED` is ``RED``, the value of :attr:`Color.BLUE` is
- ``3``, etc.)
-
----------------
Module Contents
---------------
- :class:`EnumType`
-
- The ``type`` for Enum and its subclasses.
-
- :class:`Enum`
-
- Base class for creating enumerated constants.
-
- :class:`IntEnum`
+This module defines four enumeration classes that can be used to define unique
+sets of names and values: :class:`Enum`, :class:`IntEnum`, :class:`Flag`, and
+:class:`IntFlag`. It also defines one decorator, :func:`unique`, and one
+helper, :class:`auto`.
- Base class for creating enumerated constants that are also
- subclasses of :class:`int`. (`Notes`_)
-
- :class:`StrEnum`
-
- Base class for creating enumerated constants that are also
- subclasses of :class:`str`. (`Notes`_)
-
- :class:`Flag`
-
- Base class for creating enumerated constants that can be combined using
- the bitwise operations without losing their :class:`Flag` membership.
-
- :class:`IntFlag`
-
- Base class for creating enumerated constants that can be combined using
- the bitwise operators without losing their :class:`IntFlag` membership.
- :class:`IntFlag` members are also subclasses of :class:`int`. (`Notes`_)
-
- :class:`EnumCheck`
-
- An enumeration with the values ``CONTINUOUS``, ``NAMED_FLAGS``, and
- ``UNIQUE``, for use with :func:`verify` to ensure various constraints
- are met by a given enumeration.
-
- :class:`FlagBoundary`
-
- An enumeration with the values ``STRICT``, ``CONFORM``, ``EJECT``, and
- ``KEEP`` which allows for more fine-grained control over how invalid values
- are dealt with in an enumeration.
+.. class:: Enum
- :class:`auto`
+ Base class for creating enumerated constants. See section
+ `Functional API`_ for an alternate construction syntax.
- Instances are replaced with an appropriate value for Enum members.
- :class:`StrEnum` defaults to the lower-cased version of the member name,
- while other Enums default to 1 and increase from there.
+.. class:: IntEnum
- :func:`global_enum`
+ Base class for creating enumerated constants that are also
+ subclasses of :class:`int`.
- :class:`Enum` class decorator to apply the appropriate global `__repr__`,
- and export its members into the global name space.
+.. class:: IntFlag
- :func:`.property`
+ Base class for creating enumerated constants that can be combined using
+ the bitwise operators without losing their :class:`IntFlag` membership.
+ :class:`IntFlag` members are also subclasses of :class:`int`.
- Allows :class:`Enum` members to have attributes without conflicting with
- other members' names.
+.. class:: Flag
- :func:`unique`
+ Base class for creating enumerated constants that can be combined using
+ the bitwise operations without losing their :class:`Flag` membership.
- Enum class decorator that ensures only one name is bound to any one value.
+.. function:: unique
+ :noindex:
- :func:`verify`
+ Enum class decorator that ensures only one name is bound to any one value.
- Enum class decorator that checks user-selectable constraints on an
- enumeration.
+.. class:: auto
+ Instances are replaced with an appropriate value for Enum members. By default, the initial value starts at 1.
.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
-.. versionadded:: 3.10 ``StrEnum``, ``EnumCheck``, ``FlagBoundary``
----------------
-
-Data Types
-----------
-
-
-.. class:: EnumType
- *EnumType* is the :term:`metaclass` for *enum* enumerations. It is possible
- to subclass *EnumType* -- see :ref:`Subclassing EnumType <enumtype-examples>`
- for details.
+Creating an Enum
+----------------
- .. method:: EnumType.__contains__(cls, member)
+Enumerations are created using the :keyword:`class` syntax, which makes them
+easy to read and write. An alternative creation method is described in
+`Functional API`_. To define an enumeration, subclass :class:`Enum` as
+follows::
- Returns ``True`` if member belongs to the ``cls``::
+ >>> from enum import Enum
+ >>> class Color(Enum):
+ ... RED = 1
+ ... GREEN = 2
+ ... BLUE = 3
+ ...
- >>> some_var = Color.RED
- >>> some_var in Color
- True
+.. note:: Enum member values
- .. note::
+ Member values can be anything: :class:`int`, :class:`str`, etc.. If
+ the exact value is unimportant you may use :class:`auto` instances and an
+ appropriate value will be chosen for you. Care must be taken if you mix
+ :class:`auto` with other values.
- In Python 3.12 it will be possible to check for member values and not
- just members; until then, a ``TypeError`` will be raised if a
- non-Enum-member is used in a containment check.
+.. note:: Nomenclature
- .. method:: EnumType.__dir__(cls)
+ - The class :class:`Color` is an *enumeration* (or *enum*)
+ - The attributes :attr:`Color.RED`, :attr:`Color.GREEN`, etc., are
+ *enumeration members* (or *enum members*) and are functionally constants.
+ - The enum members have *names* and *values* (the name of
+ :attr:`Color.RED` is ``RED``, the value of :attr:`Color.BLUE` is
+ ``3``, etc.)
- Returns ``['__class__', '__doc__', '__members__', '__module__']`` and the
- names of the members in *cls*::
+.. note::
- >>> dir(Color)
- ['BLUE', 'GREEN', 'RED', '__class__', '__doc__', '__members__', '__module__']
+ Even though we use the :keyword:`class` syntax to create Enums, Enums
+ are not normal Python classes. See `How are Enums different?`_ for
+ more details.
- .. method:: EnumType.__getattr__(cls, name)
+Enumeration members have human readable string representations::
- Returns the Enum member in *cls* matching *name*, or raises an :exc:`AttributeError`::
+ >>> print(Color.RED)
+ Color.RED
- >>> Color.GREEN
- Color.GREEN
+...while their ``repr`` has more information::
- .. method:: EnumType.__getitem__(cls, name)
+ >>> print(repr(Color.RED))
+ <Color.RED: 1>
- Returns the Enum member in *cls* matching *name*, or raises an :exc:`KeyError`::
+The *type* of an enumeration member is the enumeration it belongs to::
- >>> Color['BLUE']
- Color.BLUE
+ >>> type(Color.RED)
+ <enum 'Color'>
+ >>> isinstance(Color.GREEN, Color)
+ True
+ >>>
- .. method:: EnumType.__iter__(cls)
+Enum members also have a property that contains just their item name::
- Returns each member in *cls* in definition order::
+ >>> print(Color.RED.name)
+ RED
- >>> list(Color)
- [Color.RED, Color.GREEN, Color.BLUE]
+Enumerations support iteration, in definition order::
- .. method:: EnumType.__len__(cls)
+ >>> class Shake(Enum):
+ ... VANILLA = 7
+ ... CHOCOLATE = 4
+ ... COOKIES = 9
+ ... MINT = 3
+ ...
+ >>> for shake in Shake:
+ ... print(shake)
+ ...
+ Shake.VANILLA
+ Shake.CHOCOLATE
+ Shake.COOKIES
+ Shake.MINT
- Returns the number of member in *cls*::
+Enumeration members are hashable, so they can be used in dictionaries and sets::
- >>> len(Color)
- 3
+ >>> apples = {}
+ >>> apples[Color.RED] = 'red delicious'
+ >>> apples[Color.GREEN] = 'granny smith'
+ >>> apples == {Color.RED: 'red delicious', Color.GREEN: 'granny smith'}
+ True
- .. method:: EnumType.__reversed__(cls)
- Returns each member in *cls* in reverse definition order::
+Programmatic access to enumeration members and their attributes
+---------------------------------------------------------------
- >>> list(reversed(Color))
- [Color.BLUE, Color.GREEN, Color.RED]
+Sometimes it's useful to access members in enumerations programmatically (i.e.
+situations where ``Color.RED`` won't do because the exact color is not known
+at program-writing time). ``Enum`` allows such access::
+ >>> Color(1)
+ <Color.RED: 1>
+ >>> Color(3)
+ <Color.BLUE: 3>
-.. class:: Enum
+If you want to access enum members by *name*, use item access::
- *Enum* is the base class for all *enum* enumerations.
+ >>> Color['RED']
+ <Color.RED: 1>
+ >>> Color['GREEN']
+ <Color.GREEN: 2>
- .. attribute:: Enum.name
+If you have an enum member and need its :attr:`name` or :attr:`value`::
- The name used to define the ``Enum`` member::
+ >>> member = Color.RED
+ >>> member.name
+ 'RED'
+ >>> member.value
+ 1
- >>> Color.BLUE.name
- 'BLUE'
- .. attribute:: Enum.value
+Duplicating enum members and values
+-----------------------------------
- The value given to the ``Enum`` member::
+Having two enum members with the same name is invalid::
- >>> Color.RED.value
- 1
+ >>> class Shape(Enum):
+ ... SQUARE = 2
+ ... SQUARE = 3
+ ...
+ Traceback (most recent call last):
+ ...
+ TypeError: Attempted to reuse key: 'SQUARE'
- .. note:: Enum member values
+However, two enum members are allowed to have the same value. Given two members
+A and B with the same value (and A defined first), B is an alias to A. By-value
+lookup of the value of A and B will return A. By-name lookup of B will also
+return A::
- Member values can be anything: :class:`int`, :class:`str`, etc.. If
- the exact value is unimportant you may use :class:`auto` instances and an
- appropriate value will be chosen for you. Care must be taken if you mix
- :class:`auto` with other values.
+ >>> class Shape(Enum):
+ ... SQUARE = 2
+ ... DIAMOND = 1
+ ... CIRCLE = 3
+ ... ALIAS_FOR_SQUARE = 2
+ ...
+ >>> Shape.SQUARE
+ <Shape.SQUARE: 2>
+ >>> Shape.ALIAS_FOR_SQUARE
+ <Shape.SQUARE: 2>
+ >>> Shape(2)
+ <Shape.SQUARE: 2>
- .. attribute:: Enum._ignore_
+.. note::
- ``_ignore_`` is only used during creation and is removed from the
- enumeration once that is complete.
+ Attempting to create a member with the same name as an already
+ defined attribute (another member, a method, etc.) or attempting to create
+ an attribute with the same name as a member is not allowed.
- ``_ignore_`` is a list of names that will not become members, and whose
- names will also be removed from the completed enumeration. See
- :ref:`TimePeriod <enum-time-period>` for an example.
- .. method:: Enum.__call__(cls, value, names=None, \*, module=None, qualname=None, type=None, start=1, boundary=None)
+Ensuring unique enumeration values
+----------------------------------
- This method is called in two different ways:
+By default, enumerations allow multiple names as aliases for the same value.
+When this behavior isn't desired, the following decorator can be used to
+ensure each value is used only once in the enumeration:
- * to look up an existing member:
+.. decorator:: unique
- :cls: The enum class being called.
- :value: The value to lookup.
-
- * to use the ``cls`` enum to create a new enum:
-
- :cls: The enum class being called.
- :value: The name of the new Enum to create.
- :names: The names/values of the members for the new Enum.
- :module: The name of the module the new Enum is created in.
- :qualname: The actual location in the module where this Enum can be found.
- :type: A mix-in type for the new Enum.
- :start: The first integer value for the Enum (used by :class:`auto`)
- :boundary: How to handle out-of-range values from bit operations (:class:`Flag` only)
-
- .. method:: Enum.__dir__(self)
-
- Returns ``['__class__', '__doc__', '__module__', 'name', 'value']`` and
- any public methods defined on *self.__class__*::
-
- >>> from datetime import date
- >>> class Weekday(Enum):
- ... MONDAY = 1
- ... TUESDAY = 2
- ... WEDNESDAY = 3
- ... THURSDAY = 4
- ... FRIDAY = 5
- ... SATURDAY = 6
- ... SUNDAY = 7
- ... @classmethod
- ... def today(cls):
- ... print('today is %s' % cls(date.today.isoweekday).naem)
- >>> dir(Weekday.SATURDAY)
- ['__class__', '__doc__', '__module__', 'name', 'today', 'value']
-
- .. method:: Enum._generate_next_value_(name, start, count, last_values)
-
- :name: The name of the member being defined (e.g. 'RED').
- :start: The start value for the Enum; the default is 1.
- :count: The number of members currently defined, not including this one.
- :last_values: A list of the previous values.
-
- A *staticmethod* that is used to determine the next value returned by
- :class:`auto`::
-
- >>> from enum import auto
- >>> class PowersOfThree(Enum):
- ... @staticmethod
- ... def _generate_next_value_(name, start, count, last_values):
- ... return (count + 1) * 3
- ... FIRST = auto()
- ... SECOND = auto()
- >>> PowersOfThree.SECOND.value
- 6
-
- .. method:: Enum._missing_(cls, value)
-
- A *classmethod* for looking up values not found in *cls*. By default it
- does nothing, but can be overridden to implement custom search behavior::
-
- >>> from enum import StrEnum
- >>> class Build(StrEnum):
- ... DEBUG = auto()
- ... OPTIMIZED = auto()
- ... @classmethod
- ... def _missing_(cls, value):
- ... value = value.lower()
- ... for member in cls:
- ... if member.value == value:
- ... return member
- ... return None
- >>> Build.DEBUG.value
- 'debug'
- >>> Build('deBUG')
- Build.DEBUG
-
- .. method:: Enum.__repr__(self)
-
- Returns the string used for *repr()* calls. By default, returns the
- *Enum* name and the member name, but can be overridden::
-
- >>> class OldStyle(Enum):
- ... RETRO = auto()
- ... OLD_SCHOOl = auto()
- ... YESTERYEAR = auto()
- ... def __repr__(self):
- ... cls_name = self.__class__.__name__
- ... return f'<{cls_name}.{self.name}: {self.value}>'
- >>> OldStyle.RETRO
- <OldStyle.RETRO: 1>
-
- .. method:: Enum.__str__(self)
-
- Returns the string used for *str()* calls. By default, returns the
- member name, but can be overridden::
-
- >>> class OldStyle(Enum):
- ... RETRO = auto()
- ... OLD_SCHOOl = auto()
- ... YESTERYEAR = auto()
- ... def __str__(self):
- ... cls_name = self.__class__.__name__
- ... return f'{cls_name}.{self.name}'
- >>> OldStyle.RETRO
- OldStyle.RETRO
+A :keyword:`class` decorator specifically for enumerations. It searches an
+enumeration's :attr:`__members__` gathering any aliases it finds; if any are
+found :exc:`ValueError` is raised with the details::
+
+ >>> from enum import Enum, unique
+ >>> @unique
+ ... class Mistake(Enum):
+ ... ONE = 1
+ ... TWO = 2
+ ... THREE = 3
+ ... FOUR = 3
+ ...
+ Traceback (most recent call last):
+ ...
+ ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
+
+
+Using automatic values
+----------------------
+
+If the exact value is unimportant you can use :class:`auto`::
+
+ >>> from enum import Enum, auto
+ >>> class Color(Enum):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> list(Color)
+ [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]
+
+The values are chosen by :func:`_generate_next_value_`, which can be
+overridden::
+
+ >>> class AutoName(Enum):
+ ... def _generate_next_value_(name, start, count, last_values):
+ ... return name
+ ...
+ >>> class Ordinal(AutoName):
+ ... NORTH = auto()
+ ... SOUTH = auto()
+ ... EAST = auto()
+ ... WEST = auto()
+ ...
+ >>> list(Ordinal)
+ [<Ordinal.NORTH: 'NORTH'>, <Ordinal.SOUTH: 'SOUTH'>, <Ordinal.EAST: 'EAST'>, <Ordinal.WEST: 'WEST'>]
.. note::
- Using :class:`auto` with :class:`Enum` results in integers of increasing value,
- starting with ``1``.
-
-
-.. class:: IntEnum
-
- *IntEnum* is the same as *Enum*, but its members are also integers and can be
- used anywhere that an integer can be used. If any integer operation is performed
- with an *IntEnum* member, the resulting value loses its enumeration status.
-
- >>> from enum import IntEnum
- >>> class Numbers(IntEnum):
- ... ONE = 1
- ... TWO = 2
- ... THREE = 3
- >>> Numbers.THREE
- Numbers.THREE
- >>> Numbers.ONE + Numbers.TWO
- 3
- >>> Numbers.THREE + 5
- 8
- >>> Numbers.THREE == 3
- True
+ The goal of the default :meth:`_generate_next_value_` method is to provide
+ the next :class:`int` in sequence with the last :class:`int` provided, but
+ the way it does this is an implementation detail and may change.
.. note::
- Using :class:`auto` with :class:`IntEnum` results in integers of increasing value,
- starting with ``1``.
-
-
-.. class:: StrEnum
+ The :meth:`_generate_next_value_` method must be defined before any members.
- *StrEnum* is the same as *Enum*, but its members are also strings and can be used
- in most of the same places that a string can be used. The result of any string
- operation performed on or with a *StrEnum* member is not part of the enumeration.
+Iteration
+---------
- .. note:: There are places in the stdlib that check for an exact :class:`str`
- instead of a :class:`str` subclass (i.e. ``type(unknown) == str``
- instead of ``isinstance(str, unknown)``), and in those locations you
- will need to use ``str(StrEnum.member)``.
+Iterating over the members of an enum does not provide the aliases::
+
+ >>> list(Shape)
+ [<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
+
+The special attribute ``__members__`` is a read-only ordered mapping of names
+to members. It includes all names defined in the enumeration, including the
+aliases::
+
+ >>> for name, member in Shape.__members__.items():
+ ... name, member
+ ...
+ ('SQUARE', <Shape.SQUARE: 2>)
+ ('DIAMOND', <Shape.DIAMOND: 1>)
+ ('CIRCLE', <Shape.CIRCLE: 3>)
+ ('ALIAS_FOR_SQUARE', <Shape.SQUARE: 2>)
+The ``__members__`` attribute can be used for detailed programmatic access to
+the enumeration members. For example, finding all the aliases::
-.. note::
+ >>> [name for name, member in Shape.__members__.items() if member.name != name]
+ ['ALIAS_FOR_SQUARE']
- Using :class:`auto` with :class:`StrEnum` results in values of the member name,
- lower-cased.
+Comparisons
+-----------
-.. class:: Flag
+Enumeration members are compared by identity::
- *Flag* members support the bitwise operators ``&`` (*AND*), ``|`` (*OR*),
- ``^`` (*XOR*), and ``~`` (*INVERT*); the results of those operators are members
- of the enumeration.
+ >>> Color.RED is Color.RED
+ True
+ >>> Color.RED is Color.BLUE
+ False
+ >>> Color.RED is not Color.BLUE
+ True
- .. method:: __contains__(self, value)
+Ordered comparisons between enumeration values are *not* supported. Enum
+members are not integers (but see `IntEnum`_ below)::
+
+ >>> Color.RED < Color.BLUE
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ TypeError: '<' not supported between instances of 'Color' and 'Color'
+
+Equality comparisons are defined though::
+
+ >>> Color.BLUE == Color.RED
+ False
+ >>> Color.BLUE != Color.RED
+ True
+ >>> Color.BLUE == Color.BLUE
+ True
- Returns *True* if value is in self::
+Comparisons against non-enumeration values will always compare not equal
+(again, :class:`IntEnum` was explicitly designed to behave differently, see
+below)::
- >>> from enum import Flag, auto
- >>> class Color(Flag):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> purple = Color.RED | Color.BLUE
- >>> white = Color.RED | Color.GREEN | Color.BLUE
- >>> Color.GREEN in purple
- False
- >>> Color.GREEN in white
- True
- >>> purple in white
- True
- >>> white in purple
- False
+ >>> Color.BLUE == 2
+ False
- .. method:: __iter__(self):
- Returns all contained members::
+Allowed members and attributes of enumerations
+----------------------------------------------
- >>> list(Color.RED)
- [Color.RED]
- >>> list(purple)
- [Color.RED, Color.BLUE]
+The examples above use integers for enumeration values. Using integers is
+short and handy (and provided by default by the `Functional API`_), but not
+strictly enforced. In the vast majority of use-cases, one doesn't care what
+the actual value of an enumeration is. But if the value *is* important,
+enumerations can have arbitrary values.
- .. method:: __len__(self):
+Enumerations are Python classes, and can have methods and special methods as
+usual. If we have this enumeration::
- Returns number of members in flag::
+ >>> class Mood(Enum):
+ ... FUNKY = 1
+ ... HAPPY = 3
+ ...
+ ... def describe(self):
+ ... # self is the member here
+ ... return self.name, self.value
+ ...
+ ... def __str__(self):
+ ... return 'my custom str! {0}'.format(self.value)
+ ...
+ ... @classmethod
+ ... def favorite_mood(cls):
+ ... # cls here is the enumeration
+ ... return cls.HAPPY
+ ...
- >>> len(Color.GREEN)
- 1
- >>> len(white)
- 3
+Then::
- .. method:: __bool__(self):
+ >>> Mood.favorite_mood()
+ <Mood.HAPPY: 3>
+ >>> Mood.HAPPY.describe()
+ ('HAPPY', 3)
+ >>> str(Mood.FUNKY)
+ 'my custom str! 1'
+
+The rules for what is allowed are as follows: names that start and end with
+a single underscore are reserved by enum and cannot be used; all other
+attributes defined within an enumeration will become members of this
+enumeration, with the exception of special methods (:meth:`__str__`,
+:meth:`__add__`, etc.), descriptors (methods are also descriptors), and
+variable names listed in :attr:`_ignore_`.
+
+Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then
+any value(s) given to the enum member will be passed into those methods.
+See `Planet`_ for an example.
+
+
+Restricted Enum subclassing
+---------------------------
+
+A new :class:`Enum` class must have one base Enum class, up to one concrete
+data type, and as many :class:`object`-based mixin classes as needed. The
+order of these base classes is::
+
+ class EnumName([mix-in, ...,] [data-type,] base-enum):
+ pass
+
+Also, subclassing an enumeration is allowed only if the enumeration does not define
+any members. So this is forbidden::
+
+ >>> class MoreColor(Color):
+ ... PINK = 17
+ ...
+ Traceback (most recent call last):
+ ...
+ TypeError: MoreColor: cannot extend enumeration 'Color'
+
+But this is allowed::
+
+ >>> class Foo(Enum):
+ ... def some_behavior(self):
+ ... pass
+ ...
+ >>> class Bar(Foo):
+ ... HAPPY = 1
+ ... SAD = 2
+ ...
+
+Allowing subclassing of enums that define members would lead to a violation of
+some important invariants of types and instances. On the other hand, it makes
+sense to allow sharing some common behavior between a group of enumerations.
+(See `OrderedEnum`_ for an example.)
+
+
+Pickling
+--------
+
+Enumerations can be pickled and unpickled::
+
+ >>> from test.test_enum import Fruit
+ >>> from pickle import dumps, loads
+ >>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO))
+ True
+
+The usual restrictions for pickling apply: picklable enums must be defined in
+the top level of a module, since unpickling requires them to be importable
+from that module.
- Returns *True* if any members in flag, *False* otherwise::
+.. note::
- >>> bool(Color.GREEN)
- True
- >>> bool(white)
- True
- >>> black = Color(0)
- >>> bool(black)
- False
+ With pickle protocol version 4 it is possible to easily pickle enums
+ nested in other classes.
- .. method:: __or__(self, other)
+It is possible to modify how Enum members are pickled/unpickled by defining
+:meth:`__reduce_ex__` in the enumeration class.
- Returns current flag binary or'ed with other::
- >>> Color.RED | Color.GREEN
- Color.RED|Color.GREEN
+Functional API
+--------------
- .. method:: __and__(self, other)
+The :class:`Enum` class is callable, providing the following functional API::
- Returns current flag binary and'ed with other::
+ >>> Animal = Enum('Animal', 'ANT BEE CAT DOG')
+ >>> Animal
+ <enum 'Animal'>
+ >>> Animal.ANT
+ <Animal.ANT: 1>
+ >>> Animal.ANT.value
+ 1
+ >>> list(Animal)
+ [<Animal.ANT: 1>, <Animal.BEE: 2>, <Animal.CAT: 3>, <Animal.DOG: 4>]
- >>> purple & white
- Color.RED|Color.BLUE
- >>> purple & Color.GREEN
- 0x0
+The semantics of this API resemble :class:`~collections.namedtuple`. The first
+argument of the call to :class:`Enum` is the name of the enumeration.
- .. method:: __xor__(self, other)
+The second argument is the *source* of enumeration member names. It can be a
+whitespace-separated string of names, a sequence of names, a sequence of
+2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to
+values. The last two options enable assigning arbitrary values to
+enumerations; the others auto-assign increasing integers starting with 1 (use
+the ``start`` parameter to specify a different starting value). A
+new class derived from :class:`Enum` is returned. In other words, the above
+assignment to :class:`Animal` is equivalent to::
- Returns current flag binary xor'ed with other::
+ >>> class Animal(Enum):
+ ... ANT = 1
+ ... BEE = 2
+ ... CAT = 3
+ ... DOG = 4
+ ...
- >>> purple ^ white
- Color.GREEN
- >>> purple ^ Color.GREEN
- Color.RED|Color.GREEN|Color.BLUE
+The reason for defaulting to ``1`` as the starting number and not ``0`` is
+that ``0`` is ``False`` in a boolean sense, but enum members all evaluate
+to ``True``.
- .. method:: __invert__(self):
+Pickling enums created with the functional API can be tricky as frame stack
+implementation details are used to try and figure out which module the
+enumeration is being created in (e.g. it will fail if you use a utility
+function in separate module, and also may not work on IronPython or Jython).
+The solution is to specify the module name explicitly as follows::
- Returns all the flags in *type(self)* that are not in self::
+ >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__)
- >>> ~white
- 0x0
- >>> ~purple
- Color.GREEN
- >>> ~Color.RED
- Color.GREEN|Color.BLUE
+.. warning::
-.. note::
+ If ``module`` is not supplied, and Enum cannot determine what it is,
+ the new Enum members will not be unpicklable; to keep errors closer to
+ the source, pickling will be disabled.
- Using :class:`auto` with :class:`Flag` results in integers that are powers
- of two, starting with ``1``.
+The new pickle protocol 4 also, in some circumstances, relies on
+:attr:`~definition.__qualname__` being set to the location where pickle will be able
+to find the class. For example, if the class was made available in class
+SomeData in the global scope::
+ >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal')
-.. class:: IntFlag
+The complete signature is::
- *IntFlag* is the same as *Flag*, but its members are also integers and can be
- used anywhere that an integer can be used.
+ Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=<mixed-in class>, start=1)
- >>> from enum import IntFlag, auto
- >>> class Color(IntFlag):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> Color.RED & 2
- 0x0
- >>> Color.RED | 2
- Color.RED|Color.GREEN
+:value: What the new Enum class will record as its name.
- If any integer operation is performed with an *IntFlag* member, the result is
- not an *IntFlag*::
+:names: The Enum members. This can be a whitespace or comma separated string
+ (values will start at 1 unless otherwise specified)::
- >>> Color.RED + 2
- 3
+ 'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE'
- If a *Flag* operation is performed with an *IntFlag* member and:
+ or an iterator of names::
- * the result is a valid *IntFlag*: an *IntFlag* is returned
- * the result is not a valid *IntFlag*: the result depends on the *FlagBoundary* setting
+ ['RED', 'GREEN', 'BLUE']
-.. note::
+ or an iterator of (name, value) pairs::
- Using :class:`auto` with :class:`IntFlag` results in integers that are powers
- of two, starting with ``1``.
+ [('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)]
-.. class:: EnumCheck
+ or a mapping::
- *EnumCheck* contains the options used by the :func:`verify` decorator to ensure
- various constraints; failed constraints result in a :exc:`TypeError`.
+ {'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42}
- .. attribute:: UNIQUE
+:module: name of module where new Enum class can be found.
- Ensure that each value has only one name::
+:qualname: where in module new Enum class can be found.
- >>> from enum import Enum, verify, UNIQUE
- >>> @verify(UNIQUE)
- ... class Color(Enum):
- ... RED = 1
- ... GREEN = 2
- ... BLUE = 3
- ... CRIMSON = 1
- Traceback (most recent call last):
- ...
- ValueError: aliases found in <enum 'Color'>: CRIMSON -> RED
+:type: type to mix in to new Enum class.
+:start: number to start counting at if only names are passed in.
- .. attribute:: CONTINUOUS
+.. versionchanged:: 3.5
+ The *start* parameter was added.
- Ensure that there are no missing values between the lowest-valued member
- and the highest-valued member::
- >>> from enum import Enum, verify, CONTINUOUS
- >>> @verify(CONTINUOUS)
- ... class Color(Enum):
- ... RED = 1
- ... GREEN = 2
- ... BLUE = 5
- Traceback (most recent call last):
- ...
- ValueError: invalid enum 'Color': missing values 3, 4
+Derived Enumerations
+--------------------
- .. attribute:: NAMED_FLAGS
+IntEnum
+^^^^^^^
- Ensure that any flag groups/masks contain only named flags -- useful when
- values are specified instead of being generated by :func:`auto`
+The first variation of :class:`Enum` that is provided is also a subclass of
+:class:`int`. Members of an :class:`IntEnum` can be compared to integers;
+by extension, integer enumerations of different types can also be compared
+to each other::
- >>> from enum import Flag, verify, NAMED_FLAGS
- >>> @verify(NAMED_FLAGS)
- ... class Color(Flag):
- ... RED = 1
- ... GREEN = 2
- ... BLUE = 4
- ... WHITE = 15
- ... NEON = 31
- Traceback (most recent call last):
- ...
- ValueError: invalid Flag 'Color': aliases WHITE and NEON are missing combined values of 0x18 [use enum.show_flag_values(value) for details]
+ >>> from enum import IntEnum
+ >>> class Shape(IntEnum):
+ ... CIRCLE = 1
+ ... SQUARE = 2
+ ...
+ >>> class Request(IntEnum):
+ ... POST = 1
+ ... GET = 2
+ ...
+ >>> Shape == 1
+ False
+ >>> Shape.CIRCLE == 1
+ True
+ >>> Shape.CIRCLE == Request.POST
+ True
-.. note::
+However, they still can't be compared to standard :class:`Enum` enumerations::
- CONTINUOUS and NAMED_FLAGS are designed to work with integer-valued members.
+ >>> class Shape(IntEnum):
+ ... CIRCLE = 1
+ ... SQUARE = 2
+ ...
+ >>> class Color(Enum):
+ ... RED = 1
+ ... GREEN = 2
+ ...
+ >>> Shape.CIRCLE == Color.RED
+ False
-.. versionadded:: 3.10
+:class:`IntEnum` values behave like integers in other ways you'd expect::
-.. class:: FlagBoundary
+ >>> int(Shape.CIRCLE)
+ 1
+ >>> ['a', 'b', 'c'][Shape.CIRCLE]
+ 'b'
+ >>> [i for i in range(Shape.SQUARE)]
+ [0, 1]
- *FlagBoundary* controls how out-of-range values are handled in *Flag* and its
- subclasses.
- .. attribute:: STRICT
+IntFlag
+^^^^^^^
- Out-of-range values cause a :exc:`ValueError` to be raised. This is the
- default for :class:`Flag`::
+The next variation of :class:`Enum` provided, :class:`IntFlag`, is also based
+on :class:`int`. The difference being :class:`IntFlag` members can be combined
+using the bitwise operators (&, \|, ^, ~) and the result is still an
+:class:`IntFlag` member. However, as the name implies, :class:`IntFlag`
+members also subclass :class:`int` and can be used wherever an :class:`int` is
+used. Any operation on an :class:`IntFlag` member besides the bit-wise
+operations will lose the :class:`IntFlag` membership.
- >>> from enum import Flag, STRICT
- >>> class StrictFlag(Flag, boundary=STRICT):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> StrictFlag(2**2 + 2**4)
- Traceback (most recent call last):
- ...
- ValueError: StrictFlag: invalid value: 20
- given 0b0 10100
- allowed 0b0 00111
+.. versionadded:: 3.6
- .. attribute:: CONFORM
+Sample :class:`IntFlag` class::
- Out-of-range values have invalid values removed, leaving a valid *Flag*
- value::
+ >>> from enum import IntFlag
+ >>> class Perm(IntFlag):
+ ... R = 4
+ ... W = 2
+ ... X = 1
+ ...
+ >>> Perm.R | Perm.W
+ <Perm.R|W: 6>
+ >>> Perm.R + Perm.W
+ 6
+ >>> RW = Perm.R | Perm.W
+ >>> Perm.R in RW
+ True
+
+It is also possible to name the combinations::
+
+ >>> class Perm(IntFlag):
+ ... R = 4
+ ... W = 2
+ ... X = 1
+ ... RWX = 7
+ >>> Perm.RWX
+ <Perm.RWX: 7>
+ >>> ~Perm.RWX
+ <Perm.-8: -8>
+
+Another important difference between :class:`IntFlag` and :class:`Enum` is that
+if no flags are set (the value is 0), its boolean evaluation is :data:`False`::
+
+ >>> Perm.R & Perm.X
+ <Perm.0: 0>
+ >>> bool(Perm.R & Perm.X)
+ False
+
+Because :class:`IntFlag` members are also subclasses of :class:`int` they can
+be combined with them::
+
+ >>> Perm.X | 8
+ <Perm.8|X: 9>
+
+
+Flag
+^^^^
+
+The last variation is :class:`Flag`. Like :class:`IntFlag`, :class:`Flag`
+members can be combined using the bitwise operators (&, \|, ^, ~). Unlike
+:class:`IntFlag`, they cannot be combined with, nor compared against, any
+other :class:`Flag` enumeration, nor :class:`int`. While it is possible to
+specify the values directly it is recommended to use :class:`auto` as the
+value and let :class:`Flag` select an appropriate value.
+
+.. versionadded:: 3.6
+
+Like :class:`IntFlag`, if a combination of :class:`Flag` members results in no
+flags being set, the boolean evaluation is :data:`False`::
+
+ >>> from enum import Flag, auto
+ >>> class Color(Flag):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> Color.RED & Color.GREEN
+ <Color.0: 0>
+ >>> bool(Color.RED & Color.GREEN)
+ False
+
+Individual flags should have values that are powers of two (1, 2, 4, 8, ...),
+while combinations of flags won't::
+
+ >>> class Color(Flag):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ... WHITE = RED | BLUE | GREEN
+ ...
+ >>> Color.WHITE
+ <Color.WHITE: 7>
+
+Giving a name to the "no flags set" condition does not change its boolean
+value::
+
+ >>> class Color(Flag):
+ ... BLACK = 0
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> Color.BLACK
+ <Color.BLACK: 0>
+ >>> bool(Color.BLACK)
+ False
- >>> from enum import Flag, CONFORM
- >>> class ConformFlag(Flag, boundary=CONFORM):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> ConformFlag(2**2 + 2**4)
- ConformFlag.BLUE
+.. note::
- .. attribute:: EJECT
+ For the majority of new code, :class:`Enum` and :class:`Flag` are strongly
+ recommended, since :class:`IntEnum` and :class:`IntFlag` break some
+ semantic promises of an enumeration (by being comparable to integers, and
+ thus by transitivity to other unrelated enumerations). :class:`IntEnum`
+ and :class:`IntFlag` should be used only in cases where :class:`Enum` and
+ :class:`Flag` will not do; for example, when integer constants are replaced
+ with enumerations, or for interoperability with other systems.
+
+
+Others
+^^^^^^
+
+While :class:`IntEnum` is part of the :mod:`enum` module, it would be very
+simple to implement independently::
+
+ class IntEnum(int, Enum):
+ pass
+
+This demonstrates how similar derived enumerations can be defined; for example
+a :class:`StrEnum` that mixes in :class:`str` instead of :class:`int`.
+
+Some rules:
+
+1. When subclassing :class:`Enum`, mix-in types must appear before
+ :class:`Enum` itself in the sequence of bases, as in the :class:`IntEnum`
+ example above.
+2. While :class:`Enum` can have members of any type, once you mix in an
+ additional type, all the members must have values of that type, e.g.
+ :class:`int` above. This restriction does not apply to mix-ins which only
+ add methods and don't specify another type.
+3. When another data type is mixed in, the :attr:`value` attribute is *not the
+ same* as the enum member itself, although it is equivalent and will compare
+ equal.
+4. %-style formatting: `%s` and `%r` call the :class:`Enum` class's
+ :meth:`__str__` and :meth:`__repr__` respectively; other codes (such as
+ `%i` or `%h` for IntEnum) treat the enum member as its mixed-in type.
+5. :ref:`Formatted string literals <f-strings>`, :meth:`str.format`,
+ and :func:`format` will use the mixed-in type's :meth:`__format__`
+ unless :meth:`__str__` or :meth:`__format__` is overridden in the subclass,
+ in which case the overridden methods or :class:`Enum` methods will be used.
+ Use the !s and !r format codes to force usage of the :class:`Enum` class's
+ :meth:`__str__` and :meth:`__repr__` methods.
+
+When to use :meth:`__new__` vs. :meth:`__init__`
+------------------------------------------------
+
+:meth:`__new__` must be used whenever you want to customize the actual value of
+the :class:`Enum` member. Any other modifications may go in either
+:meth:`__new__` or :meth:`__init__`, with :meth:`__init__` being preferred.
+
+For example, if you want to pass several items to the constructor, but only
+want one of them to be the value::
+
+ >>> class Coordinate(bytes, Enum):
+ ... """
+ ... Coordinate with binary codes that can be indexed by the int code.
+ ... """
+ ... def __new__(cls, value, label, unit):
+ ... obj = bytes.__new__(cls, [value])
+ ... obj._value_ = value
+ ... obj.label = label
+ ... obj.unit = unit
+ ... return obj
+ ... PX = (0, 'P.X', 'km')
+ ... PY = (1, 'P.Y', 'km')
+ ... VX = (2, 'V.X', 'km/s')
+ ... VY = (3, 'V.Y', 'km/s')
+ ...
+
+ >>> print(Coordinate['PY'])
+ Coordinate.PY
+
+ >>> print(Coordinate(3))
+ Coordinate.VY
+
+Interesting examples
+--------------------
+
+While :class:`Enum`, :class:`IntEnum`, :class:`IntFlag`, and :class:`Flag` are
+expected to cover the majority of use-cases, they cannot cover them all. Here
+are recipes for some different types of enumerations that can be used directly,
+or as examples for creating one's own.
+
+
+Omitting values
+^^^^^^^^^^^^^^^
+
+In many use-cases one doesn't care what the actual value of an enumeration
+is. There are several ways to define this type of simple enumeration:
+
+- use instances of :class:`auto` for the value
+- use instances of :class:`object` as the value
+- use a descriptive string as the value
+- use a tuple as the value and a custom :meth:`__new__` to replace the
+ tuple with an :class:`int` value
+
+Using any of these methods signifies to the user that these values are not
+important, and also enables one to add, remove, or reorder members without
+having to renumber the remaining members.
+
+Whichever method you choose, you should provide a :meth:`repr` that also hides
+the (unimportant) value::
+
+ >>> class NoValue(Enum):
+ ... def __repr__(self):
+ ... return '<%s.%s>' % (self.__class__.__name__, self.name)
+ ...
+
+
+Using :class:`auto`
+"""""""""""""""""""
+
+Using :class:`auto` would look like::
+
+ >>> class Color(NoValue):
+ ... RED = auto()
+ ... BLUE = auto()
+ ... GREEN = auto()
+ ...
+ >>> Color.GREEN
+ <Color.GREEN>
+
+
+Using :class:`object`
+"""""""""""""""""""""
+
+Using :class:`object` would look like::
+
+ >>> class Color(NoValue):
+ ... RED = object()
+ ... GREEN = object()
+ ... BLUE = object()
+ ...
+ >>> Color.GREEN
+ <Color.GREEN>
+
+
+Using a descriptive string
+""""""""""""""""""""""""""
+
+Using a string as the value would look like::
+
+ >>> class Color(NoValue):
+ ... RED = 'stop'
+ ... GREEN = 'go'
+ ... BLUE = 'too fast!'
+ ...
+ >>> Color.GREEN
+ <Color.GREEN>
+ >>> Color.GREEN.value
+ 'go'
+
+
+Using a custom :meth:`__new__`
+""""""""""""""""""""""""""""""
+
+Using an auto-numbering :meth:`__new__` would look like::
+
+ >>> class AutoNumber(NoValue):
+ ... def __new__(cls):
+ ... value = len(cls.__members__) + 1
+ ... obj = object.__new__(cls)
+ ... obj._value_ = value
+ ... return obj
+ ...
+ >>> class Color(AutoNumber):
+ ... RED = ()
+ ... GREEN = ()
+ ... BLUE = ()
+ ...
+ >>> Color.GREEN
+ <Color.GREEN>
+ >>> Color.GREEN.value
+ 2
+
+To make a more general purpose ``AutoNumber``, add ``*args`` to the signature::
+
+ >>> class AutoNumber(NoValue):
+ ... def __new__(cls, *args): # this is the only change from above
+ ... value = len(cls.__members__) + 1
+ ... obj = object.__new__(cls)
+ ... obj._value_ = value
+ ... return obj
+ ...
+
+Then when you inherit from ``AutoNumber`` you can write your own ``__init__``
+to handle any extra arguments::
+
+ >>> class Swatch(AutoNumber):
+ ... def __init__(self, pantone='unknown'):
+ ... self.pantone = pantone
+ ... AUBURN = '3497'
+ ... SEA_GREEN = '1246'
+ ... BLEACHED_CORAL = () # New color, no Pantone code yet!
+ ...
+ >>> Swatch.SEA_GREEN
+ <Swatch.SEA_GREEN>
+ >>> Swatch.SEA_GREEN.pantone
+ '1246'
+ >>> Swatch.BLEACHED_CORAL.pantone
+ 'unknown'
- Out-of-range values lose their *Flag* membership and revert to :class:`int`.
- This is the default for :class:`IntFlag`::
+.. note::
- >>> from enum import Flag, EJECT
- >>> class EjectFlag(Flag, boundary=EJECT):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> EjectFlag(2**2 + 2**4)
- 20
+ The :meth:`__new__` method, if defined, is used during creation of the Enum
+ members; it is then replaced by Enum's :meth:`__new__` which is used after
+ class creation for lookup of existing members.
+
+
+OrderedEnum
+^^^^^^^^^^^
+
+An ordered enumeration that is not based on :class:`IntEnum` and so maintains
+the normal :class:`Enum` invariants (such as not being comparable to other
+enumerations)::
+
+ >>> class OrderedEnum(Enum):
+ ... def __ge__(self, other):
+ ... if self.__class__ is other.__class__:
+ ... return self.value >= other.value
+ ... return NotImplemented
+ ... def __gt__(self, other):
+ ... if self.__class__ is other.__class__:
+ ... return self.value > other.value
+ ... return NotImplemented
+ ... def __le__(self, other):
+ ... if self.__class__ is other.__class__:
+ ... return self.value <= other.value
+ ... return NotImplemented
+ ... def __lt__(self, other):
+ ... if self.__class__ is other.__class__:
+ ... return self.value < other.value
+ ... return NotImplemented
+ ...
+ >>> class Grade(OrderedEnum):
+ ... A = 5
+ ... B = 4
+ ... C = 3
+ ... D = 2
+ ... F = 1
+ ...
+ >>> Grade.C < Grade.A
+ True
+
+
+DuplicateFreeEnum
+^^^^^^^^^^^^^^^^^
+
+Raises an error if a duplicate member name is found instead of creating an
+alias::
+
+ >>> class DuplicateFreeEnum(Enum):
+ ... def __init__(self, *args):
+ ... cls = self.__class__
+ ... if any(self.value == e.value for e in cls):
+ ... a = self.name
+ ... e = cls(self.value).name
+ ... raise ValueError(
+ ... "aliases not allowed in DuplicateFreeEnum: %r --> %r"
+ ... % (a, e))
+ ...
+ >>> class Color(DuplicateFreeEnum):
+ ... RED = 1
+ ... GREEN = 2
+ ... BLUE = 3
+ ... GRENE = 2
+ ...
+ Traceback (most recent call last):
+ ...
+ ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN'
- .. attribute:: KEEP
+.. note::
- Out-of-range values are kept, and the *Flag* membership is kept. This is
- used for some stdlib flags:
+ This is a useful example for subclassing Enum to add or change other
+ behaviors as well as disallowing aliases. If the only desired change is
+ disallowing aliases, the :func:`unique` decorator can be used instead.
- >>> from enum import Flag, KEEP
- >>> class KeepFlag(Flag, boundary=KEEP):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> KeepFlag(2**2 + 2**4)
- KeepFlag.BLUE|0x10
-.. versionadded:: 3.10
+Planet
+^^^^^^
----------------
+If :meth:`__new__` or :meth:`__init__` is defined the value of the enum member
+will be passed to those methods::
+
+ >>> class Planet(Enum):
+ ... MERCURY = (3.303e+23, 2.4397e6)
+ ... VENUS = (4.869e+24, 6.0518e6)
+ ... EARTH = (5.976e+24, 6.37814e6)
+ ... MARS = (6.421e+23, 3.3972e6)
+ ... JUPITER = (1.9e+27, 7.1492e7)
+ ... SATURN = (5.688e+26, 6.0268e7)
+ ... URANUS = (8.686e+25, 2.5559e7)
+ ... NEPTUNE = (1.024e+26, 2.4746e7)
+ ... def __init__(self, mass, radius):
+ ... self.mass = mass # in kilograms
+ ... self.radius = radius # in meters
+ ... @property
+ ... def surface_gravity(self):
+ ... # universal gravitational constant (m3 kg-1 s-2)
+ ... G = 6.67300E-11
+ ... return G * self.mass / (self.radius * self.radius)
+ ...
+ >>> Planet.EARTH.value
+ (5.976e+24, 6378140.0)
+ >>> Planet.EARTH.surface_gravity
+ 9.802652743337129
+
+
+TimePeriod
+^^^^^^^^^^
+
+An example to show the :attr:`_ignore_` attribute in use::
+
+ >>> from datetime import timedelta
+ >>> class Period(timedelta, Enum):
+ ... "different lengths of time"
+ ... _ignore_ = 'Period i'
+ ... Period = vars()
+ ... for i in range(367):
+ ... Period['day_%d' % i] = i
+ ...
+ >>> list(Period)[:2]
+ [<Period.day_0: datetime.timedelta(0)>, <Period.day_1: datetime.timedelta(days=1)>]
+ >>> list(Period)[-2:]
+ [<Period.day_365: datetime.timedelta(days=365)>, <Period.day_366: datetime.timedelta(days=366)>]
+
+
+How are Enums different?
+------------------------
+
+Enums have a custom metaclass that affects many aspects of both derived Enum
+classes and their instances (members).
+
+
+Enum Classes
+^^^^^^^^^^^^
+
+The :class:`EnumMeta` metaclass is responsible for providing the
+:meth:`__contains__`, :meth:`__dir__`, :meth:`__iter__` and other methods that
+allow one to do things with an :class:`Enum` class that fail on a typical
+class, such as `list(Color)` or `some_enum_var in Color`. :class:`EnumMeta` is
+responsible for ensuring that various other methods on the final :class:`Enum`
+class are correct (such as :meth:`__new__`, :meth:`__getnewargs__`,
+:meth:`__str__` and :meth:`__repr__`).
+
+
+Enum Members (aka instances)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The most interesting thing about Enum members is that they are singletons.
+:class:`EnumMeta` creates them all while it is creating the :class:`Enum`
+class itself, and then puts a custom :meth:`__new__` in place to ensure
+that no new ones are ever instantiated by returning only the existing
+member instances.
+
+
+Finer Points
+^^^^^^^^^^^^
+
+Supported ``__dunder__`` names
+""""""""""""""""""""""""""""""
+
+:attr:`__members__` is a read-only ordered mapping of ``member_name``:``member``
+items. It is only available on the class.
+
+:meth:`__new__`, if specified, must create and return the enum members; it is
+also a very good idea to set the member's :attr:`_value_` appropriately. Once
+all the members are created it is no longer used.
+
+
+Supported ``_sunder_`` names
+""""""""""""""""""""""""""""
+
+- ``_name_`` -- name of the member
+- ``_value_`` -- value of the member; can be set / modified in ``__new__``
+
+- ``_missing_`` -- a lookup function used when a value is not found; may be
+ overridden
+- ``_ignore_`` -- a list of names, either as a :class:`list` or a :class:`str`,
+ that will not be transformed into members, and will be removed from the final
+ class
+- ``_order_`` -- used in Python 2/3 code to ensure member order is consistent
+ (class attribute, removed during class creation)
+- ``_generate_next_value_`` -- used by the `Functional API`_ and by
+ :class:`auto` to get an appropriate value for an enum member; may be
+ overridden
+
+.. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_``
+.. versionadded:: 3.7 ``_ignore_``
+
+To help keep Python 2 / Python 3 code in sync an :attr:`_order_` attribute can
+be provided. It will be checked against the actual order of the enumeration
+and raise an error if the two do not match::
+
+ >>> class Color(Enum):
+ ... _order_ = 'RED GREEN BLUE'
+ ... RED = 1
+ ... BLUE = 3
+ ... GREEN = 2
+ ...
+ Traceback (most recent call last):
+ ...
+ TypeError: member order does not match _order_
-Utilites and Decorators
------------------------
+.. note::
-.. class:: auto
+ In Python 2 code the :attr:`_order_` attribute is necessary as definition
+ order is lost before it can be recorded.
- *auto* can be used in place of a value. If used, the *Enum* machinery will
- call an *Enum*'s :meth:`_generate_next_value_` to get an appropriate value.
- For *Enum* and *IntEnum* that appropriate value will be the last value plus
- one; for *Flag* and *IntFlag* it will be the first power-of-two greater
- than the last value; for *StrEnum* it will be the lower-cased version of the
- member's name.
- ``_generate_next_value_`` can be overridden to customize the values used by
- *auto*.
+_Private__names
+"""""""""""""""
-.. decorator:: global_enum
+Private names will be normal attributes in Python 3.10 instead of either an error
+or a member (depending on if the name ends with an underscore). Using these names
+in 3.9 will issue a :exc:`DeprecationWarning`.
- A :keyword:`class` decorator specifically for enumerations. It replaces the
- :meth:`__repr__` method with one that shows *module_name*.*member_name*. It
- also injects the members, and their aliases, into the global namespace they
- were defined in.
+``Enum`` member type
+""""""""""""""""""""
-.. decorator:: property
+:class:`Enum` members are instances of their :class:`Enum` class, and are
+normally accessed as ``EnumClass.member``. Under certain circumstances they
+can also be accessed as ``EnumClass.member.member``, but you should never do
+this as that lookup may fail or, worse, return something besides the
+:class:`Enum` member you are looking for (this is another good reason to use
+all-uppercase names for members)::
- A decorator similar to the built-in *property*, but specifically for
- enumerations. It allows member attributes to have the same names as members
- themselves.
+ >>> class FieldTypes(Enum):
+ ... name = 0
+ ... value = 1
+ ... size = 2
+ ...
+ >>> FieldTypes.value.size
+ <FieldTypes.size: 2>
+ >>> FieldTypes.size.value
+ 2
- .. note:: the *property* and the member must be defined in separate classes;
- for example, the *value* and *name* attributes are defined in the
- *Enum* class, and *Enum* subclasses can define members with the
- names ``value`` and ``name``.
+.. versionchanged:: 3.5
-.. decorator:: unique
- A :keyword:`class` decorator specifically for enumerations. It searches an
- enumeration's :attr:`__members__`, gathering any aliases it finds; if any are
- found :exc:`ValueError` is raised with the details::
+Boolean value of ``Enum`` classes and members
+"""""""""""""""""""""""""""""""""""""""""""""
- >>> from enum import Enum, unique
- >>> @unique
- ... class Mistake(Enum):
- ... ONE = 1
- ... TWO = 2
- ... THREE = 3
- ... FOUR = 3
- ...
- Traceback (most recent call last):
- ...
- ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
+:class:`Enum` members that are mixed with non-:class:`Enum` types (such as
+:class:`int`, :class:`str`, etc.) are evaluated according to the mixed-in
+type's rules; otherwise, all members evaluate as :data:`True`. To make your
+own Enum's boolean evaluation depend on the member's value add the following to
+your class::
-.. decorator:: verify
+ def __bool__(self):
+ return bool(self.value)
- A :keyword:`class` decorator specifically for enumerations. Members from
- :class:`EnumCheck` are used to specify which constraints should be checked
- on the decorated enumeration.
+:class:`Enum` classes always evaluate as :data:`True`.
-.. versionadded:: 3.10
----------------
+``Enum`` classes with methods
+"""""""""""""""""""""""""""""
-Notes
------
+If you give your :class:`Enum` subclass extra methods, like the `Planet`_
+class above, those methods will show up in a :func:`dir` of the member,
+but not of the class::
-:class:`IntEnum`, :class:`StrEnum`, and :class:`IntFlag`
+ >>> dir(Planet)
+ ['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__']
+ >>> dir(Planet.EARTH)
+ ['__class__', '__doc__', '__module__', 'mass', 'name', 'radius', 'surface_gravity', 'value']
- These three enum types are designed to be drop-in replacements for existing
- integer- and string-based values; as such, they have extra limitations:
- - ``format()`` will use the value of the enum member, unless ``__str__``
- has been overridden
+Combining members of ``Flag``
+"""""""""""""""""""""""""""""
- - ``StrEnum.__str__`` uses the value and not the name of the enum member
+If a combination of Flag members is not named, the :func:`repr` will include
+all named flags and all named combinations of flags that are in the value::
- If you do not need/want those limitations, you can create your own base
- class by mixing in the ``int`` or ``str`` type yourself::
+ >>> class Color(Flag):
+ ... RED = auto()
+ ... GREEN = auto()
+ ... BLUE = auto()
+ ... MAGENTA = RED | BLUE
+ ... YELLOW = RED | GREEN
+ ... CYAN = GREEN | BLUE
+ ...
+ >>> Color(3) # named combination
+ <Color.YELLOW: 3>
+ >>> Color(7) # not named combination
+ <Color.CYAN|MAGENTA|BLUE|YELLOW|GREEN|RED: 7>
- >>> from enum import Enum
- >>> class MyIntEnum(int, Enum):
- ... pass
diff --git a/Doc/library/http.rst b/Doc/library/http.rst
index 1569d504c7f92..2466c84476c6c 100644
--- a/Doc/library/http.rst
+++ b/Doc/library/http.rst
@@ -35,7 +35,7 @@ associated messages through the :class:`http.HTTPStatus` enum:
>>> from http import HTTPStatus
>>> HTTPStatus.OK
- HTTPStatus.OK
+ <HTTPStatus.OK: 200>
>>> HTTPStatus.OK == 200
True
>>> HTTPStatus.OK.value
@@ -45,7 +45,7 @@ associated messages through the :class:`http.HTTPStatus` enum:
>>> HTTPStatus.OK.description
'Request fulfilled, document follows'
>>> list(HTTPStatus)
- [HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...]
+ [<HTTPStatus.CONTINUE: 100>, <HTTPStatus.SWITCHING_PROTOCOLS:101>, ...]
.. _http-status-codes:
diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst
index 914fdd7296dbb..ddda4d2de947a 100755
--- a/Doc/library/socket.rst
+++ b/Doc/library/socket.rst
@@ -788,9 +788,9 @@ The :mod:`socket` module also offers various network-related services:
system if IPv6 isn't enabled)::
>>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP)
- [(socket.AF_INET6, socket.SOCK_STREAM,
+ [(<AddressFamily.AF_INET6: 10>, <AddressFamily.SOCK_STREAM: 1>,
6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)),
- (socket.AF_INET, socket.SOCK_STREAM,
+ (<AddressFamily.AF_INET: 2>, <AddressFamily.SOCK_STREAM: 1>,
6, '', ('93.184.216.34', 80))]
.. versionchanged:: 3.2
diff --git a/Doc/library/ssl.rst b/Doc/library/ssl.rst
index 4902d34888ebc..4b41d1137db52 100644
--- a/Doc/library/ssl.rst
+++ b/Doc/library/ssl.rst
@@ -2070,7 +2070,7 @@ to speed up repeated connections from the same clients.
:attr:`SSLContext.verify_flags` returns :class:`VerifyFlags` flags:
>>> ssl.create_default_context().verify_flags # doctest: +SKIP
- ssl.VERIFY_X509_TRUSTED_FIRST
+ <VerifyFlags.VERIFY_X509_TRUSTED_FIRST: 32768>
.. attribute:: SSLContext.verify_mode
@@ -2082,7 +2082,7 @@ to speed up repeated connections from the same clients.
:attr:`SSLContext.verify_mode` returns :class:`VerifyMode` enum:
>>> ssl.create_default_context().verify_mode
- ssl.CERT_REQUIRED
+ <VerifyMode.CERT_REQUIRED: 2>
.. index:: single: certificates
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 7ac627ba19b0e..f74c7c771d60b 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -987,17 +987,6 @@ encodings
:func:`encodings.normalize_encoding` now ignores non-ASCII characters.
(Contributed by Hai Shi in :issue:`39337`.)
-enum
-----
-
-:class:`Enum` :func:`__repr__` now returns ``enum_name.member_name`` and
-:func:`__str__` now returns ``member_name``. Stdlib enums available as
-module constants have a :func:`repr` of ``module_name.member_name``.
-(Contributed by Ethan Furman in :issue:`40066`.)
-
-Add :class:`enum.StrEnum` for enums where all members are strings.
-(Contributed by Ethan Furman in :issue:`41816`.)
-
fileinput
---------
diff --git a/Lib/ast.py b/Lib/ast.py
index 66bcee8a252a0..f4d2f6e42c022 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -27,7 +27,7 @@
import sys
from _ast import *
from contextlib import contextmanager, nullcontext
-from enum import IntEnum, auto, _simple_enum
+from enum import IntEnum, auto
def parse(source, filename='<unknown>', mode='exec', *,
@@ -636,8 +636,7 @@ class Param(expr_context):
# We unparse those infinities to INFSTR.
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
-@_simple_enum(IntEnum)
-class _Precedence:
+class _Precedence(IntEnum):
"""Precedence table that originated from python grammar."""
TUPLE = auto()
diff --git a/Lib/enum.py b/Lib/enum.py
index 84e3cc17bbc53..db79e66903ff4 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -1,25 +1,14 @@
import sys
from types import MappingProxyType, DynamicClassAttribute
-from operator import or_ as _or_
-from functools import reduce
-from builtins import property as _bltin_property, bin as _bltin_bin
__all__ = [
- 'EnumType', 'EnumMeta',
- 'Enum', 'IntEnum', 'StrEnum', 'Flag', 'IntFlag',
- 'auto', 'unique', 'property', 'verify',
- 'FlagBoundary', 'STRICT', 'CONFORM', 'EJECT', 'KEEP',
- 'global_flag_repr', 'global_enum_repr', 'global_enum',
- 'EnumCheck', 'CONTINUOUS', 'NAMED_FLAGS', 'UNIQUE',
+ 'EnumMeta',
+ 'Enum', 'IntEnum', 'Flag', 'IntFlag',
+ 'auto', 'unique',
]
-# Dummy value for Enum and Flag as there are explicit checks for them
-# before they have been created.
-# This is also why there are checks in EnumType like `if Enum is not None`
-Enum = Flag = EJECT = None
-
def _is_descriptor(obj):
"""
Returns True if obj is a descriptor, False otherwise.
@@ -55,77 +44,24 @@ def _is_sunder(name):
def _is_private(cls_name, name):
# do not use `re` as `re` imports `enum`
pattern = '_%s__' % (cls_name, )
- pat_len = len(pattern)
if (
- len(name) > pat_len
+ len(name) >= 5
and name.startswith(pattern)
- and name[pat_len:pat_len+1] != ['_']
+ and name[len(pattern)] != '_'
and (name[-1] != '_' or name[-2] != '_')
):
return True
else:
return False
-def _is_single_bit(num):
+def _make_class_unpicklable(cls):
"""
- True if only one bit set in num (should be an int)
- """
- if num == 0:
- return False
- num &= num - 1
- return num == 0
-
-def _make_class_unpicklable(obj):
- """
- Make the given obj un-picklable.
-
- obj should be either a dictionary, or an Enum
+ Make the given class un-picklable.
"""
def _break_on_call_reduce(self, proto):
raise TypeError('%r cannot be pickled' % self)
- if isinstance(obj, dict):
- obj['__reduce_ex__'] = _break_on_call_reduce
- obj['__module__'] = '<unknown>'
- else:
- setattr(obj, '__reduce_ex__', _break_on_call_reduce)
- setattr(obj, '__module__', '<unknown>')
-
-def _iter_bits_lsb(num):
- # num must be an integer
- if isinstance(num, Enum):
- num = num.value
- while num:
- b = num & (~num + 1)
- yield b
- num ^= b
-
-def show_flag_values(value):
- return list(_iter_bits_lsb(value))
-
-def bin(num, max_bits=None):
- """
- Like built-in bin(), except negative values are represented in
- twos-compliment, and the leading bit always indicates sign
- (0=positive, 1=negative).
-
- >>> bin(10)
- '0b0 1010'
- >>> bin(~10) # ~10 is -11
- '0b1 0101'
- """
-
- ceiling = 2 ** (num).bit_length()
- if num >= 0:
- s = _bltin_bin(num + ceiling).replace('1', '0', 1)
- else:
- s = _bltin_bin(~num ^ (ceiling - 1) + ceiling)
- sign = s[:3]
- digits = s[3:]
- if max_bits is not None:
- if len(digits) < max_bits:
- digits = (sign[-1] * max_bits + digits)[-max_bits:]
- return "%s %s" % (sign, digits)
-
+ cls.__reduce_ex__ = _break_on_call_reduce
+ cls.__module__ = '<unknown>'
_auto_null = object()
class auto:
@@ -134,169 +70,12 @@ class auto:
"""
value = _auto_null
-class property(DynamicClassAttribute):
- """
- This is a descriptor, used to define attributes that act differently
- when accessed through an enum member and through an enum class.
- Instance access is the same as property(), but access to an attribute
- through the enum class will instead look in the class' _member_map_ for
- a corresponding enum member.
- """
-
- def __get__(self, instance, ownerclass=None):
- if instance is None:
- try:
- return ownerclass._member_map_[self.name]
- except KeyError:
- raise AttributeError(
- '%s: no class attribute %r' % (ownerclass.__name__, self.name)
- )
- else:
- if self.fget is None:
- # check for member
- if self.name in ownerclass._member_map_:
- import warnings
- warnings.warn(
- "accessing one member from another is not supported, "
- " and will be disabled in 3.12",
- DeprecationWarning,
- stacklevel=2,
- )
- return ownerclass._member_map_[self.name]
- raise AttributeError(
- '%s: no instance attribute %r' % (ownerclass.__name__, self.name)
- )
- else:
- return self.fget(instance)
-
- def __set__(self, instance, value):
- if self.fset is None:
- raise AttributeError(
- "%s: cannot set instance attribute %r" % (self.clsname, self.name)
- )
- else:
- return self.fset(instance, value)
-
- def __delete__(self, instance):
- if self.fdel is None:
- raise AttributeError(
- "%s: cannot delete instance attribute %r" % (self.clsname, self.name)
- )
- else:
- return self.fdel(instance)
-
- def __set_name__(self, ownerclass, name):
- self.name = name
- self.clsname = ownerclass.__name__
-
-
-class _proto_member:
- """
- intermediate step for enum members between class execution and final creation
- """
-
- def __init__(self, value):
- self.value = value
-
- def __set_name__(self, enum_class, member_name):
- """
- convert each quasi-member into an instance of the new enum class
- """
- # first step: remove ourself from enum_class
- delattr(enum_class, member_name)
- # second step: create member based on enum_class
- value = self.value
- if not isinstance(value, tuple):
- args = (value, )
- else:
- args = value
- if enum_class._member_type_ is tuple: # special case for tuple enums
- args = (args, ) # wrap it one more time
- if not enum_class._use_args_:
- enum_member = enum_class._new_member_(enum_class)
- if not hasattr(enum_member, '_value_'):
- enum_member._value_ = value
- else:
- enum_member = enum_class._new_member_(enum_class, *args)
- if not hasattr(enum_member, '_value_'):
- if enum_class._member_type_ is object:
- enum_member._value_ = value
- else:
- try:
- enum_member._value_ = enum_class._member_type_(*args)
- except Exception as exc:
- raise TypeError(
- '_value_ not set in __new__, unable to create it'
- ) from None
- value = enum_member._value_
- enum_member._name_ = member_name
- enum_member.__objclass__ = enum_class
- enum_member.__init__(*args)
- enum_member._sort_order_ = len(enum_class._member_names_)
- # If another member with the same value was already defined, the
- # new member becomes an alias to the existing one.
- for name, canonical_member in enum_class._member_map_.items():
- if canonical_member._value_ == enum_member._value_:
- enum_member = canonical_member
- break
- else:
- # this could still be an alias if the value is multi-bit and the
- # class is a flag class
- if (
- Flag is None
- or not issubclass(enum_class, Flag)
- ):
- # no other instances found, record this member in _member_names_
- enum_class._member_names_.append(member_name)
- elif (
- Flag is not None
- and issubclass(enum_class, Flag)
- and _is_single_bit(value)
- ):
- # no other instances found, record this member in _member_names_
- enum_class._member_names_.append(member_name)
- # get redirect in place before adding to _member_map_
- # but check for other instances in parent classes first
- need_override = False
- descriptor = None
- for base in enum_class.__mro__[1:]:
- descriptor = base.__dict__.get(member_name)
- if descriptor is not None:
- if isinstance(descriptor, (property, DynamicClassAttribute)):
- break
- else:
- need_override = True
- # keep looking for an enum.property
- if descriptor and not need_override:
- # previous enum.property found, no further action needed
- pass
- else:
- redirect = property()
- redirect.__set_name__(enum_class, member_name)
- if descriptor and need_override:
- # previous enum.property found, but some other inherited attribute
- # is in the way; copy fget, fset, fdel to this one
- redirect.fget = descriptor.fget
- redirect.fset = descriptor.fset
- redirect.fdel = descriptor.fdel
- setattr(enum_class, member_name, redirect)
- # now add to _member_map_ (even aliases)
- enum_class._member_map_[member_name] = enum_member
- try:
- # This may fail if value is not hashable. We can't add the value
- # to the map, and by-value lookups for this value will be
- # linear.
- enum_class._value2member_map_.setdefault(value, enum_member)
- except TypeError:
- # keep track of the value in a list so containment checks are quick
- enum_class._unhashable_values_.append(value)
-
class _EnumDict(dict):
"""
Track enum member order and ensure member names are not reused.
- EnumType will use the names found in self._member_names as the
+ EnumMeta will use the names found in self._member_names as the
enumeration member names.
"""
def __init__(self):
@@ -316,24 +95,24 @@ def __setitem__(self, key, value):
Single underscore (sunder) names are reserved.
"""
if _is_private(self._cls_name, key):
- # do nothing, name will be a normal attribute
- pass
- elif _is_sunder(key):
+ import warnings
+ warnings.warn(
+ "private variables, such as %r, will be normal attributes in 3.11"
+ % (key, ),
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ if _is_sunder(key):
if key not in (
- '_order_',
+ '_order_', '_create_pseudo_member_',
'_generate_next_value_', '_missing_', '_ignore_',
- '_iter_member_', '_iter_member_by_value_', '_iter_member_by_def_',
):
- raise ValueError(
- '_sunder_ names, such as %r, are reserved for future Enum use'
- % (key, )
- )
+ raise ValueError('_names_ are reserved for future Enum use')
if key == '_generate_next_value_':
# check if members already defined as auto()
if self._auto_called:
raise TypeError("_generate_next_value_ must be defined before members")
- _gnv = value.__func__ if isinstance(value, staticmethod) else value
- setattr(self, '_generate_next_value', _gnv)
+ setattr(self, '_generate_next_value', value)
elif key == '_ignore_':
if isinstance(value, str):
value = value.replace(',',' ').split()
@@ -351,7 +130,7 @@ def __setitem__(self, key, value):
key = '_order_'
elif key in self._member_names:
# descriptor overwriting an enum?
- raise TypeError('%r already defined as: %r' % (key, self[key]))
+ raise TypeError('Attempted to reuse key: %r' % key)
elif key in self._ignore:
pass
elif not _is_descriptor(value):
@@ -361,7 +140,10 @@ def __setitem__(self, key, value):
if isinstance(value, auto):
if value.value == _auto_null:
value.value = self._generate_next_value(
- key, 1, len(self._member_names), self._last_values[:],
+ key,
+ 1,
+ len(self._member_names),
+ self._last_values[:],
)
self._auto_called = True
value = value.value
@@ -369,22 +151,16 @@ def __setitem__(self, key, value):
self._last_values.append(value)
super().__setitem__(key, value)
- def update(self, members, **more_members):
- try:
- for name in members.keys():
- self[name] = members[name]
- except AttributeError:
- for name, value in members:
- self[name] = value
- for name, value in more_members.items():
- self[name] = value
+# Dummy value for Enum as EnumMeta explicitly checks for it, but of course
+# until EnumMeta finishes running the first time the Enum class doesn't exist.
+# This is also why there are checks in EnumMeta like `if Enum is not None`
+Enum = None
-class EnumType(type):
+class EnumMeta(type):
"""
Metaclass for Enum
"""
-
@classmethod
def __prepare__(metacls, cls, bases, **kwds):
# check that previous enum members do not exist
@@ -400,80 +176,148 @@ def __prepare__(metacls, cls, bases, **kwds):
)
return enum_dict
- def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **kwds):
+ def __new__(metacls, cls, bases, classdict, **kwds):
# an Enum class is final once enumeration items have been defined; it
# cannot be mixed with other types (int, float, etc.) if it has an
# inherited __new__ unless a new __new__ is defined (or the resulting
# class will fail).
#
# remove any keys listed in _ignore_
- if _simple:
- return super().__new__(metacls, cls, bases, classdict, **kwds)
classdict.setdefault('_ignore_', []).append('_ignore_')
ignore = classdict['_ignore_']
for key in ignore:
classdict.pop(key, None)
- #
- # grab member names
- member_names = classdict._member_names
- #
- # check for illegal enum names (any others?)
- invalid_names = set(member_names) & {'mro', ''}
- if invalid_names:
- raise ValueError('Invalid enum member name: {0}'.format(
- ','.join(invalid_names)))
- #
- # adjust the sunders
- _order_ = classdict.pop('_order_', None)
- # convert to normal dict
- classdict = dict(classdict.items())
- #
- # data type of member and the controlling Enum class
member_type, first_enum = metacls._get_mixins_(cls, bases)
__new__, save_new, use_args = metacls._find_new_(
classdict, member_type, first_enum,
)
- classdict['_new_member_'] = __new__
- classdict['_use_args_'] = use_args
- #
- # convert future enum members into temporary _proto_members
- # and record integer values in case this will be a Flag
- flag_mask = 0
- for name in member_names:
- value = classdict[name]
- if isinstance(value, int):
- flag_mask |= value
- classdict[name] = _proto_member(value)
- #
- # house-keeping structures
- classdict['_member_names_'] = []
- classdict['_member_map_'] = {}
- classdict['_value2member_map_'] = {}
- classdict['_unhashable_values_'] = []
- classdict['_member_type_'] = member_type
- #
- # Flag structures (will be removed if final class is not a Flag
- classdict['_boundary_'] = (
- boundary
- or getattr(first_enum, '_boundary_', None)
- )
- classdict['_flag_mask_'] = flag_mask
- classdict['_all_bits_'] = 2 ** ((flag_mask).bit_length()) - 1
- classdict['_inverted_'] = None
- #
+
+ # save enum items into separate mapping so they don't get baked into
+ # the new class
+ enum_members = {k: classdict[k] for k in classdict._member_names}
+ for name in classdict._member_names:
+ del classdict[name]
+
+ # adjust the sunders
+ _order_ = classdict.pop('_order_', None)
+
+ # check for illegal enum names (any others?)
+ invalid_names = set(enum_members) & {'mro', ''}
+ if invalid_names:
+ raise ValueError('Invalid enum member name: {0}'.format(
+ ','.join(invalid_names)))
+
# create a default docstring if one has not been provided
if '__doc__' not in classdict:
classdict['__doc__'] = 'An enumeration.'
- try:
- exc = None
- enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
- except RuntimeError as e:
- # any exceptions raised by member.__new__ will get converted to a
- # RuntimeError, so get that original exception back and raise it instead
- exc = e.__cause__ or e
- if exc is not None:
- raise exc
+
+ enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
+ enum_class._member_names_ = [] # names in definition order
+ enum_class._member_map_ = {} # name->value map
+ enum_class._member_type_ = member_type
+
+ # save DynamicClassAttribute attributes from super classes so we know
+ # if we can take the shortcut of storing members in the class dict
+ dynamic_attributes = {
+ k for c in enum_class.mro()
+ for k, v in c.__dict__.items()
+ if isinstance(v, DynamicClassAttribute)
+ }
+
+ # Reverse value->name map for hashable values.
+ enum_class._value2member_map_ = {}
+
+ # If a custom type is mixed into the Enum, and it does not know how
+ # to pickle itself, pickle.dumps will succeed but pickle.loads will
+ # fail. Rather than have the error show up later and possibly far
+ # from the source, sabotage the pickle protocol for this class so
+ # that pickle.dumps also fails.
#
+ # However, if the new class implements its own __reduce_ex__, do not
+ # sabotage -- it's on them to make sure it works correctly. We use
+ # __reduce_ex__ instead of any of the others as it is preferred by
+ # pickle over __reduce__, and it handles all pickle protocols.
+ if '__reduce_ex__' not in classdict:
+ if member_type is not object:
+ methods = ('__getnewargs_ex__', '__getnewargs__',
+ '__reduce_ex__', '__reduce__')
+ if not any(m in member_type.__dict__ for m in methods):
+ if '__new__' in classdict:
+ # too late, sabotage
+ _make_class_unpicklable(enum_class)
+ else:
+ # final attempt to verify that pickling would work:
+ # travel mro until __new__ is found, checking for
+ # __reduce__ and friends along the way -- if any of them
+ # are found before/when __new__ is found, pickling should
+ # work
+ sabotage = None
+ for chain in bases:
+ for base in chain.__mro__:
+ if base is object:
+ continue
+ elif any(m in base.__dict__ for m in methods):
+ # found one, we're good
+ sabotage = False
+ break
+ elif '__new__' in base.__dict__:
+ # not good
+ sabotage = True
+ break
+ if sabotage is not None:
+ break
+ if sabotage:
+ _make_class_unpicklable(enum_class)
+ # instantiate them, checking for duplicates as we go
+ # we instantiate first instead of checking for duplicates first in case
+ # a custom __new__ is doing something funky with the values -- such as
+ # auto-numbering ;)
+ for member_name in classdict._member_names:
+ value = enum_members[member_name]
+ if not isinstance(value, tuple):
+ args = (value, )
+ else:
+ args = value
+ if member_type is tuple: # special case for tuple enums
+ args = (args, ) # wrap it one more time
+ if not use_args:
+ enum_member = __new__(enum_class)
+ if not hasattr(enum_member, '_value_'):
+ enum_member._value_ = value
+ else:
+ enum_member = __new__(enum_class, *args)
+ if not hasattr(enum_member, '_value_'):
+ if member_type is object:
+ enum_member._value_ = value
+ else:
+ enum_member._value_ = member_type(*args)
+ value = enum_member._value_
+ enum_member._name_ = member_name
+ enum_member.__objclass__ = enum_class
+ enum_member.__init__(*args)
+ # If another member with the same value was already defined, the
+ # new member becomes an alias to the existing one.
+ for name, canonical_member in enum_class._member_map_.items():
+ if canonical_member._value_ == enum_member._value_:
+ enum_member = canonical_member
+ break
+ else:
+ # Aliases don't appear in member names (only in __members__).
+ enum_class._member_names_.append(member_name)
+ # performance boost for any member that would not shadow
+ # a DynamicClassAttribute
+ if member_name not in dynamic_attributes:
+ setattr(enum_class, member_name, enum_member)
+ # now add to _member_map_
+ enum_class._member_map_[member_name] = enum_member
+ try:
+ # This may fail if value is not hashable. We can't add the value
+ # to the map, and by-value lookups for this value will be
+ # linear.
+ enum_class._value2member_map_[value] = enum_member
+ except TypeError:
+ pass
+
# double check that repr and friends are not the mixin's or various
# things break (such as pickle)
# however, if the method is defined in the Enum itself, don't replace
@@ -486,7 +330,7 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
enum_method = getattr(first_enum, name, None)
if obj_method is not None and obj_method is class_method:
setattr(enum_class, name, enum_method)
- #
+
# replace any other __new__ with our own (as long as Enum is not None,
# anyway) -- again, this is to support pickle
if Enum is not None:
@@ -495,71 +339,14 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
if save_new:
enum_class.__new_member__ = __new__
enum_class.__new__ = Enum.__new__
- #
+
# py3 support for definition order (helps keep py2/py3 code in sync)
- #
- # _order_ checking is spread out into three/four steps
- # - if enum_class is a Flag:
- # - remove any non-single-bit flags from _order_
- # - remove any aliases from _order_
- # - check that _order_ and _member_names_ match
- #
- # step 1: ensure we have a list
if _order_ is not None:
if isinstance(_order_, str):
_order_ = _order_.replace(',', ' ').split()
- #
- # remove Flag structures if final class is not a Flag
- if (
- Flag is None and cls != 'Flag'
- or Flag is not None and not issubclass(enum_class, Flag)
- ):
- delattr(enum_class, '_boundary_')
- delattr(enum_class, '_flag_mask_')
- delattr(enum_class, '_all_bits_')
- delattr(enum_class, '_inverted_')
- elif Flag is not None and issubclass(enum_class, Flag):
- # ensure _all_bits_ is correct and there are no missing flags
- single_bit_total = 0
- multi_bit_total = 0
- for flag in enum_class._member_map_.values():
- flag_value = flag._value_
- if _is_single_bit(flag_value):
- single_bit_total |= flag_value
- else:
- # multi-bit flags are considered aliases
- multi_bit_total |= flag_value
- enum_class._flag_mask_ = single_bit_total
- #
- # set correct __iter__
- member_list = [m._value_ for m in enum_class]
- if member_list != sorted(member_list):
- enum_class._iter_member_ = enum_class._iter_member_by_def_
- if _order_:
- # _order_ step 2: remove any items from _order_ that are not single-bit
- _order_ = [
- o
- for o in _order_
- if o not in enum_class._member_map_ or _is_single_bit(enum_class[o]._value_)
- ]
- #
- if _order_:
- # _order_ step 3: remove aliases from _order_
- _order_ = [
- o
- for o in _order_
- if (
- o not in enum_class._member_map_
- or
- (o in enum_class._member_map_ and o in enum_class._member_names_)
- )]
- # _order_ step 4: verify that _order_ and _member_names_ match
if _order_ != enum_class._member_names_:
- raise TypeError(
- 'member order does not match _order_:\n%r\n%r'
- % (enum_class._member_names_, _order_)
- )
- #
+ raise TypeError('member order does not match _order_')
+
return enum_class
def __bool__(self):
@@ -568,7 +355,7 @@ def __bool__(self):
"""
return True
- def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None):
+ def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1):
"""
Either returns an existing member, or creates a new enum class.
@@ -603,18 +390,10 @@ def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, s
qualname=qualname,
type=type,
start=start,
- boundary=boundary,
)
def __contains__(cls, member):
if not isinstance(member, Enum):
- import warnings
- warnings.warn(
- "in 3.12 __contains__ will no longer raise TypeError, but will return True or\n"
- "False depending on whether the value is a member or the value of a member",
- DeprecationWarning,
- stacklevel=2,
- )
raise TypeError(
"unsupported operand type(s) for 'in': '%s' and '%s'" % (
type(member).__qualname__, cls.__class__.__qualname__))
@@ -624,7 +403,7 @@ def __delattr__(cls, attr):
# nicer error message when someone tries to delete an attribute
# (see issue19025).
if attr in cls._member_map_:
- raise AttributeError("%s: cannot delete Enum member %r." % (cls.__name__, attr))
+ raise AttributeError("%s: cannot delete Enum member." % cls.__name__)
super().__delattr__(attr)
def __dir__(self):
@@ -661,7 +440,7 @@ def __iter__(cls):
def __len__(cls):
return len(cls._member_names_)
- @_bltin_property
+ @property
def __members__(cls):
"""
Returns a mapping of member name->value.
@@ -672,10 +451,7 @@ def __members__(cls):
return MappingProxyType(cls._member_map_)
def __repr__(cls):
- if Flag is not None and issubclass(cls, Flag):
- return "<flag %r>" % cls.__name__
- else:
- return "<enum %r>" % cls.__name__
+ return "<enum %r>" % cls.__name__
def __reversed__(cls):
"""
@@ -693,10 +469,10 @@ def __setattr__(cls, name, value):
"""
member_map = cls.__dict__.get('_member_map_', {})
if name in member_map:
- raise AttributeError('Cannot reassign member %r.' % (name, ))
+ raise AttributeError('Cannot reassign members.')
super().__setattr__(name, value)
- def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, start=1, boundary=None):
+ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, start=1):
"""
Convenience method to create a new Enum class.
@@ -731,6 +507,7 @@ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, s
else:
member_name, member_value = item
classdict[member_name] = member_value
+ enum_class = metacls.__new__(metacls, class_name, bases, classdict)
# TODO: replace the frame hack if a blessed way to know the calling
# module is ever developed
@@ -740,16 +517,15 @@ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, s
except (AttributeError, ValueError, KeyError):
pass
if module is None:
- _make_class_unpicklable(classdict)
+ _make_class_unpicklable(enum_class)
else:
- classdict['__module__'] = module
+ enum_class.__module__ = module
if qualname is not None:
- classdict['__qualname__'] = qualname
+ enum_class.__qualname__ = qualname
- return metacls.__new__(metacls, class_name, bases, classdict, boundary=boundary)
-
- def _convert_(cls, name, module, filter, source=None, *, boundary=None):
+ return enum_class
+ def _convert_(cls, name, module, filter, source=None):
"""
Create a new Enum subclass that replaces a collection of global constants
"""
@@ -758,9 +534,9 @@ def _convert_(cls, name, module, filter, source=None, *, boundary=None):
# module;
# also, replace the __reduce_ex__ method so unpickling works in
# previous Python versions
- module_globals = sys.modules[module].__dict__
+ module_globals = vars(sys.modules[module])
if source:
- source = source.__dict__
+ source = vars(source)
else:
source = module_globals
# _value2member_map_ is populated in the same order every time
@@ -776,12 +552,9 @@ def _convert_(cls, name, module, filter, source=None, *, boundary=None):
except TypeError:
# unless some values aren't comparable, in which case sort by name
members.sort(key=lambda t: t[0])
- body = {t[0]: t[1] for t in members}
- body['__module__'] = module
- tmp_cls = type(name, (object, ), body)
- cls = _simple_enum(etype=cls, boundary=boundary or KEEP)(tmp_cls)
- cls.__reduce_ex__ = _reduce_ex_by_global_name
- global_enum(cls)
+ cls = cls(name, members, module=module)
+ cls.__reduce_ex__ = _reduce_ex_by_name
+ module_globals.update(cls.__members__)
module_globals[name] = cls
return cls
@@ -807,7 +580,7 @@ def _get_mixins_(class_name, bases):
return object, Enum
def _find_data_type(bases):
- data_types = set()
+ data_types = []
for chain in bases:
candidate = None
for base in chain.__mro__:
@@ -815,19 +588,19 @@ def _find_data_type(bases):
continue
elif issubclass(base, Enum):
if base._member_type_ is not object:
- data_types.add(base._member_type_)
+ data_types.append(base._member_type_)
break
elif '__new__' in base.__dict__:
if issubclass(base, Enum):
continue
- data_types.add(candidate or base)
+ data_types.append(candidate or base)
break
else:
candidate = candidate or base
if len(data_types) > 1:
raise TypeError('%r: too many data types: %r' % (class_name, data_types))
elif data_types:
- return data_types.pop()
+ return data_types[0]
else:
return None
@@ -857,7 +630,7 @@ def _find_new_(classdict, member_type, first_enum):
__new__ = classdict.get('__new__', None)
# should __new__ be saved as __new_member__ later?
- save_new = first_enum is not None and __new__ is not None
+ save_new = __new__ is not None
if __new__ is None:
# check all possibles for __new_member__ before falling back to
@@ -881,21 +654,19 @@ def _find_new_(classdict, member_type, first_enum):
# if a non-object.__new__ is used then whatever value/tuple was
# assigned to the enum member name will be passed to __new__ and to the
# new enum member's __init__
- if first_enum is None or __new__ in (Enum.__new__, object.__new__):
+ if __new__ is object.__new__:
use_args = False
else:
use_args = True
return __new__, save_new, use_args
-EnumMeta = EnumType
-class Enum(metaclass=EnumType):
+class Enum(metaclass=EnumMeta):
"""
Generic enumeration.
Derive from this class to define new enumerations.
"""
-
def __new__(cls, value):
# all enum instances are actually created during class construction
# without calling this method; this method is called by the metaclass'
@@ -922,30 +693,19 @@ def __new__(cls, value):
except Exception as e:
exc = e
result = None
- try:
- if isinstance(result, cls):
- return result
- elif (
- Flag is not None and issubclass(cls, Flag)
- and cls._boundary_ is EJECT and isinstance(result, int)
- ):
- return result
- else:
- ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
- if result is None and exc is None:
- raise ve_exc
- elif exc is None:
- exc = TypeError(
- 'error in %s._missing_: returned %r instead of None or a valid member'
- % (cls.__name__, result)
- )
- if not isinstance(exc, ValueError):
- exc.__context__ = ve_exc
- raise exc
- finally:
- # ensure all variables that could hold an exception are destroyed
- exc = None
- ve_exc = None
+ if isinstance(result, cls):
+ return result
+ else:
+ ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
+ if result is None and exc is None:
+ raise ve_exc
+ elif exc is None:
+ exc = TypeError(
+ 'error in %s._missing_: returned %r instead of None or a valid member'
+ % (cls.__name__, result)
+ )
+ exc.__context__ = ve_exc
+ raise exc
def _generate_next_value_(name, start, count, last_values):
"""
@@ -969,10 +729,11 @@ def _missing_(cls, value):
return None
def __repr__(self):
- return "%s.%s" % ( self.__class__.__name__, self._name_)
+ return "<%s.%s: %r>" % (
+ self.__class__.__name__, self._name_, self._value_)
def __str__(self):
- return "%s" % (self._name_, )
+ return "%s.%s" % (self.__class__.__name__, self._name_)
def __dir__(self):
"""
@@ -993,23 +754,14 @@ def __format__(self, format_spec):
# mixed-in Enums should use the mixed-in type's __format__, otherwise
# we can get strange results with the Enum name showing up instead of
# the value
- #
+
# pure Enum branch, or branch with __str__ explicitly overridden
- str_overridden = type(self).__str__ not in (Enum.__str__, IntEnum.__str__, Flag.__str__)
+ str_overridden = type(self).__str__ not in (Enum.__str__, Flag.__str__)
if self._member_type_ is object or str_overridden:
cls = str
val = str(self)
# mix-in branch
else:
- if not format_spec or format_spec in ('{}','{:}'):
- import warnings
- warnings.warn(
- "in 3.12 format() will use the enum member, not the enum member's value;\n"
- "use a format specifier, such as :d for an integer-based Enum, to maintain "
- "the current display",
- DeprecationWarning,
- stacklevel=2,
- )
cls = self._member_type_
val = self._value_
return cls.__format__(val, format_spec)
@@ -1018,104 +770,34 @@ def __hash__(self):
return hash(self._name_)
def __reduce_ex__(self, proto):
- return getattr, (self.__class__, self._name_)
+ return self.__class__, (self._value_, )
- # enum.property is used to provide access to the `name` and
- # `value` attributes of enum members while keeping some measure of
+ # DynamicClassAttribute is used to provide access to the `name` and
+ # `value` properties of enum members while keeping some measure of
# protection from modification, while still allowing for an enumeration
# to have members named `name` and `value`. This works because enumeration
- # members are not set directly on the enum class; they are kept in a
- # separate structure, _member_map_, which is where enum.property looks for
- # them
+ # members are not set directly on the enum class -- __getattr__ is
+ # used to look them up.
- @property
+ @DynamicClassAttribute
def name(self):
"""The name of the Enum member."""
return self._name_
- @property
+ @DynamicClassAttribute
def value(self):
"""The value of the Enum member."""
return self._value_
class IntEnum(int, Enum):
- """
- Enum where members are also (and must be) ints
- """
-
- def __str__(self):
- return "%s" % (self._name_, )
-
- def __format__(self, format_spec):
- """
- Returns format using actual value unless __str__ has been overridden.
- """
- str_overridden = type(self).__str__ != IntEnum.__str__
- if str_overridden:
- cls = str
- val = str(self)
- else:
- cls = self._member_type_
- val = self._value_
- return cls.__format__(val, format_spec)
+ """Enum where members are also (and must be) ints"""
-class StrEnum(str, Enum):
- """
- Enum where members are also (and must be) strings
- """
-
- def __new__(cls, *values):
- if len(values) > 3:
- raise TypeError('too many arguments for str(): %r' % (values, ))
- if len(values) == 1:
- # it must be a string
- if not isinstance(values[0], str):
- raise TypeError('%r is not a string' % (values[0], ))
- if len(values) >= 2:
- # check that encoding argument is a string
- if not isinstance(values[1], str):
- raise TypeError('encoding must be a string, not %r' % (values[1], ))
- if len(values) == 3:
- # check that errors argument is a string
- if not isinstance(values[2], str):
- raise TypeError('errors must be a string, not %r' % (values[2]))
- value = str(*values)
- member = str.__new__(cls, value)
- member._value_ = value
- return member
-
- __str__ = str.__str__
-
- __format__ = str.__format__
-
- def _generate_next_value_(name, start, count, last_values):
- """
- Return the lower-cased version of the member name.
- """
- return name.lower()
-
-
-def _reduce_ex_by_global_name(self, proto):
+def _reduce_ex_by_name(self, proto):
return self.name
-class FlagBoundary(StrEnum):
- """
- control how out of range values are handled
- "strict" -> error is raised [default for Flag]
- "conform" -> extra bits are discarded
- "eject" -> lose flag status [default for IntFlag]
- "keep" -> keep flag status and all bits
- """
- STRICT = auto()
- CONFORM = auto()
- EJECT = auto()
- KEEP = auto()
-STRICT, CONFORM, EJECT, KEEP = FlagBoundary
-
-
-class Flag(Enum, boundary=STRICT):
+class Flag(Enum):
"""
Support for flags
"""
@@ -1131,110 +813,45 @@ def _generate_next_value_(name, start, count, last_values):
"""
if not count:
return start if start is not None else 1
- last_value = max(last_values)
- try:
- high_bit = _high_bit(last_value)
- except Exception:
- raise TypeError('Invalid Flag value: %r' % last_value) from None
+ for last_value in reversed(last_values):
+ try:
+ high_bit = _high_bit(last_value)
+ break
+ except Exception:
+ raise TypeError('Invalid Flag value: %r' % last_value) from None
return 2 ** (high_bit+1)
@classmethod
- def _iter_member_by_value_(cls, value):
- """
- Extract all members from the value in definition (i.e. increasing value) order.
- """
- for val in _iter_bits_lsb(value & cls._flag_mask_):
- yield cls._value2member_map_.get(val)
-
- _iter_member_ = _iter_member_by_value_
-
- @classmethod
- def _iter_member_by_def_(cls, value):
+ def _missing_(cls, value):
"""
- Extract all members from the value in definition order.
+ Returns member (possibly creating it) if one can be found for value.
"""
- yield from sorted(
- cls._iter_member_by_value_(value),
- key=lambda m: m._sort_order_,
- )
+ original_value = value
+ if value < 0:
+ value = ~value
+ possible_member = cls._create_pseudo_member_(value)
+ if original_value < 0:
+ possible_member = ~possible_member
+ return possible_member
@classmethod
- def _missing_(cls, value):
+ def _create_pseudo_member_(cls, value):
"""
- Create a composite member containing all canonical members present in `value`.
-
- If non-member values are present, result depends on `_boundary_` setting.
+ Create a composite member iff value contains only members.
"""
- if not isinstance(value, int):
- raise ValueError(
- "%r is not a valid %s" % (value, cls.__qualname__)
- )
- # check boundaries
- # - value must be in range (e.g. -16 <-> +15, i.e. ~15 <-> 15)
- # - value must not include any skipped flags (e.g. if bit 2 is not
- # defined, then 0d10 is invalid)
- flag_mask = cls._flag_mask_
- all_bits = cls._all_bits_
- neg_value = None
- if (
- not ~all_bits <= value <= all_bits
- or value & (all_bits ^ flag_mask)
- ):
- if cls._boundary_ is STRICT:
- max_bits = max(value.bit_length(), flag_mask.bit_length())
- raise ValueError(
- "%s: invalid value: %r\n given %s\n allowed %s" % (
- cls.__name__, value, bin(value, max_bits), bin(flag_mask, max_bits),
- ))
- elif cls._boundary_ is CONFORM:
- value = value & flag_mask
- elif cls._boundary_ is EJECT:
- return value
- elif cls._boundary_ is KEEP:
- if value < 0:
- value = (
- max(all_bits+1, 2**(value.bit_length()))
- + value
- )
- else:
- raise ValueError(
- 'unknown flag boundary: %r' % (cls._boundary_, )
- )
- if value < 0:
- neg_value = value
- value = all_bits + 1 + value
- # get members and unknown
- unknown = value & ~flag_mask
- member_value = value & flag_mask
- if unknown and cls._boundary_ is not KEEP:
- raise ValueError(
- '%s(%r) --> unknown values %r [%s]'
- % (cls.__name__, value, unknown, bin(unknown))
- )
- # normal Flag?
- __new__ = getattr(cls, '__new_member__', None)
- if cls._member_type_ is object and not __new__:
+ pseudo_member = cls._value2member_map_.get(value, None)
+ if pseudo_member is None:
+ # verify all bits are accounted for
+ _, extra_flags = _decompose(cls, value)
+ if extra_flags:
+ raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
# construct a singleton enum pseudo-member
pseudo_member = object.__new__(cls)
- else:
- pseudo_member = (__new__ or cls._member_type_.__new__)(cls, value)
- if not hasattr(pseudo_member, '_value_'):
- pseudo_member._value_ = value
- if member_value:
- pseudo_member._name_ = '|'.join([
- m._name_ for m in cls._iter_member_(member_value)
- ])
- if unknown:
- pseudo_member._name_ += '|0x%x' % unknown
- else:
pseudo_member._name_ = None
- # use setdefault in case another thread already created a composite
- # with this value, but only if all members are known
- # note: zero is a special case -- add it
- if not unknown:
+ pseudo_member._value_ = value
+ # use setdefault in case another thread already created a composite
+ # with this value
pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member)
- if neg_value is not None:
- cls._value2member_map_[neg_value] = pseudo_member
return pseudo_member
def __contains__(self, other):
@@ -1245,42 +862,31 @@ def __contains__(self, other):
raise TypeError(
"unsupported operand type(s) for 'in': '%s' and '%s'" % (
type(other).__qualname__, self.__class__.__qualname__))
- if other._value_ == 0 or self._value_ == 0:
- return False
return other._value_ & self._value_ == other._value_
- def __iter__(self):
- """
- Returns flags in definition order.
- """
- yield from self._iter_member_(self._value_)
-
- def __len__(self):
- return self._value_.bit_count()
-
def __repr__(self):
- cls_name = self.__class__.__name__
- if self._name_ is None:
- return "0x%x" % (self._value_, )
- if _is_single_bit(self._value_):
- return '%s.%s' % (cls_name, self._name_)
- if self._boundary_ is not FlagBoundary.KEEP:
- return '%s.' % cls_name + ('|%s.' % cls_name).join(self.name.split('|'))
- else:
- name = []
- for n in self._name_.split('|'):
- if n.startswith('0'):
- name.append(n)
- else:
- name.append('%s.%s' % (cls_name, n))
- return '|'.join(name)
+ cls = self.__class__
+ if self._name_ is not None:
+ return '<%s.%s: %r>' % (cls.__name__, self._name_, self._value_)
+ members, uncovered = _decompose(cls, self._value_)
+ return '<%s.%s: %r>' % (
+ cls.__name__,
+ '|'.join([str(m._name_ or m._value_) for m in members]),
+ self._value_,
+ )
def __str__(self):
cls = self.__class__
- if self._name_ is None:
- return '%s(%x)' % (cls.__name__, self._value_)
+ if self._name_ is not None:
+ return '%s.%s' % (cls.__name__, self._name_)
+ members, uncovered = _decompose(cls, self._value_)
+ if len(members) == 1 and members[0]._name_ is None:
+ return '%s.%r' % (cls.__name__, members[0]._value_)
else:
- return self._name_
+ return '%s.%s' % (
+ cls.__name__,
+ '|'.join([str(m._name_ or m._value_) for m in members]),
+ )
def __bool__(self):
return bool(self._value_)
@@ -1301,67 +907,86 @@ def __xor__(self, other):
return self.__class__(self._value_ ^ other._value_)
def __invert__(self):
- if self._inverted_ is None:
- if self._boundary_ is KEEP:
- # use all bits
- self._inverted_ = self.__class__(~self._value_)
- else:
- # calculate flags not in this member
- self._inverted_ = self.__class__(self._flag_mask_ ^ self._value_)
- if isinstance(self._inverted_, self.__class__):
- self._inverted_._inverted_ = self
- return self._inverted_
+ members, uncovered = _decompose(self.__class__, self._value_)
+ inverted = self.__class__(0)
+ for m in self.__class__:
+ if m not in members and not (m._value_ & self._value_):
+ inverted = inverted | m
+ return self.__class__(inverted)
-class IntFlag(int, Flag, boundary=EJECT):
+class IntFlag(int, Flag):
"""
Support for integer-based Flags
"""
- def __format__(self, format_spec):
+ @classmethod
+ def _missing_(cls, value):
+ """
+ Returns member (possibly creating it) if one can be found for value.
+ """
+ if not isinstance(value, int):
+ raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
+ new_member = cls._create_pseudo_member_(value)
+ return new_member
+
+ @classmethod
+ def _create_pseudo_member_(cls, value):
"""
- Returns format using actual value unless __str__ has been overridden.
+ Create a composite member iff value contains only members.
"""
- str_overridden = type(self).__str__ != Flag.__str__
- value = self
- if not str_overridden:
- value = self._value_
- return int.__format__(value, format_spec)
+ pseudo_member = cls._value2member_map_.get(value, None)
+ if pseudo_member is None:
+ need_to_create = [value]
+ # get unaccounted for bits
+ _, extra_flags = _decompose(cls, value)
+ # timer = 10
+ while extra_flags:
+ # timer -= 1
+ bit = _high_bit(extra_flags)
+ flag_value = 2 ** bit
+ if (flag_value not in cls._value2member_map_ and
+ flag_value not in need_to_create
+ ):
+ need_to_create.append(flag_value)
+ if extra_flags == -flag_value:
+ extra_flags = 0
+ else:
+ extra_flags ^= flag_value
+ for value in reversed(need_to_create):
+ # construct singleton pseudo-members
+ pseudo_member = int.__new__(cls, value)
+ pseudo_member._name_ = None
+ pseudo_member._value_ = value
+ # use setdefault in case another thread already created a composite
+ # with this value
+ pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member)
+ return pseudo_member
def __or__(self, other):
- if isinstance(other, self.__class__):
- other = other._value_
- elif isinstance(other, int):
- other = other
- else:
+ if not isinstance(other, (self.__class__, int)):
return NotImplemented
- value = self._value_
- return self.__class__(value | other)
+ result = self.__class__(self._value_ | self.__class__(other)._value_)
+ return result
def __and__(self, other):
- if isinstance(other, self.__class__):
- other = other._value_
- elif isinstance(other, int):
- other = other
- else:
+ if not isinstance(other, (self.__class__, int)):
return NotImplemented
- value = self._value_
- return self.__class__(value & other)
+ return self.__class__(self._value_ & self.__class__(other)._value_)
def __xor__(self, other):
- if isinstance(other, self.__class__):
- other = other._value_
- elif isinstance(other, int):
- other = other
- else:
+ if not isinstance(other, (self.__class__, int)):
return NotImplemented
- value = self._value_
- return self.__class__(value ^ other)
+ return self.__class__(self._value_ ^ self.__class__(other)._value_)
__ror__ = __or__
__rand__ = __and__
__rxor__ = __xor__
- __invert__ = Flag.__invert__
+
+ def __invert__(self):
+ result = self.__class__(~self._value_)
+ return result
+
def _high_bit(value):
"""
@@ -1384,441 +1009,31 @@ def unique(enumeration):
(enumeration, alias_details))
return enumeration
-def _power_of_two(value):
- if value < 1:
- return False
- return value == 2 ** _high_bit(value)
-
-def global_enum_repr(self):
- return '%s.%s' % (self.__class__.__module__, self._name_)
-
-def global_flag_repr(self):
- module = self.__class__.__module__
- cls_name = self.__class__.__name__
- if self._name_ is None:
- return "%x" % (module, cls_name, self._value_)
- if _is_single_bit(self):
- return '%s.%s' % (module, self._name_)
- if self._boundary_ is not FlagBoundary.KEEP:
- return module + module.join(self.name.split('|'))
- else:
- name = []
- for n in self._name_.split('|'):
- if n.startswith('0'):
- name.append(n)
- else:
- name.append('%s.%s' % (module, n))
- return '|'.join(name)
-
-
-def global_enum(cls):
+def _decompose(flag, value):
"""
- decorator that makes the repr() of an enum member reference its module
- instead of its class; also exports all members to the enum's module's
- global namespace
+ Extract all members from the value.
"""
- if issubclass(cls, Flag):
- cls.__repr__ = global_flag_repr
- else:
- cls.__repr__ = global_enum_repr
- sys.modules[cls.__module__].__dict__.update(cls.__members__)
- return cls
-
-def _simple_enum(etype=Enum, *, boundary=None, use_args=None):
- """
- Class decorator that converts a normal class into an :class:`Enum`. No
- safety checks are done, and some advanced behavior (such as
- :func:`__init_subclass__`) is not available. Enum creation can be faster
- using :func:`simple_enum`.
-
- >>> from enum import Enum, _simple_enum
- >>> @_simple_enum(Enum)
- ... class Color:
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> Color
- <enum 'Color'>
- """
- def convert_class(cls):
- nonlocal use_args
- cls_name = cls.__name__
- if use_args is None:
- use_args = etype._use_args_
- __new__ = cls.__dict__.get('__new__')
- if __new__ is not None:
- new_member = __new__.__func__
- else:
- new_member = etype._member_type_.__new__
- attrs = {}
- body = {}
- if __new__ is not None:
- body['__new_member__'] = new_member
- body['_new_member_'] = new_member
- body['_use_args_'] = use_args
- body['_generate_next_value_'] = gnv = etype._generate_next_value_
- body['_member_names_'] = member_names = []
- body['_member_map_'] = member_map = {}
- body['_value2member_map_'] = value2member_map = {}
- body['_unhashable_values_'] = []
- body['_member_type_'] = member_type = etype._member_type_
- if issubclass(etype, Flag):
- body['_boundary_'] = boundary or etype._boundary_
- body['_flag_mask_'] = None
- body['_all_bits_'] = None
- body['_inverted_'] = None
- for name, obj in cls.__dict__.items():
- if name in ('__dict__', '__weakref__'):
- continue
- if _is_dunder(name) or _is_private(cls_name, name) or _is_sunder(name) or _is_descriptor(obj):
- body[name] = obj
- else:
- attrs[name] = obj
- if cls.__dict__.get('__doc__') is None:
- body['__doc__'] = 'An enumeration.'
- #
- # double check that repr and friends are not the mixin's or various
- # things break (such as pickle)
- # however, if the method is defined in the Enum itself, don't replace
- # it
- enum_class = type(cls_name, (etype, ), body, boundary=boundary, _simple=True)
- for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
- if name in body:
- continue
- class_method = getattr(enum_class, name)
- obj_method = getattr(member_type, name, None)
- enum_method = getattr(etype, name, None)
- if obj_method is not None and obj_method is class_method:
- setattr(enum_class, name, enum_method)
- gnv_last_values = []
- if issubclass(enum_class, Flag):
- # Flag / IntFlag
- single_bits = multi_bits = 0
- for name, value in attrs.items():
- if isinstance(value, auto) and auto.value is _auto_null:
- value = gnv(name, 1, len(member_names), gnv_last_values)
- if value in value2member_map:
- # an alias to an existing member
- redirect = property()
- redirect.__set_name__(enum_class, name)
- setattr(enum_class, name, redirect)
- member_map[name] = value2member_map[value]
- else:
- # create the member
- if use_args:
- if not isinstance(value, tuple):
- value = (value, )
- member = new_member(enum_class, *value)
- value = value[0]
- else:
- member = new_member(enum_class)
- if __new__ is None:
- member._value_ = value
- member._name_ = name
- member.__objclass__ = enum_class
- member.__init__(value)
- redirect = property()
- redirect.__set_name__(enum_class, name)
- setattr(enum_class, name, redirect)
- member_map[name] = member
- member._sort_order_ = len(member_names)
- value2member_map[value] = member
- if _is_single_bit(value):
- # not a multi-bit alias, record in _member_names_ and _flag_mask_
- member_names.append(name)
- single_bits |= value
- else:
- multi_bits |= value
- gnv_last_values.append(value)
- enum_class._flag_mask_ = single_bits
- enum_class._all_bits_ = 2 ** ((single_bits|multi_bits).bit_length()) - 1
- # set correct __iter__
- member_list = [m._value_ for m in enum_class]
- if member_list != sorted(member_list):
- enum_class._iter_member_ = enum_class._iter_member_by_def_
- else:
- # Enum / IntEnum / StrEnum
- for name, value in attrs.items():
- if isinstance(value, auto):
- if value.value is _auto_null:
- value.value = gnv(name, 1, len(member_names), gnv_last_values)
- value = value.value
- if value in value2member_map:
- # an alias to an existing member
- redirect = property()
- redirect.__set_name__(enum_class, name)
- setattr(enum_class, name, redirect)
- member_map[name] = value2member_map[value]
- else:
- # create the member
- if use_args:
- if not isinstance(value, tuple):
- value = (value, )
- member = new_member(enum_class, *value)
- value = value[0]
- else:
- member = new_member(enum_class)
- if __new__ is None:
- member._value_ = value
- member._name_ = name
- member.__objclass__ = enum_class
- member.__init__(value)
- member._sort_order_ = len(member_names)
- redirect = property()
- redirect.__set_name__(enum_class, name)
- setattr(enum_class, name, redirect)
- member_map[name] = member
- value2member_map[value] = member
- member_names.append(name)
- gnv_last_values.append(value)
- if '__new__' in body:
- enum_class.__new_member__ = enum_class.__new__
- enum_class.__new__ = Enum.__new__
- return enum_class
- return convert_class
-
-@_simple_enum(StrEnum)
-class EnumCheck:
- """
- various conditions to check an enumeration for
- """
- CONTINUOUS = "no skipped integer values"
- NAMED_FLAGS = "multi-flag aliases may not contain unnamed flags"
- UNIQUE = "one name per value"
-CONTINUOUS, NAMED_FLAGS, UNIQUE = EnumCheck
-
-
-class verify:
- """
- Check an enumeration for various constraints. (see EnumCheck)
- """
- def __init__(self, *checks):
- self.checks = checks
- def __call__(self, enumeration):
- checks = self.checks
- cls_name = enumeration.__name__
- if Flag is not None and issubclass(enumeration, Flag):
- enum_type = 'flag'
- elif issubclass(enumeration, Enum):
- enum_type = 'enum'
- else:
- raise TypeError("the 'verify' decorator only works with Enum and Flag")
- for check in checks:
- if check is UNIQUE:
- # check for duplicate names
- duplicates = []
- for name, member in enumeration.__members__.items():
- if name != member.name:
- duplicates.append((name, member.name))
- if duplicates:
- alias_details = ', '.join(
- ["%s -> %s" % (alias, name) for (alias, name) in duplicates])
- raise ValueError('aliases found in %r: %s' %
- (enumeration, alias_details))
- elif check is CONTINUOUS:
- values = set(e.value for e in enumeration)
- if len(values) < 2:
- continue
- low, high = min(values), max(values)
- missing = []
- if enum_type == 'flag':
- # check for powers of two
- for i in range(_high_bit(low)+1, _high_bit(high)):
- if 2**i not in values:
- missing.append(2**i)
- elif enum_type == 'enum':
- # check for powers of one
- for i in range(low+1, high):
- if i not in values:
- missing.append(i)
- else:
- raise Exception('verify: unknown type %r' % enum_type)
- if missing:
- raise ValueError(('invalid %s %r: missing values %s' % (
- enum_type, cls_name, ', '.join((str(m) for m in missing)))
- )[:256])
- # limit max length to protect against DOS attacks
- elif check is NAMED_FLAGS:
- # examine each alias and check for unnamed flags
- member_names = enumeration._member_names_
- member_values = [m.value for m in enumeration]
- missing_names = []
- missing_value = 0
- for name, alias in enumeration._member_map_.items():
- if name in member_names:
- # not an alias
- continue
- values = list(_iter_bits_lsb(alias.value))
- missed = [v for v in values if v not in member_values]
- if missed:
- missing_names.append(name)
- missing_value |= reduce(_or_, missed)
- if missing_names:
- if len(missing_names) == 1:
- alias = 'alias %s is missing' % missing_names[0]
- else:
- alias = 'aliases %s and %s are missing' % (
- ', '.join(missing_names[:-1]), missing_names[-1]
- )
- if _is_single_bit(missing_value):
- value = 'value 0x%x' % missing_value
- else:
- value = 'combined values of 0x%x' % missing_value
- raise ValueError(
- 'invalid Flag %r: %s %s [use enum.show_flag_values(value) for details]'
- % (cls_name, alias, value)
- )
- return enumeration
-
-def _test_simple_enum(checked_enum, simple_enum):
- """
- A function that can be used to test an enum created with :func:`_simple_enum`
- against the version created by subclassing :class:`Enum`::
-
- >>> from enum import Enum, _simple_enum, _test_simple_enum
- >>> @_simple_enum(Enum)
- ... class Color:
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> class CheckedColor(Enum):
- ... RED = auto()
- ... GREEN = auto()
- ... BLUE = auto()
- >>> _test_simple_enum(CheckedColor, Color)
-
- If differences are found, a :exc:`TypeError` is raised.
- """
- failed = []
- if checked_enum.__dict__ != simple_enum.__dict__:
- checked_dict = checked_enum.__dict__
- checked_keys = list(checked_dict.keys())
- simple_dict = simple_enum.__dict__
- simple_keys = list(simple_dict.keys())
- member_names = set(
- list(checked_enum._member_map_.keys())
- + list(simple_enum._member_map_.keys())
- )
- for key in set(checked_keys + simple_keys):
- if key in ('__module__', '_member_map_', '_value2member_map_'):
- # keys known to be different
- continue
- elif key in member_names:
- # members are checked below
- continue
- elif key not in simple_keys:
- failed.append("missing key: %r" % (key, ))
- elif key not in checked_keys:
- failed.append("extra key: %r" % (key, ))
- else:
- checked_value = checked_dict[key]
- simple_value = simple_dict[key]
- if callable(checked_value):
- continue
- if key == '__doc__':
- # remove all spaces/tabs
- compressed_checked_value = checked_value.replace(' ','').replace('\t','')
- compressed_simple_value = simple_value.replace(' ','').replace('\t','')
- if compressed_checked_value != compressed_simple_value:
- failed.append("%r:\n %s\n %s" % (
- key,
- "checked -> %r" % (checked_value, ),
- "simple -> %r" % (simple_value, ),
- ))
- elif checked_value != simple_value:
- failed.append("%r:\n %s\n %s" % (
- key,
- "checked -> %r" % (checked_value, ),
- "simple -> %r" % (simple_value, ),
- ))
- failed.sort()
- for name in member_names:
- failed_member = []
- if name not in simple_keys:
- failed.append('missing member from simple enum: %r' % name)
- elif name not in checked_keys:
- failed.append('extra member in simple enum: %r' % name)
- else:
- checked_member_dict = checked_enum[name].__dict__
- checked_member_keys = list(checked_member_dict.keys())
- simple_member_dict = simple_enum[name].__dict__
- simple_member_keys = list(simple_member_dict.keys())
- for key in set(checked_member_keys + simple_member_keys):
- if key in ('__module__', '__objclass__', '_inverted_'):
- # keys known to be different or absent
- continue
- elif key not in simple_member_keys:
- failed_member.append("missing key %r not in the simple enum member %r" % (key, name))
- elif key not in checked_member_keys:
- failed_member.append("extra key %r in simple enum member %r" % (key, name))
- else:
- checked_value = checked_member_dict[key]
- simple_value = simple_member_dict[key]
- if checked_value != simple_value:
- failed_member.append("%r:\n %s\n %s" % (
- key,
- "checked member -> %r" % (checked_value, ),
- "simple member -> %r" % (simple_value, ),
- ))
- if failed_member:
- failed.append('%r member mismatch:\n %s' % (
- name, '\n '.join(failed_member),
- ))
- for method in (
- '__str__', '__repr__', '__reduce_ex__', '__format__',
- '__getnewargs_ex__', '__getnewargs__', '__reduce_ex__', '__reduce__'
- ):
- if method in simple_keys and method in checked_keys:
- # cannot compare functions, and it exists in both, so we're good
- continue
- elif method not in simple_keys and method not in checked_keys:
- # method is inherited -- check it out
- checked_method = getattr(checked_enum, method, None)
- simple_method = getattr(simple_enum, method, None)
- if hasattr(checked_method, '__func__'):
- checked_method = checked_method.__func__
- simple_method = simple_method.__func__
- if checked_method != simple_method:
- failed.append("%r: %-30s %s" % (
- method,
- "checked -> %r" % (checked_method, ),
- "simple -> %r" % (simple_method, ),
- ))
- else:
- # if the method existed in only one of the enums, it will have been caught
- # in the first checks above
- pass
- if failed:
- raise TypeError('enum mismatch:\n %s' % '\n '.join(failed))
-
-def _old_convert_(etype, name, module, filter, source=None, *, boundary=None):
- """
- Create a new Enum subclass that replaces a collection of global constants
- """
- # convert all constants from source (or module) that pass filter() to
- # a new Enum called name, and export the enum and its members back to
- # module;
- # also, replace the __reduce_ex__ method so unpickling works in
- # previous Python versions
- module_globals = sys.modules[module].__dict__
- if source:
- source = source.__dict__
- else:
- source = module_globals
- # _value2member_map_ is populated in the same order every time
- # for a consistent reverse mapping of number to name when there
- # are multiple names for the same number.
- members = [
- (name, value)
- for name, value in source.items()
- if filter(name)]
- try:
- # sort by value
- members.sort(key=lambda t: (t[1], t[0]))
- except TypeError:
- # unless some values aren't comparable, in which case sort by name
- members.sort(key=lambda t: t[0])
- cls = etype(name, members, module=module, boundary=boundary or KEEP)
- cls.__reduce_ex__ = _reduce_ex_by_global_name
- cls.__repr__ = global_enum_repr
- return cls
+ # _decompose is only called if the value is not named
+ not_covered = value
+ negative = value < 0
+ members = []
+ for member in flag:
+ member_value = member.value
+ if member_value and member_value & value == member_value:
+ members.append(member)
+ not_covered &= ~member_value
+ if not negative:
+ tmp = not_covered
+ while tmp:
+ flag_value = 2 ** _high_bit(tmp)
+ if flag_value in flag._value2member_map_:
+ members.append(flag._value2member_map_[flag_value])
+ not_covered &= ~flag_value
+ tmp &= ~flag_value
+ if not members and value in flag._value2member_map_:
+ members.append(flag._value2member_map_[value])
+ members.sort(key=lambda m: m._value_, reverse=True)
+ if len(members) > 1 and members[0].value == value:
+ # we have the breakdown, don't need the value member itself
+ members.pop(0)
+ return members, not_covered
diff --git a/Lib/http/__init__.py b/Lib/http/__init__.py
index 8b980e24a5603..bf8d7d6886838 100644
--- a/Lib/http/__init__.py
+++ b/Lib/http/__init__.py
@@ -1,10 +1,9 @@
-from enum import IntEnum, _simple_enum
+from enum import IntEnum
__all__ = ['HTTPStatus']
-@_simple_enum(IntEnum)
-class HTTPStatus:
+class HTTPStatus(IntEnum):
"""HTTP status codes and reason phrases
Status codes from the following RFCs are all observed:
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 9f8cc01310f10..0273ffa2ba4d1 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2561,6 +2561,9 @@ class _ParameterKind(enum.IntEnum):
KEYWORD_ONLY = 3
VAR_KEYWORD = 4
+ def __str__(self):
+ return self._name_
+
@property
def description(self):
return _PARAM_NAME_MAPPING[self]
diff --git a/Lib/plistlib.py b/Lib/plistlib.py
index 5772efdfe6710..2eeebe4c9a424 100644
--- a/Lib/plistlib.py
+++ b/Lib/plistlib.py
@@ -61,7 +61,8 @@
from xml.parsers.expat import ParserCreate
-PlistFormat = enum.global_enum(enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__))
+PlistFormat = enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__)
+globals().update(PlistFormat.__members__)
class UID:
diff --git a/Lib/pstats.py b/Lib/pstats.py
index e77459d38b33e..0f93ae02c9507 100644
--- a/Lib/pstats.py
+++ b/Lib/pstats.py
@@ -26,15 +26,14 @@
import marshal
import re
-from enum import StrEnum, _simple_enum
+from enum import Enum
from functools import cmp_to_key
from dataclasses import dataclass
from typing import Dict
__all__ = ["Stats", "SortKey", "FunctionProfile", "StatsProfile"]
-@_simple_enum(StrEnum)
-class SortKey:
+class SortKey(str, Enum):
CALLS = 'calls', 'ncalls'
CUMULATIVE = 'cumulative', 'cumtime'
FILENAME = 'filename', 'module'
diff --git a/Lib/re.py b/Lib/re.py
index ea41217ce08c2..1d82b5006396c 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -142,9 +142,7 @@
__version__ = "2.2.1"
-(a)enum.global_enum
-(a)enum._simple_enum(enum.IntFlag, boundary=enum.KEEP)
-class RegexFlag:
+class RegexFlag(enum.IntFlag):
ASCII = A = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
IGNORECASE = I = sre_compile.SRE_FLAG_IGNORECASE # ignore case
LOCALE = L = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
@@ -156,6 +154,30 @@ class RegexFlag:
TEMPLATE = T = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
+ def __repr__(self):
+ if self._name_ is not None:
+ return f're.{self._name_}'
+ value = self._value_
+ members = []
+ negative = value < 0
+ if negative:
+ value = ~value
+ for m in self.__class__:
+ if value & m._value_:
+ value &= ~m._value_
+ members.append(f're.{m._name_}')
+ if value:
+ members.append(hex(value))
+ res = '|'.join(members)
+ if negative:
+ if len(members) > 1:
+ res = f'~({res})'
+ else:
+ res = f'~{res}'
+ return res
+ __str__ = object.__str__
+globals().update(RegexFlag.__members__)
+
# sre exception
error = sre_compile.error
diff --git a/Lib/ssl.py b/Lib/ssl.py
index a16ebd74a1b16..181065d2e53ba 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -94,7 +94,6 @@
import os
from collections import namedtuple
from enum import Enum as _Enum, IntEnum as _IntEnum, IntFlag as _IntFlag
-from enum import _simple_enum
import _ssl # if we can't import it, let the error propagate
@@ -156,8 +155,7 @@
_SSLv2_IF_EXISTS = getattr(_SSLMethod, 'PROTOCOL_SSLv2', None)
-@_simple_enum(_IntEnum)
-class TLSVersion:
+class TLSVersion(_IntEnum):
MINIMUM_SUPPORTED = _ssl.PROTO_MINIMUM_SUPPORTED
SSLv3 = _ssl.PROTO_SSLv3
TLSv1 = _ssl.PROTO_TLSv1
@@ -167,8 +165,7 @@ class TLSVersion:
MAXIMUM_SUPPORTED = _ssl.PROTO_MAXIMUM_SUPPORTED
-@_simple_enum(_IntEnum)
-class _TLSContentType:
+class _TLSContentType(_IntEnum):
"""Content types (record layer)
See RFC 8446, section B.1
@@ -182,8 +179,7 @@ class _TLSContentType:
INNER_CONTENT_TYPE = 0x101
-@_simple_enum(_IntEnum)
-class _TLSAlertType:
+class _TLSAlertType(_IntEnum):
"""Alert types for TLSContentType.ALERT messages
See RFC 8466, section B.2
@@ -224,8 +220,7 @@ class _TLSAlertType:
NO_APPLICATION_PROTOCOL = 120
-@_simple_enum(_IntEnum)
-class _TLSMessageType:
+class _TLSMessageType(_IntEnum):
"""Message types (handshake protocol)
See RFC 8446, section B.3
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index a44f8f551c5e0..3fac03d445256 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -1,7 +1,6 @@
import ast
import builtins
import dis
-import enum
import os
import sys
import types
@@ -699,35 +698,6 @@ def test_constant_as_name(self):
with self.assertRaisesRegex(ValueError, f"Name node can't be used with '{constant}' constant"):
compile(expr, "<test>", "eval")
- def test_precedence_enum(self):
- class _Precedence(enum.IntEnum):
- """Precedence table that originated from python grammar."""
- TUPLE = enum.auto()
- YIELD = enum.auto() # 'yield', 'yield from'
- TEST = enum.auto() # 'if'-'else', 'lambda'
- OR = enum.auto() # 'or'
- AND = enum.auto() # 'and'
- NOT = enum.auto() # 'not'
- CMP = enum.auto() # '<', '>', '==', '>=', '<=', '!=',
- # 'in', 'not in', 'is', 'is not'
- EXPR = enum.auto()
- BOR = EXPR # '|'
- BXOR = enum.auto() # '^'
- BAND = enum.auto() # '&'
- SHIFT = enum.auto() # '<<', '>>'
- ARITH = enum.auto() # '+', '-'
- TERM = enum.auto() # '*', '@', '/', '%', '//'
- FACTOR = enum.auto() # unary '+', '-', '~'
- POWER = enum.auto() # '**'
- AWAIT = enum.auto() # 'await'
- ATOM = enum.auto()
- def next(self):
- try:
- return self.__class__(self + 1)
- except ValueError:
- return self
- enum._test_simple_enum(_Precedence, ast._Precedence)
-
class ASTHelpers_Test(unittest.TestCase):
maxDiff = None
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 0267ff5684c0d..eb1266b960440 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -1,32 +1,16 @@
import enum
-import doctest
import inspect
-import os
import pydoc
import sys
import unittest
import threading
from collections import OrderedDict
-from enum import Enum, IntEnum, StrEnum, EnumType, Flag, IntFlag, unique, auto
-from enum import STRICT, CONFORM, EJECT, KEEP, _simple_enum, _test_simple_enum
-from enum import verify, UNIQUE, CONTINUOUS, NAMED_FLAGS
+from enum import Enum, IntEnum, EnumMeta, Flag, IntFlag, unique, auto
from io import StringIO
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
-from test import support
-from test.support import ALWAYS_EQ
-from test.support import threading_helper
+from test.support import ALWAYS_EQ, check__all__, threading_helper
from datetime import timedelta
-python_version = sys.version_info[:2]
-
-def load_tests(loader, tests, ignore):
- tests.addTests(doctest.DocTestSuite(enum))
- if os.path.exists('Doc/library/enum.rst'):
- tests.addTests(doctest.DocFileSuite(
- '../../Doc/library/enum.rst',
- optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
- ))
- return tests
# for pickle tests
try:
@@ -62,9 +46,14 @@ class FlagStooges(Flag):
FlagStooges = exc
# for pickle test and subclass tests
-class Name(StrEnum):
- BDFL = 'Guido van Rossum'
- FLUFL = 'Barry Warsaw'
+try:
+ class StrEnum(str, Enum):
+ 'accepts only string values'
+ class Name(StrEnum):
+ BDFL = 'Guido van Rossum'
+ FLUFL = 'Barry Warsaw'
+except Exception as exc:
+ Name = exc
try:
Question = Enum('Question', 'who what when where why', module=__name__)
@@ -265,8 +254,11 @@ def test_enum(self):
self.assertIn(e, Season)
self.assertIs(type(e), Season)
self.assertIsInstance(e, Season)
- self.assertEqual(str(e), season)
- self.assertEqual(repr(e), 'Season.{0}'.format(season))
+ self.assertEqual(str(e), 'Season.' + season)
+ self.assertEqual(
+ repr(e),
+ '<Season.{0}: {1}>'.format(season, i),
+ )
def test_value_name(self):
Season = self.Season
@@ -355,38 +347,17 @@ class IntLogic(int, Enum):
self.assertTrue(IntLogic.true)
self.assertFalse(IntLogic.false)
- @unittest.skipIf(
- python_version >= (3, 12),
- '__contains__ now returns True/False for all inputs',
- )
- def test_contains_er(self):
+ def test_contains(self):
Season = self.Season
self.assertIn(Season.AUTUMN, Season)
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 3 in Season
+ 3 in Season
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 'AUTUMN' in Season
- val = Season(3)
- self.assertIn(val, Season)
- #
- class OtherEnum(Enum):
- one = 1; two = 2
- self.assertNotIn(OtherEnum.two, Season)
+ 'AUTUMN' in Season
- @unittest.skipIf(
- python_version < (3, 12),
- '__contains__ only works with enum memmbers before 3.12',
- )
- def test_contains_tf(self):
- Season = self.Season
- self.assertIn(Season.AUTUMN, Season)
- self.assertTrue(3 in Season)
- self.assertFalse('AUTUMN' in Season)
val = Season(3)
self.assertIn(val, Season)
- #
+
class OtherEnum(Enum):
one = 1; two = 2
self.assertNotIn(OtherEnum.two, Season)
@@ -458,13 +429,6 @@ def red(self):
green = 2
blue = 3
- def test_reserved__sunder_(self):
- with self.assertRaisesRegex(
- ValueError,
- '_sunder_ names, such as ._bad_., are reserved',
- ):
- class Bad(Enum):
- _bad_ = 1
def test_enum_with_value_name(self):
class Huh(Enum):
@@ -509,7 +473,7 @@ class EnumWithFormatOverride(Enum):
two = 2.0
def __format__(self, spec):
return 'Format!!'
- self.assertEqual(str(EnumWithFormatOverride.one), 'one')
+ self.assertEqual(str(EnumWithFormatOverride.one), 'EnumWithFormatOverride.one')
self.assertEqual('{}'.format(EnumWithFormatOverride.one), 'Format!!')
def test_str_and_format_override_enum(self):
@@ -549,42 +513,11 @@ class TestFloat(float, Enum):
two = 2.0
def __format__(self, spec):
return 'TestFloat success!'
- self.assertEqual(str(TestFloat.one), 'one')
+ self.assertEqual(str(TestFloat.one), 'TestFloat.one')
self.assertEqual('{}'.format(TestFloat.one), 'TestFloat success!')
- @unittest.skipIf(
- python_version < (3, 12),
- 'mixin-format is still using member.value',
- )
- def test_mixin_format_warning(self):
- class Grades(int, Enum):
- A = 5
- B = 4
- C = 3
- D = 2
- F = 0
- self.assertEqual(f'{self.Grades.B}', 'B')
-
- @unittest.skipIf(
- python_version >= (3, 12),
- 'mixin-format now uses member instead of member.value',
- )
- def test_mixin_format_warning(self):
- class Grades(int, Enum):
- A = 5
- B = 4
- C = 3
- D = 2
- F = 0
- with self.assertWarns(DeprecationWarning):
- self.assertEqual(f'{Grades.B}', '4')
-
def assertFormatIsValue(self, spec, member):
- if python_version < (3, 12) and (not spec or spec in ('{}','{:}')):
- with self.assertWarns(DeprecationWarning):
- self.assertEqual(spec.format(member), spec.format(member.value))
- else:
- self.assertEqual(spec.format(member), spec.format(member.value))
+ self.assertEqual(spec.format(member), spec.format(member.value))
def test_format_enum_date(self):
Holiday = self.Holiday
@@ -610,12 +543,7 @@ def test_format_enum_float(self):
self.assertFormatIsValue('{:f}', Konstants.TAU)
def test_format_enum_int(self):
- class Grades(int, Enum):
- A = 5
- B = 4
- C = 3
- D = 2
- F = 0
+ Grades = self.Grades
self.assertFormatIsValue('{}', Grades.C)
self.assertFormatIsValue('{:}', Grades.C)
self.assertFormatIsValue('{:20}', Grades.C)
@@ -665,15 +593,19 @@ class Test2Enum(MyStrEnum, MyMethodEnum):
def test_inherited_data_type(self):
class HexInt(int):
+ __qualname__ = 'HexInt'
def __repr__(self):
return hex(self)
class MyEnum(HexInt, enum.Enum):
+ __qualname__ = 'MyEnum'
A = 1
B = 2
C = 3
- def __repr__(self):
- return '<%s.%s: %r>' % (self.__class__.__name__, self._name_, self._value_)
self.assertEqual(repr(MyEnum.A), '<MyEnum.A: 0x1>')
+ globals()['HexInt'] = HexInt
+ globals()['MyEnum'] = MyEnum
+ test_pickle_dump_load(self.assertIs, MyEnum.A)
+ test_pickle_dump_load(self.assertIs, MyEnum)
#
class SillyInt(HexInt):
__qualname__ = 'SillyInt'
@@ -689,22 +621,21 @@ class MyOtherEnum(SillyInt, enum.Enum):
test_pickle_dump_load(self.assertIs, MyOtherEnum.E)
test_pickle_dump_load(self.assertIs, MyOtherEnum)
#
- # This did not work in 3.9, but does now with pickling by name
- class UnBrokenInt(int):
- __qualname__ = 'UnBrokenInt'
+ class BrokenInt(int):
+ __qualname__ = 'BrokenInt'
def __new__(cls, value):
return int.__new__(cls, value)
- class MyUnBrokenEnum(UnBrokenInt, Enum):
- __qualname__ = 'MyUnBrokenEnum'
+ class MyBrokenEnum(BrokenInt, Enum):
+ __qualname__ = 'MyBrokenEnum'
G = 7
H = 8
I = 9
- self.assertIs(MyUnBrokenEnum._member_type_, UnBrokenInt)
- self.assertIs(MyUnBrokenEnum(7), MyUnBrokenEnum.G)
- globals()['UnBrokenInt'] = UnBrokenInt
- globals()['MyUnBrokenEnum'] = MyUnBrokenEnum
- test_pickle_dump_load(self.assertIs, MyUnBrokenEnum.I)
- test_pickle_dump_load(self.assertIs, MyUnBrokenEnum)
+ self.assertIs(MyBrokenEnum._member_type_, BrokenInt)
+ self.assertIs(MyBrokenEnum(7), MyBrokenEnum.G)
+ globals()['BrokenInt'] = BrokenInt
+ globals()['MyBrokenEnum'] = MyBrokenEnum
+ test_pickle_exception(self.assertRaises, TypeError, MyBrokenEnum.G)
+ test_pickle_exception(self.assertRaises, PicklingError, MyBrokenEnum)
def test_too_many_data_types(self):
with self.assertRaisesRegex(TypeError, 'too many data types'):
@@ -764,13 +695,14 @@ class phy(str, Enum):
tau = 'Tau'
self.assertTrue(phy.pi < phy.tau)
- def test_strenum_inherited_methods(self):
+ def test_strenum_inherited(self):
+ class StrEnum(str, Enum):
+ pass
class phy(StrEnum):
pi = 'Pi'
tau = 'Tau'
self.assertTrue(phy.pi < phy.tau)
- self.assertEqual(phy.pi.upper(), 'PI')
- self.assertEqual(phy.tau.count('a'), 1)
+
def test_intenum(self):
class WeekDay(IntEnum):
@@ -877,7 +809,7 @@ def test_pickle_by_name(self):
class ReplaceGlobalInt(IntEnum):
ONE = 1
TWO = 2
- ReplaceGlobalInt.__reduce_ex__ = enum._reduce_ex_by_global_name
+ ReplaceGlobalInt.__reduce_ex__ = enum._reduce_ex_by_name
for proto in range(HIGHEST_PROTOCOL):
self.assertEqual(ReplaceGlobalInt.TWO.__reduce_ex__(proto), 'TWO')
@@ -1302,11 +1234,9 @@ def __name__(self):
return self._intname
def __repr__(self):
# repr() is updated to include the name and type info
- return "{}({!r}, {})".format(
- type(self).__name__,
- self.__name__,
- int.__repr__(self),
- )
+ return "{}({!r}, {})".format(type(self).__name__,
+ self.__name__,
+ int.__repr__(self))
def __str__(self):
# str() is unchanged, even if it relies on the repr() fallback
base = int
@@ -1321,8 +1251,7 @@ def __add__(self, other):
if isinstance(self, NamedInt) and isinstance(other, NamedInt):
return NamedInt(
'({0} + {1})'.format(self.__name__, other.__name__),
- temp,
- )
+ temp )
else:
return temp
@@ -1362,11 +1291,9 @@ def __name__(self):
return self._intname
def __repr__(self):
# repr() is updated to include the name and type info
- return "{}({!r}, {})".format(
- type(self).__name__,
- self.__name__,
- int.__repr__(self),
- )
+ return "{}({!r}, {})".format(type(self).__name__,
+ self.__name__,
+ int.__repr__(self))
def __str__(self):
# str() is unchanged, even if it relies on the repr() fallback
base = int
@@ -1381,8 +1308,7 @@ def __add__(self, other):
if isinstance(self, NamedInt) and isinstance(other, NamedInt):
return NamedInt(
'({0} + {1})'.format(self.__name__, other.__name__),
- temp,
- )
+ temp )
else:
return temp
@@ -1422,11 +1348,9 @@ def __name__(self):
return self._intname
def __repr__(self):
# repr() is updated to include the name and type info
- return "{}({!r}, {})".format(
- type(self).__name__,
- self.__name__,
- int.__repr__(self),
- )
+ return "{}({!r}, {})".format(type(self).__name__,
+ self.__name__,
+ int.__repr__(self))
def __str__(self):
# str() is unchanged, even if it relies on the repr() fallback
base = int
@@ -1441,8 +1365,7 @@ def __add__(self, other):
if isinstance(self, NamedInt) and isinstance(other, NamedInt):
return NamedInt(
'({0} + {1})'.format(self.__name__, other.__name__),
- temp,
- )
+ temp )
else:
return temp
@@ -1482,11 +1405,9 @@ def __name__(self):
return self._intname
def __repr__(self):
# repr() is updated to include the name and type info
- return "{}({!r}, {})".format(
- type(self).__name__,
- self.__name__,
- int.__repr__(self),
- )
+ return "{}({!r}, {})".format(type(self).__name__,
+ self.__name__,
+ int.__repr__(self))
def __str__(self):
# str() is unchanged, even if it relies on the repr() fallback
base = int
@@ -1501,8 +1422,7 @@ def __add__(self, other):
if isinstance(self, NamedInt) and isinstance(other, NamedInt):
return NamedInt(
'({0} + {1})'.format(self.__name__, other.__name__),
- temp,
- )
+ temp )
else:
return temp
@@ -1511,6 +1431,7 @@ class NEI(NamedInt, Enum):
x = ('the-x', 1)
y = ('the-y', 2)
+
self.assertIs(NEI.__new__, Enum.__new__)
self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
globals()['NamedInt'] = NamedInt
@@ -1539,11 +1460,9 @@ def __name__(self):
return self._intname
def __repr__(self):
# repr() is updated to include the name and type info
- return "{}({!r}, {})".format(
- type(self).__name__,
- self.__name__,
- int.__repr__(self),
- )
+ return "{}({!r}, {})".format(type(self).__name__,
+ self.__name__,
+ int.__repr__(self))
def __str__(self):
# str() is unchanged, even if it relies on the repr() fallback
base = int
@@ -1574,10 +1493,10 @@ class NEI(NamedInt, Enum):
NI5 = NamedInt('test', 5)
self.assertEqual(NI5, 5)
self.assertEqual(NEI.y.value, 2)
- test_pickle_dump_load(self.assertIs, NEI.y)
- test_pickle_dump_load(self.assertIs, NEI)
+ test_pickle_exception(self.assertRaises, TypeError, NEI.x)
+ test_pickle_exception(self.assertRaises, PicklingError, NEI)
- def test_subclasses_with_direct_pickle_support(self):
+ def test_subclasses_without_direct_pickle_support_using_name(self):
class NamedInt(int):
__qualname__ = 'NamedInt'
def __new__(cls, *args):
@@ -1594,11 +1513,9 @@ def __name__(self):
return self._intname
def __repr__(self):
# repr() is updated to include the name and type info
- return "{}({!r}, {})".format(
- type(self).__name__,
- self.__name__,
- int.__repr__(self),
- )
+ return "{}({!r}, {})".format(type(self).__name__,
+ self.__name__,
+ int.__repr__(self))
def __str__(self):
# str() is unchanged, even if it relies on the repr() fallback
base = int
@@ -1613,8 +1530,7 @@ def __add__(self, other):
if isinstance(self, NamedInt) and isinstance(other, NamedInt):
return NamedInt(
'({0} + {1})'.format(self.__name__, other.__name__),
- temp,
- )
+ temp )
else:
return temp
@@ -1778,13 +1694,6 @@ def test(self):
class Test(Base):
test = 1
self.assertEqual(Test.test.test, 'dynamic')
- class Base2(Enum):
- @enum.property
- def flash(self):
- return 'flashy dynamic'
- class Test(Base2):
- flash = 1
- self.assertEqual(Test.flash.flash, 'flashy dynamic')
def test_no_duplicates(self):
class UniqueEnum(Enum):
@@ -2023,38 +1932,6 @@ def _missing_(cls, item):
else:
raise Exception('Exception not raised.')
- def test_missing_exceptions_reset(self):
- import weakref
- #
- class TestEnum(enum.Enum):
- VAL1 = 'val1'
- VAL2 = 'val2'
- #
- class Class1:
- def __init__(self):
- # Gracefully handle an exception of our own making
- try:
- raise ValueError()
- except ValueError:
- pass
- #
- class Class2:
- def __init__(self):
- # Gracefully handle an exception of Enum's making
- try:
- TestEnum('invalid_value')
- except ValueError:
- pass
- # No strong refs here so these are free to die.
- class_1_ref = weakref.ref(Class1())
- class_2_ref = weakref.ref(Class2())
- #
- # The exception raised by Enum creates a reference loop and thus
- # Class2 instances will stick around until the next gargage collection
- # cycle, unlike Class1.
- self.assertIs(class_1_ref(), None)
- self.assertIs(class_2_ref(), None)
-
def test_multiple_mixin(self):
class MaxMixin:
@classproperty
@@ -2081,7 +1958,7 @@ class Color(MaxMixin, Enum):
self.assertEqual(Color.GREEN.value, 2)
self.assertEqual(Color.BLUE.value, 3)
self.assertEqual(Color.MAX, 3)
- self.assertEqual(str(Color.BLUE), 'BLUE')
+ self.assertEqual(str(Color.BLUE), 'Color.BLUE')
class Color(MaxMixin, StrMixin, Enum):
RED = auto()
GREEN = auto()
@@ -2152,6 +2029,13 @@ class ReformedColor(StrMixin, IntEnum, SomeEnum, AnotherEnum):
self.assertTrue(issubclass(ReformedColor, int))
def test_multiple_inherited_mixin(self):
+ class StrEnum(str, Enum):
+ def __new__(cls, *args, **kwargs):
+ for a in args:
+ if not isinstance(a, str):
+ raise TypeError("Enumeration '%s' (%s) is not"
+ " a string" % (a, type(a).__name__))
+ return str.__new__(cls, *args, **kwargs)
@unique
class Decision1(StrEnum):
REVERT = "REVERT"
@@ -2191,50 +2075,6 @@ def __new__(cls, value):
return member
self.assertEqual(Fee.TEST, 2)
- def test_miltuple_mixin_with_common_data_type(self):
- class CaseInsensitiveStrEnum(str, Enum):
- @classmethod
- def _missing_(cls, value):
- for member in cls._member_map_.values():
- if member._value_.lower() == value.lower():
- return member
- return super()._missing_(value)
- #
- class LenientStrEnum(str, Enum):
- def __init__(self, *args):
- self._valid = True
- @classmethod
- def _missing_(cls, value):
- unknown = cls._member_type_.__new__(cls, value)
- unknown._valid = False
- unknown._name_ = value.upper()
- unknown._value_ = value
- cls._member_map_[value] = unknown
- return unknown
- @property
- def valid(self):
- return self._valid
- #
- class JobStatus(CaseInsensitiveStrEnum, LenientStrEnum):
- ACTIVE = "active"
- PENDING = "pending"
- TERMINATED = "terminated"
- #
- JS = JobStatus
- self.assertEqual(list(JobStatus), [JS.ACTIVE, JS.PENDING, JS.TERMINATED])
- self.assertEqual(JS.ACTIVE, 'active')
- self.assertEqual(JS.ACTIVE.value, 'active')
- self.assertIs(JS('Active'), JS.ACTIVE)
- self.assertTrue(JS.ACTIVE.valid)
- missing = JS('missing')
- self.assertEqual(list(JobStatus), [JS.ACTIVE, JS.PENDING, JS.TERMINATED])
- self.assertEqual(JS.ACTIVE, 'active')
- self.assertEqual(JS.ACTIVE.value, 'active')
- self.assertIs(JS('Active'), JS.ACTIVE)
- self.assertTrue(JS.ACTIVE.valid)
- self.assertTrue(isinstance(missing, JS))
- self.assertFalse(missing.valid)
-
def test_empty_globals(self):
# bpo-35717: sys._getframe(2).f_globals['__name__'] fails with KeyError
# when using compile and exec because f_globals is empty
@@ -2244,220 +2084,8 @@ def test_empty_globals(self):
local_ls = {}
exec(code, global_ns, local_ls)
- def test_strenum(self):
- class GoodStrEnum(StrEnum):
- one = '1'
- two = '2'
- three = b'3', 'ascii'
- four = b'4', 'latin1', 'strict'
- self.assertEqual(GoodStrEnum.one, '1')
- self.assertEqual(str(GoodStrEnum.one), '1')
- self.assertEqual('{}'.format(GoodStrEnum.one), '1')
- self.assertEqual(GoodStrEnum.one, str(GoodStrEnum.one))
- self.assertEqual(GoodStrEnum.one, '{}'.format(GoodStrEnum.one))
- self.assertEqual(repr(GoodStrEnum.one), 'GoodStrEnum.one')
- #
- class DumbMixin:
- def __str__(self):
- return "don't do this"
- class DumbStrEnum(DumbMixin, StrEnum):
- five = '5'
- six = '6'
- seven = '7'
- self.assertEqual(DumbStrEnum.seven, '7')
- self.assertEqual(str(DumbStrEnum.seven), "don't do this")
- #
- class EnumMixin(Enum):
- def hello(self):
- print('hello from %s' % (self, ))
- class HelloEnum(EnumMixin, StrEnum):
- eight = '8'
- self.assertEqual(HelloEnum.eight, '8')
- self.assertEqual(HelloEnum.eight, str(HelloEnum.eight))
- #
- class GoodbyeMixin:
- def goodbye(self):
- print('%s wishes you a fond farewell')
- class GoodbyeEnum(GoodbyeMixin, EnumMixin, StrEnum):
- nine = '9'
- self.assertEqual(GoodbyeEnum.nine, '9')
- self.assertEqual(GoodbyeEnum.nine, str(GoodbyeEnum.nine))
- #
- with self.assertRaisesRegex(TypeError, '1 is not a string'):
- class FirstFailedStrEnum(StrEnum):
- one = 1
- two = '2'
- with self.assertRaisesRegex(TypeError, "2 is not a string"):
- class SecondFailedStrEnum(StrEnum):
- one = '1'
- two = 2,
- three = '3'
- with self.assertRaisesRegex(TypeError, '2 is not a string'):
- class ThirdFailedStrEnum(StrEnum):
- one = '1'
- two = 2
- with self.assertRaisesRegex(TypeError, 'encoding must be a string, not %r' % (sys.getdefaultencoding, )):
- class ThirdFailedStrEnum(StrEnum):
- one = '1'
- two = b'2', sys.getdefaultencoding
- with self.assertRaisesRegex(TypeError, 'errors must be a string, not 9'):
- class ThirdFailedStrEnum(StrEnum):
- one = '1'
- two = b'2', 'ascii', 9
-
- @unittest.skipIf(
- python_version >= (3, 12),
- 'mixin-format now uses member instead of member.value',
- )
- def test_custom_strenum_with_warning(self):
- class CustomStrEnum(str, Enum):
- pass
- class OkayEnum(CustomStrEnum):
- one = '1'
- two = '2'
- three = b'3', 'ascii'
- four = b'4', 'latin1', 'strict'
- self.assertEqual(OkayEnum.one, '1')
- self.assertEqual(str(OkayEnum.one), 'one')
- with self.assertWarns(DeprecationWarning):
- self.assertEqual('{}'.format(OkayEnum.one), '1')
- self.assertEqual(OkayEnum.one, '{}'.format(OkayEnum.one))
- self.assertEqual(repr(OkayEnum.one), 'OkayEnum.one')
- #
- class DumbMixin:
- def __str__(self):
- return "don't do this"
- class DumbStrEnum(DumbMixin, CustomStrEnum):
- five = '5'
- six = '6'
- seven = '7'
- self.assertEqual(DumbStrEnum.seven, '7')
- self.assertEqual(str(DumbStrEnum.seven), "don't do this")
- #
- class EnumMixin(Enum):
- def hello(self):
- print('hello from %s' % (self, ))
- class HelloEnum(EnumMixin, CustomStrEnum):
- eight = '8'
- self.assertEqual(HelloEnum.eight, '8')
- self.assertEqual(str(HelloEnum.eight), 'eight')
- #
- class GoodbyeMixin:
- def goodbye(self):
- print('%s wishes you a fond farewell')
- class GoodbyeEnum(GoodbyeMixin, EnumMixin, CustomStrEnum):
- nine = '9'
- self.assertEqual(GoodbyeEnum.nine, '9')
- self.assertEqual(str(GoodbyeEnum.nine), 'nine')
- #
- class FirstFailedStrEnum(CustomStrEnum):
- one = 1 # this will become '1'
- two = '2'
- class SecondFailedStrEnum(CustomStrEnum):
- one = '1'
- two = 2, # this will become '2'
- three = '3'
- class ThirdFailedStrEnum(CustomStrEnum):
- one = '1'
- two = 2 # this will become '2'
- with self.assertRaisesRegex(TypeError, '.encoding. must be str, not '):
- class ThirdFailedStrEnum(CustomStrEnum):
- one = '1'
- two = b'2', sys.getdefaultencoding
- with self.assertRaisesRegex(TypeError, '.errors. must be str, not '):
- class ThirdFailedStrEnum(CustomStrEnum):
- one = '1'
- two = b'2', 'ascii', 9
-
- @unittest.skipIf(
- python_version < (3, 12),
- 'mixin-format currently uses member.value',
- )
- def test_custom_strenum(self):
- class CustomStrEnum(str, Enum):
- pass
- class OkayEnum(CustomStrEnum):
- one = '1'
- two = '2'
- three = b'3', 'ascii'
- four = b'4', 'latin1', 'strict'
- self.assertEqual(OkayEnum.one, '1')
- self.assertEqual(str(OkayEnum.one), 'one')
- self.assertEqual('{}'.format(OkayEnum.one), 'one')
- self.assertEqual(repr(OkayEnum.one), 'OkayEnum.one')
- #
- class DumbMixin:
- def __str__(self):
- return "don't do this"
- class DumbStrEnum(DumbMixin, CustomStrEnum):
- five = '5'
- six = '6'
- seven = '7'
- self.assertEqual(DumbStrEnum.seven, '7')
- self.assertEqual(str(DumbStrEnum.seven), "don't do this")
- #
- class EnumMixin(Enum):
- def hello(self):
- print('hello from %s' % (self, ))
- class HelloEnum(EnumMixin, CustomStrEnum):
- eight = '8'
- self.assertEqual(HelloEnum.eight, '8')
- self.assertEqual(str(HelloEnum.eight), 'eight')
- #
- class GoodbyeMixin:
- def goodbye(self):
- print('%s wishes you a fond farewell')
- class GoodbyeEnum(GoodbyeMixin, EnumMixin, CustomStrEnum):
- nine = '9'
- self.assertEqual(GoodbyeEnum.nine, '9')
- self.assertEqual(str(GoodbyeEnum.nine), 'nine')
- #
- class FirstFailedStrEnum(CustomStrEnum):
- one = 1 # this will become '1'
- two = '2'
- class SecondFailedStrEnum(CustomStrEnum):
- one = '1'
- two = 2, # this will become '2'
- three = '3'
- class ThirdFailedStrEnum(CustomStrEnum):
- one = '1'
- two = 2 # this will become '2'
- with self.assertRaisesRegex(TypeError, '.encoding. must be str, not '):
- class ThirdFailedStrEnum(CustomStrEnum):
- one = '1'
- two = b'2', sys.getdefaultencoding
- with self.assertRaisesRegex(TypeError, '.errors. must be str, not '):
- class ThirdFailedStrEnum(CustomStrEnum):
- one = '1'
- two = b'2', 'ascii', 9
-
- def test_missing_value_error(self):
- with self.assertRaisesRegex(TypeError, "_value_ not set in __new__"):
- class Combined(str, Enum):
- #
- def __new__(cls, value, sequence):
- enum = str.__new__(cls, value)
- if '(' in value:
- fis_name, segment = value.split('(', 1)
- segment = segment.strip(' )')
- else:
- fis_name = value
- segment = None
- enum.fis_name = fis_name
- enum.segment = segment
- enum.sequence = sequence
- return enum
- #
- def __repr__(self):
- return "<%s.%s>" % (self.__class__.__name__, self._name_)
- #
- key_type = 'An$(1,2)', 0
- company_id = 'An$(3,2)', 1
- code = 'An$(5,1)', 2
- description = 'Bn$', 3
-
@unittest.skipUnless(
- python_version == (3, 9),
+ sys.version_info[:2] == (3, 9),
'private variables are now normal attributes',
)
def test_warning_for_private_variables(self):
@@ -2472,70 +2100,6 @@ class Private(Enum):
except ValueError:
pass
- def test_private_variable_is_normal_attribute(self):
- class Private(Enum):
- __corporal = 'Radar'
- __major_ = 'Hoolihan'
- self.assertEqual(Private._Private__corporal, 'Radar')
- self.assertEqual(Private._Private__major_, 'Hoolihan')
-
- @unittest.skipUnless(
- python_version < (3, 12),
- 'member-member access now raises an exception',
- )
- def test_warning_for_member_from_member_access(self):
- with self.assertWarns(DeprecationWarning):
- class Di(Enum):
- YES = 1
- NO = 0
- nope = Di.YES.NO
- self.assertIs(Di.NO, nope)
-
- @unittest.skipUnless(
- python_version >= (3, 12),
- 'member-member access currently issues a warning',
- )
- def test_exception_for_member_from_member_access(self):
- with self.assertRaisesRegex(AttributeError, "Di: no instance attribute .NO."):
- class Di(Enum):
- YES = 1
- NO = 0
- nope = Di.YES.NO
-
- def test_strenum_auto(self):
- class Strings(StrEnum):
- ONE = auto()
- TWO = auto()
- self.assertEqual([Strings.ONE, Strings.TWO], ['one', 'two'])
-
-
- def test_dynamic_members_with_static_methods(self):
- #
- foo_defines = {'FOO_CAT': 'aloof', 'BAR_DOG': 'friendly', 'FOO_HORSE': 'big'}
- class Foo(Enum):
- vars().update({
- k: v
- for k, v in foo_defines.items()
- if k.startswith('FOO_')
- })
- def upper(self):
- return self.value.upper()
- self.assertEqual(list(Foo), [Foo.FOO_CAT, Foo.FOO_HORSE])
- self.assertEqual(Foo.FOO_CAT.value, 'aloof')
- self.assertEqual(Foo.FOO_HORSE.upper(), 'BIG')
- #
- with self.assertRaisesRegex(TypeError, "'FOO_CAT' already defined as: 'aloof'"):
- class FooBar(Enum):
- vars().update({
- k: v
- for k, v in foo_defines.items()
- if k.startswith('FOO_')
- },
- **{'FOO_CAT': 'small'},
- )
- def upper(self):
- return self.value.upper()
-
class TestOrder(unittest.TestCase):
@@ -2615,71 +2179,68 @@ class Open(Flag):
class Color(Flag):
BLACK = 0
RED = 1
- ROJO = 1
GREEN = 2
BLUE = 4
PURPLE = RED|BLUE
- WHITE = RED|GREEN|BLUE
- BLANCO = RED|GREEN|BLUE
def test_str(self):
Perm = self.Perm
- self.assertEqual(str(Perm.R), 'R')
- self.assertEqual(str(Perm.W), 'W')
- self.assertEqual(str(Perm.X), 'X')
- self.assertEqual(str(Perm.R | Perm.W), 'R|W')
- self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'R|W|X')
- self.assertEqual(str(Perm(0)), 'Perm(0)')
- self.assertEqual(str(~Perm.R), 'W|X')
- self.assertEqual(str(~Perm.W), 'R|X')
- self.assertEqual(str(~Perm.X), 'R|W')
- self.assertEqual(str(~(Perm.R | Perm.W)), 'X')
- self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm(0)')
- self.assertEqual(str(Perm(~0)), 'R|W|X')
+ self.assertEqual(str(Perm.R), 'Perm.R')
+ self.assertEqual(str(Perm.W), 'Perm.W')
+ self.assertEqual(str(Perm.X), 'Perm.X')
+ self.assertEqual(str(Perm.R | Perm.W), 'Perm.R|W')
+ self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'Perm.R|W|X')
+ self.assertEqual(str(Perm(0)), 'Perm.0')
+ self.assertEqual(str(~Perm.R), 'Perm.W|X')
+ self.assertEqual(str(~Perm.W), 'Perm.R|X')
+ self.assertEqual(str(~Perm.X), 'Perm.R|W')
+ self.assertEqual(str(~(Perm.R | Perm.W)), 'Perm.X')
+ self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm.0')
+ self.assertEqual(str(Perm(~0)), 'Perm.R|W|X')
Open = self.Open
- self.assertEqual(str(Open.RO), 'RO')
- self.assertEqual(str(Open.WO), 'WO')
- self.assertEqual(str(Open.AC), 'AC')
- self.assertEqual(str(Open.RO | Open.CE), 'CE')
- self.assertEqual(str(Open.WO | Open.CE), 'WO|CE')
- self.assertEqual(str(~Open.RO), 'WO|RW|CE')
- self.assertEqual(str(~Open.WO), 'RW|CE')
- self.assertEqual(str(~Open.AC), 'CE')
- self.assertEqual(str(~(Open.RO | Open.CE)), 'AC')
- self.assertEqual(str(~(Open.WO | Open.CE)), 'RW')
+ self.assertEqual(str(Open.RO), 'Open.RO')
+ self.assertEqual(str(Open.WO), 'Open.WO')
+ self.assertEqual(str(Open.AC), 'Open.AC')
+ self.assertEqual(str(Open.RO | Open.CE), 'Open.CE')
+ self.assertEqual(str(Open.WO | Open.CE), 'Open.CE|WO')
+ self.assertEqual(str(~Open.RO), 'Open.CE|AC|RW|WO')
+ self.assertEqual(str(~Open.WO), 'Open.CE|RW')
+ self.assertEqual(str(~Open.AC), 'Open.CE')
+ self.assertEqual(str(~(Open.RO | Open.CE)), 'Open.AC')
+ self.assertEqual(str(~(Open.WO | Open.CE)), 'Open.RW')
def test_repr(self):
Perm = self.Perm
- self.assertEqual(repr(Perm.R), 'Perm.R')
- self.assertEqual(repr(Perm.W), 'Perm.W')
- self.assertEqual(repr(Perm.X), 'Perm.X')
- self.assertEqual(repr(Perm.R | Perm.W), 'Perm.R|Perm.W')
- self.assertEqual(repr(Perm.R | Perm.W | Perm.X), 'Perm.R|Perm.W|Perm.X')
- self.assertEqual(repr(Perm(0)), '0x0')
- self.assertEqual(repr(~Perm.R), 'Perm.W|Perm.X')
- self.assertEqual(repr(~Perm.W), 'Perm.R|Perm.X')
- self.assertEqual(repr(~Perm.X), 'Perm.R|Perm.W')
- self.assertEqual(repr(~(Perm.R | Perm.W)), 'Perm.X')
- self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '0x0')
- self.assertEqual(repr(Perm(~0)), 'Perm.R|Perm.W|Perm.X')
+ self.assertEqual(repr(Perm.R), '<Perm.R: 4>')
+ self.assertEqual(repr(Perm.W), '<Perm.W: 2>')
+ self.assertEqual(repr(Perm.X), '<Perm.X: 1>')
+ self.assertEqual(repr(Perm.R | Perm.W), '<Perm.R|W: 6>')
+ self.assertEqual(repr(Perm.R | Perm.W | Perm.X), '<Perm.R|W|X: 7>')
+ self.assertEqual(repr(Perm(0)), '<Perm.0: 0>')
+ self.assertEqual(repr(~Perm.R), '<Perm.W|X: 3>')
+ self.assertEqual(repr(~Perm.W), '<Perm.R|X: 5>')
+ self.assertEqual(repr(~Perm.X), '<Perm.R|W: 6>')
+ self.assertEqual(repr(~(Perm.R | Perm.W)), '<Perm.X: 1>')
+ self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '<Perm.0: 0>')
+ self.assertEqual(repr(Perm(~0)), '<Perm.R|W|X: 7>')
Open = self.Open
- self.assertEqual(repr(Open.RO), 'Open.RO')
- self.assertEqual(repr(Open.WO), 'Open.WO')
- self.assertEqual(repr(Open.AC), 'Open.AC')
- self.assertEqual(repr(Open.RO | Open.CE), 'Open.CE')
- self.assertEqual(repr(Open.WO | Open.CE), 'Open.WO|Open.CE')
- self.assertEqual(repr(~Open.RO), 'Open.WO|Open.RW|Open.CE')
- self.assertEqual(repr(~Open.WO), 'Open.RW|Open.CE')
- self.assertEqual(repr(~Open.AC), 'Open.CE')
- self.assertEqual(repr(~(Open.RO | Open.CE)), 'Open.AC')
- self.assertEqual(repr(~(Open.WO | Open.CE)), 'Open.RW')
+ self.assertEqual(repr(Open.RO), '<Open.RO: 0>')
+ self.assertEqual(repr(Open.WO), '<Open.WO: 1>')
+ self.assertEqual(repr(Open.AC), '<Open.AC: 3>')
+ self.assertEqual(repr(Open.RO | Open.CE), '<Open.CE: 524288>')
+ self.assertEqual(repr(Open.WO | Open.CE), '<Open.CE|WO: 524289>')
+ self.assertEqual(repr(~Open.RO), '<Open.CE|AC|RW|WO: 524291>')
+ self.assertEqual(repr(~Open.WO), '<Open.CE|RW: 524290>')
+ self.assertEqual(repr(~Open.AC), '<Open.CE: 524288>')
+ self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC: 3>')
+ self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: 2>')
def test_format(self):
Perm = self.Perm
- self.assertEqual(format(Perm.R, ''), 'R')
- self.assertEqual(format(Perm.R | Perm.X, ''), 'R|X')
+ self.assertEqual(format(Perm.R, ''), 'Perm.R')
+ self.assertEqual(format(Perm.R | Perm.X, ''), 'Perm.R|X')
def test_or(self):
Perm = self.Perm
@@ -2748,49 +2309,6 @@ def test_bool(self):
for f in Open:
self.assertEqual(bool(f.value), bool(f))
- def test_boundary(self):
- self.assertIs(enum.Flag._boundary_, STRICT)
- class Iron(Flag, boundary=STRICT):
- ONE = 1
- TWO = 2
- EIGHT = 8
- self.assertIs(Iron._boundary_, STRICT)
- #
- class Water(Flag, boundary=CONFORM):
- ONE = 1
- TWO = 2
- EIGHT = 8
- self.assertIs(Water._boundary_, CONFORM)
- #
- class Space(Flag, boundary=EJECT):
- ONE = 1
- TWO = 2
- EIGHT = 8
- self.assertIs(Space._boundary_, EJECT)
- #
- class Bizarre(Flag, boundary=KEEP):
- b = 3
- c = 4
- d = 6
- #
- self.assertRaisesRegex(ValueError, 'invalid value: 7', Iron, 7)
- #
- self.assertIs(Water(7), Water.ONE|Water.TWO)
- self.assertIs(Water(~9), Water.TWO)
- #
- self.assertEqual(Space(7), 7)
- self.assertTrue(type(Space(7)) is int)
- #
- self.assertEqual(list(Bizarre), [Bizarre.c])
- self.assertIs(Bizarre(3), Bizarre.b)
- self.assertIs(Bizarre(6), Bizarre.d)
-
- def test_iter(self):
- Color = self.Color
- Open = self.Open
- self.assertEqual(list(Color), [Color.RED, Color.GREEN, Color.BLUE])
- self.assertEqual(list(Open), [Open.WO, Open.RW, Open.CE])
-
def test_programatic_function_string(self):
Perm = Flag('Perm', 'R W X')
lst = list(Perm)
@@ -2872,41 +2390,19 @@ def test_pickle(self):
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY|FlagStooges.MOE)
test_pickle_dump_load(self.assertIs, FlagStooges)
- @unittest.skipIf(
- python_version >= (3, 12),
- '__contains__ now returns True/False for all inputs',
- )
- def test_contains_er(self):
+ def test_contains(self):
Open = self.Open
Color = self.Color
self.assertFalse(Color.BLACK in Open)
self.assertFalse(Open.RO in Color)
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 'BLACK' in Color
+ 'BLACK' in Color
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 'RO' in Open
+ 'RO' in Open
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 1 in Color
+ 1 in Color
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 1 in Open
-
- @unittest.skipIf(
- python_version < (3, 12),
- '__contains__ only works with enum memmbers before 3.12',
- )
- def test_contains_tf(self):
- Open = self.Open
- Color = self.Color
- self.assertFalse(Color.BLACK in Open)
- self.assertFalse(Open.RO in Color)
- self.assertFalse('BLACK' in Color)
- self.assertFalse('RO' in Open)
- self.assertTrue(1 in Color)
- self.assertTrue(1 in Open)
+ 1 in Open
def test_member_contains(self):
Perm = self.Perm
@@ -2928,48 +2424,6 @@ def test_member_contains(self):
self.assertFalse(W in RX)
self.assertFalse(X in RW)
- def test_member_iter(self):
- Color = self.Color
- self.assertEqual(list(Color.BLACK), [])
- self.assertEqual(list(Color.PURPLE), [Color.RED, Color.BLUE])
- self.assertEqual(list(Color.BLUE), [Color.BLUE])
- self.assertEqual(list(Color.GREEN), [Color.GREEN])
- self.assertEqual(list(Color.WHITE), [Color.RED, Color.GREEN, Color.BLUE])
- self.assertEqual(list(Color.WHITE), [Color.RED, Color.GREEN, Color.BLUE])
-
- def test_member_length(self):
- self.assertEqual(self.Color.__len__(self.Color.BLACK), 0)
- self.assertEqual(self.Color.__len__(self.Color.GREEN), 1)
- self.assertEqual(self.Color.__len__(self.Color.PURPLE), 2)
- self.assertEqual(self.Color.__len__(self.Color.BLANCO), 3)
-
- def test_number_reset_and_order_cleanup(self):
- class Confused(Flag):
- _order_ = 'ONE TWO FOUR DOS EIGHT SIXTEEN'
- ONE = auto()
- TWO = auto()
- FOUR = auto()
- DOS = 2
- EIGHT = auto()
- SIXTEEN = auto()
- self.assertEqual(
- list(Confused),
- [Confused.ONE, Confused.TWO, Confused.FOUR, Confused.EIGHT, Confused.SIXTEEN])
- self.assertIs(Confused.TWO, Confused.DOS)
- self.assertEqual(Confused.DOS._value_, 2)
- self.assertEqual(Confused.EIGHT._value_, 8)
- self.assertEqual(Confused.SIXTEEN._value_, 16)
-
- def test_aliases(self):
- Color = self.Color
- self.assertEqual(Color(1).name, 'RED')
- self.assertEqual(Color['ROJO'].name, 'RED')
- self.assertEqual(Color(7).name, 'WHITE')
- self.assertEqual(Color['BLANCO'].name, 'WHITE')
- self.assertIs(Color.BLANCO, Color.WHITE)
- Open = self.Open
- self.assertIs(Open['AC'], Open.AC)
-
def test_auto_number(self):
class Color(Flag):
red = auto()
@@ -2987,6 +2441,20 @@ class Color(Flag):
red = 'not an int'
blue = auto()
+ def test_cascading_failure(self):
+ class Bizarre(Flag):
+ c = 3
+ d = 4
+ f = 6
+ # Bizarre.c | Bizarre.d
+ name = "TestFlag.test_cascading_failure.<locals>.Bizarre"
+ self.assertRaisesRegex(ValueError, "5 is not a valid " + name, Bizarre, 5)
+ self.assertRaisesRegex(ValueError, "5 is not a valid " + name, Bizarre, 5)
+ self.assertRaisesRegex(ValueError, "2 is not a valid " + name, Bizarre, 2)
+ self.assertRaisesRegex(ValueError, "2 is not a valid " + name, Bizarre, 2)
+ self.assertRaisesRegex(ValueError, "1 is not a valid " + name, Bizarre, 1)
+ self.assertRaisesRegex(ValueError, "1 is not a valid " + name, Bizarre, 1)
+
def test_duplicate_auto(self):
class Dupes(Enum):
first = primero = auto()
@@ -2994,6 +2462,13 @@ class Dupes(Enum):
third = auto()
self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
+ def test_bizarre(self):
+ class Bizarre(Flag):
+ b = 3
+ c = 4
+ d = 6
+ self.assertEqual(repr(Bizarre(7)), '<Bizarre.d|c|b: 7>')
+
def test_multiple_mixin(self):
class AllMixin:
@classproperty
@@ -3017,7 +2492,7 @@ class Color(AllMixin, Flag):
self.assertEqual(Color.GREEN.value, 2)
self.assertEqual(Color.BLUE.value, 4)
self.assertEqual(Color.ALL.value, 7)
- self.assertEqual(str(Color.BLUE), 'BLUE')
+ self.assertEqual(str(Color.BLUE), 'Color.BLUE')
class Color(AllMixin, StrMixin, Flag):
RED = auto()
GREEN = auto()
@@ -3116,9 +2591,9 @@ class TestIntFlag(unittest.TestCase):
"""Tests of the IntFlags."""
class Perm(IntFlag):
- R = 1 << 2
- W = 1 << 1
X = 1 << 0
+ W = 1 << 1
+ R = 1 << 2
class Open(IntFlag):
RO = 0
@@ -3130,17 +2605,9 @@ class Open(IntFlag):
class Color(IntFlag):
BLACK = 0
RED = 1
- ROJO = 1
GREEN = 2
BLUE = 4
PURPLE = RED|BLUE
- WHITE = RED|GREEN|BLUE
- BLANCO = RED|GREEN|BLUE
-
- class Skip(IntFlag):
- FIRST = 1
- SECOND = 2
- EIGHTH = 8
def test_type(self):
Perm = self.Perm
@@ -3160,83 +2627,74 @@ def test_type(self):
def test_str(self):
Perm = self.Perm
- self.assertEqual(str(Perm.R), 'R')
- self.assertEqual(str(Perm.W), 'W')
- self.assertEqual(str(Perm.X), 'X')
- self.assertEqual(str(Perm.R | Perm.W), 'R|W')
- self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'R|W|X')
- self.assertEqual(str(Perm.R | 8), '12')
- self.assertEqual(str(Perm(0)), 'Perm(0)')
- self.assertEqual(str(Perm(8)), '8')
- self.assertEqual(str(~Perm.R), 'W|X')
- self.assertEqual(str(~Perm.W), 'R|X')
- self.assertEqual(str(~Perm.X), 'R|W')
- self.assertEqual(str(~(Perm.R | Perm.W)), 'X')
- self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm(0)')
- self.assertEqual(str(~(Perm.R | 8)), '-13')
- self.assertEqual(str(Perm(~0)), 'R|W|X')
- self.assertEqual(str(Perm(~8)), '-9')
+ self.assertEqual(str(Perm.R), 'Perm.R')
+ self.assertEqual(str(Perm.W), 'Perm.W')
+ self.assertEqual(str(Perm.X), 'Perm.X')
+ self.assertEqual(str(Perm.R | Perm.W), 'Perm.R|W')
+ self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'Perm.R|W|X')
+ self.assertEqual(str(Perm.R | 8), 'Perm.8|R')
+ self.assertEqual(str(Perm(0)), 'Perm.0')
+ self.assertEqual(str(Perm(8)), 'Perm.8')
+ self.assertEqual(str(~Perm.R), 'Perm.W|X')
+ self.assertEqual(str(~Perm.W), 'Perm.R|X')
+ self.assertEqual(str(~Perm.X), 'Perm.R|W')
+ self.assertEqual(str(~(Perm.R | Perm.W)), 'Perm.X')
+ self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm.-8')
+ self.assertEqual(str(~(Perm.R | 8)), 'Perm.W|X')
+ self.assertEqual(str(Perm(~0)), 'Perm.R|W|X')
+ self.assertEqual(str(Perm(~8)), 'Perm.R|W|X')
Open = self.Open
- self.assertEqual(str(Open.RO), 'RO')
- self.assertEqual(str(Open.WO), 'WO')
- self.assertEqual(str(Open.AC), 'AC')
- self.assertEqual(str(Open.RO | Open.CE), 'CE')
- self.assertEqual(str(Open.WO | Open.CE), 'WO|CE')
- self.assertEqual(str(Open(4)), '4')
- self.assertEqual(str(~Open.RO), 'WO|RW|CE')
- self.assertEqual(str(~Open.WO), 'RW|CE')
- self.assertEqual(str(~Open.AC), 'CE')
- self.assertEqual(str(~(Open.RO | Open.CE)), 'AC')
- self.assertEqual(str(~(Open.WO | Open.CE)), 'RW')
- self.assertEqual(str(Open(~4)), '-5')
+ self.assertEqual(str(Open.RO), 'Open.RO')
+ self.assertEqual(str(Open.WO), 'Open.WO')
+ self.assertEqual(str(Open.AC), 'Open.AC')
+ self.assertEqual(str(Open.RO | Open.CE), 'Open.CE')
+ self.assertEqual(str(Open.WO | Open.CE), 'Open.CE|WO')
+ self.assertEqual(str(Open(4)), 'Open.4')
+ self.assertEqual(str(~Open.RO), 'Open.CE|AC|RW|WO')
+ self.assertEqual(str(~Open.WO), 'Open.CE|RW')
+ self.assertEqual(str(~Open.AC), 'Open.CE')
+ self.assertEqual(str(~(Open.RO | Open.CE)), 'Open.AC|RW|WO')
+ self.assertEqual(str(~(Open.WO | Open.CE)), 'Open.RW')
+ self.assertEqual(str(Open(~4)), 'Open.CE|AC|RW|WO')
def test_repr(self):
Perm = self.Perm
- self.assertEqual(repr(Perm.R), 'Perm.R')
- self.assertEqual(repr(Perm.W), 'Perm.W')
- self.assertEqual(repr(Perm.X), 'Perm.X')
- self.assertEqual(repr(Perm.R | Perm.W), 'Perm.R|Perm.W')
- self.assertEqual(repr(Perm.R | Perm.W | Perm.X), 'Perm.R|Perm.W|Perm.X')
- self.assertEqual(repr(Perm.R | 8), '12')
- self.assertEqual(repr(Perm(0)), '0x0')
- self.assertEqual(repr(Perm(8)), '8')
- self.assertEqual(repr(~Perm.R), 'Perm.W|Perm.X')
- self.assertEqual(repr(~Perm.W), 'Perm.R|Perm.X')
- self.assertEqual(repr(~Perm.X), 'Perm.R|Perm.W')
- self.assertEqual(repr(~(Perm.R | Perm.W)), 'Perm.X')
- self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '0x0')
- self.assertEqual(repr(~(Perm.R | 8)), '-13')
- self.assertEqual(repr(Perm(~0)), 'Perm.R|Perm.W|Perm.X')
- self.assertEqual(repr(Perm(~8)), '-9')
+ self.assertEqual(repr(Perm.R), '<Perm.R: 4>')
+ self.assertEqual(repr(Perm.W), '<Perm.W: 2>')
+ self.assertEqual(repr(Perm.X), '<Perm.X: 1>')
+ self.assertEqual(repr(Perm.R | Perm.W), '<Perm.R|W: 6>')
+ self.assertEqual(repr(Perm.R | Perm.W | Perm.X), '<Perm.R|W|X: 7>')
+ self.assertEqual(repr(Perm.R | 8), '<Perm.8|R: 12>')
+ self.assertEqual(repr(Perm(0)), '<Perm.0: 0>')
+ self.assertEqual(repr(Perm(8)), '<Perm.8: 8>')
+ self.assertEqual(repr(~Perm.R), '<Perm.W|X: -5>')
+ self.assertEqual(repr(~Perm.W), '<Perm.R|X: -3>')
+ self.assertEqual(repr(~Perm.X), '<Perm.R|W: -2>')
+ self.assertEqual(repr(~(Perm.R | Perm.W)), '<Perm.X: -7>')
+ self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '<Perm.-8: -8>')
+ self.assertEqual(repr(~(Perm.R | 8)), '<Perm.W|X: -13>')
+ self.assertEqual(repr(Perm(~0)), '<Perm.R|W|X: -1>')
+ self.assertEqual(repr(Perm(~8)), '<Perm.R|W|X: -9>')
Open = self.Open
- self.assertEqual(repr(Open.RO), 'Open.RO')
- self.assertEqual(repr(Open.WO), 'Open.WO')
- self.assertEqual(repr(Open.AC), 'Open.AC')
- self.assertEqual(repr(Open.RO | Open.CE), 'Open.CE')
- self.assertEqual(repr(Open.WO | Open.CE), 'Open.WO|Open.CE')
- self.assertEqual(repr(Open(4)), '4')
- self.assertEqual(repr(~Open.RO), 'Open.WO|Open.RW|Open.CE')
- self.assertEqual(repr(~Open.WO), 'Open.RW|Open.CE')
- self.assertEqual(repr(~Open.AC), 'Open.CE')
- self.assertEqual(repr(~(Open.RO | Open.CE)), 'Open.AC')
- self.assertEqual(repr(~(Open.WO | Open.CE)), 'Open.RW')
- self.assertEqual(repr(Open(~4)), '-5')
+ self.assertEqual(repr(Open.RO), '<Open.RO: 0>')
+ self.assertEqual(repr(Open.WO), '<Open.WO: 1>')
+ self.assertEqual(repr(Open.AC), '<Open.AC: 3>')
+ self.assertEqual(repr(Open.RO | Open.CE), '<Open.CE: 524288>')
+ self.assertEqual(repr(Open.WO | Open.CE), '<Open.CE|WO: 524289>')
+ self.assertEqual(repr(Open(4)), '<Open.4: 4>')
+ self.assertEqual(repr(~Open.RO), '<Open.CE|AC|RW|WO: -1>')
+ self.assertEqual(repr(~Open.WO), '<Open.CE|RW: -2>')
+ self.assertEqual(repr(~Open.AC), '<Open.CE: -4>')
+ self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC|RW|WO: -524289>')
+ self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: -524290>')
+ self.assertEqual(repr(Open(~4)), '<Open.CE|AC|RW|WO: -5>')
def test_format(self):
Perm = self.Perm
self.assertEqual(format(Perm.R, ''), '4')
self.assertEqual(format(Perm.R | Perm.X, ''), '5')
- #
- class NewPerm(IntFlag):
- R = 1 << 2
- W = 1 << 1
- X = 1 << 0
- def __str__(self):
- return self._name_
- self.assertEqual(format(NewPerm.R, ''), 'R')
- self.assertEqual(format(NewPerm.R | Perm.X, ''), 'R|X')
def test_or(self):
Perm = self.Perm
@@ -3314,7 +2772,8 @@ def test_invert(self):
RWX = Perm.R | Perm.W | Perm.X
values = list(Perm) + [RW, RX, WX, RWX, Perm(0)]
for i in values:
- self.assertEqual(~i, (~i).value)
+ self.assertEqual(~i, ~i.value)
+ self.assertEqual((~i).value, ~i.value)
self.assertIs(type(~i), Perm)
self.assertEqual(~~i, i)
for i in Perm:
@@ -3323,50 +2782,6 @@ def test_invert(self):
self.assertIs(Open.WO & ~Open.WO, Open.RO)
self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)
- def test_boundary(self):
- self.assertIs(enum.IntFlag._boundary_, EJECT)
- class Iron(IntFlag, boundary=STRICT):
- ONE = 1
- TWO = 2
- EIGHT = 8
- self.assertIs(Iron._boundary_, STRICT)
- #
- class Water(IntFlag, boundary=CONFORM):
- ONE = 1
- TWO = 2
- EIGHT = 8
- self.assertIs(Water._boundary_, CONFORM)
- #
- class Space(IntFlag, boundary=EJECT):
- ONE = 1
- TWO = 2
- EIGHT = 8
- self.assertIs(Space._boundary_, EJECT)
- #
- #
- class Bizarre(IntFlag, boundary=KEEP):
- b = 3
- c = 4
- d = 6
- #
- self.assertRaisesRegex(ValueError, 'invalid value: 5', Iron, 5)
- #
- self.assertIs(Water(7), Water.ONE|Water.TWO)
- self.assertIs(Water(~9), Water.TWO)
- #
- self.assertEqual(Space(7), 7)
- self.assertTrue(type(Space(7)) is int)
- #
- self.assertEqual(list(Bizarre), [Bizarre.c])
- self.assertIs(Bizarre(3), Bizarre.b)
- self.assertIs(Bizarre(6), Bizarre.d)
-
- def test_iter(self):
- Color = self.Color
- Open = self.Open
- self.assertEqual(list(Color), [Color.RED, Color.GREEN, Color.BLUE])
- self.assertEqual(list(Open), [Open.WO, Open.RW, Open.CE])
-
def test_programatic_function_string(self):
Perm = IntFlag('Perm', 'R W X')
lst = list(Perm)
@@ -3468,11 +2883,7 @@ def test_programatic_function_from_empty_tuple(self):
self.assertEqual(len(lst), len(Thing))
self.assertEqual(len(Thing), 0, Thing)
- @unittest.skipIf(
- python_version >= (3, 12),
- '__contains__ now returns True/False for all inputs',
- )
- def test_contains_er(self):
+ def test_contains(self):
Open = self.Open
Color = self.Color
self.assertTrue(Color.GREEN in Color)
@@ -3480,33 +2891,13 @@ def test_contains_er(self):
self.assertFalse(Color.GREEN in Open)
self.assertFalse(Open.RW in Color)
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 'GREEN' in Color
+ 'GREEN' in Color
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 'RW' in Open
+ 'RW' in Open
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 2 in Color
+ 2 in Color
with self.assertRaises(TypeError):
- with self.assertWarns(DeprecationWarning):
- 2 in Open
-
- @unittest.skipIf(
- python_version < (3, 12),
- '__contains__ only works with enum memmbers before 3.12',
- )
- def test_contains_tf(self):
- Open = self.Open
- Color = self.Color
- self.assertTrue(Color.GREEN in Color)
- self.assertTrue(Open.RW in Open)
- self.assertTrue(Color.GREEN in Open)
- self.assertTrue(Open.RW in Color)
- self.assertFalse('GREEN' in Color)
- self.assertFalse('RW' in Open)
- self.assertTrue(2 in Color)
- self.assertTrue(2 in Open)
+ 2 in Open
def test_member_contains(self):
Perm = self.Perm
@@ -3530,30 +2921,6 @@ def test_member_contains(self):
with self.assertRaises(TypeError):
self.assertFalse('test' in RW)
- def test_member_iter(self):
- Color = self.Color
- self.assertEqual(list(Color.BLACK), [])
- self.assertEqual(list(Color.PURPLE), [Color.RED, Color.BLUE])
- self.assertEqual(list(Color.BLUE), [Color.BLUE])
- self.assertEqual(list(Color.GREEN), [Color.GREEN])
- self.assertEqual(list(Color.WHITE), [Color.RED, Color.GREEN, Color.BLUE])
-
- def test_member_length(self):
- self.assertEqual(self.Color.__len__(self.Color.BLACK), 0)
- self.assertEqual(self.Color.__len__(self.Color.GREEN), 1)
- self.assertEqual(self.Color.__len__(self.Color.PURPLE), 2)
- self.assertEqual(self.Color.__len__(self.Color.BLANCO), 3)
-
- def test_aliases(self):
- Color = self.Color
- self.assertEqual(Color(1).name, 'RED')
- self.assertEqual(Color['ROJO'].name, 'RED')
- self.assertEqual(Color(7).name, 'WHITE')
- self.assertEqual(Color['BLANCO'].name, 'WHITE')
- self.assertIs(Color.BLANCO, Color.WHITE)
- Open = self.Open
- self.assertIs(Open['AC'], Open.AC)
-
def test_bool(self):
Perm = self.Perm
for f in Perm:
@@ -3562,7 +2929,6 @@ def test_bool(self):
for f in Open:
self.assertEqual(bool(f.value), bool(f))
-
def test_multiple_mixin(self):
class AllMixin:
@classproperty
@@ -3586,7 +2952,7 @@ class Color(AllMixin, IntFlag):
self.assertEqual(Color.GREEN.value, 2)
self.assertEqual(Color.BLUE.value, 4)
self.assertEqual(Color.ALL.value, 7)
- self.assertEqual(str(Color.BLUE), 'BLUE')
+ self.assertEqual(str(Color.BLUE), 'Color.BLUE')
class Color(AllMixin, StrMixin, IntFlag):
RED = auto()
GREEN = auto()
@@ -3670,7 +3036,6 @@ class Clean(Enum):
one = 1
two = 'dos'
tres = 4.0
- #
@unique
class Cleaner(IntEnum):
single = 1
@@ -3696,178 +3061,25 @@ class Dirtier(IntEnum):
turkey = 3
def test_unique_with_name(self):
- @verify(UNIQUE)
- class Silly(Enum):
- one = 1
- two = 'dos'
- name = 3
- #
- @verify(UNIQUE)
- class Sillier(IntEnum):
- single = 1
- name = 2
- triple = 3
- value = 4
-
-class TestVerify(unittest.TestCase):
-
- def test_continuous(self):
- @verify(CONTINUOUS)
- class Auto(Enum):
- FIRST = auto()
- SECOND = auto()
- THIRD = auto()
- FORTH = auto()
- #
- @verify(CONTINUOUS)
- class Manual(Enum):
- FIRST = 3
- SECOND = 4
- THIRD = 5
- FORTH = 6
- #
- with self.assertRaisesRegex(ValueError, 'invalid enum .Missing.: missing values 5, 6, 7, 8, 9, 10, 12'):
- @verify(CONTINUOUS)
- class Missing(Enum):
- FIRST = 3
- SECOND = 4
- THIRD = 11
- FORTH = 13
- #
- with self.assertRaisesRegex(ValueError, 'invalid flag .Incomplete.: missing values 32'):
- @verify(CONTINUOUS)
- class Incomplete(Flag):
- FIRST = 4
- SECOND = 8
- THIRD = 16
- FORTH = 64
- #
- with self.assertRaisesRegex(ValueError, 'invalid flag .StillIncomplete.: missing values 16'):
- @verify(CONTINUOUS)
- class StillIncomplete(Flag):
- FIRST = 4
- SECOND = 8
- THIRD = 11
- FORTH = 32
-
-
- def test_composite(self):
- class Bizarre(Flag):
- b = 3
- c = 4
- d = 6
- self.assertEqual(list(Bizarre), [Bizarre.c])
- self.assertEqual(Bizarre.b.value, 3)
- self.assertEqual(Bizarre.c.value, 4)
- self.assertEqual(Bizarre.d.value, 6)
- with self.assertRaisesRegex(
- ValueError,
- "invalid Flag 'Bizarre': aliases b and d are missing combined values of 0x3 .use enum.show_flag_values.value. for details.",
- ):
- @verify(NAMED_FLAGS)
- class Bizarre(Flag):
- b = 3
- c = 4
- d = 6
- #
- self.assertEqual(enum.show_flag_values(3), [1, 2])
- class Bizarre(IntFlag):
- b = 3
- c = 4
- d = 6
- self.assertEqual(list(Bizarre), [Bizarre.c])
- self.assertEqual(Bizarre.b.value, 3)
- self.assertEqual(Bizarre.c.value, 4)
- self.assertEqual(Bizarre.d.value, 6)
- with self.assertRaisesRegex(
- ValueError,
- "invalid Flag 'Bizarre': alias d is missing value 0x2 .use enum.show_flag_values.value. for details.",
- ):
- @verify(NAMED_FLAGS)
- class Bizarre(IntFlag):
- c = 4
- d = 6
- self.assertEqual(enum.show_flag_values(2), [2])
-
- def test_unique_clean(self):
- @verify(UNIQUE)
- class Clean(Enum):
- one = 1
- two = 'dos'
- tres = 4.0
- #
- @verify(UNIQUE)
- class Cleaner(IntEnum):
- single = 1
- double = 2
- triple = 3
-
- def test_unique_dirty(self):
- with self.assertRaisesRegex(ValueError, 'tres.*one'):
- @verify(UNIQUE)
- class Dirty(Enum):
- one = 1
- two = 'dos'
- tres = 1
- with self.assertRaisesRegex(
- ValueError,
- 'double.*single.*turkey.*triple',
- ):
- @verify(UNIQUE)
- class Dirtier(IntEnum):
- single = 1
- double = 1
- triple = 3
- turkey = 3
-
- def test_unique_with_name(self):
- @verify(UNIQUE)
+ @unique
class Silly(Enum):
one = 1
two = 'dos'
name = 3
- #
- @verify(UNIQUE)
+ @unique
class Sillier(IntEnum):
single = 1
name = 2
triple = 3
value = 4
-class TestHelpers(unittest.TestCase):
- sunder_names = '_bad_', '_good_', '_what_ho_'
- dunder_names = '__mal__', '__bien__', '__que_que__'
- private_names = '_MyEnum__private', '_MyEnum__still_private'
- private_and_sunder_names = '_MyEnum__private_', '_MyEnum__also_private_'
- random_names = 'okay', '_semi_private', '_weird__', '_MyEnum__'
-
- def test_sunder(self):
- for name in self.sunder_names + self.private_and_sunder_names:
- self.assertTrue(enum._is_sunder(name), '%r is a not sunder name?' % name)
- for name in self.dunder_names + self.private_names + self.random_names:
- self.assertFalse(enum._is_sunder(name), '%r is a sunder name?' % name)
-
- def test_dunder(self):
- for name in self.dunder_names:
- self.assertTrue(enum._is_dunder(name), '%r is a not dunder name?' % name)
- for name in self.sunder_names + self.private_names + self.private_and_sunder_names + self.random_names:
- self.assertFalse(enum._is_dunder(name), '%r is a dunder name?' % name)
-
- def test_is_private(self):
- for name in self.private_names + self.private_and_sunder_names:
- self.assertTrue(enum._is_private('MyEnum', name), '%r is a not private name?')
- for name in self.sunder_names + self.dunder_names + self.random_names:
- self.assertFalse(enum._is_private('MyEnum', name), '%r is a private name?')
-
-class TestEnumTypeSubclassing(unittest.TestCase):
- pass
expected_help_output_with_docs = """\
Help on class Color in module %s:
class Color(enum.Enum)
- | Color(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
+ | Color(value, names=None, *, module=None, qualname=None, type=None, start=1)
|\x20\x20
| An enumeration.
|\x20\x20
@@ -3878,11 +3090,11 @@ class Color(enum.Enum)
|\x20\x20
| Data and other attributes defined here:
|\x20\x20
- | blue = Color.blue
+ | blue = <Color.blue: 3>
|\x20\x20
- | green = Color.green
+ | green = <Color.green: 2>
|\x20\x20
- | red = Color.red
+ | red = <Color.red: 1>
|\x20\x20
| ----------------------------------------------------------------------
| Data descriptors inherited from enum.Enum:
@@ -3894,7 +3106,7 @@ class Color(enum.Enum)
| The value of the Enum member.
|\x20\x20
| ----------------------------------------------------------------------
- | Readonly properties inherited from enum.EnumType:
+ | Readonly properties inherited from enum.EnumMeta:
|\x20\x20
| __members__
| Returns a mapping of member name->value.
@@ -3915,11 +3127,11 @@ class Color(enum.Enum)
|\x20\x20
| Data and other attributes defined here:
|\x20\x20
- | blue = Color.blue
+ | blue = <Color.blue: 3>
|\x20\x20
- | green = Color.green
+ | green = <Color.green: 2>
|\x20\x20
- | red = Color.red
+ | red = <Color.red: 1>
|\x20\x20
| ----------------------------------------------------------------------
| Data descriptors inherited from enum.Enum:
@@ -3929,7 +3141,7 @@ class Color(enum.Enum)
| value
|\x20\x20
| ----------------------------------------------------------------------
- | Data descriptors inherited from enum.EnumType:
+ | Data descriptors inherited from enum.EnumMeta:
|\x20\x20
| __members__"""
@@ -3956,7 +3168,7 @@ def test_pydoc(self):
def test_inspect_getmembers(self):
values = dict((
- ('__class__', EnumType),
+ ('__class__', EnumMeta),
('__doc__', 'An enumeration.'),
('__members__', self.Color.__members__),
('__module__', __name__),
@@ -3967,7 +3179,7 @@ def test_inspect_getmembers(self):
('value', Enum.__dict__['value']),
))
result = dict(inspect.getmembers(self.Color))
- self.assertEqual(set(values.keys()), set(result.keys()))
+ self.assertEqual(values.keys(), result.keys())
failed = False
for k in values.keys():
if result[k] != values[k]:
@@ -3983,11 +3195,11 @@ def test_inspect_classify_class_attrs(self):
from inspect import Attribute
values = [
Attribute(name='__class__', kind='data',
- defining_class=object, object=EnumType),
+ defining_class=object, object=EnumMeta),
Attribute(name='__doc__', kind='data',
defining_class=self.Color, object='An enumeration.'),
Attribute(name='__members__', kind='property',
- defining_class=EnumType, object=EnumType.__members__),
+ defining_class=EnumMeta, object=EnumMeta.__members__),
Attribute(name='__module__', kind='data',
defining_class=self.Color, object=__name__),
Attribute(name='blue', kind='data',
@@ -4004,10 +3216,6 @@ def test_inspect_classify_class_attrs(self):
values.sort(key=lambda item: item.name)
result = list(inspect.classify_class_attrs(self.Color))
result.sort(key=lambda item: item.name)
- self.assertEqual(
- len(values), len(result),
- "%s != %s" % ([a.name for a in values], [a.name for a in result])
- )
failed = False
for v, r in zip(values, result):
if r != v:
@@ -4016,45 +3224,10 @@ def test_inspect_classify_class_attrs(self):
if failed:
self.fail("result does not equal expected, see print above")
- def test_test_simple_enum(self):
- @_simple_enum(Enum)
- class SimpleColor:
- RED = 1
- GREEN = 2
- BLUE = 3
- class CheckedColor(Enum):
- RED = 1
- GREEN = 2
- BLUE = 3
- self.assertTrue(_test_simple_enum(CheckedColor, SimpleColor) is None)
- SimpleColor.GREEN._value_ = 9
- self.assertRaisesRegex(
- TypeError, "enum mismatch",
- _test_simple_enum, CheckedColor, SimpleColor,
- )
- class CheckedMissing(IntFlag, boundary=KEEP):
- SIXTY_FOUR = 64
- ONE_TWENTY_EIGHT = 128
- TWENTY_FORTY_EIGHT = 2048
- ALL = 2048 + 128 + 64 + 12
- CM = CheckedMissing
- self.assertEqual(list(CheckedMissing), [CM.SIXTY_FOUR, CM.ONE_TWENTY_EIGHT, CM.TWENTY_FORTY_EIGHT])
- #
- @_simple_enum(IntFlag, boundary=KEEP)
- class Missing:
- SIXTY_FOUR = 64
- ONE_TWENTY_EIGHT = 128
- TWENTY_FORTY_EIGHT = 2048
- ALL = 2048 + 128 + 64 + 12
- M = Missing
- self.assertEqual(list(CheckedMissing), [M.SIXTY_FOUR, M.ONE_TWENTY_EIGHT, M.TWENTY_FORTY_EIGHT])
- #
- _test_simple_enum(CheckedMissing, Missing)
-
class MiscTestCase(unittest.TestCase):
def test__all__(self):
- support.check__all__(self, enum, not_exported={'bin', 'show_flag_values'})
+ check__all__(self, enum)
# These are unordered here on purpose to ensure that declaration order
@@ -4066,22 +3239,7 @@ def test__all__(self):
CONVERT_TEST_NAME_E = 5
CONVERT_TEST_NAME_F = 5
-CONVERT_STRING_TEST_NAME_D = 5
-CONVERT_STRING_TEST_NAME_C = 5
-CONVERT_STRING_TEST_NAME_B = 5
-CONVERT_STRING_TEST_NAME_A = 5 # This one should sort first.
-CONVERT_STRING_TEST_NAME_E = 5
-CONVERT_STRING_TEST_NAME_F = 5
-
class TestIntEnumConvert(unittest.TestCase):
- def setUp(self):
- # Reset the module-level test variables to their original integer
- # values, otherwise the already created enum values get converted
- # instead.
- for suffix in ['A', 'B', 'C', 'D', 'E', 'F']:
- globals()[f'CONVERT_TEST_NAME_{suffix}'] = 5
- globals()[f'CONVERT_STRING_TEST_NAME_{suffix}'] = 5
-
def test_convert_value_lookup_priority(self):
test_type = enum.IntEnum._convert_(
'UnittestConvert',
@@ -4109,7 +3267,7 @@ def test_convert(self):
if name[0:2] not in ('CO', '__')],
[], msg='Names other than CONVERT_TEST_* found.')
- @unittest.skipUnless(python_version == (3, 8),
+ @unittest.skipUnless(sys.version_info[:2] == (3, 8),
'_convert was deprecated in 3.8')
def test_convert_warn(self):
with self.assertWarns(DeprecationWarning):
@@ -4118,7 +3276,7 @@ def test_convert_warn(self):
('test.test_enum', '__main__')[__name__=='__main__'],
filter=lambda x: x.startswith('CONVERT_TEST_'))
- @unittest.skipUnless(python_version >= (3, 9),
+ @unittest.skipUnless(sys.version_info >= (3, 9),
'_convert was removed in 3.9')
def test_convert_raise(self):
with self.assertRaises(AttributeError):
@@ -4127,50 +3285,6 @@ def test_convert_raise(self):
('test.test_enum', '__main__')[__name__=='__main__'],
filter=lambda x: x.startswith('CONVERT_TEST_'))
- def test_convert_repr_and_str(self):
- module = ('test.test_enum', '__main__')[__name__=='__main__']
- test_type = enum.IntEnum._convert_(
- 'UnittestConvert',
- module,
- filter=lambda x: x.startswith('CONVERT_STRING_TEST_'))
- self.assertEqual(repr(test_type.CONVERT_STRING_TEST_NAME_A), '%s.CONVERT_STRING_TEST_NAME_A' % module)
- self.assertEqual(str(test_type.CONVERT_STRING_TEST_NAME_A), 'CONVERT_STRING_TEST_NAME_A')
- self.assertEqual(format(test_type.CONVERT_STRING_TEST_NAME_A), '5')
-
-# global names for StrEnum._convert_ test
-CONVERT_STR_TEST_2 = 'goodbye'
-CONVERT_STR_TEST_1 = 'hello'
-
-class TestStrEnumConvert(unittest.TestCase):
- def setUp(self):
- global CONVERT_STR_TEST_1
- global CONVERT_STR_TEST_2
- CONVERT_STR_TEST_2 = 'goodbye'
- CONVERT_STR_TEST_1 = 'hello'
-
- def test_convert(self):
- test_type = enum.StrEnum._convert_(
- 'UnittestConvert',
- ('test.test_enum', '__main__')[__name__=='__main__'],
- filter=lambda x: x.startswith('CONVERT_STR_'))
- # Ensure that test_type has all of the desired names and values.
- self.assertEqual(test_type.CONVERT_STR_TEST_1, 'hello')
- self.assertEqual(test_type.CONVERT_STR_TEST_2, 'goodbye')
- # Ensure that test_type only picked up names matching the filter.
- self.assertEqual([name for name in dir(test_type)
- if name[0:2] not in ('CO', '__')],
- [], msg='Names other than CONVERT_STR_* found.')
-
- def test_convert_repr_and_str(self):
- module = ('test.test_enum', '__main__')[__name__=='__main__']
- test_type = enum.StrEnum._convert_(
- 'UnittestConvert',
- module,
- filter=lambda x: x.startswith('CONVERT_STR_'))
- self.assertEqual(repr(test_type.CONVERT_STR_TEST_1), '%s.CONVERT_STR_TEST_1' % module)
- self.assertEqual(str(test_type.CONVERT_STR_TEST_2), 'goodbye')
- self.assertEqual(format(test_type.CONVERT_STR_TEST_1), 'hello')
-
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 8265b8d1d6d2d..77152cf64565e 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -1,4 +1,3 @@
-import enum
import errno
from http import client, HTTPStatus
import io
@@ -525,150 +524,6 @@ def test_dir_with_added_behavior_on_status(self):
# see issue40084
self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))
- def test_simple_httpstatus(self):
- class CheckedHTTPStatus(enum.IntEnum):
- """HTTP status codes and reason phrases
-
- Status codes from the following RFCs are all observed:
-
- * RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616
- * RFC 6585: Additional HTTP Status Codes
- * RFC 3229: Delta encoding in HTTP
- * RFC 4918: HTTP Extensions for WebDAV, obsoletes 2518
- * RFC 5842: Binding Extensions to WebDAV
- * RFC 7238: Permanent Redirect
- * RFC 2295: Transparent Content Negotiation in HTTP
- * RFC 2774: An HTTP Extension Framework
- * RFC 7725: An HTTP Status Code to Report Legal Obstacles
- * RFC 7540: Hypertext Transfer Protocol Version 2 (HTTP/2)
- * RFC 2324: Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0)
- * RFC 8297: An HTTP Status Code for Indicating Hints
- * RFC 8470: Using Early Data in HTTP
- """
- def __new__(cls, value, phrase, description=''):
- obj = int.__new__(cls, value)
- obj._value_ = value
-
- obj.phrase = phrase
- obj.description = description
- return obj
- # informational
- CONTINUE = 100, 'Continue', 'Request received, please continue'
- SWITCHING_PROTOCOLS = (101, 'Switching Protocols',
- 'Switching to new protocol; obey Upgrade header')
- PROCESSING = 102, 'Processing'
- EARLY_HINTS = 103, 'Early Hints'
- # success
- OK = 200, 'OK', 'Request fulfilled, document follows'
- CREATED = 201, 'Created', 'Document created, URL follows'
- ACCEPTED = (202, 'Accepted',
- 'Request accepted, processing continues off-line')
- NON_AUTHORITATIVE_INFORMATION = (203,
- 'Non-Authoritative Information', 'Request fulfilled from cache')
- NO_CONTENT = 204, 'No Content', 'Request fulfilled, nothing follows'
- RESET_CONTENT = 205, 'Reset Content', 'Clear input form for further input'
- PARTIAL_CONTENT = 206, 'Partial Content', 'Partial content follows'
- MULTI_STATUS = 207, 'Multi-Status'
- ALREADY_REPORTED = 208, 'Already Reported'
- IM_USED = 226, 'IM Used'
- # redirection
- MULTIPLE_CHOICES = (300, 'Multiple Choices',
- 'Object has several resources -- see URI list')
- MOVED_PERMANENTLY = (301, 'Moved Permanently',
- 'Object moved permanently -- see URI list')
- FOUND = 302, 'Found', 'Object moved temporarily -- see URI list'
- SEE_OTHER = 303, 'See Other', 'Object moved -- see Method and URL list'
- NOT_MODIFIED = (304, 'Not Modified',
- 'Document has not changed since given time')
- USE_PROXY = (305, 'Use Proxy',
- 'You must use proxy specified in Location to access this resource')
- TEMPORARY_REDIRECT = (307, 'Temporary Redirect',
- 'Object moved temporarily -- see URI list')
- PERMANENT_REDIRECT = (308, 'Permanent Redirect',
- 'Object moved permanently -- see URI list')
- # client error
- BAD_REQUEST = (400, 'Bad Request',
- 'Bad request syntax or unsupported method')
- UNAUTHORIZED = (401, 'Unauthorized',
- 'No permission -- see authorization schemes')
- PAYMENT_REQUIRED = (402, 'Payment Required',
- 'No payment -- see charging schemes')
- FORBIDDEN = (403, 'Forbidden',
- 'Request forbidden -- authorization will not help')
- NOT_FOUND = (404, 'Not Found',
- 'Nothing matches the given URI')
- METHOD_NOT_ALLOWED = (405, 'Method Not Allowed',
- 'Specified method is invalid for this resource')
- NOT_ACCEPTABLE = (406, 'Not Acceptable',
- 'URI not available in preferred format')
- PROXY_AUTHENTICATION_REQUIRED = (407,
- 'Proxy Authentication Required',
- 'You must authenticate with this proxy before proceeding')
- REQUEST_TIMEOUT = (408, 'Request Timeout',
- 'Request timed out; try again later')
- CONFLICT = 409, 'Conflict', 'Request conflict'
- GONE = (410, 'Gone',
- 'URI no longer exists and has been permanently removed')
- LENGTH_REQUIRED = (411, 'Length Required',
- 'Client must specify Content-Length')
- PRECONDITION_FAILED = (412, 'Precondition Failed',
- 'Precondition in headers is false')
- REQUEST_ENTITY_TOO_LARGE = (413, 'Request Entity Too Large',
- 'Entity is too large')
- REQUEST_URI_TOO_LONG = (414, 'Request-URI Too Long',
- 'URI is too long')
- UNSUPPORTED_MEDIA_TYPE = (415, 'Unsupported Media Type',
- 'Entity body in unsupported format')
- REQUESTED_RANGE_NOT_SATISFIABLE = (416,
- 'Requested Range Not Satisfiable',
- 'Cannot satisfy request range')
- EXPECTATION_FAILED = (417, 'Expectation Failed',
- 'Expect condition could not be satisfied')
- IM_A_TEAPOT = (418, 'I\'m a Teapot',
- 'Server refuses to brew coffee because it is a teapot.')
- MISDIRECTED_REQUEST = (421, 'Misdirected Request',
- 'Server is not able to produce a response')
- UNPROCESSABLE_ENTITY = 422, 'Unprocessable Entity'
- LOCKED = 423, 'Locked'
- FAILED_DEPENDENCY = 424, 'Failed Dependency'
- TOO_EARLY = 425, 'Too Early'
- UPGRADE_REQUIRED = 426, 'Upgrade Required'
- PRECONDITION_REQUIRED = (428, 'Precondition Required',
- 'The origin server requires the request to be conditional')
- TOO_MANY_REQUESTS = (429, 'Too Many Requests',
- 'The user has sent too many requests in '
- 'a given amount of time ("rate limiting")')
- REQUEST_HEADER_FIELDS_TOO_LARGE = (431,
- 'Request Header Fields Too Large',
- 'The server is unwilling to process the request because its header '
- 'fields are too large')
- UNAVAILABLE_FOR_LEGAL_REASONS = (451,
- 'Unavailable For Legal Reasons',
- 'The server is denying access to the '
- 'resource as a consequence of a legal demand')
- # server errors
- INTERNAL_SERVER_ERROR = (500, 'Internal Server Error',
- 'Server got itself in trouble')
- NOT_IMPLEMENTED = (501, 'Not Implemented',
- 'Server does not support this operation')
- BAD_GATEWAY = (502, 'Bad Gateway',
- 'Invalid responses from another server/proxy')
- SERVICE_UNAVAILABLE = (503, 'Service Unavailable',
- 'The server cannot process the request due to a high load')
- GATEWAY_TIMEOUT = (504, 'Gateway Timeout',
- 'The gateway server did not receive a timely response')
- HTTP_VERSION_NOT_SUPPORTED = (505, 'HTTP Version Not Supported',
- 'Cannot fulfill request')
- VARIANT_ALSO_NEGOTIATES = 506, 'Variant Also Negotiates'
- INSUFFICIENT_STORAGE = 507, 'Insufficient Storage'
- LOOP_DETECTED = 508, 'Loop Detected'
- NOT_EXTENDED = 510, 'Not Extended'
- NETWORK_AUTHENTICATION_REQUIRED = (511,
- 'Network Authentication Required',
- 'The client needs to authenticate to gain network access')
- enum._test_simple_enum(CheckedHTTPStatus, HTTPStatus)
-
-
def test_status_lines(self):
# Test HTTP status lines
diff --git a/Lib/test/test_pstats.py b/Lib/test/test_pstats.py
index acc2fa5385d92..c9a5fd5d0ed60 100644
--- a/Lib/test/test_pstats.py
+++ b/Lib/test/test_pstats.py
@@ -3,7 +3,6 @@
from test import support
from io import StringIO
from pstats import SortKey
-from enum import StrEnum, _test_simple_enum
import pstats
import cProfile
@@ -62,32 +61,6 @@ def test_sort_stats_partial(self):
self.assertEqual(self.stats.sort_type,
self.stats.sort_arg_dict_default[sortkey][-1])
- def test_sort_stats_enum(self):
- for member in SortKey:
- self.stats.sort_stats(member)
- self.assertEqual(
- self.stats.sort_type,
- self.stats.sort_arg_dict_default[member.value][-1])
- class CheckedSortKey(StrEnum):
- CALLS = 'calls', 'ncalls'
- CUMULATIVE = 'cumulative', 'cumtime'
- FILENAME = 'filename', 'module'
- LINE = 'line'
- NAME = 'name'
- NFL = 'nfl'
- PCALLS = 'pcalls'
- STDNAME = 'stdname'
- TIME = 'time', 'tottime'
- def __new__(cls, *values):
- value = values[0]
- obj = str.__new__(cls, value)
- obj._value_ = value
- for other_value in values[1:]:
- cls._value2member_map_[other_value] = obj
- obj._all_values = values
- return obj
- _test_simple_enum(CheckedSortKey, SortKey)
-
def test_sort_starts_mix(self):
self.assertRaises(TypeError, self.stats.sort_stats,
'calls',
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
index c862a805bbcce..0afa3e6fce181 100644
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -453,7 +453,7 @@ class BinaryInteger(enum.IntEnum):
zero = 0
one = 1
doc = pydoc.render_doc(BinaryInteger)
- self.assertIn('BinaryInteger.zero', doc)
+ self.assertIn('<BinaryInteger.zero: 0>', doc)
def test_mixed_case_module_names_are_lower_cased(self):
# issue16484
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 4231e962b23e8..588cc7930c327 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -2177,13 +2177,11 @@ def test_flags_repr(self):
"re.IGNORECASE|re.DOTALL|re.VERBOSE")
self.assertEqual(repr(re.I|re.S|re.X|(1<<20)),
"re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000")
- self.assertEqual(
- repr(~re.I),
- "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.TEMPLATE|re.DEBUG")
+ self.assertEqual(repr(~re.I), "~re.IGNORECASE")
self.assertEqual(repr(~(re.I|re.S|re.X)),
- "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG")
+ "~(re.IGNORECASE|re.DOTALL|re.VERBOSE)")
self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))),
- "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG|0xffe00")
+ "~(re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000)")
class ImplementationTest(unittest.TestCase):
diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py
index daecf196fa146..2144d619630d1 100644
--- a/Lib/test/test_signal.py
+++ b/Lib/test/test_signal.py
@@ -1,4 +1,3 @@
-import enum
import errno
import os
import random
@@ -34,32 +33,6 @@ def test_enums(self):
self.assertIsInstance(sig, signal.Signals)
self.assertEqual(sys.platform, "win32")
- CheckedSignals = enum._old_convert_(
- enum.IntEnum, 'Signals', 'signal',
- lambda name:
- name.isupper()
- and (name.startswith('SIG') and not name.startswith('SIG_'))
- or name.startswith('CTRL_'),
- source=signal,
- )
- enum._test_simple_enum(CheckedSignals, signal.Signals)
-
- CheckedHandlers = enum._old_convert_(
- enum.IntEnum, 'Handlers', 'signal',
- lambda name: name in ('SIG_DFL', 'SIG_IGN'),
- source=signal,
- )
- enum._test_simple_enum(CheckedHandlers, signal.Handlers)
-
- Sigmasks = getattr(signal, 'Sigmasks', None)
- if Sigmasks is not None:
- CheckedSigmasks = enum._old_convert_(
- enum.IntEnum, 'Sigmasks', 'signal',
- lambda name: name in ('SIG_BLOCK', 'SIG_UNBLOCK', 'SIG_SETMASK'),
- source=signal,
- )
- enum._test_simple_enum(CheckedSigmasks, Sigmasks)
-
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
class PosixTests(unittest.TestCase):
@@ -899,7 +872,7 @@ def handler(signum, frame):
%s
- blocked = %r
+ blocked = %s
signum = signal.SIGALRM
# child: block and wait the signal
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 828d1f3dcc670..a59afd414e73d 100755
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1518,9 +1518,9 @@ def testGetaddrinfo(self):
infos = socket.getaddrinfo(HOST, 80, socket.AF_INET, socket.SOCK_STREAM)
for family, type, _, _, _ in infos:
self.assertEqual(family, socket.AF_INET)
- self.assertEqual(str(family), 'AF_INET')
+ self.assertEqual(str(family), 'AddressFamily.AF_INET')
self.assertEqual(type, socket.SOCK_STREAM)
- self.assertEqual(str(type), 'SOCK_STREAM')
+ self.assertEqual(str(type), 'SocketKind.SOCK_STREAM')
infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
for _, socktype, _, _, _ in infos:
self.assertEqual(socktype, socket.SOCK_STREAM)
@@ -1794,8 +1794,8 @@ def test_str_for_enums(self):
# Make sure that the AF_* and SOCK_* constants have enum-like string
# reprs.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- self.assertEqual(str(s.family), 'AF_INET')
- self.assertEqual(str(s.type), 'SOCK_STREAM')
+ self.assertEqual(str(s.family), 'AddressFamily.AF_INET')
+ self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM')
def test_socket_consistent_sock_type(self):
SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
@@ -1942,41 +1942,6 @@ def test_socket_fileno_requires_socket_fd(self):
fileno=afile.fileno())
self.assertEqual(cm.exception.errno, errno.ENOTSOCK)
- def test_addressfamily_enum(self):
- import _socket, enum
- CheckedAddressFamily = enum._old_convert_(
- enum.IntEnum, 'AddressFamily', 'socket',
- lambda C: C.isupper() and C.startswith('AF_'),
- source=_socket,
- )
- enum._test_simple_enum(CheckedAddressFamily, socket.AddressFamily)
-
- def test_socketkind_enum(self):
- import _socket, enum
- CheckedSocketKind = enum._old_convert_(
- enum.IntEnum, 'SocketKind', 'socket',
- lambda C: C.isupper() and C.startswith('SOCK_'),
- source=_socket,
- )
- enum._test_simple_enum(CheckedSocketKind, socket.SocketKind)
-
- def test_msgflag_enum(self):
- import _socket, enum
- CheckedMsgFlag = enum._old_convert_(
- enum.IntFlag, 'MsgFlag', 'socket',
- lambda C: C.isupper() and C.startswith('MSG_'),
- source=_socket,
- )
- enum._test_simple_enum(CheckedMsgFlag, socket.MsgFlag)
-
- def test_addressinfo_enum(self):
- import _socket, enum
- CheckedAddressInfo = enum._old_convert_(
- enum.IntFlag, 'AddressInfo', 'socket',
- lambda C: C.isupper() and C.startswith('AI_'),
- source=_socket)
- enum._test_simple_enum(CheckedAddressInfo, socket.AddressInfo)
-
@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.')
class BasicCANTest(unittest.TestCase):
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index fab382f43c7a4..e523634c915f4 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -13,7 +13,6 @@
import select
import time
import datetime
-import enum
import gc
import os
import errno
@@ -373,7 +372,7 @@ def test_str_for_enums(self):
# Make sure that the PROTOCOL_* constants have enum-like string
# reprs.
proto = ssl.PROTOCOL_TLS_CLIENT
- self.assertEqual(str(proto), 'PROTOCOL_TLS_CLIENT')
+ self.assertEqual(str(proto), '_SSLMethod.PROTOCOL_TLS_CLIENT')
ctx = ssl.SSLContext(proto)
self.assertIs(ctx.protocol, proto)
@@ -618,7 +617,7 @@ def test_openssl111_deprecations(self):
with self.assertWarns(DeprecationWarning) as cm:
ssl.SSLContext(protocol)
self.assertEqual(
- f'{protocol!r} is deprecated',
+ f'ssl.{protocol.name} is deprecated',
str(cm.warning)
)
@@ -628,7 +627,7 @@ def test_openssl111_deprecations(self):
with self.assertWarns(DeprecationWarning) as cm:
ctx.minimum_version = version
self.assertEqual(
- f'ssl.{version!r} is deprecated',
+ f'ssl.{version!s} is deprecated',
str(cm.warning)
)
@@ -4870,155 +4869,6 @@ def sni_cb(sock, servername, ctx):
s.connect((HOST, server.port))
-class TestEnumerations(unittest.TestCase):
-
- def test_tlsversion(self):
- class CheckedTLSVersion(enum.IntEnum):
- MINIMUM_SUPPORTED = _ssl.PROTO_MINIMUM_SUPPORTED
- SSLv3 = _ssl.PROTO_SSLv3
- TLSv1 = _ssl.PROTO_TLSv1
- TLSv1_1 = _ssl.PROTO_TLSv1_1
- TLSv1_2 = _ssl.PROTO_TLSv1_2
- TLSv1_3 = _ssl.PROTO_TLSv1_3
- MAXIMUM_SUPPORTED = _ssl.PROTO_MAXIMUM_SUPPORTED
- enum._test_simple_enum(CheckedTLSVersion, TLSVersion)
-
- def test_tlscontenttype(self):
- class Checked_TLSContentType(enum.IntEnum):
- """Content types (record layer)
-
- See RFC 8446, section B.1
- """
- CHANGE_CIPHER_SPEC = 20
- ALERT = 21
- HANDSHAKE = 22
- APPLICATION_DATA = 23
- # pseudo content types
- HEADER = 0x100
- INNER_CONTENT_TYPE = 0x101
- enum._test_simple_enum(Checked_TLSContentType, _TLSContentType)
-
- def test_tlsalerttype(self):
- class Checked_TLSAlertType(enum.IntEnum):
- """Alert types for TLSContentType.ALERT messages
-
- See RFC 8466, section B.2
- """
- CLOSE_NOTIFY = 0
- UNEXPECTED_MESSAGE = 10
- BAD_RECORD_MAC = 20
- DECRYPTION_FAILED = 21
- RECORD_OVERFLOW = 22
- DECOMPRESSION_FAILURE = 30
- HANDSHAKE_FAILURE = 40
- NO_CERTIFICATE = 41
- BAD_CERTIFICATE = 42
- UNSUPPORTED_CERTIFICATE = 43
- CERTIFICATE_REVOKED = 44
- CERTIFICATE_EXPIRED = 45
- CERTIFICATE_UNKNOWN = 46
- ILLEGAL_PARAMETER = 47
- UNKNOWN_CA = 48
- ACCESS_DENIED = 49
- DECODE_ERROR = 50
- DECRYPT_ERROR = 51
- EXPORT_RESTRICTION = 60
- PROTOCOL_VERSION = 70
- INSUFFICIENT_SECURITY = 71
- INTERNAL_ERROR = 80
- INAPPROPRIATE_FALLBACK = 86
- USER_CANCELED = 90
- NO_RENEGOTIATION = 100
- MISSING_EXTENSION = 109
- UNSUPPORTED_EXTENSION = 110
- CERTIFICATE_UNOBTAINABLE = 111
- UNRECOGNIZED_NAME = 112
- BAD_CERTIFICATE_STATUS_RESPONSE = 113
- BAD_CERTIFICATE_HASH_VALUE = 114
- UNKNOWN_PSK_IDENTITY = 115
- CERTIFICATE_REQUIRED = 116
- NO_APPLICATION_PROTOCOL = 120
- enum._test_simple_enum(Checked_TLSAlertType, _TLSAlertType)
-
- def test_tlsmessagetype(self):
- class Checked_TLSMessageType(enum.IntEnum):
- """Message types (handshake protocol)
-
- See RFC 8446, section B.3
- """
- HELLO_REQUEST = 0
- CLIENT_HELLO = 1
- SERVER_HELLO = 2
- HELLO_VERIFY_REQUEST = 3
- NEWSESSION_TICKET = 4
- END_OF_EARLY_DATA = 5
- HELLO_RETRY_REQUEST = 6
- ENCRYPTED_EXTENSIONS = 8
- CERTIFICATE = 11
- SERVER_KEY_EXCHANGE = 12
- CERTIFICATE_REQUEST = 13
- SERVER_DONE = 14
- CERTIFICATE_VERIFY = 15
- CLIENT_KEY_EXCHANGE = 16
- FINISHED = 20
- CERTIFICATE_URL = 21
- CERTIFICATE_STATUS = 22
- SUPPLEMENTAL_DATA = 23
- KEY_UPDATE = 24
- NEXT_PROTO = 67
- MESSAGE_HASH = 254
- CHANGE_CIPHER_SPEC = 0x0101
- enum._test_simple_enum(Checked_TLSMessageType, _TLSMessageType)
-
- def test_sslmethod(self):
- Checked_SSLMethod = enum._old_convert_(
- enum.IntEnum, '_SSLMethod', 'ssl',
- lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23',
- source=ssl._ssl,
- )
- enum._test_simple_enum(Checked_SSLMethod, ssl._SSLMethod)
-
- def test_options(self):
- CheckedOptions = enum._old_convert_(
- enum.FlagEnum, 'Options', 'ssl',
- lambda name: name.startswith('OP_'),
- source=ssl._ssl,
- )
- enum._test_simple_enum(CheckedOptions, ssl.Options)
-
-
- def test_alertdescription(self):
- CheckedAlertDescription = enum._old_convert_(
- enum.IntEnum, 'AlertDescription', 'ssl',
- lambda name: name.startswith('ALERT_DESCRIPTION_'),
- source=ssl._ssl,
- )
- enum._test_simple_enum(CheckedAlertDescription, ssl.AlertDescription)
-
- def test_sslerrornumber(self):
- Checked_SSLMethod = enum._old_convert_(
- enum.IntEnum, '_SSLMethod', 'ssl',
- lambda name: name.startswith('PROTOCOL_') and name != 'PROTOCOL_SSLv23',
- source=ssl._ssl,
- )
- enum._test_simple_enum(Checked_SSLMethod, ssl._SSLMethod)
-
- def test_verifyflags(self):
- CheckedVerifyFlags = enum._old_convert_(
- enum.FlagEnum, 'VerifyFlags', 'ssl',
- lambda name: name.startswith('VERIFY_'),
- source=ssl._ssl,
- )
- enum._test_simple_enum(CheckedVerifyFlags, ssl.VerifyFlags)
-
- def test_verifymode(self):
- CheckedVerifyMode = enum._old_convert_(
- enum.IntEnum, 'VerifyMode', 'ssl',
- lambda name: name.startswith('CERT_'),
- source=ssl._ssl,
- )
- enum._test_simple_enum(CheckedVerifyMode, ssl.VerifyMode)
-
def test_main(verbose=False):
if support.verbose:
plats = {
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 5db7a9b078070..a6549c79ff05a 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -1466,23 +1466,22 @@ class Float(float, enum.Enum):
PI = 3.1415926
class Int(enum.IntEnum):
IDES = 15
- class Str(enum.StrEnum):
- # StrEnum uses the value and not the name for %s etc.
+ class Str(str, enum.Enum):
ABC = 'abc'
# Testing Unicode formatting strings...
self.assertEqual("%s, %s" % (Str.ABC, Str.ABC),
- 'abc, abc')
+ 'Str.ABC, Str.ABC')
self.assertEqual("%s, %s, %d, %i, %u, %f, %5.2f" %
(Str.ABC, Str.ABC,
Int.IDES, Int.IDES, Int.IDES,
Float.PI, Float.PI),
- 'abc, abc, 15, 15, 15, 3.141593, 3.14')
+ 'Str.ABC, Str.ABC, 15, 15, 15, 3.141593, 3.14')
# formatting jobs delegated from the string implementation:
self.assertEqual('...%(foo)s...' % {'foo':Str.ABC},
- '...abc...')
+ '...Str.ABC...')
self.assertEqual('...%(foo)s...' % {'foo':Int.IDES},
- '...IDES...')
+ '...Int.IDES...')
self.assertEqual('...%(foo)i...' % {'foo':Int.IDES},
'...15...')
self.assertEqual('...%(foo)d...' % {'foo':Int.IDES},
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py
index 3f56192c70e84..d6a8333427a4a 100755
--- a/Lib/test/test_uuid.py
+++ b/Lib/test/test_uuid.py
@@ -4,7 +4,6 @@
import builtins
import contextlib
import copy
-import enum
import io
import os
import pickle
@@ -32,13 +31,6 @@ def get_command_stdout(command, args):
class BaseTestUUID:
uuid = None
- def test_safe_uuid_enum(self):
- class CheckedSafeUUID(enum.Enum):
- safe = 0
- unsafe = -1
- unknown = None
- enum._test_simple_enum(CheckedSafeUUID, py_uuid.SafeUUID)
-
def test_UUID(self):
equal = self.assertEqual
ascending = []
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 2513c972bc77f..552fc54a4934d 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -144,8 +144,7 @@ def _splitdict(tk, v, cut_minus=True, conv=None):
return dict
-(a)enum._simple_enum(enum.StrEnum)
-class EventType:
+class EventType(str, enum.Enum):
KeyPress = '2'
Key = KeyPress
KeyRelease = '3'
@@ -186,6 +185,8 @@ class EventType:
Deactivate = '37'
MouseWheel = '38'
+ __str__ = str.__str__
+
class Event:
"""Container for the properties of an event.
diff --git a/Lib/tkinter/test/test_tkinter/test_misc.py b/Lib/tkinter/test/test_tkinter/test_misc.py
index ab8f64790dfc0..7e78433a7c9ac 100644
--- a/Lib/tkinter/test/test_tkinter/test_misc.py
+++ b/Lib/tkinter/test/test_tkinter/test_misc.py
@@ -1,7 +1,6 @@
import functools
import unittest
import tkinter
-import enum
from test import support
from tkinter.test.support import AbstractTkTest, AbstractDefaultRootTest
@@ -269,49 +268,6 @@ def test_event_repr(self):
" num=3 delta=-1 focus=True"
" x=10 y=20 width=300 height=200>")
- def test_eventtype_enum(self):
- class CheckedEventType(enum.StrEnum):
- KeyPress = '2'
- Key = KeyPress
- KeyRelease = '3'
- ButtonPress = '4'
- Button = ButtonPress
- ButtonRelease = '5'
- Motion = '6'
- Enter = '7'
- Leave = '8'
- FocusIn = '9'
- FocusOut = '10'
- Keymap = '11' # undocumented
- Expose = '12'
- GraphicsExpose = '13' # undocumented
- NoExpose = '14' # undocumented
- Visibility = '15'
- Create = '16'
- Destroy = '17'
- Unmap = '18'
- Map = '19'
- MapRequest = '20'
- Reparent = '21'
- Configure = '22'
- ConfigureRequest = '23'
- Gravity = '24'
- ResizeRequest = '25'
- Circulate = '26'
- CirculateRequest = '27'
- Property = '28'
- SelectionClear = '29' # undocumented
- SelectionRequest = '30' # undocumented
- Selection = '31' # undocumented
- Colormap = '32'
- ClientMessage = '33' # undocumented
- Mapping = '34' # undocumented
- VirtualEvent = '35' # undocumented
- Activate = '36'
- Deactivate = '37'
- MouseWheel = '38'
- enum._test_simple_enum(CheckedEventType, tkinter.EventType)
-
def test_getboolean(self):
for v in 'true', 'yes', 'on', '1', 't', 'y', 1, True:
self.assertIs(self.root.getboolean(v), True)
diff --git a/Lib/uuid.py b/Lib/uuid.py
index 67da88560cfb1..5ae0a3e5fa449 100644
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -47,7 +47,7 @@
import os
import sys
-from enum import Enum, _simple_enum
+from enum import Enum
__author__ = 'Ka-Ping Yee <ping(a)zesty.ca>'
@@ -75,8 +75,7 @@
bytes_ = bytes # The built-in bytes type
-@_simple_enum(Enum)
-class SafeUUID:
+class SafeUUID(Enum):
safe = 0
unsafe = -1
unknown = None
1
0
bpo-44553: Correct failure in tp_new for the union object (GH-27008) (GH-27009)
by pablogsal 03 Jul '21
by pablogsal 03 Jul '21
03 Jul '21
https://github.com/python/cpython/commit/000b9e803a7ec067da0a43f9a3fec16f10…
commit: 000b9e803a7ec067da0a43f9a3fec16f1078215a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: pablogsal <Pablogsal(a)gmail.com>
date: 2021-07-03T21:51:10+01:00
summary:
bpo-44553: Correct failure in tp_new for the union object (GH-27008) (GH-27009)
(cherry picked from commit bc3961485639cc73de7c4c7eed1b56f3c74939bf)
Co-authored-by: Pablo Galindo <Pablogsal(a)gmail.com>
Co-authored-by: Pablo Galindo <Pablogsal(a)gmail.com>
files:
M Objects/unionobject.c
diff --git a/Objects/unionobject.c b/Objects/unionobject.c
index cc7181d2475ca..8435763b5ea7c 100644
--- a/Objects/unionobject.c
+++ b/Objects/unionobject.c
@@ -490,10 +490,10 @@ _Py_Union(PyObject *args)
}
result->args = dedup_and_flatten_args(args);
+ _PyObject_GC_TRACK(result);
if (result->args == NULL) {
- PyObject_GC_Del(result);
+ Py_DECREF(result);
return NULL;
}
- _PyObject_GC_TRACK(result);
return (PyObject*)result;
}
1
0