Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 94fd08a

Browse files
committed
Match 3.7's handling of doc strings on functions
Summary: Python 3.7 no longer skips doc strings for functions. They still won't actually be emitted due to just being constants expr's which get optimized away. But they do impact the line number information for debugging purposes. Test Plan: ./python -m test.test_compiler
1 parent 5b11939 commit 94fd08a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

compiler/pycodegen.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ def processBody(self, body, gen):
358358
def _visitAnnotation(self, node):
359359
return self.visit(node)
360360

361+
skip_func_docstring = skip_docstring
362+
361363
def _visitFuncOrLambda(self, node, isLambda=0):
362364
if not isLambda and node.decorator_list:
363365
for decorator in node.decorator_list:
@@ -370,7 +372,7 @@ def _visitFuncOrLambda(self, node, isLambda=0):
370372
self.class_name, self.module)
371373
body = node.body
372374
if not isLambda:
373-
body = self.skip_docstring(body)
375+
body = self.skip_func_docstring(body)
374376

375377
self.processBody(body, gen)
376378

@@ -2115,6 +2117,9 @@ def visitTryFinally(self, node, except_protect=False):
21152117
self.emit('POP_EXCEPT')
21162118
self.setups.pop()
21172119

2120+
def skip_func_docstring(self, body):
2121+
return body
2122+
21182123
def emitMapUnpack(self, containers, is_unpacking):
21192124
if containers > 1 or is_unpacking:
21202125
self.emit('BUILD_MAP_UNPACK', containers)

test_compiler/test_py37.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ def test_try_except_pop_except(self):
9999
self.assertEqual(prev_instr.opname == "END_FINALLY", generator is Python37CodeGenerator, prev_instr.opname)
100100
prev_instr = instr
101101

102+
def test_func_doc_str(self):
103+
"""POP_EXCEPT moved after END_FINALLY in Python 3.7"""
104+
test_code = """
105+
def f():
106+
107+
'''hello there
108+
109+
'''
110+
"""
111+
112+
py37_code = self.find_code(self.compile(test_code, Python37CodeGenerator))
113+
self.assertEqual(py37_code.co_lnotab, b"\x00\x04")
114+
115+
py36_code = self.find_code(self.compile(test_code, CodeGenerator))
116+
self.assertEqual(py36_code.co_lnotab, b"")
102117

103118
def test_future_annotations(self):
104119
annotations = ["42"]

0 commit comments

Comments
 (0)