From e7302f0e50dfe158d3f4771d3e6d2181f5ac0351 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 15 Mar 2021 15:07:02 -0700 Subject: 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. --- bot/exts/info/codeblock/_parsing.py | 3 +++ 1 file changed, 3 insertions(+) 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) -- cgit v1.2.3