aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2021-03-15 15:07:02 -0700
committerGravatar MarkKoz <[email protected]>2021-03-15 15:07:02 -0700
commite7302f0e50dfe158d3f4771d3e6d2181f5ac0351 (patch)
tree74856403c1667f4dc3d54e4780432659fe1094b5
parentMerge pull request #1465 from python-discord/gitattributes (diff)
Code block: remove null bytes before parsing AST
`ast.parse` raises a ValueError complaining that source code strings cannot contain null bytes. It seems like they may accidentally get pasted into Discord by users sometimes.
-rw-r--r--bot/exts/info/codeblock/_parsing.py3
1 files changed, 3 insertions, 0 deletions
diff --git a/bot/exts/info/codeblock/_parsing.py b/bot/exts/info/codeblock/_parsing.py
index e35fbca22..73fd11b94 100644
--- a/bot/exts/info/codeblock/_parsing.py
+++ b/bot/exts/info/codeblock/_parsing.py
@@ -103,6 +103,9 @@ def _is_python_code(content: str) -> bool:
"""Return True if `content` is valid Python consisting of more than just expressions."""
log.trace("Checking if content is Python code.")
try:
+ # Remove null bytes because they cause ast.parse to raise a ValueError.
+ content = content.replace("\x00", "")
+
# Attempt to parse the message into an AST node.
# Invalid Python code will raise a SyntaxError.
tree = ast.parse(content)