Skip to content

Commit

Permalink
Support Python 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
hanya committed Aug 8, 2012
1 parent 956f948 commit 801ad6c
Show file tree
Hide file tree
Showing 11 changed files with 571 additions and 100 deletions.
11 changes: 10 additions & 1 deletion README
Expand Up @@ -4,7 +4,8 @@ This can be build without hole source tree of the office.

Requirements
- Apache OpenOffice 3.4? and its SDK.
- Python 2.7 and its include files.
- Python 3.3 and its include files.
Python 3.0 to 3.2 are not supported yet.

Build
Setup SDK environment to build.
Expand All @@ -24,3 +25,11 @@ Build
- LD_LIBRARY_PATH or PATH
specifies basis3.X/program and ure/lib to be found required libraries.

Difference between Python 2 and Python 3 on PyUNO
Most of differences are caused by Python itself.
- String must be unicode known as normal str.
- There is not int type anymore.
- uno.ByteSequence must be initialized with bytes, bytearray or
ByteSequence instance.
- No __members__ and __methods__ on pyuno instance.

246 changes: 193 additions & 53 deletions source/module/pyuno.cxx
Expand Up @@ -342,7 +342,7 @@ PyObject *PyUNO_invoke( PyObject *object, const char *name , PyObject *args )
Runtime runtime;

PyRef paras,callable;
if( PyObject_IsInstance( object, getPyUnoClass( runtime ).get() ) )
if( PyObject_IsInstance( object, getPyUnoClass().get() ) )
{
PyUNO* me = (PyUNO*) object;
OUString attrName = OUString::createFromAscii(name);
Expand Down Expand Up @@ -440,20 +440,30 @@ PyObject *PyUNO_str( PyObject * self )
me->members->wrappedObject.getValueType().getTypeLibType() );
buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) );
}

#if PY_VERSION_HEX >= 0x03030000
return PyUnicode_FromString( buf.getStr() );
#else
return PyString_FromString( buf.getStr());
#endif
}

