2

I have a python application that communicates with a PHP application. Right now, I have defined error codes on both sides so they match (i.e. we can detect the python-side failures in PHP and react accordingly).

My problem comes from python being an interpreted language, here is why : If the code has a syntax problem, then the interpreter will return error codes. But these interpreter error codes will be indistinguishable from the application errors.

I am not in charge of the python code, but I was asked to be able to detect eventual python interpreter failures so I can notify the end user using PHP.

Because of that, I need to find a list of the interpreter return codes when malfunctionning. I failed to find this information on both google, python doc, and man page.

Does anybody have/know where to find this information ? (also, if you have any other ideas on how to get around this problem, I'd be happy to hear them)

Thanks in advance !

5
  • 1
    What do you mean by "error codes on both sides so that they match"? The Python interpreter will raise exceptions for all errors (or else it's a bug in the interpreter). You should be able to catch these and massage them into a format that the PHP app can understand as per your communication protocol. Commented May 9, 2012 at 12:23
  • can't you just make sure your python scripts don't have syntax errors? you shouldn't be executing code you don't control on your servers anyway... Commented May 9, 2012 at 12:25
  • @NoufalIbrahim "error codes on both sides so that they match" => the PHP part is the user interface. I need to be able to know what exactly was the cause of the error (using exit/return codes) so that I react accordingly on the PHP part. Commented May 9, 2012 at 13:13
  • @DarenThomas I am only in charge of the PHP part and have no influence over the Python part. If the python developers did a mistake there is nothing I can do about it, but I was asked to be able to detect it and react accordingly on the PHP interface. Commented May 9, 2012 at 13:18
  • I have edited my question to reflect the fact that I was asked to be able to detect interpreter failures, and that I am not the one coding the python part, having little influence over it. Commented May 9, 2012 at 14:03

2 Answers 2

2

The best solution would be setting an exception hook that always exits with a certain code.

import sys
def excepthook(type, value, traceback):
    sys.exit(X) # X is your exit code
sys.excepthook = excepthook

Syntax errors should be irrelevant - why would you ever put a script that contains syntax errors in production?

But anyway, Python always exits with a non-zero code (apparently always 1) in case of an uncaught exception and 0 if everything went fine.

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

5 Comments

I agree with you that syntax errors should be irrelevant. But I am not the one coding the python part and I was asked to be able to detect these errors and display the according message to the end user with PHP. As for the non-zero code, it "apparently" is always one, but I don't want to take any wild guess. If it somehow starts returning 2 for another error type, this'll have to begin all over again. I'm liking your excepthook idea and I'm going to try :)
I've tried your solution and it doesn't work in my case. Here is why : the python interpreter checks for syntax error before running the code (obviously so, I'm quite ashamed I didn't even think of that), and so the modified excepthook isn't executed yet. In short, it won't be able to take care of python interpreter errors. If what you were thinking about was using it to have all python-side errors fall under the same error code, this isn't possible since the error codes is precisely how we differentiate errors PHP-side.
I like this excepthook suggestion. I'd add that you probably should be pylint'ing (or pychecker'ing or at least pyflakes'ing) your python code before deploying it into production.
Oh. I think get it. So you were saying that "1" would be syntax error and that by using excepthook I would be able to get all uncaught exceptions (meaning most if not all other bad programming errors). I'll use this. Thanks a lot !
Syntax errors can be in production. I've got such a thing currently: One module was written using Python 2.7 features (namely the with statement). The product was installed on two machines, one quite old only has a Python 2.4 installed. In Python 2.4 this otherwise valid code contains a syntax error for each with statement. That modern module isn't needed on the old machine, though, so I just put a try/except around the import of the module. Conclusion: Syntax errors can on purpose occur in production systems.
1

I believe this might be what you need. It will map the error codes to their respective string messages.

http://docs.python.org/library/errno.html

Specifically:

http://docs.python.org/library/os.html#os.strerror

2 Comments

You're welcome. This would map the error codes to the messages which is was the original OP wanted. Granted did does not provide a mechanism to translate automatically.
This seems to be only a way to communicate with OS error codes using python. Neither the application (with its own error codes) nor the interpreter (with its own other error codes) have anything to do with this error code system I'm afraid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.