Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 68a7171

Browse files
committed
Used ast.literal_eval instead of eval
1 parent 01d8fd4 commit 68a7171

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/pep257.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import os
1717
import sys
18+
import ast
1819
import copy
19-
import string
2020
import logging
2121
import tokenize as tk
2222
from itertools import takewhile, dropwhile, chain
@@ -1370,7 +1370,7 @@ def check_docstring_missing(self, definition, docstring):
13701370
13711371
"""
13721372
if (not docstring and definition.is_public or
1373-
docstring and is_blank(eval(docstring))):
1373+
docstring and is_blank(ast.literal_eval(docstring))):
13741374
codes = {Module: D100, Class: D101, NestedClass: D101,
13751375
Method: (lambda: D105() if is_magic(definition.name)
13761376
else D102()),
@@ -1386,7 +1386,7 @@ def check_one_liners(self, definition, docstring):
13861386
13871387
"""
13881388
if docstring:
1389-
lines = eval(docstring).split('\n')
1389+
lines = ast.literal_eval(docstring).split('\n')
13901390
if len(lines) > 1:
13911391
non_empty_lines = sum(1 for l in lines if not is_blank(l))
13921392
if non_empty_lines == 1:
@@ -1456,7 +1456,7 @@ def check_blank_after_summary(self, definition, docstring):
14561456
14571457
"""
14581458
if docstring:
1459-
lines = eval(docstring).strip().split('\n')
1459+
lines = ast.literal_eval(docstring).strip().split('\n')
14601460
if len(lines) > 1:
14611461
post_summary_blanks = list(map(is_blank, lines[1:]))
14621462
blanks_count = sum(takewhile(bool, post_summary_blanks))
@@ -1495,7 +1495,8 @@ def check_newline_after_last_paragraph(self, definition, docstring):
14951495
14961496
"""
14971497
if docstring:
1498-
lines = [l for l in eval(docstring).split('\n') if not is_blank(l)]
1498+
lines = [l for l in ast.literal_eval(docstring).split('\n')
1499+
if not is_blank(l)]
14991500
if len(lines) > 1:
15001501
if docstring.split("\n")[-1].strip() not in ['"""', "'''"]:
15011502
return D209()
@@ -1504,7 +1505,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
15041505
def check_surrounding_whitespaces(self, definition, docstring):
15051506
"""D210: No whitespaces allowed surrounding docstring text."""
15061507
if docstring:
1507-
lines = eval(docstring).split('\n')
1508+
lines = ast.literal_eval(docstring).split('\n')
15081509
if lines[0].startswith(' ') or \
15091510
len(lines) == 1 and lines[0].endswith(' '):
15101511
return D210()
@@ -1522,8 +1523,8 @@ def check_triple_double_quotes(self, definition, docstring):
15221523
""" quotes in its body.
15231524
15241525
'''
1525-
if docstring and '"""' in eval(docstring) and docstring.startswith(
1526-
("'''", "r'''", "u'''", "ur'''")):
1526+
if (docstring and '"""' in ast.literal_eval(docstring) and
1527+
docstring.startswith(("'''", "r'''", "u'''", "ur'''"))):
15271528
# Allow ''' quotes if docstring contains """, because otherwise """
15281529
# quotes could not be expressed inside docstring. Not in PEP 257.
15291530
return
@@ -1571,7 +1572,7 @@ def check_ends_with_period(self, definition, docstring):
15711572
15721573
"""
15731574
if docstring:
1574-
summary_line = eval(docstring).strip().split('\n')[0]
1575+
summary_line = ast.literal_eval(docstring).strip().split('\n')[0]
15751576
if not summary_line.endswith('.'):
15761577
return D400(summary_line[-1])
15771578

@@ -1585,7 +1586,7 @@ def check_imperative_mood(self, function, docstring): # def context
15851586
15861587
"""
15871588
if docstring:
1588-
stripped = eval(docstring).strip()
1589+
stripped = ast.literal_eval(docstring).strip()
15891590
if stripped:
15901591
first_word = stripped.split()[0]
15911592
if first_word.endswith('s') and not first_word.endswith('ss'):
@@ -1600,7 +1601,7 @@ def check_no_signature(self, function, docstring): # def context
16001601
16011602
"""
16021603
if docstring:
1603-
first_line = eval(docstring).strip().split('\n')[0]
1604+
first_line = ast.literal_eval(docstring).strip().split('\n')[0]
16041605
if function.name + '(' in first_line.replace(' ', ''):
16051606
return D402()
16061607

@@ -1612,7 +1613,7 @@ def check_capitalized(self, function, docstring):
16121613
16131614
"""
16141615
if docstring:
1615-
first_word = eval(docstring).split()[0]
1616+
first_word = ast.literal_eval(docstring).split()[0]
16161617
if first_word != first_word.capitalize():
16171618
return D403(first_word.capitalize(), first_word)
16181619

0 commit comments

Comments
 (0)