#if PY_VERSION_HEX >= 0x03030000
PyObject* PyUNO_getattr (PyObject* self, PyObject *attr_name)
#else
PyObject* PyUNO_getattr (PyObject* self, char* name)
#endif
{
PyUNO* me;

#if PY_VERSION_HEX >= 0x03030000
char *name = PyUnicode_AsUTF8(attr_name);
#endif
try
{

Runtime runtime;

me = (PyUNO*) self;
#if PY_VERSION_HEX < 0x03000000
//Handle Python dir () stuff first...
if (strcmp (name, "__members__") == 0)
{
Expand All @@ -469,17 +479,20 @@ PyObject* PyUNO_getattr (PyObject* self, char* name)
}
return member_list;
}
#endif
if (strcmp (name, "__dict__") == 0)
{
Py_INCREF (Py_None);
return Py_None;
}
#if PY_VERSION_HEX < 0x03000000
if (strcmp (name, "__methods__") == 0)
{
Py_INCREF (Py_None);
return Py_None;
}
#endif

if (strcmp (name, "__class__") == 0)
{
if( me->members->wrappedObject.getValueTypeClass() ==
Expand Down Expand Up @@ -550,10 +563,16 @@ PyObject* PyUNO_getattr (PyObject* self, char* name)
return NULL;
}

#if PY_VERSION_HEX >= 0x03030000
int PyUNO_setattr (PyObject* self, PyObject *attr_name, PyObject* value)
#else
int PyUNO_setattr (PyObject* self, char* name, PyObject* value)
#endif
{
PyUNO* me;

#if PY_VERSION_HEX >= 0x03030000
char *name = PyUnicode_AsUTF8(attr_name);
#endif
me = (PyUNO*) self;
try
{
Expand Down Expand Up @@ -594,6 +613,102 @@ int PyUNO_setattr (PyObject* self, char* name, PyObject* value)
return 1; //as above.
}


#if PY_VERSION_HEX >= 0x03000000
static PyObject *PyUNO_dir( PyObject *self, PyObject *that )
{
PyUNO* me;
PyObject* member_list;
Sequence<OUString> oo_member_list;

me = (PyUNO*) self;
oo_member_list = me->members->xInvocation->getMemberNames ();
member_list = PyList_New (oo_member_list.getLength ());
for (int i = 0; i < oo_member_list.getLength (); i++)
{
// setitem steals a reference
PyList_SetItem (member_list, i, ustring2PyUnicode(oo_member_list[i]).getAcquired() );
}
return member_list;
}


static PyObject *PyUNO_richcompare( PyObject *self, PyObject *that, int op )
{
switch (op)
{
case Py_EQ:
case Py_NE:
if( self == that )
{
if (op == Py_EQ)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
try
{
Runtime runtime;
if( PyObject_IsInstance( that, getPyUnoClass().get() ) )
{
PyUNO *me = reinterpret_cast< PyUNO*> ( self );
PyUNO *other = reinterpret_cast< PyUNO *> (that );
com::sun::star::uno::TypeClass tcMe = me->members->wrappedObject.getValueTypeClass();
com::sun::star::uno::TypeClass tcOther = other->members->wrappedObject.getValueTypeClass();

if( tcMe == tcOther )
{
if( tcMe == com::sun::star::uno::TypeClass_STRUCT ||
tcMe == com::sun::star::uno::TypeClass_EXCEPTION )
{
Reference< XMaterialHolder > xMe( me->members->xInvocation,UNO_QUERY);
Reference< XMaterialHolder > xOther( other->members->xInvocation,UNO_QUERY );
if( xMe->getMaterial() == xOther->getMaterial() )
{
if (op == Py_EQ)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
}
else if( tcMe == com::sun::star::uno::TypeClass_INTERFACE )
{
if( me->members->wrappedObject == other->members->wrappedObject )
{
if (op == Py_EQ)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
}
}
}
if (op == Py_EQ)
Py_RETURN_FALSE;
else
Py_RETURN_TRUE;
}
catch( com::sun::star::uno::RuntimeException & e)
{
raisePyExceptionWithAny( makeAny( e ) );
}
break;
default:
PyErr_SetString(Py_NotImplemented, "not implemented");
break;
}

return NULL;
}


static struct PyMethodDef PyUNO_methods[] = {
{ "__dir__", (PyCFunction)PyUNO_dir, METH_VARARGS, NULL},
{ NULL, NULL }
};


#else
// ensure object identity and struct equality
static int PyUNO_cmp( PyObject *self, PyObject *that )
{
Expand Down Expand Up @@ -636,61 +751,86 @@ static int PyUNO_cmp( PyObject *self, PyObject *that )
}
return retDefault;
}
#endif

static PyTypeObject PyUNOType =
{
PyVarObject_HEAD_INIT(&PyType_Type, 0)
const_cast< char * >("pyuno"),
sizeof (PyUNO),
0,
(destructor) PyUNO_del,
(printfunc) 0,
(getattrfunc) PyUNO_getattr,
(setattrfunc) PyUNO_setattr,
(cmpfunc) PyUNO_cmp,
(reprfunc) PyUNO_repr,
0,
0,
0,
(hashfunc) 0,
(ternaryfunc) 0,
(reprfunc) PyUNO_str,
(getattrofunc)0,
(setattrofunc)0,
NULL,
0,
NULL,
(traverseproc)0,
(inquiry)0,
(richcmpfunc)0,
0,
(getiterfunc)0,
(iternextfunc)0,
NULL,
NULL,
NULL,
NULL,
NULL,
(descrgetfunc)0,
(descrsetfunc)0,
0,
(initproc)0,
(allocfunc)0,
(newfunc)0,
(freefunc)0,
(inquiry)0,
NULL,
NULL,
NULL,
NULL,
NULL,
(destructor)0
#if PY_VERSION_HEX >= 0x03000000
PyVarObject_HEAD_INIT (&PyType_Type, 0)
#else
PyObject_HEAD_INIT (&PyType_Type)
0, /* ob_size */
#endif
const_cast< char * >("pyuno"), /* tp_name */
sizeof (PyUNO), /* tp_basicsize */
0, /* tp_itemsize */
(destructor) PyUNO_del, /* tp_dealloc */
(printfunc) 0, /* tp_print */
#if PY_VERSION_HEX >= 0x03030000
(getattrfunc) 0, /* tp_getattr */
(setattrfunc) 0, /* tp_setattr */
0, /* tp_reserved */
#else
(getattrfunc) PyUNO_getattr, /* tp_getattr */
(setattrfunc) PyUNO_setattr, /* tp_setattr */
(cmpfunc) PyUNO_cmp, /*tp_compare*/
#endif
(reprfunc) PyUNO_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc) 0, /* tp_hash */
(ternaryfunc) 0, /* tp_call */
(reprfunc) PyUNO_str, /* tp_str */
#if PY_VERSION_HEX >= 0x03030000
(getattrofunc)PyUNO_getattr, /* tp_getattro */
(setattrofunc)PyUNO_setattr, /* tp_setattro */
#else
(getattrofunc)0, /* tp_getattro */
(setattrofunc)0, /* tp_setattro */
#endif
NULL, /* tp_as_buffer */
0, /* tp_flags */
NULL, /* tp_doc */
(traverseproc)0, /* tp_traverse */
(inquiry)0, /* tp_clear */
#if PY_VERSION_HEX >= 0x03000000
PyUNO_richcompare,
#else
(richcmpfunc)0, /* tp_richcompare */
#endif
0, /* tp_weaklistoffset */
(getiterfunc)0, /* tp_iter */
(iternextfunc)0, /* tp_iternext */
#if PY_VERSION_HEX >= 0x03000000
PyUNO_methods,
#else
NULL, /* tp_methods */
#endif
NULL, /* tp_members */
NULL, /* tp_getset */
NULL, /* tp_base */
NULL, /* tp_dict */
(descrgetfunc)0, /* tp_descr_get */
(descrsetfunc)0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)0, /* tp_init */
(allocfunc)0, /* tp_alloc */
(newfunc)0, /* tp_new */
(freefunc)0, /* tp_free */
(inquiry)0, /* tp_is_gc */
NULL, /* tp_bases */
NULL, /* tp_mro */
NULL, /* tp_cache */
NULL, /* tp_subclasses */
NULL, /* tp_weaklist */
(destructor)0 /* tp_del */
#if PY_VERSION_HEX >= 0x02060000
, 0
, 0 /* tp_version_tag */
#endif
};

PyRef getPyUnoClass( const Runtime &)
PyRef getPyUnoClass()
{
return PyRef( reinterpret_cast< PyObject * > ( &PyUNOType ) );
}
Expand Down
4 changes: 4 additions & 0 deletions source/module/pyuno_adapter.cxx
Expand Up @@ -246,7 +246,11 @@ Any Adapter::invoke( const OUString &aFunctionName,
buf.appendAscii( "pyuno::Adapater: Method " ).append( aFunctionName );
buf.appendAscii( " is not implemented at object " );
PyRef str( PyObject_Repr( mWrappedObject.get() ), SAL_NO_ACQUIRE );
#if PY_VERSION_HEX >= 0x03030000
buf.appendAscii( PyUnicode_AsUTF8( str.get() ));
#else
buf.appendAscii( PyString_AsString( str.get() ));
#endif
throw IllegalArgumentException( buf.makeStringAndClear(), Reference< XInterface > (),0 );
}

Expand Down
11 changes: 10 additions & 1 deletion source/module/pyuno_callable.cxx
Expand Up @@ -191,15 +191,24 @@ PyObject* PyUNO_callable_call (PyObject* self, PyObject* args, PyObject*)

static PyTypeObject PyUNO_callable_Type =
{
PyVarObject_HEAD_INIT(&PyType_Type, 0)
#if PY_VERSION_HEX >= 0x03000000
PyVarObject_HEAD_INIT (&PyType_Type, 0)
#else
PyObject_HEAD_INIT (&PyType_Type)
0, /* ob_size */
#endif
const_cast< char * >("PyUNO_callable"),
sizeof (PyUNO_callable),
0,
(destructor) ::pyuno::PyUNO_callable_del,
(printfunc) 0,
(getattrfunc) 0,
(setattrfunc) 0,
#if PY_VERSION_HEX >= 0x03000000
0, /* tp_reserved */
#else
(cmpfunc) 0,
#endif
(reprfunc) 0,
0,
0,
Expand Down

0 comments on commit 801ad6c

Please sign in to comment.