Skip to content

Commit

Permalink
Hy/plpython3 (#2)
Browse files Browse the repository at this point in the history
* feat: add guc to plpython3u PYTHONPATH
  • Loading branch information
yihong0618 authored Apr 21, 2022
1 parent 8b94b9e commit 7fb66c8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
39 changes: 39 additions & 0 deletions src/pl/plpython/expected/plpython3_path_guc.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--
-- Tests for functions that return set PYTHONPATH guc only for gpdb6 plpython3
--
LOAD 'plpython3';
show plpython3.python_path;
plpython3.python_path
-----------------------

(1 row)

set plpython3.python_path='/foo';
show plpython3.python_path;
plpython3.python_path
-----------------------
/foo
(1 row)

-- Set up test path functions first.
CREATE FUNCTION test_path_added()
RETURNS text AS $$
import sys
return str(sys.path[0])
$$ language plpython3u;
SELECT test_path_added();
test_path_added
-----------------
/foo
(1 row)

-- when the plpython init can not set again --
set plpython3.python_path='/bar';
ERROR: SET PYTHONPATH failed, the GUC value can only be changed before initializing the python interpreter.
-- run again the path will not change still /foo --
SELECT test_path_added();
test_path_added
-----------------
/foo
(1 row)

49 changes: 42 additions & 7 deletions src/pl/plpython/plpy_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "utils/rel.h"
#include "utils/syscache.h"

#include "cdb/cdbvars.h"

#include "plpython.h"

#include "plpy_main.h"
Expand Down Expand Up @@ -81,6 +83,23 @@ void PLy_handle_cancel_interrupt(void);

bool PLy_enter_python_intepreter = false;

static bool inited = false;

/* GUC variables */
#if PY_MAJOR_VERSION >= 3
static char *plpython3_path = NULL;

static bool
plpython3_check_python_path(char **newval, void **extra, GucSource source) {
if (inited)
{
GUC_check_errmsg("SET PYTHONPATH failed, the GUC value can only be changed before initializing the python interpreter.");
return false;
}
return true;
}
#endif

void
_PG_init(void)
{
Expand Down Expand Up @@ -137,6 +156,18 @@ _PG_init(void)
prev_cancel_pending_hook = cancel_pending_hook;
cancel_pending_hook = PLy_handle_cancel_interrupt;

#if PY_MAJOR_VERSION >= 3
DefineCustomStringVariable("plpython3.python_path",
gettext_noop("PYTHONPATH for plpython3."),
NULL,
&plpython3_path,
"", // default path need to set empty for init
PGC_USERSET,
GUC_GPDB_NEED_SYNC,
plpython3_check_python_path,
NULL,
NULL);
#endif
pg_bindtextdomain(TEXTDOMAIN);
}

Expand All @@ -148,7 +179,6 @@ _PG_init(void)
static void
PLy_initialize(void)
{
static bool inited = false;

/*
* Check for multiple Python libraries before actively doing anything with
Expand All @@ -162,18 +192,23 @@ PLy_initialize(void)
ereport(FATAL,
(errmsg("multiple Python libraries are present in session"),
errdetail("Only one Python major version can be used in one session.")));

#if PY_MAJOR_VERSION >= 3
/* PYTHONPATH and PYTHONHOME has been set to GPDB's python2.7 in Postmaster when
* gpstart. So for plpython3u, we need to unset PYTHONPATH and PYTHONHOME.
* if user set PYTHONPATH then we set it in the env
*/
if (plpython3_path && *plpython3_path)
setenv("PYTHONPATH", plpython3_path, 1);
else
unsetenv("PYTHONPATH");
unsetenv("PYTHONHOME");
#endif
/* The rest should only be done once per session */
if (inited)
return;

#if PY_MAJOR_VERSION >= 3
PyImport_AppendInittab("plpy", PyInit_plpy);
/* PYTHONPATH and PYTHONHOME has been set to GPDB's python2.7 in Postmaster when
* gpstart. So for plpython3u, we need to unset PYTHONPATH and PYTHONHOME.
*/
unsetenv("PYTHONPATH");
unsetenv("PYTHONHOME");
#endif
Py_Initialize();
#if PY_MAJOR_VERSION >= 3
Expand Down
23 changes: 23 additions & 0 deletions src/pl/plpython/sql/plpython3_path_guc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--
-- Tests for functions that return set PYTHONPATH guc only for gpdb6 plpython3
--
LOAD 'plpython3';
show plpython3.python_path;

set plpython3.python_path='/foo';
show plpython3.python_path;

-- Set up test path functions first.
CREATE FUNCTION test_path_added()
RETURNS text AS $$
import sys
return str(sys.path[0])
$$ language plpython3u;

SELECT test_path_added();

-- when the plpython init can not set again --
set plpython3.python_path='/bar';

-- run again the path will not change still /foo --
SELECT test_path_added();
1 change: 1 addition & 0 deletions src/pl/plpython3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ REGRESS = \
plpython_schema \
plpython_populate \
plpython_test \
plpython3_path_guc \
plpython_do \
plpython_global \
plpython_import \
Expand Down

0 comments on commit 7fb66c8

Please sign in to comment.