Mercurial > pytave
changeset 341:b0d012e9975f
pyversion: new function to report Python runtime version and path information
* pyversion.m: New function file.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Tue, 16 Aug 2016 00:31:22 -0700 |
parents | 06b8aeea456f |
children | 6bd8f5e3542a |
files | pyversion.m |
diffstat | 1 files changed, 88 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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 +## <http://www.gnu.org/licenses/>. + +## -*- 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")