Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f0688d6

Browse files
committed
PL/Python: Clean up extended error reporting docs and tests
Format the example and test code more to Python style standards. Improve whitespace. Improve documentation formatting.
1 parent fab9d1d commit f0688d6

File tree

3 files changed

+162
-141
lines changed

3 files changed

+162
-141
lines changed

doc/src/sgml/plpython.sgml

+29-20
Original file line numberDiff line numberDiff line change
@@ -1341,13 +1341,15 @@ $$ LANGUAGE plpythonu;
13411341
<title>Utility Functions</title>
13421342
<para>
13431343
The <literal>plpy</literal> module also provides the functions
1344-
<literal>plpy.debug(<replaceable>msg, **kwargs</>)</literal>,
1345-
<literal>plpy.log(<replaceable>msg, **kwargs</>)</literal>,
1346-
<literal>plpy.info(<replaceable>msg, **kwargs</>)</literal>,
1347-
<literal>plpy.notice(<replaceable>msg, **kwargs</>)</literal>,
1348-
<literal>plpy.warning(<replaceable>msg, **kwargs</>)</literal>,
1349-
<literal>plpy.error(<replaceable>msg, **kwargs</>)</literal>, and
1350-
<literal>plpy.fatal(<replaceable>msg, **kwargs</>)</literal>.
1344+
<simplelist>
1345+
<member><literal>plpy.debug(<replaceable>msg, **kwargs</>)</literal></member>
1346+
<member><literal>plpy.log(<replaceable>msg, **kwargs</>)</literal></member>
1347+
<member><literal>plpy.info(<replaceable>msg, **kwargs</>)</literal></member>
1348+
<member><literal>plpy.notice(<replaceable>msg, **kwargs</>)</literal></member>
1349+
<member><literal>plpy.warning(<replaceable>msg, **kwargs</>)</literal></member>
1350+
<member><literal>plpy.error(<replaceable>msg, **kwargs</>)</literal></member>
1351+
<member><literal>plpy.fatal(<replaceable>msg, **kwargs</>)</literal></member>
1352+
</simplelist>
13511353
<indexterm><primary>elog</><secondary>in PL/Python</></indexterm>
13521354
<function>plpy.error</function> and <function>plpy.fatal</function>
13531355
actually raise a Python exception which, if uncaught, propagates out to
@@ -1366,35 +1368,42 @@ $$ LANGUAGE plpythonu;
13661368
</para>
13671369

13681370
<para>
1369-
13701371
The <replaceable>msg</> argument is given as a positional argument. For
13711372
backward compatibility, more than one positional argument can be given. In
13721373
that case, the string representation of the tuple of positional arguments
13731374
becomes the message reported to the client.
1375+
</para>
1376+
1377+
<para>
13741378
The following keyword-only arguments are accepted:
1375-
<literal>
1376-
<replaceable>detail</replaceable>, <replaceable>hint</replaceable>,
1377-
<replaceable>sqlstate</replaceable>, <replaceable>schema_name</replaceable>,
1378-
<replaceable>table_name</replaceable>, <replaceable>column_name</replaceable>,
1379-
<replaceable>datatype_name</replaceable> , <replaceable>constraint_name</replaceable>
1380-
</literal>.
1379+
<simplelist>
1380+
<member><literal>detail</literal></member>
1381+
<member><literal>hint</literal></member>
1382+
<member><literal>sqlstate</literal></member>
1383+
<member><literal>schema_name</literal></member>
1384+
<member><literal>table_name</literal></member>
1385+
<member><literal>column_name</literal></member>
1386+
<member><literal>datatype_name</literal></member>
1387+
<member><literal>constraint_name</literal></member>
1388+
</simplelist>
13811389
The string representation of the objects passed as keyword-only arguments
13821390
is used to enrich the messages reported to the client. For example:
13831391

13841392
<programlisting>
13851393
CREATE FUNCTION raise_custom_exception() RETURNS void AS $$
1386-
plpy.error("custom exception message", detail = "some info about exception", hint = "hint for users")
1394+
plpy.error("custom exception message",
1395+
detail="some info about exception",
1396+
hint="hint for users")
13871397
$$ LANGUAGE plpythonu;
13881398

1389-
postgres=# select raise_custom_exception();
1390-
ERROR: XX000: plpy.Error: custom exception message
1399+
=# SELECT raise_custom_exception();
1400+
ERROR: plpy.Error: custom exception message
13911401
DETAIL: some info about exception
13921402
HINT: hint for users
13931403
CONTEXT: Traceback (most recent call last):
1394-
PL/Python function "raise_custom_exception", line 2, in &lt;module&gt;
1395-
plpy.error("custom exception message", detail = "some info about exception", hint = "hint for users")
1404+
PL/Python function "raise_custom_exception", line 4, in &lt;module&gt;
1405+
hint="hint for users")
13961406
PL/Python function "raise_custom_exception"
1397-
LOCATION: PLy_elog, plpy_elog.c:132
13981407
</programlisting>
13991408
</para>
14001409

