Embedded System - Trapping Errors

Jeff Collins collins at seal.aero.org
Thu Jul 20 15:53:20 EDT 2000


StringIO provides a file object interface to string buffers.  In
python, you can do the following:
>>> import sys
>>> stderr_save = sys.stderr
>>> import StringIO
>>> sys.stderr = StringIO.StringIO()
>>> float("jeff")
>>> sys.stderr.getvalue()
'Traceback (most recent call last):\012  File "<stdin>", line 1, in ?\012ValueError: invalid literal for float(): jeff\012Traceback (most recent call last):\012  File "<stdin>", line 1, in ?\012AttributeError: \'StringIO\' instance has no attribute \'getresult\'\012'
>>> sys.stderr = stderr_save
>>> float("jeff")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): jeff
>>> 

To get the equivalent working in C, try using cStringIO.c (C-based
replacement for StringIO.py) - there is a CAPI available.  Set up the
stderr attribute of the sysmodule with a cStringIO instance to
capture the error and parse the contents when PyRun_AnyFile()
completes.

I haven't tried the above, but I hope it helps.

Jeff


nelson_chenkin at seagate.com writes:
 > I am embedding Python on a machine that does not have an output device.
 > A Python script file is sent down to this machine from a host via an
 > Ethernet connection and is then executed remotely. This is all working
 > like a charm.
 > 
 > Now I want to start intercepting script errors, converting the errors to
 > an format of my own design, then send them back up to the host machine
 > where they can be reported to the user.
 > 
 > Can this be done? I have stepped through the code in PyArg_ParseTuple()
 > to see what happpens when I intentionally provide the wrong number of
 > arguments. It seems like all this error handling is hard coded. There is
 > not any indirection that allows me to install an error handling hook.
 > 
 > I am running the script using PyRun_AnyFile(). By the time that returns
 > to me the error has been cleared - the hardcoded call to PyErr_Print()
 > which then calls PyErr_Fetch() clears it.
 > 
 > Obviously I can change the source code, but this presents a problem when
 > new Pythons versions are released.
 > 
 > Any help would be appreciated.
 > Thanks,
 > NC
 > 
 > 
 > Sent via Deja.com http://www.deja.com/
 > Before you buy.
 > -- 
 > http://www.python.org/mailman/listinfo/python-list
 > 




More information about the Python-list mailing list