From 3b1244831e826b0a6ebe9163d138f4c91aecda8e Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 27 Mar 2020 16:28:43 -0400 Subject: [PATCH] Add Python::has_minimum_version --- src/KIM/kim_property.cpp | 18 +++++++----------- src/PYTHON/python_impl.cpp | 7 +++++++ src/PYTHON/python_impl.h | 1 + src/lmppython.cpp | 8 ++++++++ src/lmppython.h | 2 ++ 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/KIM/kim_property.cpp b/src/KIM/kim_property.cpp index 0a55d6aae9..d2f1d145c9 100644 --- a/src/KIM/kim_property.cpp +++ b/src/KIM/kim_property.cpp @@ -75,24 +75,20 @@ using namespace LAMMPS_NS; kimProperty::kimProperty(LAMMPS *lmp) : Pointers(lmp) { -#if LMP_PYTHON -#if PY_MAJOR_VERSION != 3 - error->all(FLERR, "Invalid Python version.\n" - "The kim-property Python package requires Python " - "3 >= 3.6 support."); -#endif // one-time initialization of Python interpreter python->init(); -#else - error->all(FLERR, "Error Python support missing! Compile with PYTHON " - "package installed!"); -#endif // LMP_PYTHON + + if (!python->has_minimum_version(3, 6)) { + error->all(FLERR, "Invalid Python version.\n" + "The kim-property Python package requires Python " + "3 >= 3.6 support."); + } } void kimProperty::command(int narg, char **arg) { #if LMP_PYTHON -#if PY_MAJOR_VERSION == 3 +#if PY_MAJOR_VERSION >= 3 if (narg < 2) error->all(FLERR, "Invalid `kim_property` command."); diff --git a/src/PYTHON/python_impl.cpp b/src/PYTHON/python_impl.cpp index 1188942e75..e027079f1f 100644 --- a/src/PYTHON/python_impl.cpp +++ b/src/PYTHON/python_impl.cpp @@ -535,3 +535,10 @@ void PythonImpl::deallocate(int i) delete [] pfuncs[i].ovarname; delete [] pfuncs[i].longstr; } + +/* ------------------------------------------------------------------ */ + +bool PythonImpl::has_minimum_version(int major, int minor) +{ + return (PY_MAJOR_VERSION == major && PY_MINOR_VERSION >= minor) || (PY_MAJOR_VERSION > major); +} diff --git a/src/PYTHON/python_impl.h b/src/PYTHON/python_impl.h index f890f54f3b..2fd52b8ead 100644 --- a/src/PYTHON/python_impl.h +++ b/src/PYTHON/python_impl.h @@ -32,6 +32,7 @@ class PythonImpl : protected Pointers, public PythonInterface { char *long_string(int); int execute_string(char *); int execute_file(char *); + bool has_minimum_version(int major, int minor); private: int ninput,noutput,length_longstr; diff --git a/src/lmppython.cpp b/src/lmppython.cpp index 52abd93f10..132790bdbe 100644 --- a/src/lmppython.cpp +++ b/src/lmppython.cpp @@ -117,3 +117,11 @@ int Python::execute_file(char *fname) init(); return impl->execute_file(fname); } + +/* ------------------------------------------------------------------ */ + +bool Python::has_minimum_version(int major, int minor) +{ + init(); + return impl->has_minimum_version(major, minor); +} diff --git a/src/lmppython.h b/src/lmppython.h index 3d55c4827b..fab307d9e6 100644 --- a/src/lmppython.h +++ b/src/lmppython.h @@ -28,6 +28,7 @@ public: virtual char * long_string(int ifunc) = 0; virtual int execute_string(char *) = 0; virtual int execute_file(char *) = 0; + virtual bool has_minimum_version(int major, int minor) = 0; }; class Python : protected Pointers { @@ -42,6 +43,7 @@ public: char * long_string(int ifunc); int execute_string(char *); int execute_file(char *); + bool has_minimum_version(int major, int minor); bool is_enabled() const; void init();