We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.
Python but annoying
Jump to navigation
Jump to search
Python but annoying is designed by PSTF inspired by Python But Bad.
In this programming language, all programs run like Python, but if any exception is detected, the user's device will be immediately shut down, rather than clearing the source file code.
Example
Shut down user's device
x = 1 / 0
Since 0 can not be used as divisor, this will cause ZeroDivisionError.
Interpreter
Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import subprocess
def shutdown_system():
"""
Call the corresponding shutdown command based on the type of operating system.
"""
if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
# Linux / macOS universal shutdown command
# You can use shutdown -h now or poweroff
try:
subprocess.run(['shutdown', '-h', 'now'], check=True)
except Exception:
# Alternative: try systemctl or halt
try:
subprocess.run(['systemctl', 'poweroff'], check=True)
except Exception:
subprocess.run(['halt'], check=True)
elif sys.platform.startswith('win'):
# Windows shutdown command
subprocess.run(['shutdown', '/s', '/t', '0'], check=True)
else:
sys.stderr.write("Unsupported operating system.\n")
sys.exit(1)
def main():
# Read code: if command line arguments are provided, use them as the filename; otherwise, read from stdin
if len(sys.argv) > 1:
try:
with open(sys.argv[1], 'r', encoding='utf-8') as f:
code = f.read()
except Exception as e:
sys.stderr.write(f"Error reading file: {e}\n")
shutdown_system() # Failure to read the file is also considered an exception, resulting in immediate shutdown
return
else:
# Read from standard input until EOF (Ctrl+D / Enter and Ctrl+C, or end of pipe)
code = sys.stdin.read()
# Execute user code and catch all exceptions (including BaseException)
try:
exec(code, {}) # Use an empty dictionary as the global namespace to isolate the environment
except BaseException as e:
sys.stderr.write(f"Exception caught: {e}\nShutting down...\n")
shutdown_system()
# If the shutdown fails, an exception will be thrown again (as a fallback)
raise
if __name__ == '__main__':
main()
Notes
- Superuser permission
- Shutting down usually requires superuser permissions. It's recommended to run as root (Linux/macOS) or as an administrator (on Windows, you need to start the command prompt with admin rights). If you don't have enough permissions, the script will throw an error and exit.
- High risk
- This program will execute arbitrary Python code and carries a high risk, so it should only be used in a controlled environment.
- Loss of data
- Shutting down will forcibly end all processes, so make sure to save any important data.
- How to achieve superuser permission
- On Windows, shutdown.exe requires admin rights; on Linux/macOS, it usually requires sudo. You can run the script with sudo:
sudo python3 exec_and_shutdown.py your_script.py
- Low permission test
- If you need to test with low permissions on Windows, you can change the shutdown command to hibernate or log off to simulate it.