postgresql/contrib/ltree_plpython/ltree_plpython.c
Tom Lane 4bd1994650 Make DatumGetFoo/PG_GETARG_FOO/PG_RETURN_FOO macro names more consistent.
By project convention, these names should include "P" when dealing with a
pointer type; that is, if the result of a GETARG macro is of type FOO *,
it should be called PG_GETARG_FOO_P not just PG_GETARG_FOO.  Some newer
types such as JSONB and ranges had not followed the convention, and a
number of contrib modules hadn't gotten that memo either.  Rename the
offending macros to improve consistency.

In passing, fix a few places that thought PG_DETOAST_DATUM() returns
a Datum; it does not, it returns "struct varlena *".  Applying
DatumGetPointer to that happens not to cause any bad effects today,
but it's formally wrong.  Also, adjust an ltree macro that was designed
without any thought for what pgindent would do with it.

This is all cosmetic and shouldn't have any impact on generated code.

Mark Dilger, some further tweaks by me

Discussion: https://postgr.es/m/EA5676F4-766F-4F38-8348-ECC7DB427C6A@gmail.com
2017-09-18 15:21:23 -04:00

61 lines
1.4 KiB
C

#include "postgres.h"
#include "fmgr.h"
#include "plpython.h"
#include "ltree.h"
PG_MODULE_MAGIC;
extern void _PG_init(void);
/* Linkage to functions in plpython module */
#if PY_MAJOR_VERSION >= 3
typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
#endif
/*
* Module initialize function: fetch function pointers for cross-module calls.
*/
void
_PG_init(void)
{
/* Asserts verify that typedefs above match original declarations */
#if PY_MAJOR_VERSION >= 3
AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
true, NULL);
#endif
}
/* These defines must be after the module init function */
#define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
PG_FUNCTION_INFO_V1(ltree_to_plpython);
Datum
ltree_to_plpython(PG_FUNCTION_ARGS)
{
ltree *in = PG_GETARG_LTREE_P(0);
int i;
PyObject *list;
ltree_level *curlevel;
list = PyList_New(in->numlevel);
curlevel = LTREE_FIRST(in);
for (i = 0; i < in->numlevel; i++)
{
PyList_SetItem(list, i, PyString_FromStringAndSize(curlevel->name, curlevel->len));
curlevel = LEVEL_NEXT(curlevel);
}
PG_FREE_IF_COPY(in, 0);
return PointerGetDatum(list);
}