Issue35899
Created on 2019-02-05 14:27 by Maxpxt, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 11840 | closed | matrixise, 2019-02-13 12:22 | |
| PR 11891 | merged | bdbaraban, 2019-02-16 07:06 | |
| PR 12150 | merged | miss-islington, 2019-03-03 22:09 | |
| PR 12151 | closed | miss-islington, 2019-03-03 22:09 | |
| Messages (17) | |||
|---|---|---|---|
| msg334866 - (view) | Author: Maxwell (Maxpxt) | Date: 2019-02-05 14:27 | |
This is a really minor bug.
In enum.py the function _is_sunder(name) fails on empty string with an IndexError.
As a result, attempting to create an Enum with an empty string fails.
>>> from enum import Enum
>>> Yay = Enum('Yay', ('', 'B', 'C'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python37\lib\enum.py", line 311, in __call__
return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start)
File "C:\Program Files\Python37\lib\enum.py", line 422, in _create_
classdict[member_name] = member_value
File "C:\Program Files\Python37\lib\enum.py", line 78, in __setitem__
if _is_sunder(key):
File "C:\Program Files\Python37\lib\enum.py", line 36, in _is_sunder
return (name[0] == name[-1] == '_' and
IndexError: string index out of range
>>>
Expected behavior is for it to not fail, as Enum accepts wierd strings. Example:
>>> from enum import Enum
>>> Yay = Enum('Yay', ('!', 'B', 'C'))
>>> getattr(Yay, '!')
<Yay.!: 1>
>>>
Transcript of lines 26 to 39 of enum.py:
def _is_dunder(name):
"""Returns True if a __dunder__ name, False otherwise."""
return (name[:2] == name[-2:] == '__' and
name[2:3] != '_' and
name[-3:-2] != '_' and
len(name) > 4)
def _is_sunder(name):
"""Returns True if a _sunder_ name, False otherwise."""
return (name[0] == name[-1] == '_' and
name[1:2] != '_' and
name[-2:-1] != '_' and
len(name) > 2)
Solution 1: Replace with:
def _is_dunder(name):
"""Returns True if a __dunder__ name, False otherwise."""
return (len(name) > 4 and
name[:2] == name[-2:] == '__' and
name[2] != '_' and
name[-3] != '_')
def _is_sunder(name):
"""Returns True if a _sunder_ name, False otherwise."""
return (len(name) > 2 and
name[0] == name[-1] == '_' and
name[1:2] != '_' and
name[-2:-1] != '_')
In this solution, function '_is_dunder' was also altered for consistency.
Altering '_is_dunder' is not necessary, though.
Solution 2: Replace with:
def _is_dunder(name):
"""Returns True if a __dunder__ name, False otherwise."""
return (name[:2] == name[-2:] == '__' and
name[2:3] != '_' and
name[-3:-2] != '_' and
len(name) > 4)
def _is_sunder(name):
"""Returns True if a _sunder_ name, False otherwise."""
return (name[:0] == name[-1:] == '_' and
name[1:2] != '_' and
name[-2:-1] != '_' and
len(name) > 2)
In this solution, function '_is_sunder' was altered to follow
the style used in function '_is_dunder'.
|
|||
| msg334867 - (view) | Author: Maxwell (Maxpxt) | Date: 2019-02-05 14:32 | |
Typo fix on solution 2:
def _is_sunder(name):
"""Returns True if a _sunder_ name, False otherwise."""
return (name[:1] == name[-1:] == '_' and
name[1:2] != '_' and
name[-2:-1] != '_' and
len(name) > 2)
|
|||
| msg334869 - (view) | Author: Rémi Lapeyre (remi.lapeyre) * | Date: 2019-02-05 14:39 | |
Hi @Maxpxt, I think your first solution is appropriate, do you want to open a new pull request with your solution and an appropriate test case? |
|||
| msg334971 - (view) | Author: Cheryl Sabella (cheryl.sabella) * ![]() |
Date: 2019-02-06 18:58 | |
I agree with Rémi Lapeyre. For reference, the len() check and current tests were added under issue 19156. |
|||
| msg335028 - (view) | Author: Ethan Furman (ethan.furman) * ![]() |
Date: 2019-02-07 17:20 | |
Yes, the first solution will be fine. Maxwell, can you create a github pull request with that? |
|||
| msg335176 - (view) | Author: Brennan D Baraban (bdbaraban) * | Date: 2019-02-10 22:46 | |
I'm not sure if Maxwell is still working on this issue, but may I pick it up? I can submit a PR within the day. |
|||
| msg335178 - (view) | Author: Ethan Furman (ethan.furman) * ![]() |
Date: 2019-02-10 23:15 | |
Let's give Maxwell until the 14th (so a week from when I asked him to turn his code into a patch) and if nothing from him by then you are welcome to take it over. |
|||
| msg335180 - (view) | Author: Brennan D Baraban (bdbaraban) * | Date: 2019-02-10 23:20 | |
Got it, makes sense. Thank you. New contributor here :) |
|||
| msg335427 - (view) | Author: Stéphane Wirtel (matrixise) * ![]() |
Date: 2019-02-13 12:23 | |
Hi, I have created the PR for Maxwell. After tomorrow, if we have no news from him, I propose to you to update/comment the PR. Of course, I will add a co-authored-by field in the commit. |
|||
| msg335500 - (view) | Author: Brennan D Baraban (bdbaraban) * | Date: 2019-02-14 06:37 | |
Thank you, Stéphane. I submitted a change request to your PR just now. |
|||
| msg335595 - (view) | Author: Stéphane Wirtel (matrixise) * ![]() |
Date: 2019-02-15 09:57 | |
Hi Brennan, Normally, you wanted to work on this issue and you have waited for one week after the last message of Maxwell. Do you want to work on this issue and submit your PR? Have a nice day, Stéphane |
|||
| msg335644 - (view) | Author: Brennan D Baraban (bdbaraban) * | Date: 2019-02-15 20:47 | |
Yes, I will submit a new PR today. |
|||
| msg335799 - (view) | Author: Stéphane Wirtel (matrixise) * ![]() |
Date: 2019-02-18 08:04 | |
Thank you for your contribution. Have a nice day, |
|||
| msg336261 - (view) | Author: Ethan Furman (ethan.furman) * ![]() |
Date: 2019-02-21 21:14 | |
The changes to `_is_sunder` and `_is_dunder` look good, but there is a problem with the underlying assumptions of what Enum should be doing: - nameless members are not to be allowed - non-alphanumeric characters are not supported In other words, while `_is_sunder` should not fail, neither should an empty string be allowed as a member name. This can be checked at line 154 (just add '' to the set) -- then double check that the error raised is a ValueError and not an IndexError. For the strange character portion, use some non-latin numbers and letters to make sure they work, but don't check for symbols such as exclamation points -- while they might work, we are not supporting such things, and having a test that checks to make sure they work suggests that we do support it. |
|||
| msg337048 - (view) | Author: miss-islington (miss-islington) | Date: 2019-03-03 22:09 | |
New changeset 8b914d2767acba3a9e78f1dacdc2d61dbfd7e304 by Miss Islington (bot) (Brennan D Baraban) in branch 'master': bpo-35899: Fix Enum handling of empty and weird strings (GH-11891) https://github.com/python/cpython/commit/8b914d2767acba3a9e78f1dacdc2d61dbfd7e304 |
|||
| msg337059 - (view) | Author: Ethan Furman (ethan.furman) * ![]() |
Date: 2019-03-04 01:36 | |
Thank you, everyone! |
|||
| msg337534 - (view) | Author: miss-islington (miss-islington) | Date: 2019-03-08 21:44 | |
New changeset 8755f0aeb67125a154e5665a24276fe85d269d85 by Miss Islington (bot) in branch '3.7': bpo-35899: Fix Enum handling of empty and weird strings (GH-11891) https://github.com/python/cpython/commit/8755f0aeb67125a154e5665a24276fe85d269d85 |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:10 | admin | set | github: 80080 |
| 2019-03-08 21:44:27 | miss-islington | set | messages: + msg337534 |
| 2019-03-04 01:36:30 | ethan.furman | set | status: open -> closed versions: + Python 3.6, Python 3.8 messages: + msg337059 resolution: fixed |
| 2019-03-03 22:09:43 | miss-islington | set | pull_requests: + pull_request12151 |
| 2019-03-03 22:09:33 | miss-islington | set | pull_requests: + pull_request12150 |
| 2019-03-03 22:09:16 | miss-islington | set | nosy:
+ miss-islington messages: + msg337048 |
| 2019-02-21 21:14:11 | ethan.furman | set | messages: + msg336261 |
| 2019-02-18 08:04:18 | matrixise | set | messages: + msg335799 |
| 2019-02-16 07:06:10 | bdbaraban | set | keywords:
+ patch stage: needs patch -> patch review pull_requests: + pull_request11919 |
| 2019-02-15 20:47:37 | bdbaraban | set | messages: + msg335644 |
| 2019-02-15 09:57:37 | matrixise | set | messages: + msg335595 |
| 2019-02-14 06:37:55 | bdbaraban | set | messages: + msg335500 |
| 2019-02-14 01:01:36 | cheryl.sabella | set | messages: - msg335483 |
| 2019-02-14 00:07:56 | cheryl.sabella | set | messages: + msg335483 |
| 2019-02-13 12:23:59 | matrixise | set | nosy:
+ matrixise messages: + msg335427 keywords:
- patch |
| 2019-02-13 12:22:24 | matrixise | set | keywords:
+ patch stage: needs patch -> patch review pull_requests: + pull_request11870 |
| 2019-02-10 23:20:55 | bdbaraban | set | messages: + msg335180 |
| 2019-02-10 23:15:22 | ethan.furman | set | messages: + msg335178 |
| 2019-02-10 22:46:29 | bdbaraban | set | nosy:
+ bdbaraban messages: + msg335176 |
| 2019-02-08 01:34:16 | corona10 | set | nosy:
+ corona10 |
| 2019-02-07 17:20:36 | ethan.furman | set | keywords:
+ easy type: crash -> behavior messages: + msg335028 stage: needs patch |
| 2019-02-06 18:58:19 | cheryl.sabella | set | nosy:
+ cheryl.sabella messages: + msg334971 |
| 2019-02-05 16:12:16 | rhettinger | set | assignee: ethan.furman |
| 2019-02-05 15:22:55 | xtreak | set | nosy:
+ barry, eli.bendersky, ethan.furman |
| 2019-02-05 14:39:37 | remi.lapeyre | set | nosy:
+ remi.lapeyre messages: + msg334869 |
| 2019-02-05 14:32:26 | Maxpxt | set | messages: + msg334867 |
| 2019-02-05 14:27:39 | Maxpxt | create | |
