pyuno/source/loader/pythonloader.py | 2 pyuno/source/module/pyuno.cxx | 2 pyuno/source/module/pyuno_adapter.cxx | 2 pyuno/source/module/pyuno_impl.hxx | 76 +++++++++++++++++++++++--------- pyuno/source/module/pyuno_module.cxx | 29 +++++------- pyuno/source/module/pyuno_runtime.cxx | 18 +++---- pyuno/source/module/pyuno_type.cxx | 22 ++++----- pyuno/source/module/pyuno_util.cxx | 16 +++++- pyuno/source/module/uno.py | 14 ++++- scripting/examples/python/Capitalise.py | 8 +-- scripting/source/pyprov/pythonscript.py | 75 ++++++++++++++++++++----------- 11 files changed, 167 insertions(+), 97 deletions(-)
New commits: commit 0e7db6c33bfa2c31439eb602065377428c8c700b Author: Michael Stahl <mst...@redhat.com> Date: Sun Nov 25 16:05:34 2012 +0100 scripting: Capitalise.py example: fix Python 3 syntax errors Change-Id: I000e32ed1701c657046ae3b7f836012a7fd56fe4 diff --git a/scripting/examples/python/Capitalise.py b/scripting/examples/python/Capitalise.py index 274a502..d9c6f07 100644 --- a/scripting/examples/python/Capitalise.py +++ b/scripting/examples/python/Capitalise.py @@ -22,10 +22,10 @@ def getNewString( theString ) : return "" # should we tokenize on "."? if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() : - # first two chars are UC => first UC, rest LC + # first two chars are UC => first UC, rest LC newString=theString[0:1].upper() + theString[1:].lower(); elif theString[0].isupper(): - # first char UC => all to LC + # first char UC => all to LC newString=theString.lower() else: # all to UC. newString=theString.upper() @@ -47,7 +47,7 @@ def capitalisePython( ): count = xIndexAccess.getCount(); if(count>=1): #ie we have a selection i=0 - while i < count : + while i < count : xTextRange = xIndexAccess.getByIndex(i); #print "string: " + xTextRange.getString(); theString = xTextRange.getString(); @@ -70,7 +70,7 @@ def capitalisePython( ): if newString: xTextRange.setString(newString); xSelectionSupplier.select(xTextRange); - i+= 1 + i+= 1 # lists the scripts, that shall be visible inside OOo. Can be omited, if commit 6af846d0a13a8fb6e5153a0759e4952907bb86e5 Author: Michael Stahl <mst...@redhat.com> Date: Sun Nov 25 16:03:47 2012 +0100 scripting: pythonscript.py: adapt to Python 3 - "unicode" compatiblity - convert "exec", "print" statements - exception syntax - use "ast" module instead of deprecated "compiler" Change-Id: I2995b79d8854433824fdfafe8314ee5c7a3eacf6 diff --git a/scripting/source/pyprov/pythonscript.py b/scripting/source/pyprov/pythonscript.py index 1695e05..78c6226 100755 --- a/scripting/source/pyprov/pythonscript.py +++ b/scripting/source/pyprov/pythonscript.py @@ -22,7 +22,13 @@ import sys import os import imp import time -import compiler +import ast + +try: + unicode +except NameError: + # Python 3 compatibility + unicode = str class LogLevel: NONE = 0 @@ -66,7 +72,7 @@ def getLogTarget(): systemPath = uno.fileUrlToSystemPath( userInstallation + "/Scripts/python/log.txt" ) ret = file( systemPath , "a" ) except: - print "Exception during creation of pythonscript logfile: "+ lastException2String() + "\n, delagating log to stdout\n" + print("Exception during creation of pythonscript logfile: "+ lastException2String() + "\n, delagating log to stdout\n") return ret class Logger(LogLevel): @@ -99,7 +105,7 @@ class Logger(LogLevel): "\n" ) self.target.flush() except: - print "Error during writing to stdout: " +lastException2String() + "\n" + print("Error during writing to stdout: " +lastException2String() + "\n") log = Logger( getLogTarget() ) @@ -200,10 +206,10 @@ class MyUriHelper: ret = self.m_baseUri + "/" + myUri.getName().replace( "|", "/" ) log.isDebugLevel() and log.debug( "converting scriptURI="+scriptURI + " to storageURI=" + ret ) return ret - except UnoException, e: + except UnoException as e: log.error( "error during converting scriptURI="+scriptURI + ": " + e.Message) raise RuntimeException( "pythonscript:scriptURI2StorageUri: " +e.getMessage(), None ) - except Exception, e: + except Exception as e: log.error( "error during converting scriptURI="+scriptURI + ": " + str(e)) raise RuntimeException( "pythonscript:scriptURI2StorageUri: " + str(e), None ) @@ -223,9 +229,9 @@ def hasChanged( oldDate, newDate ): newDate.HundredthSeconds > oldDate.HundredthSeconds def ensureSourceState( code ): - if not code.endswith( "\n" ): - code = code + "\n" - code = code.replace( "\r", "" ) + if code.endswith(b"\n"): + code = code + b"\n" + code = code.replace(b"\r", b"") return code @@ -332,10 +338,10 @@ class ProviderContext: def isUrlInPackage( self, url ): values = self.mapPackageName2Path.values() for i in values: -# print "checking " + url + " in " + str(i.pathes) +# print ("checking " + url + " in " + str(i.pathes)) if url in i.pathes: return True -# print "false" +# print ("false") return False def setPackageAttributes( self, mapPackageName2Path, rootUrl ): @@ -366,27 +372,42 @@ class ProviderContext: checkForPythonPathBesideScript( url[0:url.rfind('/')] ) src = ensureSourceState( src ) - code = compiler.parse( src ) + try: + code = ast.parse( src ) + except: + log.isDebugLevel() and log.debug( "pythonscript: getFuncsByUrl: exception while parsing: " + lastException2String()) + raise allFuncs = [] if code == None: return allFuncs - + g_exportedScripts = [] - for node in code.node.nodes: - if node.__class__.__name__ == 'Function': + for node in ast.iter_child_nodes(code): + if isinstance(node, ast.FunctionDef): allFuncs.append(node.name) - elif node.__class__.__name__ == 'Assign': - for assignee in node.nodes: - if assignee.name == 'g_exportedScripts': - for item in node.expr.nodes: - if item.__class__.__name__ == 'Name': - g_exportedScripts.append(item.name) + elif isinstance(node, ast.Assign): + for target in node.targets: + if target.id == "g_exportedScripts": + for value in node.value.elts: + g_exportedScripts.append(value.id) return g_exportedScripts +# Python 2 only +# for node in code.node.nodes: +# if node.__class__.__name__ == 'Function': +# allFuncs.append(node.name) +# elif node.__class__.__name__ == 'Assign': +# for assignee in node.nodes: +# if assignee.name == 'g_exportedScripts': +# for item in node.expr.nodes: +# if item.__class__.__name__ == 'Name': +# g_exportedScripts.append(item.name) +# return g_exportedScripts + return allFuncs - + def getModuleByUrl( self, url ): entry = self.modules.get(url) load = True @@ -413,7 +434,7 @@ class ProviderContext: code = compile( src, encfile(uno.fileUrlToSystemPath( url ) ), "exec" ) else: code = compile( src, url, "exec" ) - exec code in entry.module.__dict__ + exec(code, entry.module.__dict__) entry.module.__file__ = url self.modules[ url ] = entry log.isDebugLevel() and log.debug( "mapped " + url + " to " + str( entry.module ) ) @@ -501,7 +522,7 @@ class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, code = ensureSourceState( code ) mod = imp.new_module("ooo_script_framework") mod.__dict__[GLOBAL_SCRIPTCONTEXT_NAME] = self.provCtx.scriptContext - exec code in mod.__dict__ + exec(code, mod.__dict__) values = mod.__dict__.get( CALLABLE_CONTAINER_NAME , None ) if not values: values = mod.__dict__.values() @@ -606,7 +627,7 @@ class DirBrowseNode( unohelper.Base, XBrowseNode ): log.isDebugLevel() and log.debug( "adding DirBrowseNode " + i ) browseNodeList.append( DirBrowseNode( self.provCtx, i[i.rfind("/")+1:len(i)],i)) return tuple( browseNodeList ) - except Exception, e: + except Exception as e: text = lastException2String() log.error( "DirBrowseNode error: " + str(e) + " while evaluating " + self.rootUrl) log.error( text) @@ -807,7 +828,7 @@ class PythonScript( unohelper.Base, XScript ): log.isDebugLevel() and log.debug( "PythonScript.invoke " + str( args ) ) try: ret = self.func( *args ) - except UnoException,e: + except UnoException as e: # UNO Exception continue to fly ... text = lastException2String() complete = "Error during invoking function " + \ @@ -820,7 +841,7 @@ class PythonScript( unohelper.Base, XScript ): # this is really bad for most users. e.Message = e.Message + " (" + complete + ")" raise - except Exception,e: + except Exception as e: # General python exception are converted to uno RuntimeException text = lastException2String() complete = "Error during invoking function " + \ @@ -882,7 +903,7 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC else: self.dirBrowseNode = DirBrowseNode( self.provCtx, LANGUAGENAME, rootUrl ) - except Exception, e: + except Exception as e: text = lastException2String() log.debug( "PythonScriptProvider could not be instantiated because of : " + text ) raise e commit af2b7fac27aa812229c6611fd35a77aa51b290f2 Author: Michael Stahl <mst...@redhat.com> Date: Sun Nov 25 15:59:18 2012 +0100 pyuno: fix handling of "str", "unicode", "bytes" types: Replace currrent wrappers of Python 2 only PyString_* functions with better abstractions that handle default "str" (PyStr_*) or byte strings ("str"/"bytes" depending on version, PyStrBytes_*) and adjust all invocations to work on appropriate string types. Fixes obvious "attributes typeName and/or value of uno.Enum are not strings" exceptions with Python 3. Change-Id: I255dcb1bc198fd7f6a62b83b957901521071a480 diff --git a/pyuno/source/module/pyuno.cxx b/pyuno/source/module/pyuno.cxx index 17507bd..18d2f9d 100644 --- a/pyuno/source/module/pyuno.cxx +++ b/pyuno/source/module/pyuno.cxx @@ -428,7 +428,7 @@ PyObject *PyUNO_str( PyObject * self ) buf.append( OUStringToOString(s,RTL_TEXTENCODING_ASCII_US) ); } - return PyString_FromString( buf.getStr()); + return PyStr_FromString( buf.getStr()); } PyObject* PyUNO_getattr (PyObject* self, char* name) diff --git a/pyuno/source/module/pyuno_adapter.cxx b/pyuno/source/module/pyuno_adapter.cxx index beb2d04..35b6ce4 100644 --- a/pyuno/source/module/pyuno_adapter.cxx +++ b/pyuno/source/module/pyuno_adapter.cxx @@ -242,7 +242,7 @@ 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 ); - buf.appendAscii( PyString_AsString( str.get() )); + buf.append(pyString2ustring(str.get())); throw IllegalArgumentException( buf.makeStringAndClear(), Reference< XInterface > (),0 ); } diff --git a/pyuno/source/module/pyuno_impl.hxx b/pyuno/source/module/pyuno_impl.hxx index 601673d..e794c9d 100644 --- a/pyuno/source/module/pyuno_impl.hxx +++ b/pyuno/source/module/pyuno_impl.hxx @@ -49,44 +49,78 @@ // In Python 3, the PyString_* functions have been replaced by PyBytes_* // and PyUnicode_* functions. #if PY_MAJOR_VERSION >= 3 -inline char* PyString_AsString(PyObject *object) + +// compatibility wrappers for Python "str" type (PyUnicode in 3, PyString in 2) +inline PyObject* PyStr_FromString(const char *string) { - // check whether object is already of type "PyBytes" - if(PyBytes_Check(object)) - { - return PyBytes_AsString(object); - } - - // object is not encoded yet, so encode it to utf-8 - PyObject *pystring; - pystring = PyUnicode_AsUTF8String(object); - if(!pystring) - { - PyErr_SetString(PyExc_ValueError, "cannot utf-8 decode string"); - return 0; - } - return PyBytes_AsString(pystring); + return PyUnicode_FromString(string); } -inline PyObject* PyString_FromString(const char *string) +inline char * PyStr_AsString(PyObject *object) { - return PyUnicode_FromString(string); + return PyUnicode_AsUTF8(object); } -inline int PyString_Check(PyObject *object) +inline int PyStr_Check(PyObject *object) +{ + return PyUnicode_Check(object); +} + +// compatibility wrappers for Python non-Unicode string/buffer type +// (PyBytes in 3, PyString in 2) +inline int PyStrBytes_Check(PyObject *object) { return PyBytes_Check(object); } -inline Py_ssize_t PyString_Size(PyObject *object) +inline char* PyStrBytes_AsString(PyObject *object) +{ + return PyBytes_AsString(object); +} + +inline Py_ssize_t PyStrBytes_Size(PyObject *object) { return PyBytes_Size(object); } -inline PyObject* PyString_FromStringAndSize(const char *string, Py_ssize_t len) +inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len) { return PyBytes_FromStringAndSize(string, len); } +#else +inline char * PyStr_AsString(PyObject *object) +{ + return PyString_AsString(object); +} + +inline PyObject* PyStr_FromString(const char *string) +{ + return PyString_FromString(string); +} + +inline int PyStr_Check(PyObject *object) +{ + return PyString_Check(object); +} +inline int PyStrBytes_Check(PyObject *object) +{ + return PyString_Check(object); +} + +inline char* PyStrBytes_AsString(PyObject *object) +{ + return PyString_AsString(object); +} + +inline Py_ssize_t PyStrBytes_Size(PyObject *object) +{ + return PyString_Size(object); +} + +inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len) +{ + return PyString_FromStringAndSize(string, len); +} #endif /* PY_MAJOR_VERSION >= 3 */ namespace pyuno diff --git a/pyuno/source/module/pyuno_module.cxx b/pyuno/source/module/pyuno_module.cxx index bd592e3..bd73c0b 100644 --- a/pyuno/source/module/pyuno_module.cxx +++ b/pyuno/source/module/pyuno_module.cxx @@ -169,7 +169,9 @@ static void fillStruct( for( int i = 0 ; i < nMembers ; i ++ ) { const OUString OUMemberName (pCompType->ppMemberNames[i]); - PyObject *pyMemberName = PyString_FromString(::rtl::OUStringToOString( OUMemberName, RTL_TEXTENCODING_UTF8 ).getStr()); + PyObject *pyMemberName = + PyStr_FromString(::rtl::OUStringToOString(OUMemberName, + RTL_TEXTENCODING_UTF8).getStr()); if ( PyObject *element = PyDict_GetItem(kwinitializer, pyMemberName ) ) { state.setInitialised(OUMemberName); @@ -333,7 +335,7 @@ PyObject * extractOneStringArg( PyObject *args, char const *funcName ) return NULL; } PyObject *obj = PyTuple_GetItem( args, 0 ); - if(!PyString_Check(obj) && !PyUnicode_Check(obj)) + if (!PyStr_Check(obj) && !PyUnicode_Check(obj)) { OStringBuffer buf; buf.append( funcName ).append( ": expecting one string argument" ); @@ -356,13 +358,11 @@ static PyObject *createUnoStructHelper( PyObject *structName = PyTuple_GetItem(args, 0); PyObject *initializer = PyTuple_GetItem(args, 1); - // 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 (PyStr_Check(structName)) { if( PyTuple_Check( initializer ) && PyDict_Check ( keywordArgs ) ) { - OUString typeName( OUString::createFromAscii(PyString_AsString(structName))); + OUString typeName( OUString::createFromAscii(PyStr_AsString(structName))); RuntimeCargo *c = runtime.getImpl()->cargo; Reference<XIdlClass> idl_class ( c->xCoreReflection->forName (typeName),UNO_QUERY); if (idl_class.is ()) @@ -394,7 +394,7 @@ static PyObject *createUnoStructHelper( { OStringBuffer buf; buf.append( "UNO struct " ); - buf.append( PyString_AsString(structName) ); + buf.append( PyStr_AsString(structName) ); buf.append( " is unkown" ); PyErr_SetString (PyExc_RuntimeError, buf.getStr()); } @@ -571,9 +571,7 @@ static PyObject *getClass( SAL_UNUSED_PARAMETER PyObject *, PyObject *args ) try { Runtime runtime; - PyRef ret = getClass( - OUString( PyString_AsString( obj), strlen(PyString_AsString(obj)),RTL_TEXTENCODING_ASCII_US), - runtime ); + PyRef ret = getClass(pyString2ustring(obj), runtime); Py_XINCREF( ret.get() ); return ret.get(); } @@ -706,9 +704,9 @@ static PyObject * invoke(SAL_UNUSED_PARAMETER PyObject *, PyObject *args) { PyObject *object = PyTuple_GetItem(args, 0); PyObject *item1 = PyTuple_GetItem(args, 1); - if(PyString_Check(item1) || PyUnicode_Check(item1)) + if (PyStr_Check(item1)) { - const char *name = PyString_AsString(item1); + const char *name = PyStr_AsString(item1); PyObject *item2 = PyTuple_GetItem(args, 2); if(PyTuple_Check(item2)) { @@ -718,7 +716,7 @@ static PyObject * invoke(SAL_UNUSED_PARAMETER PyObject *, PyObject *args) { OStringBuffer buf; buf.append("uno.invoke expects a tuple as 3rd argument, got "); - buf.append(PyString_AsString(PyObject_Str(item2))); + buf.append(PyStr_AsString(PyObject_Str(item2))); PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear().getStr()); } @@ -727,7 +725,7 @@ static PyObject * invoke(SAL_UNUSED_PARAMETER PyObject *, PyObject *args) { OStringBuffer buf; buf.append("uno.invoke expected a string as 2nd argument, got "); - buf.append(PyString_AsString(PyObject_Str(item1))); + buf.append(PyStr_AsString(PyObject_Str(item1))); PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear().getStr()); } @@ -780,7 +778,8 @@ static PyObject *setCurrentContext( { OStringBuffer buf; buf.append( "uno.setCurrentContext expects an XComponentContext implementation, got " ); - buf.append( PyString_AsString( PyObject_Str( PyTuple_GetItem( args, 0) ) ) ); + buf.append( + PyStr_AsString(PyObject_Str(PyTuple_GetItem(args, 0)))); PyErr_SetString( PyExc_RuntimeError, buf.makeStringAndClear().getStr() ); } diff --git a/pyuno/source/module/pyuno_runtime.cxx b/pyuno/source/module/pyuno_runtime.cxx index 2ad7416..6e282a7 100644 --- a/pyuno/source/module/pyuno_runtime.cxx +++ b/pyuno/source/module/pyuno_runtime.cxx @@ -162,8 +162,8 @@ static PyRef importUnoModule( ) throw ( RuntimeException ) OUStringBuffer buf; buf.appendAscii( "python object raised an unknown exception (" ); PyRef valueRep( PyObject_Repr( excValue.get() ), SAL_NO_ACQUIRE ); - buf.appendAscii( PyString_AsString( valueRep.get())).appendAscii( ", traceback follows\n" ); - buf.appendAscii( PyString_AsString( str.get() ) ); + buf.appendAscii( PyStr_AsString( valueRep.get())).appendAscii( ", traceback follows\n" ); + buf.appendAscii( PyStr_AsString( str.get() ) ); buf.appendAscii( ")" ); throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface > () ); } @@ -613,7 +613,7 @@ lcl_ExceptionMessage(PyObject *const o, OUString const*const pWrapped) OUStringBuffer buf; buf.appendAscii("Couldn't convert "); PyRef reprString( PyObject_Str(o), SAL_NO_ACQUIRE ); - buf.appendAscii( PyString_AsString(reprString.get()) ); + buf.appendAscii( PyStr_AsString(reprString.get()) ); buf.appendAscii(" to a UNO type"); if (pWrapped) { @@ -719,7 +719,7 @@ Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) con double d = PyFloat_AsDouble (o); a <<= d; } - else if (PyString_Check(o) || PyUnicode_Check(o)) + else if (PyStrBytes_Check(o) || PyUnicode_Check(o)) { a <<= pyString2ustring(o); } @@ -740,10 +740,10 @@ Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) con { PyRef str(PyObject_GetAttrString( o , "value" ),SAL_NO_ACQUIRE); Sequence< sal_Int8 > seq; - if( PyString_Check( str.get() ) ) + if( PyStrBytes_Check( str.get() ) ) { seq = Sequence<sal_Int8 > ( - (sal_Int8*) PyString_AsString(str.get()), PyString_Size(str.get())); + (sal_Int8*) PyStrBytes_AsString(str.get()), PyStrBytes_Size(str.get())); } a <<= seq; } @@ -916,7 +916,7 @@ Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue, PyRef args( PyTuple_New( 1), SAL_NO_ACQUIRE ); PyTuple_SetItem( args.get(), 0, excTraceback.getAcquired() ); PyRef pyStr( PyObject_CallObject( extractTraceback.get(),args.get() ), SAL_NO_ACQUIRE); - str = rtl::OUString::createFromAscii( PyString_AsString(pyStr.get()) ); + str = rtl::OUString::createFromAscii( PyStr_AsString(pyStr.get()) ); } else { @@ -951,7 +951,7 @@ Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue, PyRef typeName( PyObject_Str( excType.get() ), SAL_NO_ACQUIRE ); if( typeName.is() ) { - buf.appendAscii( PyString_AsString( typeName.get() ) ); + buf.appendAscii( PyStr_AsString( typeName.get() ) ); } else { @@ -961,7 +961,7 @@ Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue, PyRef valueRep( PyObject_Str( excValue.get() ), SAL_NO_ACQUIRE ); if( valueRep.is() ) { - buf.appendAscii( PyString_AsString( valueRep.get())); + buf.appendAscii( PyStr_AsString( valueRep.get())); } else { diff --git a/pyuno/source/module/pyuno_type.cxx b/pyuno/source/module/pyuno_type.cxx index 1ea9a89..d30e07d 100644 --- a/pyuno/source/module/pyuno_type.cxx +++ b/pyuno/source/module/pyuno_type.cxx @@ -162,15 +162,15 @@ Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException ) Any ret; PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE); PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE); - if( !PyString_Check( typeName.get() ) || ! PyString_Check( value.get() ) ) + if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) ) { throw RuntimeException( USTR_ASCII( "attributes typeName and/or value of uno.Enum are not strings" ), Reference< XInterface > () ); } - OUString strTypeName( OUString::createFromAscii( PyString_AsString( typeName.get() ) ) ); - char *stringValue = PyString_AsString( value.get() ); + OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) ); + char *stringValue = PyStr_AsString( value.get() ); TypeDescription desc( strTypeName ); if( desc.is() ) @@ -200,7 +200,7 @@ Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException ) { OUStringBuffer buf; buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " ); - buf.appendAscii( PyString_AsString( typeName.get() ) ); + buf.appendAscii( PyStr_AsString( typeName.get() ) ); throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () ); } ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef ); @@ -208,7 +208,7 @@ Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException ) else { OUStringBuffer buf; - buf.appendAscii( "enum " ).appendAscii( PyString_AsString(typeName.get()) ).appendAscii( " is unknown" ); + buf.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName.get()) ).appendAscii( " is unknown" ); throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface> () ); } return ret; @@ -218,7 +218,7 @@ Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException ) Type PyType2Type( PyObject * o ) throw(RuntimeException ) { PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE); - if( !PyString_Check( pyName.get() ) ) + if( !PyStr_Check( pyName.get() ) ) { throw RuntimeException( USTR_ASCII( "type object does not have typeName property" ), @@ -228,7 +228,7 @@ Type PyType2Type( PyObject * o ) throw(RuntimeException ) PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE ); Any enumValue = PyEnum2Enum( pyTC.get() ); - OUString name( OUString::createFromAscii( PyString_AsString( pyName.get() ) ) ); + OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) ); TypeDescription desc( name ); if( ! desc.is() ) { @@ -271,8 +271,8 @@ static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r ) { PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE ); - PyTuple_SetItem( args.get() , 0 , PyString_FromString( enumBase ) ); - PyTuple_SetItem( args.get() , 1 , PyString_FromString( enumValue ) ); + PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) ); + PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) ); return callCtor( r, "Enum" , args ); } @@ -283,7 +283,7 @@ PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r // retrieve type object PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE ); - PyTuple_SetItem( args.get() , 0 , PyString_FromString( typeName ) ); + PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) ); PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r ); if( ! typeClass ) return NULL; @@ -309,7 +309,7 @@ PyObject *PyUNO_ByteSequence_new( const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r ) { PyRef str( - PyString_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()), + PyStrBytes_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()), SAL_NO_ACQUIRE ); PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE ); PyTuple_SetItem( args.get() , 0 , str.getAcquired() ); diff --git a/pyuno/source/module/pyuno_util.cxx b/pyuno/source/module/pyuno_util.cxx index 88136b3..9f058ef 100644 --- a/pyuno/source/module/pyuno_util.cxx +++ b/pyuno/source/module/pyuno_util.cxx @@ -69,7 +69,7 @@ PyRef ustring2PyUnicode( const OUString & str ) PyRef ustring2PyString( const OUString &str ) { OString o = OUStringToOString( str, osl_getThreadTextEncoding() ); - return PyRef( PyString_FromString( o.getStr() ), SAL_NO_ACQUIRE ); + return PyRef( PyStr_FromString( o.getStr() ), SAL_NO_ACQUIRE ); } OUString pyString2ustring( PyObject *pystr ) @@ -80,14 +80,24 @@ OUString pyString2ustring( PyObject *pystr ) #if Py_UNICODE_SIZE == 2 ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) ); #else +#if PY_MAJOR_VERSION >= 3 + Py_ssize_t size(0); + char *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size)); + ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8); +#else PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr); - ret = OUString(PyString_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8); + ret = OUString(PyStr_AsString(pUtf8), PyStr_Size(pUtf8), RTL_TEXTENCODING_UTF8); Py_DECREF(pUtf8); #endif +#endif } else { - char *name = PyString_AsString(pystr ); +#if PY_MAJOR_VERSION >= 3 + char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea? +#else + char *name = PyString_AsString(pystr); +#endif ret = OUString( name, strlen(name), osl_getThreadTextEncoding() ); } return ret; commit a38b59265c08276fce6d73ce541cadb41aa6d347 Author: Michael Stahl <mst...@redhat.com> Date: Sun Nov 25 15:57:38 2012 +0100 pyuno: adjust uno.ByteSequence to work with "bytes" This is necessary for Python3; "str" type is still accepted so it runs on Python 2 as well. Change-Id: I51098feca00e4cd8ce3ceebf663d4ce79252cbcd diff --git a/pyuno/source/loader/pythonloader.py b/pyuno/source/loader/pythonloader.py index a6f1cd5..03bf7d2 100644 --- a/pyuno/source/loader/pythonloader.py +++ b/pyuno/source/loader/pythonloader.py @@ -90,7 +90,7 @@ class Loader( XImplementationLoader, XServiceInfo, unohelper.Base ): # read the file filename = unohelper.fileUrlToSystemPath( url ) - fileHandle = file( filename ) + fileHandle = open( filename ) src = fileHandle.read().replace("\r","") if not src.endswith( "\n" ): src = src + "\n" diff --git a/pyuno/source/module/uno.py b/pyuno/source/module/uno.py index 4ff2606..86011f3 100644 --- a/pyuno/source/module/uno.py +++ b/pyuno/source/module/uno.py @@ -199,8 +199,10 @@ class Char: class ByteSequence: def __init__(self, value): - if isinstance(value, str): + if isinstance(value, bytes): self.value = value + elif isinstance(value, str): + self.value = value.encode("utf-8") # Python 2 compatibility elif isinstance(value, ByteSequence): self.value = value.value else: @@ -212,8 +214,10 @@ class ByteSequence: def __eq__(self, that): if isinstance( that, ByteSequence): return self.value == that.value - if isinstance(that, str): + if isinstance(that, bytes): return self.value == that + if isinstance(that, str): + return self.value == that.encode("utf-8") return False def __len__(self): @@ -226,8 +230,10 @@ class ByteSequence: return self.value.__iter__() def __add__( self , b ): - if isinstance( b, str ): - return ByteSequence( self.value + b ) + if isinstance( b, bytes): + return ByteSequence(self.value + b) + elif isinstance( b, str ): + return ByteSequence( self.value + b.encode("utf-8") ) elif isinstance( b, ByteSequence ): return ByteSequence( self.value + b.value ) raise TypeError( "expected string or ByteSequence as operand" ) _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits