Database Reference
In-Depth Information
CREATE OR REPLACE FUNCTION fact(x int)
RETURNS int
AS $$
global x
assert x>=0, "argument must be a positive
integer"
f = 1
while (x > 0):
f = f * x
x = x - 1
return f
$$ LANGUAGE plpythonu;
To test this, call fact() with a negative number:
hannu=# select fact(-1);
ERROR: AssertionError: argument must be a
positive integer
CONTEXT: Traceback (most recent call last):
PL/Python function "fact", line 3, in <module>
assert x>=0, "argument must be a positive
integer"
PL/Python function "fact"
You will get a message about AssertionError together with the location of the
failing line number.
Redirecting sys.stdout and sys.stderr
If all the code you need to debug is your own, the preceding two techniques will
cover most of your needs. However, what do you do in cases where you use
some third party libraries which print out debug information to sys.stdout and/or
sys.stderr ?
Well, in this case you can replace Python's sys.stdout and sys.stdin with your
own pseudo file object that stores everything written there for later retrieval. Here is
a pair of functions. The first of which does the capturing of sys.stdout or uncap-
Search WWH ::




Custom Search