LCOV - code coverage report
Current view: top level - contrib/ltree_plpython - ltree_plpython.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 14 15 93.3 %
Date: 2019-09-19 02:07:14 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "postgres.h"
       2             : 
       3             : #include "fmgr.h"
       4             : #include "plpython.h"
       5             : #include "ltree/ltree.h"
       6             : 
       7           4 : PG_MODULE_MAGIC;
       8             : 
       9             : extern void _PG_init(void);
      10             : 
      11             : /* Linkage to functions in plpython module */
      12             : #if PY_MAJOR_VERSION >= 3
      13             : typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
      14             : static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
      15             : #endif
      16             : 
      17             : 
      18             : /*
      19             :  * Module initialize function: fetch function pointers for cross-module calls.
      20             :  */
      21             : void
      22           4 : _PG_init(void)
      23             : {
      24             :     /* Asserts verify that typedefs above match original declarations */
      25             : #if PY_MAJOR_VERSION >= 3
      26             :     AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
      27             :     PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
      28             :         load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
      29             :                                true, NULL);
      30             : #endif
      31           4 : }
      32             : 
      33             : 
      34             : /* These defines must be after the module init function */
      35             : #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
      36             : 
      37             : 
      38           8 : PG_FUNCTION_INFO_V1(ltree_to_plpython);
      39             : 
      40             : Datum
      41           4 : ltree_to_plpython(PG_FUNCTION_ARGS)
      42             : {
      43           4 :     ltree      *in = PG_GETARG_LTREE_P(0);
      44             :     int         i;
      45             :     PyObject   *list;
      46             :     ltree_level *curlevel;
      47             : 
      48           4 :     list = PyList_New(in->numlevel);
      49           4 :     if (!list)
      50           0 :         ereport(ERROR,
      51             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
      52             :                  errmsg("out of memory")));
      53             : 
      54           4 :     curlevel = LTREE_FIRST(in);
      55          16 :     for (i = 0; i < in->numlevel; i++)
      56             :     {
      57          12 :         PyList_SetItem(list, i, PyString_FromStringAndSize(curlevel->name, curlevel->len));
      58          12 :         curlevel = LEVEL_NEXT(curlevel);
      59             :     }
      60             : 
      61           4 :     PG_FREE_IF_COPY(in, 0);
      62             : 
      63           4 :     return PointerGetDatum(list);
      64             : }

Generated by: LCOV version 1.13