Skip to content

Commit f15db4b

Browse files
Ovascosahmed-irfan
andauthored
Remove Python 2 bindings (#107)
* Removed Python2 from polypy * Code fixes in pypoly code * Fixed make check * Update cmake version requirement * assert(false) -> assert(0) * Python detection update * Python detection update --------- Co-authored-by: Thomas Hader <[email protected]> Co-authored-by: Ahmed Irfan <[email protected]>
1 parent 0ffad69 commit f15db4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+5425
-11305
lines changed

CMakeLists.txt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.5)
1+
cmake_minimum_required(VERSION 3.12)
22

33
#
44
# Stop warning on cygwin
@@ -67,16 +67,11 @@ add_subdirectory(src)
6767
add_subdirectory(include)
6868

6969
if(LIBPOLY_BUILD_PYTHON_API)
70-
71-
# Need the Python binary to run tests
72-
find_package(PythonInterp)
73-
70+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
7471
# Configure the Python bindings
7572
add_subdirectory(python)
76-
7773
# Configure the Python tests
7874
add_subdirectory(test/python)
79-
8075
endif()
8176

8277
if (BUILD_TESTING)

python/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ set(polypy_SOURCES
1313
polypyFeasibilitySet.c
1414
)
1515

16-
find_package(PythonLibs)
17-
1816
include_directories(${GMP_INCLUDE_DIR})
1917
include_directories(${libpoly_SOURCE_DIR}/include)
20-
include_directories(${PYTHON_INCLUDE_PATH})
2118

