Skip to content

Files

Latest commit

 

History

History
29 lines (18 loc) · 844 Bytes

B102.md

File metadata and controls

29 lines (18 loc) · 844 Bytes

Pattern: Avoid using exec() when possible

Issue: -

Description

The exec() statement is dangerous, hard to test, and hard to read. Avoid it, as much as possible. This is largely due to the fact that exec() enables you to dynamically execute arbitrary Python code which is stored in literal strings. Consider going back to the code to check if there is a clearer, more direct way to accomplish the task.

Example of insecure code:

text = "print \"suspicious code\""
exec text

Example of secure code:

def foo():
    print "suspicious code"
    
foo()

Further Reading