Skip to content

Commit

Permalink
Remove Python2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
hobu committed Jul 23, 2019
1 parent 28541f6 commit 3ddc09f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 87 deletions.
57 changes: 13 additions & 44 deletions cmake/python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,31 @@
#

#
# Version 3.12 has shiny new FindPython2 and FindPython3 scripts
# Version 3.12 has shiny new FindPython3 scripts
# Version 3.14 introduces direct support for NumPy.
# When we require newer cmake versions, ditch the older support.
#
if (NOT (CMAKE_VERSION VERSION_LESS "3.14.0"))
find_package(Python3 COMPONENTS Interpreter Development NumPy)
if (NOT Python3_FOUND)
find_package(Python2 2.7 REQUIRED EXACT
COMPONENTS Interpreter Development NumPy)

# Since we've required 2.7, these should all be valid
set(PYTHON_LIBRARY ${Python2_LIBRARIES}
CACHE FILEPATH "Python library")
set(PYTHON_INCLUDE_DIR ${Python2_INCLUDE_DIRS}
CACHE PATH "Location of Python include files")
set(PYTHON_NUMPY_INCLUDE_DIR ${Python2_NumPy_INCLUDE_DIRS}
CACHE PATH "Location of NumPy include files.")
else()
set(PYTHON_LIBRARY ${Python3_LIBRARIES}
CACHE FILEPATH "Python library")
set(PYTHON_INCLUDE_DIR ${Python3_INCLUDE_DIRS}
CACHE PATH "Location of Python include files.")
set(PYTHON_NUMPY_INCLUDE_DIR ${Python3_NumPy_INCLUDE_DIRS}
CACHE PATH "Location of NumPy include files.")
endif()
set(PYTHON_LIBRARY ${Python3_LIBRARIES}
CACHE FILEPATH "Python library")
set(PYTHON_INCLUDE_DIR ${Python3_INCLUDE_DIRS}
CACHE PATH "Location of Python include files.")
set(PYTHON_NUMPY_INCLUDE_DIR ${Python3_NumPy_INCLUDE_DIRS}
CACHE PATH "Location of NumPy include files.")
set(PDAL_HAVE_PYTHON 1)
elseif (NOT (CMAKE_VERSION VERSION_LESS "3.12.0"))
find_package(Python3 COMPONENTS Interpreter Development)
if (NOT Python3_FOUND)
find_package(Python2 2.7 REQUIRED EXACT
COMPONENTS Interpreter Development)

# Since we've required 2.7, these should all be valid
set(PYTHON_LIBRARY ${Python2_LIBRARIES}
CACHE FILEPATH "Python library")
set(PYTHON_INCLUDE_DIR ${Python2_INCLUDE_DIRS}
CACHE PATH "Location of Python include files")
set(PYTHON_EXECUTABLE ${Python2_EXECUTABLE})
find_package(NumPy 1.5 REQUIRED)
else()
set(PYTHON_LIBRARY ${Python3_LIBRARIES}
CACHE FILEPATH "Python library")
set(PYTHON_INCLUDE_DIR ${Python3_INCLUDE_DIRS}
CACHE PATH "Location of Python include files.")
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
find_package(NumPy 1.5 REQUIRED)
endif()
set(PYTHON_LIBRARY ${Python3_LIBRARIES}
CACHE FILEPATH "Python library")
set(PYTHON_INCLUDE_DIR ${Python3_INCLUDE_DIRS}
CACHE PATH "Location of Python include files.")
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
find_package(NumPy 1.5 REQUIRED)
set(PDAL_HAVE_PYTHON 1)
else()
find_package(PythonInterp 3 QUIET)
find_package(PythonLibs 3 QUIET)
if ((NOT PYTHONINTERP_FOUND) OR (NOT PYTHONLIBS_FOUND))
unset(PYTHON_EXECUTABLE CACHE)
find_package(PythonInterp 2.7 EXACT REQUIRED)
find_package(PythonLibs 2.7 EXACT REQUIRED)
endif()
set(PDAL_HAVE_PYTHON 1)
find_package(NumPy 1.5 REQUIRED)
endif()
Expand Down
12 changes: 0 additions & 12 deletions plugins/python/plang/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,8 @@ std::string getTraceback()
PyObject* r = PyObject_Repr(l);
if (!r)
throw pdal::pdal_error("unable to get repr in getTraceback");
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
const char *d = PyUnicode_AsUTF8AndSize(r, &size);
#else
const char *d = PyString_AsString(r);
#endif
mssg << d;
}