2219
add_library(polypy MODULE ${polypy_SOURCES})
20+
target_include_directories(polypy PRIVATE ${Python3_INCLUDE_DIRS})
21+
target_link_libraries(polypy PRIVATE poly ${Python3_LIBRARIES})
22+
2323
set_target_properties(polypy PROPERTIES PREFIX "")
24+
#set_target_properties(polypy PROPERTIES SUFFIX "${Python3_EXTENSION_SUFFIX}")
25+
2426
if (APPLE)
2527
set_target_properties(polypy PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
2628
endif()
27-
target_link_libraries(polypy poly)
2829

2930
string (REPLACE ";" "', '" polypy_SOURCES_STR "${polypy_SOURCES}")
3031
configure_file(setup.py.in ${CMAKE_CURRENT_SOURCE_DIR}/setup.py)

python/polypy.c

Lines changed: 167 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,172 @@
1717
* along with LibPoly. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
#define PY_SSIZE_T_CLEAN
21-
#include "Python.h"
20+
#include "python.h"
2221

23-
#if PY_MAJOR_VERSION >= 3
24-
#include "polypy3.c"
25-
#else
26-
#include "polypy2.c"
22+
#include "polypyInteger.h"
23+
#include "polypyVariable.h"
24+
#include "polypyVariableOrder.h"
25+
#include "polypyUPolynomial.h"
26+
#include "polypyAlgebraicNumber.h"
27+
#include "polypyPolynomial.h"
28+
#include "polypyAssignment.h"
29+
#include "polypyValue.h"
30+
#include "polypyInterval.h"
31+
#include "polypyFeasibilitySet.h"
32+
33+
static PyObject*
34+
Trace_enable(PyObject* self, PyObject* args) {
35+
#ifndef NDEBUG
36+
const char *tag;
37+
if (!PyArg_ParseTuple(args, "s", &tag)) {
38+
return NULL;
39+
}
40+
lp_trace_enable(tag);
41+
#endif
42+
Py_RETURN_NONE;
43+
}
44+
45+
static PyObject*
46+
Trace_disable(PyObject* self, PyObject* args) {
47+
#ifndef NDEBUG
48+
const char *tag;
49+
if (!PyArg_ParseTuple(args, "s", &tag)) {
50+
return NULL;
51+
}
52+
lp_trace_disable(tag);
53+
#endif
54+
Py_RETURN_NONE;
55+
}
56+
57+
static PyObject*
58+
Stats_print(PyObject* self) {
59+
lp_stats_print(stdout);
60+
Py_RETURN_NONE;
61+
}
62+
63+
static PyMethodDef polypy_methods[] = {
64+
{"trace_enable", (PyCFunction)Trace_enable, METH_VARARGS, "Enables tracing for the given tag"},
65+
{"trace_disable", (PyCFunction)Trace_disable, METH_VARARGS, "Disables tracing for the given tag"},
66+
{"stats_print", (PyCFunction)Stats_print, METH_NOARGS, "Prints the statistics"},
67+
{NULL} /* Sentinel */
68+
};
69+
70+
71+
static struct PyModuleDef polypymodule = {
72+
PyModuleDef_HEAD_INIT,
73+
"polypy",
74+
NULL,
75+
0, // sizeof polypy struct
76+
polypy_methods,
77+
NULL,
78+
NULL,
79+
NULL
80+
};
81+
82+
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
83+
#define PyMODINIT_FUNC void
2784
#endif
85+
PyMODINIT_FUNC
86+
PyInit_polypy(void)
87+
{
88+
PyObject* m;
89+
90+
if (PyType_Ready(&CoefficientRingType) < 0)
91+
return NULL;
92+
if (PyType_Ready(&VariableType) < 0)
93+
return NULL;
94+
if (PyType_Ready(&VariableOrderType) < 0)
95+
return NULL;
96+
if (PyType_Ready(&AssignmentType) < 0)
97+
return NULL;
98+
if (PyType_Ready(&PolynomialType) < 0)
99+
return NULL;
100+
if (PyType_Ready(&UPolynomialType) < 0)
101+
return NULL;
102+
if (PyType_Ready(&AlgebraicNumberType) < 0)
103+
return NULL;
104+
if (PyType_Ready(&ValueType) < 0)
105+
return NULL;
106+
if (PyType_Ready(&IntervalType) < 0)
107+
return NULL;
108+
if (PyType_Ready(&FeasibilitySetType) < 0)
109+
return NULL;
110+
111+
m = PyModule_Create(&polypymodule);
112+
113+
// Initialize the library
114+
lp_set_output_language(LP_OUTPUT_PYTHON);
115+
116+
Py_INCREF(&CoefficientRingType);
117+
PyModule_AddObject(m, "CoefficientRing", (PyObject*)&CoefficientRingType);
118+
119+
PyObject* PyZ = PyCoefficientRing_create(lp_Z);
120+
Py_INCREF(PyZ);
121+
PyModule_AddObject(m, "Z", PyZ);
122+
123+
Py_INCREF(&VariableType);
124+
PyModule_AddObject(m, "Variable", (PyObject*)&VariableType);
125+
126+
Py_INCREF(&VariableOrderType);
127+
PyModule_AddObject(m, "VariableOrder", (PyObject*)&VariableOrderType);
128+
129+
PyObject* variable_order = VariableOrder_create(VariableOrder_get_default_order());
130+
Py_INCREF(variable_order);
131+
PyModule_AddObject(m, "variable_order", variable_order);
132+
133+
Py_INCREF(&PolynomialType);
134+
PyModule_AddObject(m, "Polynomial", (PyObject*)&PolynomialType);
135+
136+
// Sign conditions
137+
PyObject* Py_SGN_LT_0 = PyLong_FromLong(LP_SGN_LT_0);
138+
PyObject* Py_SGN_LE_0 = PyLong_FromLong(LP_SGN_LE_0);
139+
PyObject* Py_SGN_EQ_0 = PyLong_FromLong(LP_SGN_EQ_0);
140+
PyObject* Py_SGN_NE_0 = PyLong_FromLong(LP_SGN_NE_0);
141+
PyObject* Py_SGN_GT_0 = PyLong_FromLong(LP_SGN_GT_0);
142+
PyObject* Py_SGN_GE_0 = PyLong_FromLong(LP_SGN_GE_0);
143+
PyModule_AddObject(m, "SGN_LT_0", Py_SGN_LT_0);
144+
PyModule_AddObject(m, "SGN_LE_0", Py_SGN_LE_0);
145+
PyModule_AddObject(m, "SGN_EQ_0", Py_SGN_EQ_0);
146+
PyModule_AddObject(m, "SGN_NE_0", Py_SGN_NE_0);
147+
PyModule_AddObject(m, "SGN_GT_0", Py_SGN_GT_0);
148+
PyModule_AddObject(m, "SGN_GE_0", Py_SGN_GE_0);
149+
150+
Py_INCREF(&AssignmentType);
151+
PyModule_AddObject(m, "Assignment", (PyObject*)&AssignmentType);
152+
153+
Py_INCREF(&UPolynomialType);
154+
PyModule_AddObject(m, "UPolynomial", (PyObject*)&UPolynomialType);
155+
156+
int x_coeff[2] = { 0, 1 };
157+
lp_upolynomial_t* x_poly = lp_upolynomial_construct_from_int(lp_Z, 1, x_coeff);
158+
PyObject* x = PyUPolynomial_create(x_poly);
159+
Py_INCREF(x);
160+
PyModule_AddObject(m, "x", x);
161+
162+
Py_INCREF(&AlgebraicNumberType);
163+
PyModule_AddObject(m, "AlgebraicNumber", (PyObject*)&AlgebraicNumberType);
164+
165+
Py_INCREF(&ValueType);
166+
PyModule_AddObject(m, "Value", (PyObject*)&ValueType);
167+
168+
// Sign conditions
169+
lp_value_t value_inf_pos, value_inf_neg;
170+
lp_value_construct(&value_inf_pos, LP_VALUE_PLUS_INFINITY, NULL);
171+
lp_value_construct(&value_inf_neg, LP_VALUE_MINUS_INFINITY, NULL);
172+
PyObject* inf_pos = PyValue_create(&value_inf_pos);
173+
PyObject* inf_neg = PyValue_create(&value_inf_neg);
174+
PyModule_AddObject(m, "INFINITY_POS", inf_pos);
175+
PyModule_AddObject(m, "INFINITY_NEG", inf_neg);
176+
Py_INCREF(inf_pos);
177+
Py_INCREF(inf_neg);
178+
lp_value_destruct(&value_inf_pos);
179+
lp_value_destruct(&value_inf_neg);
180+
181+
Py_INCREF(&IntervalType);
182+
PyModule_AddObject(m, "Interval", (PyObject*)&IntervalType);
183+
184+
Py_INCREF(&FeasibilitySetType);
185+
PyModule_AddObject(m, "FeasibilitySet", (PyObject*)&FeasibilitySetType);
186+
187+
return m;
188+
}

python/polypy2.c

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)