LCOV - differential code coverage report
Current view: top level - src/pl/plpython - plpy_plpymodule.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 89.7 % 185 166 19 166
Current Date: 2025-09-06 07:49:51 +0900 Functions: 94.1 % 17 16 1 16
Baseline: lcov-20250906-005545-baseline Branches: 80.4 % 112 90 22 90
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 89.7 % 185 166 19 166
Function coverage date bins:
(360..) days: 94.1 % 17 16 1 16
Branch coverage date bins:
(360..) days: 80.4 % 112 90 22 90

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * the plpy module
                                  3                 :                :  *
                                  4                 :                :  * src/pl/plpython/plpy_plpymodule.c
                                  5                 :                :  */
                                  6                 :                : 
                                  7                 :                : #include "postgres.h"
                                  8                 :                : 
                                  9                 :                : #include "mb/pg_wchar.h"
                                 10                 :                : #include "plpy_cursorobject.h"
                                 11                 :                : #include "plpy_elog.h"
                                 12                 :                : #include "plpy_planobject.h"
                                 13                 :                : #include "plpy_plpymodule.h"
                                 14                 :                : #include "plpy_resultobject.h"
                                 15                 :                : #include "plpy_spi.h"
                                 16                 :                : #include "plpy_subxactobject.h"
                                 17                 :                : #include "plpy_util.h"
                                 18                 :                : #include "utils/builtins.h"
                                 19                 :                : 
                                 20                 :                : HTAB       *PLy_spi_exceptions = NULL;
                                 21                 :                : 
                                 22                 :                : 
                                 23                 :                : static void PLy_add_exceptions(PyObject *plpy);
                                 24                 :                : static PyObject *PLy_create_exception(char *name,
                                 25                 :                :                                       PyObject *base, PyObject *dict,
                                 26                 :                :                                       const char *modname, PyObject *mod);
                                 27                 :                : static void PLy_generate_spi_exceptions(PyObject *mod, PyObject *base);
                                 28                 :                : 
                                 29                 :                : /* module functions */
                                 30                 :                : static PyObject *PLy_debug(PyObject *self, PyObject *args, PyObject *kw);
                                 31                 :                : static PyObject *PLy_log(PyObject *self, PyObject *args, PyObject *kw);
                                 32                 :                : static PyObject *PLy_info(PyObject *self, PyObject *args, PyObject *kw);
                                 33                 :                : static PyObject *PLy_notice(PyObject *self, PyObject *args, PyObject *kw);
                                 34                 :                : static PyObject *PLy_warning(PyObject *self, PyObject *args, PyObject *kw);
                                 35                 :                : static PyObject *PLy_error(PyObject *self, PyObject *args, PyObject *kw);
                                 36                 :                : static PyObject *PLy_fatal(PyObject *self, PyObject *args, PyObject *kw);
                                 37                 :                : static PyObject *PLy_quote_literal(PyObject *self, PyObject *args);
                                 38                 :                : static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args);
                                 39                 :                : static PyObject *PLy_quote_ident(PyObject *self, PyObject *args);
                                 40                 :                : 
                                 41                 :                : 
                                 42                 :                : /* A list of all known exceptions, generated from backend/utils/errcodes.txt */
                                 43                 :                : typedef struct ExceptionMap
                                 44                 :                : {
                                 45                 :                :     char       *name;
                                 46                 :                :     char       *classname;
                                 47                 :                :     int         sqlstate;
                                 48                 :                : } ExceptionMap;
                                 49                 :                : 
                                 50                 :                : static const ExceptionMap exception_map[] = {
                                 51                 :                : #include "spiexceptions.h"
                                 52                 :                :     {NULL, NULL, 0}
                                 53                 :                : };
                                 54                 :                : 
                                 55                 :                : static PyMethodDef PLy_methods[] = {
                                 56                 :                :     /*
                                 57                 :                :      * logging methods
                                 58                 :                :      */
                                 59                 :                :     {"debug", (PyCFunction) (pg_funcptr_t) PLy_debug, METH_VARARGS | METH_KEYWORDS, NULL},
                                 60                 :                :     {"log", (PyCFunction) (pg_funcptr_t) PLy_log, METH_VARARGS | METH_KEYWORDS, NULL},
                                 61                 :                :     {"info", (PyCFunction) (pg_funcptr_t) PLy_info, METH_VARARGS | METH_KEYWORDS, NULL},
                                 62                 :                :     {"notice", (PyCFunction) (pg_funcptr_t) PLy_notice, METH_VARARGS | METH_KEYWORDS, NULL},
                                 63                 :                :     {"warning", (PyCFunction) (pg_funcptr_t) PLy_warning, METH_VARARGS | METH_KEYWORDS, NULL},
                                 64                 :                :     {"error", (PyCFunction) (pg_funcptr_t) PLy_error, METH_VARARGS | METH_KEYWORDS, NULL},
                                 65                 :                :     {"fatal", (PyCFunction) (pg_funcptr_t) PLy_fatal, METH_VARARGS | METH_KEYWORDS, NULL},
                                 66                 :                : 
                                 67                 :                :     /*
                                 68                 :                :      * create a stored plan
                                 69                 :                :      */
                                 70                 :                :     {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
                                 71                 :                : 
                                 72                 :                :     /*
                                 73                 :                :      * execute a plan or query
                                 74                 :                :      */
                                 75                 :                :     {"execute", PLy_spi_execute, METH_VARARGS, NULL},
                                 76                 :                : 
                                 77                 :                :     /*
                                 78                 :                :      * escaping strings
                                 79                 :                :      */
                                 80                 :                :     {"quote_literal", PLy_quote_literal, METH_VARARGS, NULL},
                                 81                 :                :     {"quote_nullable", PLy_quote_nullable, METH_VARARGS, NULL},
                                 82                 :                :     {"quote_ident", PLy_quote_ident, METH_VARARGS, NULL},
                                 83                 :                : 
                                 84                 :                :     /*
                                 85                 :                :      * create the subtransaction context manager
                                 86                 :                :      */
                                 87                 :                :     {"subtransaction", PLy_subtransaction_new, METH_NOARGS, NULL},
                                 88                 :                : 
                                 89                 :                :     /*
                                 90                 :                :      * create a cursor
                                 91                 :                :      */
                                 92                 :                :     {"cursor", PLy_cursor, METH_VARARGS, NULL},
                                 93                 :                : 
                                 94                 :                :     /*
                                 95                 :                :      * transaction control
                                 96                 :                :      */
                                 97                 :                :     {"commit", PLy_commit, METH_NOARGS, NULL},
                                 98                 :                :     {"rollback", PLy_rollback, METH_NOARGS, NULL},
                                 99                 :                : 
                                100                 :                :     {NULL, NULL, 0, NULL}
                                101                 :                : };
                                102                 :                : 
                                103                 :                : static PyMethodDef PLy_exc_methods[] = {
                                104                 :                :     {NULL, NULL, 0, NULL}
                                105                 :                : };
                                106                 :                : 
                                107                 :                : static PyModuleDef PLy_module = {
                                108                 :                :     PyModuleDef_HEAD_INIT,
                                109                 :                :     .m_name = "plpy",
                                110                 :                :     .m_size = -1,
                                111                 :                :     .m_methods = PLy_methods,
                                112                 :                : };
                                113                 :                : 
                                114                 :                : static PyModuleDef PLy_exc_module = {
                                115                 :                :     PyModuleDef_HEAD_INIT,
                                116                 :                :     .m_name = "spiexceptions",
                                117                 :                :     .m_size = -1,
                                118                 :                :     .m_methods = PLy_exc_methods,
                                119                 :                : };
                                120                 :                : 
                                121                 :                : /*
                                122                 :                :  * Must have external linkage, because PyMODINIT_FUNC does dllexport on
                                123                 :                :  * Windows-like platforms.
                                124                 :                :  */
                                125                 :                : PyMODINIT_FUNC
 5011 peter_e@gmx.net           126                 :CBC          23 : PyInit_plpy(void)
                                127                 :                : {
                                128                 :                :     PyObject   *m;
                                129                 :                : 
                                130                 :             23 :     m = PyModule_Create(&PLy_module);
                                131         [ -  + ]:             23 :     if (m == NULL)
 5011 peter_e@gmx.net           132                 :UBC           0 :         return NULL;
                                133                 :                : 
 5011 peter_e@gmx.net           134                 :CBC          23 :     PLy_add_exceptions(m);
                                135                 :                : 
                                136                 :             23 :     return m;
                                137                 :                : }
                                138                 :                : 
                                139                 :                : void
                                140                 :             23 : PLy_init_plpy(void)
                                141                 :                : {
                                142                 :                :     PyObject   *main_mod,
                                143                 :                :                *main_dict,
                                144                 :                :                *plpy_mod;
                                145                 :                : 
                                146                 :                :     /*
                                147                 :                :      * initialize plpy module
                                148                 :                :      */
                                149                 :             23 :     PLy_plan_init_type();
                                150                 :             23 :     PLy_result_init_type();
                                151                 :             23 :     PLy_subtransaction_init_type();
                                152                 :             23 :     PLy_cursor_init_type();
                                153                 :                : 
                                154                 :             23 :     PyModule_Create(&PLy_module);
                                155                 :                : 
                                156                 :                :     /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
                                157                 :                : 
                                158                 :                :     /*
                                159                 :                :      * initialize main module, and add plpy
                                160                 :                :      */
                                161                 :             23 :     main_mod = PyImport_AddModule("__main__");
                                162                 :             23 :     main_dict = PyModule_GetDict(main_mod);
                                163                 :             23 :     plpy_mod = PyImport_AddModule("plpy");
 4925 tgl@sss.pgh.pa.us         164         [ -  + ]:             23 :     if (plpy_mod == NULL)
 4882 peter_e@gmx.net           165                 :UBC           0 :         PLy_elog(ERROR, "could not import \"plpy\" module");
 5011 peter_e@gmx.net           166                 :CBC          23 :     PyDict_SetItemString(main_dict, "plpy", plpy_mod);
                                167         [ -  + ]:             23 :     if (PyErr_Occurred())
 4882 peter_e@gmx.net           168                 :UBC           0 :         PLy_elog(ERROR, "could not import \"plpy\" module");
 5011 peter_e@gmx.net           169                 :CBC          23 : }
                                170                 :                : 
                                171                 :                : static void
                                172                 :             23 : PLy_add_exceptions(PyObject *plpy)
                                173                 :                : {
                                174                 :                :     PyObject   *excmod;
                                175                 :                :     HASHCTL     hash_ctl;
                                176                 :                : 
                                177                 :             23 :     excmod = PyModule_Create(&PLy_exc_module);
 3193 tgl@sss.pgh.pa.us         178         [ -  + ]:             23 :     if (excmod == NULL)
 3193 tgl@sss.pgh.pa.us         179                 :UBC           0 :         PLy_elog(ERROR, "could not create the spiexceptions module");
                                180                 :                : 
                                181                 :                :     /*
                                182                 :                :      * PyModule_AddObject does not add a refcount to the object, for some odd
                                183                 :                :      * reason; we must do that.
                                184                 :                :      */
                                185                 :                :     Py_INCREF(excmod);
 3193 tgl@sss.pgh.pa.us         186         [ -  + ]:CBC          23 :     if (PyModule_AddObject(plpy, "spiexceptions", excmod) < 0)
 3193 tgl@sss.pgh.pa.us         187                 :UBC           0 :         PLy_elog(ERROR, "could not add the spiexceptions module");
                                188                 :                : 
 3193 tgl@sss.pgh.pa.us         189                 :CBC          23 :     PLy_exc_error = PLy_create_exception("plpy.Error", NULL, NULL,
                                190                 :                :                                          "Error", plpy);
                                191                 :             23 :     PLy_exc_fatal = PLy_create_exception("plpy.Fatal", NULL, NULL,
                                192                 :                :                                          "Fatal", plpy);
                                193                 :             23 :     PLy_exc_spi_error = PLy_create_exception("plpy.SPIError", NULL, NULL,
                                194                 :                :                                              "SPIError", plpy);
                                195                 :                : 
 5011 peter_e@gmx.net           196                 :             23 :     hash_ctl.keysize = sizeof(int);
                                197                 :             23 :     hash_ctl.entrysize = sizeof(PLyExceptionEntry);
 3193 tgl@sss.pgh.pa.us         198                 :             23 :     PLy_spi_exceptions = hash_create("PL/Python SPI exceptions", 256,
                                199                 :                :                                      &hash_ctl, HASH_ELEM | HASH_BLOBS);
                                200                 :                : 
 5011 peter_e@gmx.net           201                 :             23 :     PLy_generate_spi_exceptions(excmod, PLy_exc_spi_error);
                                202                 :             23 : }
                                203                 :                : 
                                204                 :                : /*
                                205                 :                :  * Create an exception object and add it to the module
                                206                 :                :  */
                                207                 :                : static PyObject *
 3193 tgl@sss.pgh.pa.us         208                 :           5842 : PLy_create_exception(char *name, PyObject *base, PyObject *dict,
                                209                 :                :                      const char *modname, PyObject *mod)
                                210                 :                : {
                                211                 :                :     PyObject   *exc;
                                212                 :                : 
                                213                 :           5842 :     exc = PyErr_NewException(name, base, dict);
                                214         [ -  + ]:           5842 :     if (exc == NULL)
 2867 peter_e@gmx.net           215                 :UBC           0 :         PLy_elog(ERROR, NULL);
                                216                 :                : 
                                217                 :                :     /*
                                218                 :                :      * PyModule_AddObject does not add a refcount to the object, for some odd
                                219                 :                :      * reason; we must do that.
                                220                 :                :      */
                                221                 :                :     Py_INCREF(exc);
 3193 tgl@sss.pgh.pa.us         222                 :CBC        5842 :     PyModule_AddObject(mod, modname, exc);
                                223                 :                : 
                                224                 :                :     /*
                                225                 :                :      * The caller will also store a pointer to the exception object in some
                                226                 :                :      * permanent variable, so add another ref to account for that.  This is
                                227                 :                :      * probably excessively paranoid, but let's be sure.
                                228                 :                :      */
                                229                 :                :     Py_INCREF(exc);
                                230                 :           5842 :     return exc;
                                231                 :                : }
                                232                 :                : 
                                233                 :                : /*
                                234                 :                :  * Add all the autogenerated exceptions as subclasses of SPIError
                                235                 :                :  */
                                236                 :                : static void
 5011 peter_e@gmx.net           237                 :             23 : PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
                                238                 :                : {
                                239                 :                :     int         i;
                                240                 :                : 
                                241         [ +  + ]:           5796 :     for (i = 0; exception_map[i].name != NULL; i++)
                                242                 :                :     {
                                243                 :                :         bool        found;
                                244                 :                :         PyObject   *exc;
                                245                 :                :         PLyExceptionEntry *entry;
                                246                 :                :         PyObject   *sqlstate;
                                247                 :           5773 :         PyObject   *dict = PyDict_New();
                                248                 :                : 
 4925 tgl@sss.pgh.pa.us         249         [ -  + ]:           5773 :         if (dict == NULL)
 2867 peter_e@gmx.net           250                 :UBC           0 :             PLy_elog(ERROR, NULL);
                                251                 :                : 
 1279 andres@anarazel.de        252                 :CBC        5773 :         sqlstate = PLyUnicode_FromString(unpack_sql_state(exception_map[i].sqlstate));
 4925 tgl@sss.pgh.pa.us         253         [ -  + ]:           5773 :         if (sqlstate == NULL)
 4925 tgl@sss.pgh.pa.us         254                 :UBC           0 :             PLy_elog(ERROR, "could not generate SPI exceptions");
                                255                 :                : 
 5011 peter_e@gmx.net           256                 :CBC        5773 :         PyDict_SetItemString(dict, "sqlstate", sqlstate);
                                257                 :                :         Py_DECREF(sqlstate);
                                258                 :                : 
 3193 tgl@sss.pgh.pa.us         259                 :           5773 :         exc = PLy_create_exception(exception_map[i].name, base, dict,
                                260                 :           5773 :                                    exception_map[i].classname, mod);
                                261                 :                : 
 5011 peter_e@gmx.net           262                 :           5773 :         entry = hash_search(PLy_spi_exceptions, &exception_map[i].sqlstate,
                                263                 :                :                             HASH_ENTER, &found);
                                264         [ -  + ]:           5773 :         Assert(!found);
 3193 tgl@sss.pgh.pa.us         265                 :           5773 :         entry->exc = exc;
                                266                 :                :     }
 5011 peter_e@gmx.net           267                 :             23 : }
                                268                 :                : 
                                269                 :                : 
                                270                 :                : /*
                                271                 :                :  * the python interface to the elog function
                                272                 :                :  * don't confuse these with PLy_elog
                                273                 :                :  */
                                274                 :                : static PyObject *PLy_output(volatile int level, PyObject *self,
                                275                 :                :                             PyObject *args, PyObject *kw);
                                276                 :                : 
                                277                 :                : static PyObject *
 3438 teodor@sigaev.ru          278                 :              2 : PLy_debug(PyObject *self, PyObject *args, PyObject *kw)
                                279                 :                : {
                                280                 :              2 :     return PLy_output(DEBUG2, self, args, kw);
                                281                 :                : }
                                282                 :                : 
                                283                 :                : static PyObject *
                                284                 :              2 : PLy_log(PyObject *self, PyObject *args, PyObject *kw)
                                285                 :                : {
                                286                 :              2 :     return PLy_output(LOG, self, args, kw);
                                287                 :                : }
                                288                 :                : 
                                289                 :                : static PyObject *
                                290                 :            112 : PLy_info(PyObject *self, PyObject *args, PyObject *kw)
                                291                 :                : {
                                292                 :            112 :     return PLy_output(INFO, self, args, kw);
                                293                 :                : }
                                294                 :                : 
                                295                 :                : static PyObject *
                                296                 :            215 : PLy_notice(PyObject *self, PyObject *args, PyObject *kw)
                                297                 :                : {
                                298                 :            215 :     return PLy_output(NOTICE, self, args, kw);
                                299                 :                : }
                                300                 :                : 
                                301                 :                : static PyObject *
                                302                 :              5 : PLy_warning(PyObject *self, PyObject *args, PyObject *kw)
                                303                 :                : {
                                304                 :              5 :     return PLy_output(WARNING, self, args, kw);
                                305                 :                : }
                                306                 :                : 
                                307                 :                : static PyObject *
                                308                 :             11 : PLy_error(PyObject *self, PyObject *args, PyObject *kw)
                                309                 :                : {
                                310                 :             11 :     return PLy_output(ERROR, self, args, kw);
                                311                 :                : }
                                312                 :                : 
                                313                 :                : static PyObject *
 3438 teodor@sigaev.ru          314                 :UBC           0 : PLy_fatal(PyObject *self, PyObject *args, PyObject *kw)
                                315                 :                : {
                                316                 :              0 :     return PLy_output(FATAL, self, args, kw);
                                317                 :                : }
                                318                 :                : 
                                319                 :                : static PyObject *
 5011 peter_e@gmx.net           320                 :CBC           6 : PLy_quote_literal(PyObject *self, PyObject *args)
                                321                 :                : {
                                322                 :                :     const char *str;
                                323                 :                :     char       *quoted;
                                324                 :                :     PyObject   *ret;
                                325                 :                : 
 3236                           326         [ -  + ]:              6 :     if (!PyArg_ParseTuple(args, "s:quote_literal", &str))
 5011 peter_e@gmx.net           327                 :UBC           0 :         return NULL;
                                328                 :                : 
 5011 peter_e@gmx.net           329                 :CBC           6 :     quoted = quote_literal_cstr(str);
 1279 andres@anarazel.de        330                 :              6 :     ret = PLyUnicode_FromString(quoted);
 5011 peter_e@gmx.net           331                 :              6 :     pfree(quoted);
                                332                 :                : 
                                333                 :              6 :     return ret;
                                334                 :                : }
                                335                 :                : 
                                336                 :                : static PyObject *
                                337                 :              6 : PLy_quote_nullable(PyObject *self, PyObject *args)
                                338                 :                : {
                                339                 :                :     const char *str;
                                340                 :                :     char       *quoted;
                                341                 :                :     PyObject   *ret;
                                342                 :                : 
 3236                           343         [ -  + ]:              6 :     if (!PyArg_ParseTuple(args, "z:quote_nullable", &str))
 5011 peter_e@gmx.net           344                 :UBC           0 :         return NULL;
                                345                 :                : 
 5011 peter_e@gmx.net           346         [ +  + ]:CBC           6 :     if (str == NULL)
 1279 andres@anarazel.de        347                 :              1 :         return PLyUnicode_FromString("NULL");
                                348                 :                : 
 5011 peter_e@gmx.net           349                 :              5 :     quoted = quote_literal_cstr(str);
 1279 andres@anarazel.de        350                 :              5 :     ret = PLyUnicode_FromString(quoted);
 5011 peter_e@gmx.net           351                 :              5 :     pfree(quoted);
                                352                 :                : 
                                353                 :              5 :     return ret;
                                354                 :                : }
                                355                 :                : 
                                356                 :                : static PyObject *
                                357                 :              3 : PLy_quote_ident(PyObject *self, PyObject *args)
                                358                 :                : {
                                359                 :                :     const char *str;
                                360                 :                :     const char *quoted;
                                361                 :                :     PyObject   *ret;
                                362                 :                : 
 3236                           363         [ -  + ]:              3 :     if (!PyArg_ParseTuple(args, "s:quote_ident", &str))
 5011 peter_e@gmx.net           364                 :UBC           0 :         return NULL;
                                365                 :                : 
 5011 peter_e@gmx.net           366                 :CBC           3 :     quoted = quote_identifier(str);
 1279 andres@anarazel.de        367                 :              3 :     ret = PLyUnicode_FromString(quoted);
                                368                 :                : 
 5011 peter_e@gmx.net           369                 :              3 :     return ret;
                                370                 :                : }
                                371                 :                : 
                                372                 :                : /* enforce cast of object to string */
                                373                 :                : static char *
 3438 teodor@sigaev.ru          374                 :             58 : object_to_string(PyObject *obj)
                                375                 :                : {
                                376         [ +  - ]:             58 :     if (obj)
                                377                 :                :     {
 3376 rhaas@postgresql.org      378                 :             58 :         PyObject   *so = PyObject_Str(obj);
                                379                 :                : 
 3438 teodor@sigaev.ru          380         [ +  - ]:             58 :         if (so != NULL)
                                381                 :                :         {
                                382                 :                :             char       *str;
                                383                 :                : 
 1279 andres@anarazel.de        384                 :             58 :             str = pstrdup(PLyUnicode_AsString(so));
                                385                 :                :             Py_DECREF(so);
                                386                 :                : 
 3438 teodor@sigaev.ru          387                 :             58 :             return str;
                                388                 :                :         }
                                389                 :                :     }
                                390                 :                : 
 3438 teodor@sigaev.ru          391                 :UBC           0 :     return NULL;
                                392                 :                : }
                                393                 :                : 
                                394                 :                : static PyObject *
 3438 teodor@sigaev.ru          395                 :CBC         347 : PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
                                396                 :                : {
 3376 rhaas@postgresql.org      397                 :            347 :     int         sqlstate = 0;
                                398                 :            347 :     char       *volatile sqlstatestr = NULL;
                                399                 :            347 :     char       *volatile message = NULL;
                                400                 :            347 :     char       *volatile detail = NULL;
                                401                 :            347 :     char       *volatile hint = NULL;
 3374 peter_e@gmx.net           402                 :            347 :     char       *volatile column_name = NULL;
                                403                 :            347 :     char       *volatile constraint_name = NULL;
                                404                 :            347 :     char       *volatile datatype_name = NULL;
                                405                 :            347 :     char       *volatile table_name = NULL;
                                406                 :            347 :     char       *volatile schema_name = NULL;
                                407                 :                :     volatile MemoryContext oldcontext;
                                408                 :                :     PyObject   *key,
                                409                 :                :                *value;
                                410                 :                :     PyObject   *volatile so;
 3376 rhaas@postgresql.org      411                 :            347 :     Py_ssize_t  pos = 0;
                                412                 :                : 
 5011 peter_e@gmx.net           413         [ +  + ]:            347 :     if (PyTuple_Size(args) == 1)
                                414                 :                :     {
                                415                 :                :         /*
                                416                 :                :          * Treat single argument specially to avoid undesirable ('tuple',)
                                417                 :                :          * decoration.
                                418                 :                :          */
                                419                 :                :         PyObject   *o;
                                420                 :                : 
 4925 tgl@sss.pgh.pa.us         421         [ -  + ]:            261 :         if (!PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o))
 4925 tgl@sss.pgh.pa.us         422                 :UBC           0 :             PLy_elog(ERROR, "could not unpack arguments in plpy.elog");
 5011 peter_e@gmx.net           423                 :CBC         261 :         so = PyObject_Str(o);
                                424                 :                :     }
                                425                 :                :     else
                                426                 :             86 :         so = PyObject_Str(args);
                                427                 :                : 
 1279 andres@anarazel.de        428   [ +  -  -  + ]:            347 :     if (so == NULL || ((message = PLyUnicode_AsString(so)) == NULL))
                                429                 :                :     {
 5011 peter_e@gmx.net           430                 :UBC           0 :         level = ERROR;
 3438 teodor@sigaev.ru          431                 :              0 :         message = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
                                432                 :                :     }
 3435 tgl@sss.pgh.pa.us         433                 :CBC         347 :     message = pstrdup(message);
                                434                 :                : 
 3438 teodor@sigaev.ru          435                 :            347 :     Py_XDECREF(so);
                                436                 :                : 
                                437         [ +  + ]:            347 :     if (kw != NULL)
                                438                 :                :     {
                                439         [ +  + ]:             80 :         while (PyDict_Next(kw, &pos, &key, &value))
                                440                 :                :         {
 1279 andres@anarazel.de        441                 :             61 :             char       *keyword = PLyUnicode_AsString(key);
                                442                 :                : 
 3438 teodor@sigaev.ru          443         [ +  + ]:             61 :             if (strcmp(keyword, "message") == 0)
                                444                 :                :             {
                                445                 :                :                 /* the message should not be overwritten */
                                446         [ +  + ]:              9 :                 if (PyTuple_Size(args) != 0)
                                447                 :                :                 {
 3026 alvherre@alvh.no-ip.      448                 :              2 :                     PLy_exception_set(PyExc_TypeError, "argument 'message' given by name and position");
 3353 peter_e@gmx.net           449                 :              2 :                     return NULL;
                                450                 :                :                 }
                                451                 :                : 
 3435 tgl@sss.pgh.pa.us         452         [ +  - ]:              7 :                 if (message)
                                453                 :              7 :                     pfree(message);
 3438 teodor@sigaev.ru          454                 :              7 :                 message = object_to_string(value);
                                455                 :                :             }
                                456         [ +  + ]:             52 :             else if (strcmp(keyword, "detail") == 0)
                                457                 :             15 :                 detail = object_to_string(value);
                                458         [ +  + ]:             37 :             else if (strcmp(keyword, "hint") == 0)
                                459                 :              7 :                 hint = object_to_string(value);
                                460         [ +  + ]:             30 :             else if (strcmp(keyword, "sqlstate") == 0)
                                461                 :              7 :                 sqlstatestr = object_to_string(value);
 3374 peter_e@gmx.net           462         [ +  + ]:             23 :             else if (strcmp(keyword, "schema_name") == 0)
                                463                 :              4 :                 schema_name = object_to_string(value);
                                464         [ +  + ]:             19 :             else if (strcmp(keyword, "table_name") == 0)
                                465                 :              5 :                 table_name = object_to_string(value);
                                466         [ +  + ]:             14 :             else if (strcmp(keyword, "column_name") == 0)
                                467                 :              4 :                 column_name = object_to_string(value);
                                468         [ +  + ]:             10 :             else if (strcmp(keyword, "datatype_name") == 0)
                                469                 :              5 :                 datatype_name = object_to_string(value);
                                470         [ +  + ]:              5 :             else if (strcmp(keyword, "constraint_name") == 0)
                                471                 :              4 :                 constraint_name = object_to_string(value);
                                472                 :                :             else
                                473                 :                :             {
 3353                           474                 :              1 :                 PLy_exception_set(PyExc_TypeError,
                                475                 :                :                                   "'%s' is an invalid keyword argument for this function",
                                476                 :                :                                   keyword);
                                477                 :              1 :                 return NULL;
                                478                 :                :             }
                                479                 :                :         }
                                480                 :                :     }
                                481                 :                : 
 3438 teodor@sigaev.ru          482         [ +  + ]:            344 :     if (sqlstatestr != NULL)
                                483                 :                :     {
                                484         [ +  + ]:              7 :         if (strlen(sqlstatestr) != 5)
                                485                 :                :         {
 3353 peter_e@gmx.net           486                 :              1 :             PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
                                487                 :              1 :             return NULL;
                                488                 :                :         }
                                489                 :                : 
 3438 teodor@sigaev.ru          490         [ -  + ]:              6 :         if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
                                491                 :                :         {
 3353 peter_e@gmx.net           492                 :UBC           0 :             PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
                                493                 :              0 :             return NULL;
                                494                 :                :         }
                                495                 :                : 
 3438 teodor@sigaev.ru          496                 :CBC           6 :         sqlstate = MAKE_SQLSTATE(sqlstatestr[0],
                                497                 :                :                                  sqlstatestr[1],
                                498                 :                :                                  sqlstatestr[2],
                                499                 :                :                                  sqlstatestr[3],
                                500                 :                :                                  sqlstatestr[4]);
                                501                 :                :     }
                                502                 :                : 
 5011 peter_e@gmx.net           503                 :            343 :     oldcontext = CurrentMemoryContext;
                                504         [ +  + ]:            343 :     PG_TRY();
                                505                 :                :     {
 3438 teodor@sigaev.ru          506         [ +  - ]:            343 :         if (message != NULL)
                                507                 :            343 :             pg_verifymbstr(message, strlen(message), false);
                                508         [ +  + ]:            343 :         if (detail != NULL)
                                509                 :             15 :             pg_verifymbstr(detail, strlen(detail), false);
                                510         [ +  + ]:            343 :         if (hint != NULL)
                                511                 :              7 :             pg_verifymbstr(hint, strlen(hint), false);
 3374 peter_e@gmx.net           512         [ +  + ]:            343 :         if (schema_name != NULL)
                                513                 :              4 :             pg_verifymbstr(schema_name, strlen(schema_name), false);
                                514         [ +  + ]:            343 :         if (table_name != NULL)
                                515                 :              5 :             pg_verifymbstr(table_name, strlen(table_name), false);
                                516         [ +  + ]:            343 :         if (column_name != NULL)
                                517                 :              4 :             pg_verifymbstr(column_name, strlen(column_name), false);
                                518         [ +  + ]:            343 :         if (datatype_name != NULL)
                                519                 :              5 :             pg_verifymbstr(datatype_name, strlen(datatype_name), false);
                                520         [ +  + ]:            343 :         if (constraint_name != NULL)
                                521                 :              4 :             pg_verifymbstr(constraint_name, strlen(constraint_name), false);
                                522                 :                : 
 3438 teodor@sigaev.ru          523   [ +  +  +  +  :            343 :         ereport(level,
                                     +  -  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                                 + ]
                                524                 :                :                 ((sqlstate != 0) ? errcode(sqlstate) : 0,
                                525                 :                :                  (message != NULL) ? errmsg_internal("%s", message) : 0,
                                526                 :                :                  (detail != NULL) ? errdetail_internal("%s", detail) : 0,
                                527                 :                :                  (hint != NULL) ? errhint("%s", hint) : 0,
                                528                 :                :                  (column_name != NULL) ?
                                529                 :                :                  err_generic_string(PG_DIAG_COLUMN_NAME, column_name) : 0,
                                530                 :                :                  (constraint_name != NULL) ?
                                531                 :                :                  err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0,
                                532                 :                :                  (datatype_name != NULL) ?
                                533                 :                :                  err_generic_string(PG_DIAG_DATATYPE_NAME, datatype_name) : 0,
                                534                 :                :                  (table_name != NULL) ?
                                535                 :                :                  err_generic_string(PG_DIAG_TABLE_NAME, table_name) : 0,
                                536                 :                :                  (schema_name != NULL) ?
                                537                 :                :                  err_generic_string(PG_DIAG_SCHEMA_NAME, schema_name) : 0));
                                538                 :                :     }
 5011 peter_e@gmx.net           539                 :             11 :     PG_CATCH();
                                540                 :                :     {
                                541                 :                :         ErrorData  *edata;
                                542                 :                : 
                                543                 :             11 :         MemoryContextSwitchTo(oldcontext);
                                544                 :             11 :         edata = CopyErrorData();
                                545                 :             11 :         FlushErrorState();
                                546                 :                : 
 3438 teodor@sigaev.ru          547                 :             11 :         PLy_exception_set_with_details(PLy_exc_error, edata);
                                548                 :             11 :         FreeErrorData(edata);
                                549                 :                : 
 5011 peter_e@gmx.net           550                 :             11 :         return NULL;
                                551                 :                :     }
                                552         [ -  + ]:            332 :     PG_END_TRY();
                                553                 :                : 
                                554                 :                :     /*
                                555                 :                :      * return a legal object so the interpreter will continue on its merry way
                                556                 :                :      */
 2899                           557                 :            332 :     Py_RETURN_NONE;
                                558                 :                : }
        

Generated by: LCOV version 2.4-beta