Expand All @@ -215,12 +211,8 @@ std::string getTraceback()
PyObject* r = PyObject_Repr(value);
if (!r)
throw pdal::pdal_error("couldn't make string representation of traceback value");
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
const char *d = PyUnicode_AsUTF8AndSize(r, &size);
#else
const char *d = PyString_AsString(r);
#endif
mssg << d;
}
else
Expand Down Expand Up @@ -276,12 +268,8 @@ std::string readPythonString(PyObject* dict, const std::string& key)
PyObject* r = PyObject_Str(o);
if (!r)
throw pdal::pdal_error("unable to get repr in readPythonString");
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
const char *d = PyUnicode_AsUTF8AndSize(r, &size);
#else
const char *d = PyString_AsString(r);
#endif
ss << d;

return ss.str();
Expand Down
8 changes: 0 additions & 8 deletions plugins/python/plang/Invocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ void Printobject(PyObject* o)
PyObject* r = PyObject_Repr(o);
if (!r)
throw pdal::pdal_error("couldn't make string representation of traceback value");
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
const char *d = PyUnicode_AsUTF8AndSize(r, &size);
#else
const char *d = PyString_AsString(r);
#endif
std::cout << "raw_json" << d << std::endl;
}
void Invocation::compile()
Expand Down Expand Up @@ -265,11 +261,7 @@ void Invocation::getOutputNames(std::vector<std::string>& names)
while (PyDict_Next(m_varsOut, &pos, &key, &value))
{
const char* p(0);
#if PY_MAJOR_VERSION >= 3
p = PyBytes_AsString(PyUnicode_AsUTF8String(key));
#else
p = PyString_AsString(key);
#endif
if (p)
names.push_back(p);
}
Expand Down
34 changes: 11 additions & 23 deletions plugins/python/plang/Redirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ static PyTypeObject StdoutType =
0, /* tp_weaklist */
0, /* tp_del */
0, /* tp_version_tag */
#if PY_MAJOR_VERSION >= 3
0, /* tp_finalilzer */
#endif
};


Expand All @@ -135,37 +133,27 @@ Redirector::~Redirector()

PyMODINIT_FUNC redirector_init(void)
{
#if PY_MAJOR_VERSION >= 3
return Redirector::init();
#else
Redirector::init();
#endif
}

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef redirectordef = {
PyModuleDef_HEAD_INIT,
"redirector", /* m_name */
"redirector.Stdout objects", /* m_doc */
-1, /* m_size */
Stdout_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif
static struct PyModuleDef redirectordef = {
PyModuleDef_HEAD_INIT,
"redirector", /* m_name */
"redirector.Stdout objects", /* m_doc */
-1, /* m_size */
Stdout_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};

PyObject* Redirector::init()
{
StdoutType.tp_new = PyType_GenericNew;
if (PyType_Ready(&StdoutType) < 0)
return NULL;
#if PY_MAJOR_VERSION >= 3
PyObject* m = PyModule_Create(&redirectordef);
#else
PyObject* m = Py_InitModule3("redirector", 0, 0);
#endif
if (m)
{
//ABELL - This is bad code as the type cast is invalid. (type pun
Expand Down

0 comments on commit 3ddc09f

Please sign in to comment.