Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4609caf

Browse files
committed
Correctly add exceptions to the plpy module for Python 3
The way the exception types where added to the module was wrong forPython 3. Exception classes were not actually available from plpy.Fix that by factoring out code that is responsible for defining newPython exceptions and make it work with Python 3. New regression testmakes sure the plpy module has the expected contents.Jan Urbanśki, slightly revised by me
1 parentd3b372e commit4609caf

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

‎src/pl/plpython/expected/plpython_test.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ select argument_test_one(users, fname, lname) from users where lname = 'doe' ord
3535
willem doe => {fname: willem, lname: doe, userid: 3, username: w_doe}
3636
(3 rows)
3737

38+
-- check module contents
39+
CREATE FUNCTION module_contents() RETURNS text AS
40+
$$
41+
contents = list(filter(lambda x: not x.startswith("__"), dir(plpy)))
42+
contents.sort()
43+
return ", ".join(contents)
44+
$$ LANGUAGE plpythonu;
45+
select module_contents();
46+
module_contents
47+
-------------------------------------------------------------------------------------------
48+
Error, Fatal, SPIError, debug, error, execute, fatal, info, log, notice, prepare, warning
49+
(1 row)
50+
3851
CREATE FUNCTION elog_test() RETURNS void
3952
AS $$
4053
plpy.debug('debug')

‎src/pl/plpython/plpython.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,11 +3268,37 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
32683268
* language handler and interpreter initialization
32693269
*/
32703270

3271+
/*
3272+
* Add exceptions to the plpy module
3273+
*/
3274+
staticvoid
3275+
PLy_add_exceptions(PyObject*plpy)
3276+
{
3277+
PLy_exc_error=PyErr_NewException("plpy.Error",NULL,NULL);
3278+
PLy_exc_fatal=PyErr_NewException("plpy.Fatal",NULL,NULL);
3279+
PLy_exc_spi_error=PyErr_NewException("plpy.SPIError",NULL,NULL);
3280+
3281+
Py_INCREF(PLy_exc_error);
3282+
PyModule_AddObject(plpy,"Error",PLy_exc_error);
3283+
Py_INCREF(PLy_exc_fatal);
3284+
PyModule_AddObject(plpy,"Fatal",PLy_exc_fatal);
3285+
Py_INCREF(PLy_exc_spi_error);
3286+
PyModule_AddObject(plpy,"SPIError",PLy_exc_spi_error);
3287+
}
3288+
32713289
#ifPY_MAJOR_VERSION >=3
32723290
staticPyMODINIT_FUNC
32733291
PyInit_plpy(void)
32743292
{
3275-
returnPyModule_Create(&PLy_module);
3293+
PyObject*m;
3294+
3295+
m=PyModule_Create(&PLy_module);
3296+
if (m==NULL)
3297+
returnNULL;
3298+
3299+
PLy_add_exceptions(m);
3300+
3301+
returnm;
32763302
}
32773303
#endif
32783304

@@ -3363,8 +3389,7 @@ PLy_init_plpy(void)
33633389
PyObject*main_mod,
33643390
*main_dict,
33653391
*plpy_mod;
3366-
PyObject*plpy,
3367-
*plpy_dict;
3392+
PyObject*plpy;
33683393

33693394
/*
33703395
* initialize plpy module
@@ -3376,20 +3401,14 @@ PLy_init_plpy(void)
33763401

33773402
#ifPY_MAJOR_VERSION >=3
33783403
plpy=PyModule_Create(&PLy_module);
3404+
/* for Python 3 we initialized the exceptions in PyInit_plpy */
33793405
#else
33803406
plpy=Py_InitModule("plpy",PLy_methods);
3407+
PLy_add_exceptions(plpy);
33813408
#endif
3382-
plpy_dict=PyModule_GetDict(plpy);
33833409

33843410
/* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
33853411

3386-
PLy_exc_error=PyErr_NewException("plpy.Error",NULL,NULL);
3387-
PLy_exc_fatal=PyErr_NewException("plpy.Fatal",NULL,NULL);
3388-
PLy_exc_spi_error=PyErr_NewException("plpy.SPIError",NULL,NULL);
3389-
PyDict_SetItemString(plpy_dict,"Error",PLy_exc_error);
3390-
PyDict_SetItemString(plpy_dict,"Fatal",PLy_exc_fatal);
3391-
PyDict_SetItemString(plpy_dict,"SPIError",PLy_exc_spi_error);
3392-
33933412
/*
33943413
* initialize main module, and add plpy
33953414
*/

‎src/pl/plpython/sql/plpython_test.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ return words'
2626
select argument_test_one(users, fname, lname)from userswhere lname='doe'order by1;
2727

2828

29+
-- check module contents
30+
CREATEFUNCTIONmodule_contents() RETURNStextAS
31+
$$
32+
contents= list(filter(lambda x: notx.startswith("__"), dir(plpy)))
33+
contents.sort()
34+
return",".join(contents)
35+
$$ LANGUAGE plpythonu;
36+
37+
select module_contents();
38+
39+
2940
CREATEFUNCTIONelog_test() RETURNS void
3041
AS $$
3142
plpy.debug('debug')

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp