diff options
author | Andreas Becker <atayoohoo@googlemail.com> | 2011-05-07 20:35:03 +0100 |
---|---|---|
committer | Michael Meeks <michael.meeks@novell.com> | 2011-05-07 20:35:03 +0100 |
commit | a09ce46818fd4d5e08b3af9a478501cd8ef5b4fe (patch) | |
tree | 187c9164d436201442794dee227627e2b9173124 /pyuno/source/module/pyuno_module.cxx | |
parent | 7cf799064f5d64bca62626dc73723c2c5e95ca00 (diff) |
Port PyUno to support Python 3
Diffstat (limited to 'pyuno/source/module/pyuno_module.cxx')
-rw-r--r-- | pyuno/source/module/pyuno_module.cxx | 101 |
1 files changed, 62 insertions, 39 deletions
diff --git a/pyuno/source/module/pyuno_module.cxx b/pyuno/source/module/pyuno_module.cxx index 7a9327894c21..4788a2f1846b 100644 --- a/pyuno/source/module/pyuno_module.cxx +++ b/pyuno/source/module/pyuno_module.cxx @@ -230,7 +230,7 @@ PyObject * extractOneStringArg( PyObject *args, char const *funcName ) return NULL; } PyObject *obj = PyTuple_GetItem( args, 0 ); - if( !PyString_Check( obj ) && ! PyUnicode_Check(obj)) + if(!PyString_Check(obj) && !PyUnicode_Check(obj)) { OStringBuffer buf; buf.append( funcName ).append( ": expecting one string argument" ); @@ -244,16 +244,17 @@ static PyObject *createUnoStructHelper(PyObject *, PyObject* args ) { Any IdlStruct; PyRef ret; - try { Runtime runtime; if( PyTuple_Size( args ) == 2 ) { - PyObject *structName = PyTuple_GetItem( args,0 ); - PyObject *initializer = PyTuple_GetItem( args ,1 ); + PyObject *structName = PyTuple_GetItem(args, 0); + PyObject *initializer = PyTuple_GetItem(args, 1); - if( PyString_Check( structName ) ) + // Perhaps in Python 3, only PyUnicode_Check returns true and + // in Python 2, only PyString_Check returns true. + if(PyString_Check(structName) || PyUnicode_Check(structName)) { if( PyTuple_Check( initializer ) ) { @@ -491,9 +492,9 @@ static PyObject *isInterface( PyObject *, PyObject *args ) { PyObject *obj = PyTuple_GetItem( args, 0 ); Runtime r; - return PyInt_FromLong( isInterfaceClass( r, obj ) ); + return PyLong_FromLong( isInterfaceClass( r, obj ) ); } - return PyInt_FromLong( 0 ); + return PyLong_FromLong( 0 ); } static PyObject * generateUuid( PyObject *, PyObject * ) @@ -592,41 +593,42 @@ static PyObject * absolutize( PyObject *, PyObject * args ) return 0; } -static PyObject * invoke ( PyObject *, PyObject * args ) +static PyObject * invoke(PyObject *, PyObject *args) { PyObject *ret = 0; - if( PyTuple_Check( args ) && PyTuple_Size( args ) == 3 ) + if(PyTuple_Check(args) && PyTuple_Size(args) == 3) { - PyObject *object = PyTuple_GetItem( args, 0 ); - - if( PyString_Check( PyTuple_GetItem( args, 1 ) ) ) + PyObject *object = PyTuple_GetItem(args, 0); + PyObject *item1 = PyTuple_GetItem(args, 1); + if(PyString_Check(item1) || PyUnicode_Check(item1)) { - const char *name = PyString_AsString( PyTuple_GetItem( args, 1 ) ); - if( PyTuple_Check( PyTuple_GetItem( args , 2 ))) + const char *name = PyString_AsString(item1); + PyObject *item2 = PyTuple_GetItem(args, 2); + if(PyTuple_Check(item2)) { - ret = PyUNO_invoke( object, name , PyTuple_GetItem( args, 2 ) ); + ret = PyUNO_invoke(object, name, item2); } else { OStringBuffer buf; - buf.append( "uno.invoke expects a tuple as 3rd argument, got " ); - buf.append( PyString_AsString( PyObject_Str( PyTuple_GetItem( args, 2) ) ) ); - PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() ); + buf.append("uno.invoke expects a tuple as 3rd argument, got "); + buf.append(PyString_AsString(PyObject_Str(item2))); + PyErr_SetString(PyExc_RuntimeError, buf.makeStringAndClear()); } } else { OStringBuffer buf; - buf.append( "uno.invoke expected a string as 2nd argument, got " ); - buf.append( PyString_AsString( PyObject_Str( PyTuple_GetItem( args, 1) ) ) ); - PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() ); + buf.append("uno.invoke expected a string as 2nd argument, got "); + buf.append(PyString_AsString(PyObject_Str(item1))); + PyErr_SetString(PyExc_RuntimeError, buf.makeStringAndClear()); } } else { OStringBuffer buf; - buf.append( "uno.invoke expects object, name, (arg1, arg2, ... )\n" ); - PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear() ); + buf.append("uno.invoke expects object, name, (arg1, arg2, ... )\n"); + PyErr_SetString(PyExc_RuntimeError, buf.makeStringAndClear()); } return ret; } @@ -690,31 +692,52 @@ static PyObject *setCurrentContext( PyObject *, PyObject * args ) struct PyMethodDef PyUNOModule_methods [] = { - {const_cast< char * >("getComponentContext"), getComponentContext, 1, NULL}, - {const_cast< char * >("_createUnoStructHelper"), createUnoStructHelper, 2, NULL}, - {const_cast< char * >("getTypeByName"), getTypeByName, 1, NULL}, - {const_cast< char * >("getConstantByName"), getConstantByName,1, NULL}, - {const_cast< char * >("getClass"), getClass,1, NULL}, - {const_cast< char * >("checkEnum"), checkEnum, 1, NULL}, - {const_cast< char * >("checkType"), checkType, 1, NULL}, - {const_cast< char * >("generateUuid"), generateUuid,0, NULL}, - {const_cast< char * >("systemPathToFileUrl"),systemPathToFileUrl,1, NULL}, - {const_cast< char * >("fileUrlToSystemPath"),fileUrlToSystemPath,1, NULL}, - {const_cast< char * >("absolutize"),absolutize,2, NULL}, - {const_cast< char * >("isInterface"),isInterface,1, NULL}, - {const_cast< char * >("invoke"),invoke, 2, NULL}, - {const_cast< char * >("setCurrentContext"),setCurrentContext,1, NULL}, - {const_cast< char * >("getCurrentContext"),getCurrentContext,1, NULL}, + {const_cast< char * >("getComponentContext"), getComponentContext, METH_VARARGS, NULL}, + {const_cast< char * >("_createUnoStructHelper"), createUnoStructHelper, METH_VARARGS | METH_KEYWORDS, NULL}, + {const_cast< char * >("getTypeByName"), getTypeByName, METH_VARARGS, NULL}, + {const_cast< char * >("getConstantByName"), getConstantByName, METH_VARARGS, NULL}, + {const_cast< char * >("getClass"), getClass, METH_VARARGS, NULL}, + {const_cast< char * >("checkEnum"), checkEnum, METH_VARARGS, NULL}, + {const_cast< char * >("checkType"), checkType, METH_VARARGS, NULL}, + {const_cast< char * >("generateUuid"), generateUuid, METH_VARARGS, NULL}, + {const_cast< char * >("systemPathToFileUrl"), systemPathToFileUrl, METH_VARARGS, NULL}, + {const_cast< char * >("fileUrlToSystemPath"), fileUrlToSystemPath, METH_VARARGS, NULL}, + {const_cast< char * >("absolutize"), absolutize, METH_VARARGS | METH_KEYWORDS, NULL}, + {const_cast< char * >("isInterface"), isInterface, METH_VARARGS, NULL}, + {const_cast< char * >("invoke"), invoke, METH_VARARGS | METH_KEYWORDS, NULL}, + {const_cast< char * >("setCurrentContext"), setCurrentContext, METH_VARARGS, NULL}, + {const_cast< char * >("getCurrentContext"), getCurrentContext, METH_VARARGS, NULL}, {NULL, NULL, 0, NULL} }; } -extern "C" PY_DLLEXPORT void initpyuno() +extern "C" PY_DLLEXPORT +#if PY_MAJOR_VERSION >= 3 +PyObject* PyInit_pyuno() { // noop when called already, otherwise needed to allow multiple threads PyEval_InitThreads(); + static struct PyModuleDef moduledef = + { + PyModuleDef_HEAD_INIT, + "pyuno", // module name + 0, // module documentation + -1, // module keeps state in global variables, + PyUNOModule_methods, // modules methods + 0, // m_reload (must be 0) + 0, // m_traverse + 0, // m_clear + 0, // m_free + }; + return PyModule_Create(&moduledef); +} +#else +void initpyuno() +{ + PyEval_InitThreads(); Py_InitModule (const_cast< char * >("pyuno"), PyUNOModule_methods); } +#endif /* PY_MAJOR_VERSION >= 3 */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |