mirror of
https://github.com/python/cpython
synced 2024-09-16 03:20:00 +00:00
bpo-43202: More codeop._maybe_compile clean-ups (GH-24512)
Add comment, end others with period, remove unused variables, initialize others only when needed, and add explicit return.
This commit is contained in:
parent
762fe7deed
commit
b676f5f809
|
@ -64,24 +64,21 @@
|
||||||
|
|
||||||
__all__ = ["compile_command", "Compile", "CommandCompiler"]
|
__all__ = ["compile_command", "Compile", "CommandCompiler"]
|
||||||
|
|
||||||
PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h
|
PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h.
|
||||||
|
|
||||||
def _maybe_compile(compiler, source, filename, symbol):
|
def _maybe_compile(compiler, source, filename, symbol):
|
||||||
# Check for source consisting of only blank lines and comments
|
# Check for source consisting of only blank lines and comments.
|
||||||
for line in source.split("\n"):
|
for line in source.split("\n"):
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if line and line[0] != '#':
|
if line and line[0] != '#':
|
||||||
break # Leave it alone
|
break # Leave it alone.
|
||||||
else:
|
else:
|
||||||
if symbol != "eval":
|
if symbol != "eval":
|
||||||
source = "pass" # Replace it with a 'pass' statement
|
source = "pass" # Replace it with a 'pass' statement
|
||||||
|
|
||||||
err = err1 = err2 = None
|
|
||||||
code1 = code2 = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return compiler(source, filename, symbol)
|
return compiler(source, filename, symbol)
|
||||||
except SyntaxError:
|
except SyntaxError: # Let other compile() errors propagate.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Catch syntax warnings after the first compile
|
# Catch syntax warnings after the first compile
|
||||||
|
@ -89,6 +86,7 @@ def _maybe_compile(compiler, source, filename, symbol):
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
warnings.simplefilter("error")
|
warnings.simplefilter("error")
|
||||||
|
|
||||||
|
code1 = err1 = err2 = None
|
||||||
try:
|
try:
|
||||||
code1 = compiler(source + "\n", filename, symbol)
|
code1 = compiler(source + "\n", filename, symbol)
|
||||||
except SyntaxError as e:
|
except SyntaxError as e:
|
||||||
|
@ -102,6 +100,8 @@ def _maybe_compile(compiler, source, filename, symbol):
|
||||||
try:
|
try:
|
||||||
if not code1 and _is_syntax_error(err1, err2):
|
if not code1 and _is_syntax_error(err1, err2):
|
||||||
raise err1
|
raise err1
|
||||||
|
else:
|
||||||
|
return None
|
||||||
finally:
|
finally:
|
||||||
err1 = err2 = None
|
err1 = err2 = None
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue