0

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.

4
  • 1
    This question is similar to: How to catch exception output from Python subprocess.check_output()?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 24, 2024 at 1:05
  • Also, please produce a minimal reproducible example, as well as the error + traceback as code-formatted text. Commented Aug 24, 2024 at 3:02
  • no errors, it just doesnt work the way i want it to Commented Aug 24, 2024 at 13:33
  • also for a minimal example just put this in a folder with a file named Y_frontend, it doesnt need to do anything to see the issue. Commented Aug 24, 2024 at 13:50

2 Answers 2

1

Solution 1: According to your code, have you inputted the line to run the function? If so you should because it may be the reason why your code isn't running properly or even isn't running at all.Within the "run_c(c)" function, the c variable should be defined aswell.

Solution 2: Within the line of output, try using .run instead of .checkoutput. According to the python documentation 3.12.5, the .run function when called upon would return the CompletedProcess and by then captures the stdout and stderr, you could single out the stderr and delibriately decode the stderr from there.Moreover, you could check the capture_output into true if you would like to go more detail about the output from there and check for errors.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Fixed the problem by using DEVNULL and text=true. Basically i did random things until it works, i dont even really understand it myself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.