From 69ccbd1562525f6825db4a0a1674c835b5290398 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 15 May 2017 17:45:36 -0400 Subject: [PATCH] Extract common wrappers to Python compatibility header --- src/.gitignore | 1 + src/PYTHON/fix_python.cpp | 12 +++++------- src/PYTHON/pair_python.cpp | 17 +---------------- src/PYTHON/python_compat.h | 33 +++++++++++++++++++++++++++++++++ src/PYTHON/python_impl.cpp | 20 +++++--------------- 5 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 src/PYTHON/python_compat.h diff --git a/src/.gitignore b/src/.gitignore index 6171b29af9..4e5f7d9ebc 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -850,6 +850,7 @@ /prd.h /python_impl.cpp /python_impl.h +/python_compat.h /fix_python.cpp /fix_python.h /pair_python.cpp diff --git a/src/PYTHON/fix_python.cpp b/src/PYTHON/fix_python.cpp index 4f437b7488..e1c0dd1fbc 100644 --- a/src/PYTHON/fix_python.cpp +++ b/src/PYTHON/fix_python.cpp @@ -11,6 +11,10 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------- + Contributing author: Axel Kohlmeyer and Richard Berger (Temple U) +------------------------------------------------------------------------- */ + #include #include #include "fix_python.h" @@ -20,17 +24,11 @@ #include "respa.h" #include "error.h" #include "python.h" +#include "python_compat.h" using namespace LAMMPS_NS; using namespace FixConst; -// Wrap API changes between Python 2 and 3 using macros -#if PY_MAJOR_VERSION == 2 -#define PY_VOID_POINTER(X) PyCObject_FromVoidPtr((void *) X, NULL) -#elif PY_MAJOR_VERSION == 3 -#define PY_VOID_POINTER(X) PyCapsule_New((void *) X, NULL, NULL) -#endif - /* ---------------------------------------------------------------------- */ FixPython::FixPython(LAMMPS *lmp, int narg, char **arg) : diff --git a/src/PYTHON/pair_python.cpp b/src/PYTHON/pair_python.cpp index 1d37a9fd06..485efee58d 100644 --- a/src/PYTHON/pair_python.cpp +++ b/src/PYTHON/pair_python.cpp @@ -27,25 +27,10 @@ #include "neigh_list.h" #include "python.h" #include "error.h" +#include "python_compat.h" using namespace LAMMPS_NS; -// Wrap API changes between Python 2 and 3 using macros -#if PY_MAJOR_VERSION == 2 -#define PY_INT_FROM_LONG(X) PyInt_FromLong(X) -#define PY_INT_AS_LONG(X) PyInt_AsLong(X) -#define PY_STRING_FROM_STRING(X) PyString_FromString(X) -#define PY_VOID_POINTER(X) PyCObject_FromVoidPtr((void *) X, NULL) -#define PY_STRING_AS_STRING(X) PyString_AsString(X) - -#elif PY_MAJOR_VERSION == 3 -#define PY_INT_FROM_LONG(X) PyLong_FromLong(X) -#define PY_INT_AS_LONG(X) PyLong_AsLong(X) -#define PY_STRING_FROM_STRING(X) PyUnicode_FromString(X) -#define PY_VOID_POINTER(X) PyCapsule_New((void *) X, NULL, NULL) -#define PY_STRING_AS_STRING(X) PyUnicode_AsUTF8(X) -#endif - /* ---------------------------------------------------------------------- */ PairPython::PairPython(LAMMPS *lmp) : Pair(lmp) { diff --git a/src/PYTHON/python_compat.h b/src/PYTHON/python_compat.h new file mode 100644 index 0000000000..175d797ffa --- /dev/null +++ b/src/PYTHON/python_compat.h @@ -0,0 +1,33 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#ifndef LMP_PYTHON_COMPAT_H +#define LMP_PYTHON_COMPAT_H + +// Wrap API changes between Python 2 and 3 using macros +#if PY_MAJOR_VERSION == 2 +#define PY_INT_FROM_LONG(X) PyInt_FromLong(X) +#define PY_INT_AS_LONG(X) PyInt_AsLong(X) +#define PY_STRING_FROM_STRING(X) PyString_FromString(X) +#define PY_VOID_POINTER(X) PyCObject_FromVoidPtr((void *) X, NULL) +#define PY_STRING_AS_STRING(X) PyString_AsString(X) + +#elif PY_MAJOR_VERSION == 3 +#define PY_INT_FROM_LONG(X) PyLong_FromLong(X) +#define PY_INT_AS_LONG(X) PyLong_AsLong(X) +#define PY_STRING_FROM_STRING(X) PyUnicode_FromString(X) +#define PY_VOID_POINTER(X) PyCapsule_New((void *) X, NULL, NULL) +#define PY_STRING_AS_STRING(X) PyUnicode_AsUTF8(X) +#endif + +#endif diff --git a/src/PYTHON/python_impl.cpp b/src/PYTHON/python_impl.cpp index c66e003228..dadcbfeade 100644 --- a/src/PYTHON/python_impl.cpp +++ b/src/PYTHON/python_impl.cpp @@ -11,6 +11,10 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------- + Contributing author: Axel Kohlmeyer and Richard Berger (Temple U) +------------------------------------------------------------------------- */ + #include #include "python.h" #include "force.h" @@ -18,6 +22,7 @@ #include "variable.h" #include "memory.h" #include "error.h" +#include "python_compat.h" using namespace LAMMPS_NS; @@ -25,21 +30,6 @@ enum{NONE,INT,DOUBLE,STRING,PTR}; #define VALUELENGTH 64 // also in variable.cpp -// Wrap API changes between Python 2 and 3 using macros -#if PY_MAJOR_VERSION == 2 -#define PY_INT_FROM_LONG(X) PyInt_FromLong(X) -#define PY_INT_AS_LONG(X) PyInt_AsLong(X) -#define PY_STRING_FROM_STRING(X) PyString_FromString(X) -#define PY_VOID_POINTER(X) PyCObject_FromVoidPtr((void *) X, NULL) -#define PY_STRING_AS_STRING(X) PyString_AsString(X) - -#elif PY_MAJOR_VERSION == 3 -#define PY_INT_FROM_LONG(X) PyLong_FromLong(X) -#define PY_INT_AS_LONG(X) PyLong_AsLong(X) -#define PY_STRING_FROM_STRING(X) PyUnicode_FromString(X) -#define PY_VOID_POINTER(X) PyCapsule_New((void *) X, NULL, NULL) -#define PY_STRING_AS_STRING(X) PyUnicode_AsUTF8(X) -#endif /* ---------------------------------------------------------------------- */