1

Is there a possibility to check on Runtime errors? It is clear from a string that pri1nt is a function and it is not defined in this string.

import ast

def is_valid_python(code):
  try:
     ast.parse(code)
  except SyntaxError:
     return False
  return True

mycode = 'pri1nt("hello world")'

is_valid_python(mycode) # true

exec(mycode) # NameError: name 'pri1nt' is not defined
9
  • 6
    This wouldn't be a syntax error. pr1nt is a perfectly valid name in Python; the fact that it is not defined means there would be a NameError. But generally this kind of static analysis is hard to do in Python without actually executing the code. Commented Mar 27, 2019 at 14:25
  • If you don't know what the problem is there are many python programming softwares that automatically regognise mistakes and show the exact line and the type of error. Also in your script pri1nt is supposed to be print. Commented Mar 27, 2019 at 14:27
  • 1
    @Hoog No.. that's the entire premise of the question. Commented Mar 27, 2019 at 14:28
  • Strictly speaking, you can only know whether python code runs or not by actually running it. But with the ast module you can do some of the stuff that I imagine you try to achieve, e.g. check if names can resolve. Commented Mar 27, 2019 at 14:28
  • 1
    "All the information is present in the string." No it's not. Does from os import environ; print(environ['FOO']) produce a key error? Nobody knows, unless you run the code. Commented Mar 27, 2019 at 15:11

2 Answers 2

1

Try using BaseException instead of SyntaxError. This would check every type of python error, including NameError. Also, because ast.parse never raises any errors, you should use exec instead.

So it should be like this:

def is_valid_python(code):
  try:
     exec(code)
  except BaseException:
     return False
  Return True

mycode = 'pri1nt("hello world")'

is_valid_python(mycode) # false
Sign up to request clarification or add additional context in comments.

2 Comments

Parsing code doesn't raise its errors. Executing ast.parse('x = y') doesn't raise a NameError, even though it obviously does once the code is actually run.
Well.. just execing the code runs into the same problems as the other answer.
0

maybe something like this?

import subprocess

script_string = "prnt(\"Hello World!\")"    
proc = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
res = proc.communicate(bytes(script_string, "UTF-8"))

what it basically does is pipeing the string to a python interpreter. if no error, then the script_string is valid.

Edit: res will contain (stdout_data, stderr_data) (see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate)

4 Comments

This won't work for something like while True: pass, which runs with no errors.
NameError: name 'true' is not defined is my output
Sorry, typo on my part.
yeah, string_script gets executed. thats probably not intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.