summaryrefslogtreecommitdiff
path: root/astroid
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-01-13 15:07:31 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2019-01-13 15:07:31 +0100
commitfd41070d76f323f089a120c72fb9b0d87e7f4a20 (patch)
tree5d782a7de1458df1fa6fb87fec790f10ab3bb628 /astroid
parent16f69882f14a2946c6bb14fd82be27d377e1ac3e (diff)
downloadastroid-git-fd41070d76f323f089a120c72fb9b0d87e7f4a20.tar.gz
Prevent crashing when processing ``enums`` with mixed single and double quotes.
Close PyCQA/pylint#2676
Diffstat (limited to 'astroid')
-rw-r--r--astroid/brain/brain_namedtuple_enum.py2
-rw-r--r--astroid/tests/unittest_brain.py24
2 files changed, 25 insertions, 1 deletions
diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py
index 6dc07f5e..58402f18 100644
--- a/astroid/brain/brain_namedtuple_enum.py
+++ b/astroid/brain/brain_namedtuple_enum.py
@@ -322,7 +322,7 @@ def infer_enum_class(node):
inferred_return_value = None
if isinstance(stmt.value, nodes.Const):
if isinstance(stmt.value.value, str):
- inferred_return_value = '"{}"'.format(stmt.value.value)
+ inferred_return_value = repr(stmt.value.value)
else:
inferred_return_value = stmt.value.value
diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py
index 55936009..5b9c68b8 100644
--- a/astroid/tests/unittest_brain.py
+++ b/astroid/tests/unittest_brain.py
@@ -740,6 +740,30 @@ class EnumBrainTest(unittest.TestCase):
isinstance(elem, astroid.Const) and elem.value == 1 for elem in inferred_int
)
+ def test_mingled_single_and_double_quotes_does_not_crash(self):
+ node = builder.extract_node(
+ """
+ from enum import Enum
+ class A(Enum):
+ a = 'x"y"'
+ A.a.value #@
+ """
+ )
+ inferred_string = next(node.infer())
+ assert inferred_string.value == 'x"y"'
+
+ def test_special_characters_does_not_crash(self):
+ node = builder.extract_node(
+ """
+ import enum
+ class Example(enum.Enum):
+ NULL = '\\N{NULL}'
+ Example.NULL.value
+ """
+ )
+ inferred_string = next(node.infer())
+ assert inferred_string.value == "\N{NULL}"
+
@unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.")
class DateutilBrainTest(unittest.TestCase):