Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
08f127a3
Commit
08f127a3
authored
Jun 15, 2018
by
Serhiy Storchaka
Committed by
GitHub
Jun 15, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-33851: Fix ast.get_docstring() for a node that lacks a docstring. (GH-7682)
parent
9e7c9219
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
2 deletions
+34
-2
Lib/ast.py
Lib/ast.py
+2
-2
Lib/test/test_ast.py
Lib/test/test_ast.py
+31
-0
Misc/NEWS.d/next/Library/2018-06-13-15-12-25.bpo-33851.SVbqlz.rst
...S.d/next/Library/2018-06-13-15-12-25.bpo-33851.SVbqlz.rst
+1
-0
No files found.
Lib/ast.py
View file @
08f127a3
...
...
@@ -206,7 +206,7 @@ def get_docstring(node, clean=True):
"""
if
not
isinstance
(
node
,
(
AsyncFunctionDef
,
FunctionDef
,
ClassDef
,
Module
)):
raise
TypeError
(
"%r can't have docstrings"
%
node
.
__class__
.
__name__
)
if
not
node
.
body
:
if
not
(
node
.
body
and
isinstance
(
node
.
body
[
0
],
Expr
))
:
return
None
node
=
node
.
body
[
0
].
value
if
isinstance
(
node
,
Str
):
...
...
@@ -215,7 +215,7 @@ def get_docstring(node, clean=True):
text
=
node
.
value
else
:
return
None
if
clean
and
text
:
if
clean
:
import
inspect
text
=
inspect
.
cleandoc
(
text
)
return
text
...
...
Lib/test/test_ast.py
View file @
08f127a3
...
...
@@ -521,13 +521,44 @@ class ASTHelpers_Test(unittest.TestCase):
)
def
test_get_docstring
(
self
):
node
=
ast
.
parse
(
'"""line one
\
n
line two"""'
)
self
.
assertEqual
(
ast
.
get_docstring
(
node
),
'line one
\
n
line two'
)
node
=
ast
.
parse
(
'class foo:
\
n
"""line one
\
n
line two"""'
)
self
.
assertEqual
(
ast
.
get_docstring
(
node
.
body
[
0
]),
'line one
\
n
line two'
)
node
=
ast
.
parse
(
'def foo():
\
n
"""line one
\
n
line two"""'
)
self
.
assertEqual
(
ast
.
get_docstring
(
node
.
body
[
0
]),
'line one
\
n
line two'
)
node
=
ast
.
parse
(
'async def foo():
\
n
"""spam
\
n
ham"""'
)
self
.
assertEqual
(
ast
.
get_docstring
(
node
.
body
[
0
]),
'spam
\
n
ham'
)
def
test_get_docstring_none
(
self
):
self
.
assertIsNone
(
ast
.
get_docstring
(
ast
.
parse
(
''
)))
node
=
ast
.
parse
(
'x = "not docstring"'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
))
node
=
ast
.
parse
(
'def foo():
\
n
pass'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
))
node
=
ast
.
parse
(
'class foo:
\
n
pass'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
.
body
[
0
]))
node
=
ast
.
parse
(
'class foo:
\
n
x = "not docstring"'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
.
body
[
0
]))
node
=
ast
.
parse
(
'class foo:
\
n
def bar(self): pass'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
.
body
[
0
]))
node
=
ast
.
parse
(
'def foo():
\
n
pass'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
.
body
[
0
]))
node
=
ast
.
parse
(
'def foo():
\
n
x = "not docstring"'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
.
body
[
0
]))
node
=
ast
.
parse
(
'async def foo():
\
n
pass'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
.
body
[
0
]))
node
=
ast
.
parse
(
'async def foo():
\
n
x = "not docstring"'
)
self
.
assertIsNone
(
ast
.
get_docstring
(
node
.
body
[
0
]))
def
test_literal_eval
(
self
):
self
.
assertEqual
(
ast
.
literal_eval
(
'[1, 2, 3]'
),
[
1
,
2
,
3
])
...
...
Misc/NEWS.d/next/Library/2018-06-13-15-12-25.bpo-33851.SVbqlz.rst
0 → 100644
View file @
08f127a3
Fix :func:`ast.get_docstring` for a node that lacks a docstring.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment