# HG changeset patch # User Mike Miller # Date 1471332682 25200 # Node ID b0d012e9975fb9967861438c24516848a920152b # Parent 06b8aeea456fbdbcc96f539c855938b76926d297 pyversion: new function to report Python runtime version and path information * pyversion.m: New function file. diff -r 06b8aeea456f -r b0d012e9975f pyversion.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyversion.m Tue Aug 16 00:31:22 2016 -0700 @@ -0,0 +1,88 @@ +## Copyright (C) 2016 Mike Miller +## +## This file is part of Pytave. +## +## Pytave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Pytave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Pytave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {} {} pyversion +## @deftypefnx {} {@var{ver} =} pyversion +## @deftypefnx {} {[@var{ver}, @var{exec}, @var{loaded}] =} pyversion +## List information about the Python runtime used by Octave Python functions. +## @end deftypefn + +function varargout = pyversion () + + if (nargin > 0) + print_usage (); + endif + + ver = char (pycall ("sysconfig.get_python_version")); + + if ((nargout == 0) || (nargout > 1)) + exec = char (py.sys.executable); + endif + + ## FIXME: determine whether the Python runtime has been loaded yet + if ((nargout == 0) || (nargout > 2)) + loaded = true; + endif + + if (nargout == 0) + dlllibrary = char (pycall ("sysconfig.get_config_var", "DLLLIBRARY")); + if (isempty (dlllibrary)) + libdir = pycall ("sysconfig.get_config_var", "LIBDIR"); + ldlibrary = pycall ("sysconfig.get_config_var", "LDLIBRARY"); + else + libdir = pycall ("sysconfig.get_config_var", "BINDIR"); + ldlibrary = dlllibrary; + endif + multiarch = pycall ("sysconfig.get_config_var", "MULTIARCH"); + lib = char (pycall ("os.path.join", libdir, multiarch, ldlibrary)); + prefix = char (pycall ("sysconfig.get_config_var", "prefix")); + + printf (" version: \"%s\"\n", ver); + printf (" executable: \"%s\"\n", exec); + printf (" library: \"%s\"\n", lib); + printf (" prefix: \"%s\"\n", prefix); + printf (" loaded: %d\n", loaded); + else + if (nargout > 0) + varargout{1} = ver; + endif + if (nargout > 1) + varargout{2} = exec; + endif + if (nargout > 2) + varargout{3} = loaded; + endif + endif + +endfunction + + +%!assert (ischar (pyversion ())) + +%!test +%! [m, n] = regexp (pyversion (), '^(\d\.\d)$'); +%! assert ([m, n], [1, 3]) + +%!test +%! [~, ~, v] = pyversion (); +%! assert (v, true) + +%!error pyversion (1) +%!error pyversion ("python") +%!error pyversion ("/usr/bin/python")