New submission from Dan Gass <dan.g...@gmail.com>:

Given
(1) instantiation of an enumeration class with an invalid value
(2) a try/except around the instantiation where the exception is ignored

Then:
An unneeded reference to the bad value is lost (as well as other values that I 
suspect are local variables within a participating method)

When run, the attached sample script shows before and after reference counts 
which demonstrate the potential resource leaks. The sample script includes the 
output from running the script on Python version 3.7.5 within the module 
docstring.

The root cause appears to be in the exception handling in the Enum.__new__ 
method (in the area where it calls the _missing_ hook). The attached sample 
script includes a simplified version of those methods that should help pinpoint 
the code in question and confirm the root cause.

Not being an exception nitty-gritty expert, I have suspicions that users should 
be warned about using this pattern of exception handling. I suspect this 
pattern would be worth avoiding in the Enum implementation.

I am willing to take a stab at submitting a patch for Enum. I hesitate slightly 
not knowing if there are specific reasons for the code existing in its current 
form. Alternatively, I plan on being at PyCon2020 for the sprints and could be 
available then to work on it.

----------
components: Library (Lib)
files: sample.py
messages: 360670
nosy: dan.g...@gmail.com
priority: normal
severity: normal
status: open
title: enum.Enum reference count leaks
type: resource usage
versions: Python 3.7
Added file: https://bugs.python.org/file48863/sample.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39451>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to