Print error_traceback from script (python)
Evening all, I have a script (python) that has various try/except clauses in it. I would like to be able to get hold of the traceback from within the except block so that I can (optionally) send it as an argument to a user-friendly page summarising how the last lot of transactions went. So, I tried... try: foo() except: tb = error_traceback() AND tb = context.error_traceback() AND tb = context.error_traceback AND tb = context.REQUEST['error_traceback'] all of which fail with any of keyerror, attributeError and possibly some others. Am I even in the right ball-park here? Can I do this? If I'm honest, I don't really know how to do it in straight python, let alone these limited scripts. Thanks if you can help, tim
Tim Hicks writes:
I have a script (python) that has various try/except clauses in it. I would like to be able to get hold of the traceback from within the except block so that I can (optionally) send it as an argument to a user-friendly page summarising how the last lot of transactions went. The "README" tab in the PythonScripts product description (of, if you prefer, the corresponding file in "Products/PythonScripts") tells you how to make modules and functions available in PythonScripts (i.e. lose security restrictions).
To print the traceback, you need "sys.exc_info" and functions from the traceback module (e.g. "print_exception"). Look at the Python documentation, for details. Dieter
Tim Hicks writes:
I have a script (python) that has various try/except clauses in it. I would like to be able to get hold of the traceback from within the except block so that I can (optionally) send it as an argument to a user-friendly page summarising how the last lot of transactions went. The "README" tab in the PythonScripts product description (of, if you prefer, the corresponding file in "Products/PythonScripts") tells you how to make modules and functions available in PythonScripts (i.e. lose security restrictions).
Yup, I've read about this.
To print the traceback, you need "sys.exc_info" and functions from the traceback module (e.g. "print_exception"). Look at the Python documentation, for details.
OK. I thought (hoped) I would be able to get hold of the traceback without going through this because standard_error_message can do it, and I found threads on the list about using <dtml-var error_traceback>. Can I just say at this point (to anyone who's listening) that it would be quite a handy feature (for me anyway) to be able to do this without having to allow the import of extra modules :-). thanks for your help again Dieter, tim
Tim Hicks writes:
OK. I thought (hoped) I would be able to get hold of the traceback without going through this because standard_error_message can do it, and I found threads on the list about using <dtml-var error_traceback>. The "error_*" variables are provided to the error pages by "ZPublisher.HTTPResponse". Outside error pages, you must get hold of them more elementary.
Can I just say at this point (to anyone who's listening) that it would be quite a handy feature (for me anyway) to be able to do this without having to allow the import of extra modules :-). Chris Withers will probably be with you. He already complained about the difficult access to exception information.
Dieter
Tim Hicks writes:
I have a script (python) that has various try/except clauses in it. I would like to be able to get hold of the traceback from within the except block so that I can (optionally) send it as an argument to a user-friendly page summarising how the last lot of transactions went. The "README" tab in the PythonScripts product description (of, if you prefer, the corresponding file in "Products/PythonScripts") tells you how to make modules and functions available in PythonScripts (i.e. lose security restrictions).
To print the traceback, you need "sys.exc_info" and functions from the traceback module (e.g. "print_exception"). Look at the Python documentation, for details.
Further to Dieter's advice, I have created a 'customImports' product with the following __init__.py script: ---Begin __init__.py--- from Products.PythonScripts.Utility import allow_module, allow_class from AccessControl import ModuleSecurityInfo, ClassSecurityInfo from Globals import InitializeClass allow_module('traceback') ModuleSecurityInfo('sys').declarePublic('exc_info') ---End __init__.py--- This has allowd me to get hold of the traceback and exception/error information that I was after. I am just wondering if I've left myself open to security issues by allowing these modules/part-modules in my python scripts. I read that 'sys' is a particular worry, so is there anything bad that can happen by allowing 'exc_info'? And what about allowing the 'traceback' module? cheers tim ps Here is how I use the modules in my script (python). Does this look appropriate? (N.B. aaatest is a ZSQL method) ---Begin script--- from sys import exc_info import traceback try: context.aaatest() except: print context.standard_html_header(context, context.REQUEST) tuple = exc_info() print '<h3>%s</h3>' % tuple[0] print '<h3>%s</h3>' % str(tuple[1]) print traceback.extract_tb(tuple[2]) print context.standard_html_footer(context, context.REQUEST) return printed ---End script---
participants (2)
-
Dieter Maurer -
Tim Hicks