Is input = input exec valid Python code
“Does input = input (English goes here: ") exec (input) work And is it valid Python code and will it do what the English says in the English goes here part when I change it to what I want it to do English is the only thing that will go where it says English goes here It’s a yes or a no non of that yes it works if it’s valid python your ment to tell me if it’s valid python or not and what this will do”
Summary
The snippet as written is syntactically incorrect and will raise a syntax error. To work, it must be split into two proper statements, e.g., `code = input("English goes here: "); exec(code)`. When corrected, it will execute whatever code the user enters, which is possible but highly insecure.
Sources 60 searched
- 25. Validation, Exceptions, and Error Handling — Programming for Financial Technology
Remember - a function call is an ... one or more Python statements. While legitimate use cases exist for both functions, developers must use extreme care to ensure that any string values passed to these functions are safe to execute. As an example, create a code cell and execute ...
- Input from the console – Clayton Cafiero
Enter an integer: cheese Enter another integer: bananas Traceback (most recent call last): File "/myfiles/addition_fixed.py", line 5, in <module> a = int(a) ValueError: invalid literal for int() with base 10: 'cheese' Process finished with exit code 1 · This occurs because 'cheese' cannot be converted to an int. In this case, Python reports a ValueError and indicates the invalid literal 'cheese'. We’ll see how to handle problems like this later on in Chapter 15. It’s important to note that input() does not validate the user’s input.
- Python exec() with Examples - Python Geeks
It takes either a string or an object as the first and necessary parameter for the dynamic execution of the input. If a string is given as an input, then it is parsed as a suite of Python code.
- exec() in Python - GeeksforGeeks
We must be careful that the return ... any value, hence returns None. ... In this example, we can see dynamic execution in Python using the exec() function....
- 6.13 The exec statement
Also, in the current implementation, multi-line compound statements must end with a newline: exec "for v in seq:\n\tprint v\n" works, but exec "for v in seq:\n\tprint v" fails with SyntaxError.
- subprocess — Subprocess management
Execute a child program in a new process. On POSIX, the class uses os.execvpe()-like behavior to execute the child program. On Windows, the class uses the Windows CreateProcess() function.
- python - Why should exec() and eval() be avoided? - Stack Overflow
Technically, any place where exec() and eval() run inputs that are directly given or influenced by user is insecure. As a knowledgeable user I could make the code do what it shouldn't be doing. Wherever this situation occurs, it should be avoided.
- python 3.x - How to avoid the security risk of exec() - Stack Overflow
I'm doing a test web service project where I need to create some dynamic variables. To implement this, I have no option but using exec() function like follow. for parameter in parameter_names:...
- Code Injection in Python | Semgrep
The exec() function supports the dynamic execution of Python code. The exec() function can be dangerous if it is used to execute dynamic content (non-literal content).