Commit 831ebfcc authored by FPoupon's avatar FPoupon
Browse files

code compatibility with python2 and python3

parent 7fc4e886
......@@ -48,7 +48,7 @@ add_custom_command( OUTPUT ${TARGET_MODULE}
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_BINARY_DIR}/setup.py build
COMMAND ${CMAKE_COMMAND} -E copy
build/*/${TARGET_MODULE}
build/*/${PROJECT_NAME}*.so
${TARGET_MODULE}
DEPENDS ${SOURCES} )
add_custom_target( GkgPythonModule ALL
......
......@@ -39,7 +39,6 @@ static PyObject* executeCommand_wrapper( PyObject* self,
PyObject* value = 0;
Py_ssize_t pos = 0;
std::string algorithmName;
bool verbose = false;
PyObject* dictionaryValue = 0;
......@@ -49,7 +48,14 @@ static PyObject* executeCommand_wrapper( PyObject* self,
while ( PyDict_Next( obj, &pos, &key, &value ) )
{
std::string theKey( PyString_AsString( key ) );
std::string theKey;
if ( PyBytes_Check( key ) )
{
theKey = PyBytes_AsString( key );
}
if ( ( theKey != "algorithm" ) &&
( theKey != "parameters" ) &&
......@@ -65,11 +71,10 @@ static PyObject* executeCommand_wrapper( PyObject* self,
if ( theKey == "algorithm" )
{
if ( PyString_Check( value ) )
if ( PyBytes_Check( value ) )
{
algorithmName = std::string( PyString_AsString( value ) );
algorithmName = std::string( PyBytes_AsString( value ) );
}
else
......@@ -86,7 +91,7 @@ static PyObject* executeCommand_wrapper( PyObject* self,
if ( PyBool_Check( value ) )
{
verbose = ( ( ( PyBoolObject* )value )->ob_ival ? true : false );
verbose = ( value == Py_True ) ? true : false;
}
else
......@@ -136,16 +141,31 @@ static PyObject* executeCommand_wrapper( PyObject* self,
&parameterValue ) )
{
if ( !PyString_Check( parameterKey ) )
if ( !PyBytes_Check( parameterKey ) )
{
throw std::runtime_error( "parameter keys must be strings" );
}
std::string parameterName = std::string(
PyString_AsString( parameterKey ) );
PyBytes_AsString( parameterKey ) );
if ( PyLong_Check( parameterValue ) )
{
int32_t theParameterValue = _PyLong_AsInt( parameterValue );
parameters[ parameterName ] = theParameterValue;
if ( verbose )
{
std::cout << " " << parameterName << " -> "
<< theParameterValue << std::endl;
}
if ( PyInt_Check( parameterValue ) )
}
#if PY_MAJOR_VERSION < 3
else if ( PyInt_Check( parameterValue ) )
{
int32_t theParameterValue = _PyInt_AsInt( parameterValue );
......@@ -159,6 +179,7 @@ static PyObject* executeCommand_wrapper( PyObject* self,
}
}
#endif
else if ( PyFloat_Check( parameterValue ) )
{
......@@ -176,8 +197,7 @@ static PyObject* executeCommand_wrapper( PyObject* self,
else if ( PyBool_Check( parameterValue ) )
{
long theParameterValue =
( ( PyBoolObject* )parameterValue )->ob_ival;
long theParameterValue = ( parameterValue == Py_True ) ? 1 : 0;
parameters[ parameterName ] = ( int32_t )theParameterValue;
if ( verbose )
......@@ -189,11 +209,10 @@ static PyObject* executeCommand_wrapper( PyObject* self,
}
}
else if ( PyString_Check( parameterValue ) )
else if ( PyBytes_Check( parameterValue ) )
{
std::string theParameterValue(
PyString_AsString( parameterValue ) );
std::string theParameterValue( PyBytes_AsString( parameterValue ) );
parameters[ parameterName ] = theParameterValue;
if ( verbose )
......@@ -208,7 +227,6 @@ static PyObject* executeCommand_wrapper( PyObject* self,
else if ( PyTuple_Check( parameterValue ) )
{
if ( verbose )
{
......@@ -224,7 +242,22 @@ static PyObject* executeCommand_wrapper( PyObject* self,
{
PyObject* item = PyTuple_GetItem( parameterValue, index );
if ( PyInt_Check( item ) )
if ( PyLong_Check( item ) )
{
int32_t theItemValue = _PyLong_AsInt( item );
genericObjectList[ index ] = theItemValue;
if ( verbose )
{
std::cout << theItemValue << " " << std::flush;
}
}
#if PY_MAJOR_VERSION < 3
else if ( PyInt_Check( item ) )
{
int32_t theItemValue = _PyInt_AsInt( item );
......@@ -238,6 +271,7 @@ static PyObject* executeCommand_wrapper( PyObject* self,
}
}
#endif
else if ( PyFloat_Check( item ) )
{
......@@ -255,7 +289,7 @@ static PyObject* executeCommand_wrapper( PyObject* self,
else if ( PyBool_Check( item ) )
{
long theItemValue = ( ( PyBoolObject* )item )->ob_ival;
long theItemValue = ( item == Py_True ) ? 1 : 0;
genericObjectList[ index ] = ( int32_t )theItemValue;
if ( verbose )
......@@ -266,10 +300,10 @@ static PyObject* executeCommand_wrapper( PyObject* self,
}
}
else if ( PyString_Check( item ) )
else if ( PyBytes_Check( item ) )
{
std::string theItemValue( PyString_AsString( item ) );
std::string theItemValue( PyBytes_AsString( item ) );
genericObjectList[ index ] = theItemValue;
if ( verbose )
......@@ -332,6 +366,32 @@ static PyMethodDef GkgMethods[] =
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef gkgPyModule =
{
PyModuleDef_HEAD_INIT,
"gkg",
"Extension module of GKG",
-1,
GkgMethods
};
PyMODINIT_FUNC PyInit_gkg()
{
PyObject* pyObject = PyModule_Create( &gkgPyModule );
gkg::PluginLoader::getInstance().load( false, true );
return pyObject;
}
#else
PyMODINIT_FUNC initgkg()
{
......@@ -339,3 +399,5 @@ PyMODINIT_FUNC initgkg()
gkg::PluginLoader::getInstance().load( false, true );
}
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment