15
15
16
16
import os
17
17
import sys
18
+ import ast
18
19
import copy
19
- import string
20
20
import logging
21
21
import tokenize as tk
22
22
from itertools import takewhile , dropwhile , chain
@@ -1370,7 +1370,7 @@ def check_docstring_missing(self, definition, docstring):
1370
1370
1371
1371
"""
1372
1372
if (not docstring and definition .is_public or
1373
- docstring and is_blank (eval (docstring ))):
1373
+ docstring and is_blank (ast . literal_eval (docstring ))):
1374
1374
codes = {Module : D100 , Class : D101 , NestedClass : D101 ,
1375
1375
Method : (lambda : D105 () if is_magic (definition .name )
1376
1376
else D102 ()),
@@ -1386,7 +1386,7 @@ def check_one_liners(self, definition, docstring):
1386
1386
1387
1387
"""
1388
1388
if docstring :
1389
- lines = eval (docstring ).split ('\n ' )
1389
+ lines = ast . literal_eval (docstring ).split ('\n ' )
1390
1390
if len (lines ) > 1 :
1391
1391
non_empty_lines = sum (1 for l in lines if not is_blank (l ))
1392
1392
if non_empty_lines == 1 :
@@ -1456,7 +1456,7 @@ def check_blank_after_summary(self, definition, docstring):
1456
1456
1457
1457
"""
1458
1458
if docstring :
1459
- lines = eval (docstring ).strip ().split ('\n ' )
1459
+ lines = ast . literal_eval (docstring ).strip ().split ('\n ' )
1460
1460
if len (lines ) > 1 :
1461
1461
post_summary_blanks = list (map (is_blank , lines [1 :]))
1462
1462
blanks_count = sum (takewhile (bool , post_summary_blanks ))
@@ -1495,7 +1495,8 @@ def check_newline_after_last_paragraph(self, definition, docstring):
1495
1495
1496
1496
"""
1497
1497
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 )]
1499
1500
if len (lines ) > 1 :
1500
1501
if docstring .split ("\n " )[- 1 ].strip () not in ['"""' , "'''" ]:
1501
1502
return D209 ()
@@ -1504,7 +1505,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
1504
1505
def check_surrounding_whitespaces (self , definition , docstring ):
1505
1506
"""D210: No whitespaces allowed surrounding docstring text."""
1506
1507
if docstring :
1507
- lines = eval (docstring ).split ('\n ' )
1508
+ lines = ast . literal_eval (docstring ).split ('\n ' )
1508
1509
if lines [0 ].startswith (' ' ) or \
1509
1510
len (lines ) == 1 and lines [0 ].endswith (' ' ):
1510
1511
return D210 ()
@@ -1522,8 +1523,8 @@ def check_triple_double_quotes(self, definition, docstring):
1522
1523
""" quotes in its body.
1523
1524
1524
1525
'''
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'''" ) )):
1527
1528
# Allow ''' quotes if docstring contains """, because otherwise """
1528
1529
# quotes could not be expressed inside docstring. Not in PEP 257.
1529
1530
return
@@ -1571,7 +1572,7 @@ def check_ends_with_period(self, definition, docstring):
1571
1572
1572
1573
"""
1573
1574
if docstring :
1574
- summary_line = eval (docstring ).strip ().split ('\n ' )[0 ]
1575
+ summary_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
1575
1576
if not summary_line .endswith ('.' ):
1576
1577
return D400 (summary_line [- 1 ])
1577
1578
@@ -1585,7 +1586,7 @@ def check_imperative_mood(self, function, docstring): # def context
1585
1586
1586
1587
"""
1587
1588
if docstring :
1588
- stripped = eval (docstring ).strip ()
1589
+ stripped = ast . literal_eval (docstring ).strip ()
1589
1590
if stripped :
1590
1591
first_word = stripped .split ()[0 ]
1591
1592
if first_word .endswith ('s' ) and not first_word .endswith ('ss' ):
@@ -1600,7 +1601,7 @@ def check_no_signature(self, function, docstring): # def context
1600
1601
1601
1602
"""
1602
1603
if docstring :
1603
- first_line = eval (docstring ).strip ().split ('\n ' )[0 ]
1604
+ first_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
1604
1605
if function .name + '(' in first_line .replace (' ' , '' ):
1605
1606
return D402 ()
1606
1607
@@ -1612,7 +1613,7 @@ def check_capitalized(self, function, docstring):
1612
1613
1613
1614
"""
1614
1615
if docstring :
1615
- first_word = eval (docstring ).split ()[0 ]
1616
+ first_word = ast . literal_eval (docstring ).split ()[0 ]
1616
1617
if first_word != first_word .capitalize ():
1617
1618
return D403 (first_word .capitalize (), first_word )
1618
1619
0 commit comments