src/pl/plpython/expected/plpython_ereport.out

+69-63
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
CREATE FUNCTION elog_test() RETURNS void
22
AS $$
3-
plpy.debug('debug', detail = 'some detail')
4-
plpy.log('log', detail = 'some detail')
5-
plpy.info('info', detail = 'some detail')
3+
plpy.debug('debug', detail='some detail')
4+
plpy.log('log', detail='some detail')
5+
plpy.info('info', detail='some detail')
66
plpy.info()
7-
plpy.info('the question', detail = 42);
7+
plpy.info('the question', detail=42);
88
plpy.info('This is message text.',
9-
detail = 'This is detail text',
10-
hint = 'This is hint text.',
11-
sqlstate = 'XX000',
12-
schema_name = 'any info about schema',
13-
table_name = 'any info about table',
14-
column_name = 'any info about column',
15-
datatype_name = 'any info about datatype',
16-
constraint_name = 'any info about constraint')
17-
plpy.notice('notice', detail = 'some detail')
18-
plpy.warning('warning', detail = 'some detail')
19-
plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
9+
detail='This is detail text',
10+
hint='This is hint text.',
11+
sqlstate='XX000',
12+
schema_name='any info about schema',
13+
table_name='any info about table',
14+
column_name='any info about column',
15+
datatype_name='any info about datatype',
16+
constraint_name='any info about constraint')
17+
plpy.notice('notice', detail='some detail')
18+
plpy.warning('warning', detail='some detail')
19+
plpy.error('stop on error', detail='some detail', hint='some hint')
2020
$$ LANGUAGE plpythonu;
2121
SELECT elog_test();
2222
INFO: info
@@ -36,92 +36,98 @@ DETAIL: some detail
3636
HINT: some hint
3737
CONTEXT: Traceback (most recent call last):
3838
PL/Python function "elog_test", line 18, in <module>
39-
plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
39+
plpy.error('stop on error', detail='some detail', hint='some hint')
4040
PL/Python function "elog_test"
41-
do $$ plpy.info('other types', detail = (10,20)) $$ LANGUAGE plpythonu;
41+
DO $$ plpy.info('other types', detail=(10, 20)) $$ LANGUAGE plpythonu;
4242
INFO: other types
4343
DETAIL: (10, 20)
44-
do $$
44+
DO $$
4545
import time;
4646
from datetime import date
47-
plpy.info('other types', detail = date(2016,2,26))
47+
plpy.info('other types', detail=date(2016, 2, 26))
4848
$$ LANGUAGE plpythonu;
4949
INFO: other types
5050
DETAIL: 2016-02-26
51-
do $$
51+
DO $$
5252
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
53-
plpy.info('other types', detail = basket)
53+
plpy.info('other types', detail=basket)
5454
$$ LANGUAGE plpythonu;
5555
INFO: other types
5656
DETAIL: ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
5757
-- should fail
58-
do $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
58+
DO $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
5959
ERROR: invalid SQLSTATE code
6060
CONTEXT: PL/Python anonymous code block
61-
do $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
61+
DO $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
6262
ERROR: 'blabla' is an invalid keyword argument for this function
6363
CONTEXT: PL/Python anonymous code block
64-
do $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
64+
DO $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
6565
ERROR: the message is already specified
6666
CONTEXT: PL/Python anonymous code block
67-
do $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
67+
DO $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
6868
ERROR: the message is already specified
6969
CONTEXT: PL/Python anonymous code block
7070
-- raise exception in python, handle exception in plgsql
7171
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
72-
_sqlstate text DEFAULT NULL,
73-
_schema_name text DEFAULT NULL, _table_name text DEFAULT NULL, _column_name text DEFAULT NULL,
74-
_datatype_name text DEFAULT NULL, _constraint_name text DEFAULT NULL)
72+
_sqlstate text DEFAULT NULL,
73+
_schema_name text DEFAULT NULL,
74+
_table_name text DEFAULT NULL,
75+
_column_name text DEFAULT NULL,
76+
_datatype_name text DEFAULT NULL,
77+
_constraint_name text DEFAULT NULL)
7578
RETURNS void AS $$
76-
kwargs = { "message":_message, "detail":_detail, "hint":_hint,
77-
"sqlstate":_sqlstate, "schema_name":_schema_name, "table_name":_table_name,
78-
"column_name":_column_name, "datatype_name":_datatype_name, "constraint_name":_constraint_name }
79+
kwargs = {
80+
"message": _message, "detail": _detail, "hint": _hint,
81+
"sqlstate": _sqlstate, "schema_name": _schema_name, "table_name": _table_name,
82+
"column_name": _column_name, "datatype_name": _datatype_name,
83+
"constraint_name": _constraint_name
84+
}
7985
# ignore None values - should work on Python2.3
8086
dict = {}
8187
for k in kwargs:
82-
if kwargs[k] is not None:
83-
dict[k] = kwargs[k]
88+
if kwargs[k] is not None:
89+
dict[k] = kwargs[k]
8490
plpy.error(**dict)
8591
$$ LANGUAGE plpythonu;
8692
SELECT raise_exception('hello', 'world');
8793
ERROR: plpy.Error: hello
8894
DETAIL: world
8995
CONTEXT: Traceback (most recent call last):
90-
PL/Python function "raise_exception", line 10, in <module>
96+
PL/Python function "raise_exception", line 13, in <module>
9197
plpy.error(**dict)
9298
PL/Python function "raise_exception"
9399
SELECT raise_exception('message text', 'detail text', _sqlstate => 'YY333');
94100
ERROR: plpy.Error: message text
95101
DETAIL: detail text
96102
CONTEXT: Traceback (most recent call last):
97-
PL/Python function "raise_exception", line 10, in <module>
103+
PL/Python function "raise_exception", line 13, in <module>
98104
plpy.error(**dict)
99105
PL/Python function "raise_exception"
100106
SELECT raise_exception(_message => 'message text',
101-
_detail => 'detail text',
102-
_hint => 'hint text',
103-
_sqlstate => 'XX555',
104-
_schema_name => 'schema text',
105-
_table_name => 'table text',
106-
_column_name => 'column text',
107-
_datatype_name => 'datatype text',
108-
_constraint_name => 'constraint text');
107+
_detail => 'detail text',
108+
_hint => 'hint text',
109+
_sqlstate => 'XX555',
110+
_schema_name => 'schema text',
111+
_table_name => 'table text',
112+
_column_name => 'column text',
113+
_datatype_name => 'datatype text',
114+
_constraint_name => 'constraint text');
109115
ERROR: plpy.Error: message text
110116
DETAIL: detail text
111117
HINT: hint text
112118
CONTEXT: Traceback (most recent call last):
113-
PL/Python function "raise_exception", line 10, in <module>
119+
PL/Python function "raise_exception", line 13, in <module>
114120
plpy.error(**dict)
115121
PL/Python function "raise_exception"
116122
SELECT raise_exception(_message => 'message text',
117-
_hint => 'hint text',
118-
_schema_name => 'schema text',
119-
_column_name => 'column text',
120-
_constraint_name => 'constraint text');
123+
_hint => 'hint text',
124+
_schema_name => 'schema text',
125+
_column_name => 'column text',
126+
_constraint_name => 'constraint text');
121127
ERROR: plpy.Error: message text
122128
HINT: hint text
123129
CONTEXT: Traceback (most recent call last):
124-
PL/Python function "raise_exception", line 10, in <module>
130+
PL/Python function "raise_exception", line 13, in <module>
125131
plpy.error(**dict)
126132
PL/Python function "raise_exception"
127133
DO $$
@@ -157,34 +163,34 @@ BEGIN
157163
__datatype_name = PG_DATATYPE_NAME,
158164
__constraint_name = CONSTRAINT_NAME;
159165
RAISE NOTICE 'handled exception'
160-
USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
161-
'schema_name:(%s), table_name:(%s), column_name:(%s), datatype_name:(%s), constraint_name:(%s)',
162-
__message, __detail, __hint, __sqlstate, __schema_name,
163-
__table_name, __column_name, __datatype_name, __constraint_name);
166+
USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
167+
'schema_name:(%s), table_name:(%s), column_name:(%s), datatype_name:(%s), constraint_name:(%s)',
168+
__message, __detail, __hint, __sqlstate, __schema_name,
169+
__table_name, __column_name, __datatype_name, __constraint_name);
164170
END;
165171
END;
166172
$$;
167173
NOTICE: handled exception
168174
DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint text), sqlstate: (XX555), schema_name:(schema text), table_name:(table text), column_name:(column text), datatype_name:(datatype text), constraint_name:(constraint text)
169-
-- the displayed context is different between Python2 and Python3,
170-
-- but that's not important for this test
175+
-- The displayed context is different between Python2 and Python3,
176+
-- but that's not important for this test.
171177
\set SHOW_CONTEXT never
172-
do $$
178+
DO $$
173179
try:
174-
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
180+
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
175181
except Exception, e:
176-
plpy.info(e.spidata)
177-
raise e
182+
plpy.info(e.spidata)
183+
raise e
178184
$$ LANGUAGE plpythonu;
179185
INFO: (119577128, None, 'some hint', None, 0, None, 'users_tab', None, 'user_type', None)
180186
ERROR: plpy.SPIError: plpy.Error: my message
181187
HINT: some hint
182-
do $$
188+
DO $$
183189
try:
184-
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
190+
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
185191
except Exception, e:
186-
plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
187-
raise e
192+
plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
193+
raise e
188194
$$ LANGUAGE plpythonu;
189195
INFO: sqlstate: XX987, hint: some hint, table_name: users_tab, datatype_name: user_type
190196
ERROR: plpy.Error: my message

0 commit comments

Comments
 (0)