I am creating a Python programming language, named Spearhead. I have a working terminal, made via the Subprocess module. The issue is that I am using check_output to catch python errors, and not output them. But for some reason, this doesn't work, and it still produces said errors.
All the code for reference PLEASE READ: [Y_frontend does not matter, the issue is that when i run anything through it that doesnt work, i get a python error, which i dont want, i only want my custom error messages, for example if you type "a" into the terminal, it outputs a python error for it not being a recognizable command. i tried turning shell off, but it just crashes when i do something that would cause an error]:
import subprocess
import sys
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = dir_path + "\\Y_frontend.py"
def run_c(c):
try:
#start terminal and get output
output = subprocess.check_output(c, shell=True, stderr=subprocess.STDOUT)
return output.decode()
except subprocess.CalledProcessError as e:
#incase i need to handle errors later
return e.output().decode()
def main():
while True:
#get user input and check if it is "exit"
u_i = input("cmd< ")
if u_i.lower() == "exit":
break
#get user input and check if it is "+r"
elif u_i[:2] == "+r":
try:
subprocess.run(["python", dir_path, u_i])
except FileNotFoundError:
print("Directory invalid")
continue
else:
print("Invalid command, or shell command")
#actually run the commands provided and print output
output = run_c(u_i)
print(output)
#__name == __main__ so it actually works, although i honestly dont understand this at all, it makes everything work like a charm
if __name__ == '__main__':
main()
Note there is a very similar question that already exists about this issue, but most of what people said to do, either doesn't work, or is like 100x my skill level.