From fe8e47a7295900951e4510bdf582dc6b836efad5 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Mon, 3 Oct 2016 13:55:26 +0100 Subject: [PATCH 10/11] Fix build for Python 3. Python 3 does not have PyString_FromString. Use a near-equivalent instead. To build for Python 3 you must point the PYTHON environment variable to the Python 3 interpreter when configuring. For example: PYTHON=/usr/bin/python3 ./configure --- README | 10 ++++++++++ configure.ac | 13 ++++++++++++- plugins/python/nbdkit-python-plugin.pod | 9 +++++++++ plugins/python/python.c | 9 +++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README b/README index 135c8d5..2723d5e 100644 --- a/README +++ b/README @@ -102,6 +102,16 @@ Optionally run this as root to install everything: make install +Python +------ + +By default nbdkit uses the Python version of the Python interpreter +called "python" on the current $PATH. To use another version of +Python you may need to set the PYTHON environment variable when +configuring. For example: + + PYTHON=/usr/bin/python3 ./configure + Tests ----- diff --git a/configure.ac b/configure.ac index c064420..a475e8e 100644 --- a/configure.ac +++ b/configure.ac @@ -201,7 +201,18 @@ AS_IF([test "x$PYTHON" != "xno" && test "x$enable_python" != "xno"],[ ]) ]) - dnl XXX Could check these actually work. + dnl Check for various functions needed by the bindings. + old_LIBS="$LIBS" + + PYTHON_BLDLIBRARY=`$PYTHON -c "import distutils.sysconfig; \ + print (distutils.sysconfig.get_config_var('BLDLIBRARY'))"` + AC_CHECK_LIB([c],[PyString_FromString], + [AC_DEFINE([HAVE_PYSTRING_FROMSTRING],1, + [Found PyString_FromString in libpython.])], + [],[$PYTHON_BLDLIBRARY]) + + LIBS="$old_LIBS" + ]) AM_CONDITIONAL([HAVE_PYTHON],[test "x$enable_python" != "xno" && test "x$PYTHON" != "xno"]) AC_SUBST([PYTHON_CFLAGS]) diff --git a/plugins/python/nbdkit-python-plugin.pod b/plugins/python/nbdkit-python-plugin.pod index 70d3e17..9b0f0ef 100644 --- a/plugins/python/nbdkit-python-plugin.pod +++ b/plugins/python/nbdkit-python-plugin.pod @@ -16,6 +16,15 @@ L, allowing you to write nbdkit plugins in Python. Broadly speaking, Python nbdkit plugins work like C ones, so you should read L first. +=head2 PYTHON 2 AND PYTHON 3 + +The Python plugin has to be compiled for either Python 2 or Python 3 +when building nbdkit. You can set the C environment variable +to the desired interpreter, otherwise nbdkit will use the interpreter +called C on the current C<$PATH>. For example: + + PYTHON=/usr/bin/python3 ./configure + =head2 USING A PYTHON NBDKIT PLUGIN Assuming you have a Python script which is an nbdkit plugin, you run it diff --git a/plugins/python/python.c b/plugins/python/python.c index fcbb9f2..0504715 100644 --- a/plugins/python/python.c +++ b/plugins/python/python.c @@ -133,7 +133,11 @@ py_config (const char *key, const char *value) /* Note that because closeit flag == 1, fp is now closed. */ /* The script should define a module called __main__. */ +#ifdef HAVE_PYSTRING_FROMSTRING modname = PyString_FromString ("__main__"); +#else + modname = PyUnicode_FromString ("__main__"); +#endif module = PyImport_Import (modname); Py_DECREF (modname); if (!module) { @@ -154,8 +158,13 @@ py_config (const char *key, const char *value) PyErr_Clear (); args = PyTuple_New (2); +#ifdef HAVE_PYSTRING_FROMSTRING PyTuple_SetItem (args, 0, PyString_FromString (key)); PyTuple_SetItem (args, 1, PyString_FromString (value)); +#else + PyTuple_SetItem (args, 0, PyUnicode_FromString (key)); + PyTuple_SetItem (args, 1, PyUnicode_FromString (value)); +#endif r = PyObject_CallObject (fn, args); Py_DECREF (fn); Py_DECREF (args); -- 2.7.4