[Libreoffice-commits] core.git: pyuno/source

Michael Stahl (via logerrit) logerrit at kemper.freedesktop.org
Thu Apr 9 14:44:38 UTC 2020


 pyuno/source/module/pyuno.cxx         |   10 ++++----
 pyuno/source/module/pyuno_impl.hxx    |   41 ----------------------------------
 pyuno/source/module/pyuno_module.cxx  |   20 ++++++++--------
 pyuno/source/module/pyuno_runtime.cxx |   18 +++++++-------
 pyuno/source/module/pyuno_struct.cxx  |    2 -
 pyuno/source/module/pyuno_type.cxx    |   22 +++++++++---------
 pyuno/source/module/pyuno_util.cxx    |    2 -
 7 files changed, 37 insertions(+), 78 deletions(-)

New commits:
commit 086142ba70c51be7487a40d433dc75656f637cdf
Author:     Michael Stahl <Michael.Stahl at cib.de>
AuthorDate: Sat Apr 4 20:16:28 2020 +0200
Commit:     Michael Stahl <michael.stahl at cib.de>
CommitDate: Thu Apr 9 16:44:01 2020 +0200

    pyuno: sed Python2/3 string compatibility wrappers
    
    Change-Id: I9ed25c5efaa4b447ab14a497a58bbe1147a6e7b4
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/91698
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <michael.stahl at cib.de>

diff --git a/pyuno/source/module/pyuno.cxx b/pyuno/source/module/pyuno.cxx
index 1b76a3fb6a9d..bdd717cc397e 100644
--- a/pyuno/source/module/pyuno.cxx
+++ b/pyuno/source/module/pyuno.cxx
@@ -461,7 +461,7 @@ PyObject *PyUNO_str( PyObject * self )
         buf = "pyuno object " + OUStringToOString(s,RTL_TEXTENCODING_ASCII_US);
     }
 
-    return PyStr_FromString( buf.getStr() );
+    return PyUnicode_FromString( buf.getStr() );
 }
 
 static PyObject* PyUNO_dir (PyObject* self)
@@ -592,7 +592,7 @@ static PyObject* lcl_getitem_XCellRange( PyUNO const * me, PyObject* pKey )
     Any aRet;
 
     // Single string key is sugar for getCellRangeByName()
-    if ( PyStr_Check( pKey ) ) {
+    if ( PyUnicode_Check( pKey ) ) {
 
         aParams.realloc (1);
         aParams[0] <<= pyString2ustring( pKey );
@@ -822,7 +822,7 @@ static PyObject* PyUNO_getitem( PyObject *self, PyObject *pKey )
         }
 
         // XNameAccess access by key
-        if ( PyStr_Check( pKey ) )
+        if ( PyUnicode_Check( pKey ) )
         {
             PyObject* pRet = lcl_getitem_string( me, pKey, runtime );
             if ( pRet != nullptr )
@@ -1157,7 +1157,7 @@ static int PyUNO_setitem( PyObject *self, PyObject *pKey, PyObject *pValue )
         {
             return lcl_setitem_slice( me, pKey, pValue );
         }
-        else if ( PyStr_Check( pKey ) )
+        else if ( PyUnicode_Check( pKey ) )
         {
             return lcl_setitem_string( me, pKey, pValue );
         }
@@ -1297,7 +1297,7 @@ static int PyUNO_contains( PyObject *self, PyObject *pKey )
         // useful for objects which implement both XIndexAccess and XNameAccess
 
         // For XNameAccess
-        if ( PyStr_Check( pKey ) )
+        if ( PyUnicode_Check( pKey ) )
         {
             OUString sKey;
             aValue >>= sKey;
diff --git a/pyuno/source/module/pyuno_impl.hxx b/pyuno/source/module/pyuno_impl.hxx
index 44d6bb8985a6..05ff32252670 100644
--- a/pyuno/source/module/pyuno_impl.hxx
+++ b/pyuno/source/module/pyuno_impl.hxx
@@ -60,47 +60,6 @@ namespace com::sun::star::script { class XInvocation2; }
 namespace com::sun::star::script { class XInvocationAdapterFactory2; }
 namespace com::sun::star::script { class XTypeConverter; }
 
-// In Python 3, the PyString_* functions have been replaced by PyBytes_*
-// and PyUnicode_* functions.
-
-// compatibility wrappers for Python "str" type (PyUnicode in 3, PyString in 2)
-inline PyObject* PyStr_FromString(const char *string)
-{
-    return PyUnicode_FromString(string);
-}
-
-inline char const * PyStr_AsString(PyObject *object)
-{
-    return PyUnicode_AsUTF8(object);
-}
-
-inline bool PyStr_Check(PyObject const *object)
-{
-    return PyUnicode_Check(object);
-}
-
-// compatibility wrappers for Python non-Unicode string/buffer type
-// (PyBytes in 3, PyString in 2)
-inline bool PyStrBytes_Check(PyObject const *object)
-{
-    return PyBytes_Check(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* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len)
-{
-    return PyBytes_FromStringAndSize(string, len);
-}
-
 namespace pyuno
 {
 
diff --git a/pyuno/source/module/pyuno_module.cxx b/pyuno/source/module/pyuno_module.cxx
index df7874467503..579da0a583a0 100644
--- a/pyuno/source/module/pyuno_module.cxx
+++ b/pyuno/source/module/pyuno_module.cxx
@@ -156,7 +156,7 @@ void fillStruct(
         {
             const OUString OUMemberName (pCompType->ppMemberNames[i]);
             PyObject *pyMemberName =
-                PyStr_FromString(OUStringToOString(OUMemberName,
+                PyUnicode_FromString(OUStringToOString(OUMemberName,
                         RTL_TEXTENCODING_UTF8).getStr());
             if ( PyObject *element = PyDict_GetItem(kwinitializer, pyMemberName ) )
             {
@@ -390,7 +390,7 @@ PyObject * extractOneStringArg( PyObject *args, char const *funcName )
         return nullptr;
     }
     PyObject *obj = PyTuple_GetItem( args, 0 );
-    if (!PyStr_Check(obj) && !PyUnicode_Check(obj))
+    if (!PyUnicode_Check(obj))
     {
         OString buf = funcName + OStringLiteral(": expecting one string argument");
         PyErr_SetString( PyExc_TypeError, buf.getStr());
@@ -412,11 +412,11 @@ static PyObject *createUnoStructHelper(
             PyObject *structName = PyTuple_GetItem(args, 0);
             PyObject *initializer = PyTuple_GetItem(args, 1);
 
-            if (PyStr_Check(structName))
+            if (PyUnicode_Check(structName))
             {
                 if( PyTuple_Check( initializer ) && PyDict_Check ( keywordArgs ) )
                 {
-                    OUString typeName( OUString::createFromAscii(PyStr_AsString(structName)));
+                    OUString typeName( OUString::createFromAscii(PyUnicode_AsUTF8(structName)));
                     RuntimeCargo *c = runtime.getImpl()->cargo;
                     Reference<XIdlClass> idl_class = c->xCoreReflection->forName (typeName);
                     if (idl_class.is ())
@@ -445,7 +445,7 @@ static PyObject *createUnoStructHelper(
                     {
                         OStringBuffer buf;
                         buf.append( "UNO struct " );
-                        buf.append( PyStr_AsString(structName) );
+                        buf.append( PyUnicode_AsUTF8(structName) );
                         buf.append( " is unknown" );
                         PyErr_SetString (PyExc_RuntimeError, buf.getStr());
                     }
@@ -742,9 +742,9 @@ static PyObject * invoke(SAL_UNUSED_PARAMETER PyObject *, PyObject *args)
     {
         PyObject *object = PyTuple_GetItem(args, 0);
         PyObject *item1 = PyTuple_GetItem(args, 1);
-        if (PyStr_Check(item1))
+        if (PyUnicode_Check(item1))
         {
-            const char *name = PyStr_AsString(item1);
+            const char *name = PyUnicode_AsUTF8(item1);
             PyObject *item2 = PyTuple_GetItem(args, 2);
             if(PyTuple_Check(item2))
             {
@@ -754,7 +754,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(PyStr_AsString(PyObject_Str(item2)));
+                buf.append(PyUnicode_AsUTF8(PyObject_Str(item2)));
                 PyErr_SetString(
                     PyExc_RuntimeError, buf.makeStringAndClear().getStr());
             }
@@ -763,7 +763,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(PyStr_AsString(PyObject_Str(item1)));
+            buf.append(PyUnicode_AsUTF8(PyObject_Str(item1)));
             PyErr_SetString(
                 PyExc_RuntimeError, buf.makeStringAndClear().getStr());
         }
@@ -816,7 +816,7 @@ static PyObject *setCurrentContext(
                 OStringBuffer buf;
                 buf.append( "uno.setCurrentContext expects an XComponentContext implementation, got " );
                 buf.append(
-                    PyStr_AsString(PyObject_Str(PyTuple_GetItem(args, 0))));
+                    PyUnicode_AsUTF8(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 ba0f5adefa98..8bfb9767baed 100644
--- a/pyuno/source/module/pyuno_runtime.cxx
+++ b/pyuno/source/module/pyuno_runtime.cxx
@@ -178,8 +178,8 @@ static PyRef importUnoModule( )
         OUStringBuffer buf;
         buf.append( "python object raised an unknown exception (" );
         PyRef valueRep( PyObject_Repr( excValue.get() ), SAL_NO_ACQUIRE );
-        buf.appendAscii( PyStr_AsString( valueRep.get())).append( ", traceback follows\n" );
-        buf.appendAscii( PyStr_AsString( str.get() ) );
+        buf.appendAscii( PyUnicode_AsUTF8( valueRep.get())).append( ", traceback follows\n" );
+        buf.appendAscii( PyUnicode_AsUTF8( str.get() ) );
         buf.append( ")" );
         throw RuntimeException( buf.makeStringAndClear() );
     }
@@ -589,7 +589,7 @@ lcl_ExceptionMessage(PyObject *const o, OUString const*const pWrapped)
     OUStringBuffer buf;
     buf.append("Couldn't convert ");
     PyRef reprString( PyObject_Str(o), SAL_NO_ACQUIRE );
-    buf.appendAscii( PyStr_AsString(reprString.get()) );
+    buf.appendAscii( PyUnicode_AsUTF8(reprString.get()) );
     buf.append(" to a UNO type");
     if (pWrapped)
     {
@@ -683,7 +683,7 @@ Any Runtime::pyObject2Any ( const PyRef & source, enum ConversionMode mode ) con
         double d = PyFloat_AsDouble (o);
         a <<= d;
     }
-    else if (PyStrBytes_Check(o) || PyUnicode_Check(o))
+    else if (PyBytes_Check(o) || PyUnicode_Check(o))
     {
         a <<= pyString2ustring(o);
     }
@@ -714,10 +714,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( PyStrBytes_Check( str.get() ) )
+            if( PyBytes_Check( str.get() ) )
             {
                 seq = Sequence<sal_Int8 > (
-                    reinterpret_cast<sal_Int8*>(PyStrBytes_AsString(str.get())), PyStrBytes_Size(str.get()));
+                    reinterpret_cast<sal_Int8*>(PyBytes_AsString(str.get())), PyBytes_Size(str.get()));
             }
             a <<= seq;
         }
@@ -869,7 +869,7 @@ Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue,
                 PyRef args( PyTuple_New( 1), SAL_NO_ACQUIRE, NOT_NULL );
                 PyTuple_SetItem( args.get(), 0, excTraceback.getAcquired() );
                 PyRef pyStr( PyObject_CallObject( extractTraceback.get(),args.get() ), SAL_NO_ACQUIRE);
-                str = OUString::createFromAscii( PyStr_AsString(pyStr.get()) );
+                str = OUString::createFromAscii( PyUnicode_AsUTF8(pyStr.get()) );
             }
             else
             {
@@ -902,7 +902,7 @@ Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue,
         PyRef typeName( PyObject_Str( excType.get() ), SAL_NO_ACQUIRE );
         if( typeName.is() )
         {
-            buf.appendAscii( PyStr_AsString( typeName.get() ) );
+            buf.appendAscii( PyUnicode_AsUTF8( typeName.get() ) );
         }
         else
         {
@@ -912,7 +912,7 @@ Any Runtime::extractUnoException( const PyRef & excType, const PyRef &excValue,
         PyRef valueRep( PyObject_Str( excValue.get() ), SAL_NO_ACQUIRE );
         if( valueRep.is() )
         {
-            buf.appendAscii( PyStr_AsString( valueRep.get()));
+            buf.appendAscii( PyUnicode_AsUTF8( valueRep.get()));
         }
         else
         {
diff --git a/pyuno/source/module/pyuno_struct.cxx b/pyuno/source/module/pyuno_struct.cxx
index 56b71cdf5cc4..c816e1351c8c 100644
--- a/pyuno/source/module/pyuno_struct.cxx
+++ b/pyuno/source/module/pyuno_struct.cxx
@@ -68,7 +68,7 @@ static PyObject *PyUNOStruct_str( PyObject *self )
         buf = OUStringToOString( s, RTL_TEXTENCODING_ASCII_US );
     }
 
-    return PyStr_FromString( buf.getStr());
+    return PyUnicode_FromString( buf.getStr());
 }
 
 static PyObject *PyUNOStruct_repr( PyObject *self )
diff --git a/pyuno/source/module/pyuno_type.cxx b/pyuno/source/module/pyuno_type.cxx
index 19de9430a360..a6c1bc447379 100644
--- a/pyuno/source/module/pyuno_type.cxx
+++ b/pyuno/source/module/pyuno_type.cxx
@@ -149,19 +149,19 @@ Any PyEnum2Enum( PyObject *obj )
     Any ret;
     PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
     PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
-    if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) )
+    if( !PyUnicode_Check( typeName.get() ) || ! PyUnicode_Check( value.get() ) )
     {
         throw RuntimeException(
             "attributes typeName and/or value of uno.Enum are not strings" );
     }
 
-    OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
-    char const *stringValue = PyStr_AsString( value.get() );
+    OUString strTypeName( OUString::createFromAscii( PyUnicode_AsUTF8( typeName.get() ) ) );
+    char const *stringValue = PyUnicode_AsUTF8( value.get() );
 
     TypeDescription desc( strTypeName );
     if( !desc.is() )
     {
-        throw RuntimeException( "enum " + OUString::createFromAscii( PyStr_AsString(typeName.get()) ) + " is unknown" );
+        throw RuntimeException( "enum " + OUString::createFromAscii( PyUnicode_AsUTF8(typeName.get()) ) + " is unknown" );
     }
 
     if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
@@ -186,7 +186,7 @@ Any PyEnum2Enum( PyObject *obj )
     {
         throw RuntimeException( "value " + OUString::createFromAscii( stringValue ) +
             "is unknown in enum " +
-            OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
+            OUString::createFromAscii( PyUnicode_AsUTF8( typeName.get() ) ) );
     }
     ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
 
@@ -197,7 +197,7 @@ Any PyEnum2Enum( PyObject *obj )
 Type PyType2Type( PyObject * o )
 {
     PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
-    if( !PyStr_Check( pyName.get() ) )
+    if( !PyUnicode_Check( pyName.get() ) )
     {
         throw RuntimeException(
             "type object does not have typeName property" );
@@ -206,7 +206,7 @@ Type PyType2Type( PyObject * o )
     PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
     Any enumValue = PyEnum2Enum( pyTC.get() );
 
-    OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) );
+    OUString name( OUString::createFromAscii( PyUnicode_AsUTF8( pyName.get() ) ) );
     TypeDescription desc( name );
     if( ! desc.is() )
     {
@@ -242,8 +242,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, NOT_NULL );
-    PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) );
-    PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) );
+    PyTuple_SetItem( args.get() , 0 , PyUnicode_FromString( enumBase ) );
+    PyTuple_SetItem( args.get() , 1 , PyUnicode_FromString( enumValue ) );
 
     return callCtor( r, "Enum" , args );
 }
@@ -254,7 +254,7 @@ PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r
     // retrieve type object
     PyRef args(PyTuple_New( 2 ), SAL_NO_ACQUIRE, NOT_NULL);
 
-    PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) );
+    PyTuple_SetItem( args.get() , 0 , PyUnicode_FromString( typeName ) );
     PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
     if( ! typeClass )
         return nullptr;
@@ -280,7 +280,7 @@ PyObject *PyUNO_ByteSequence_new(
     const css::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
 {
     PyRef str(
-        PyStrBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence.getConstArray()), byteSequence.getLength()),
+        PyBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence.getConstArray()), byteSequence.getLength()),
         SAL_NO_ACQUIRE );
     PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
     PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
diff --git a/pyuno/source/module/pyuno_util.cxx b/pyuno/source/module/pyuno_util.cxx
index 3d4f452103a3..af22b7a8a8d5 100644
--- a/pyuno/source/module/pyuno_util.cxx
+++ b/pyuno/source/module/pyuno_util.cxx
@@ -51,7 +51,7 @@ PyRef ustring2PyUnicode( const OUString & str )
 PyRef ustring2PyString( const OUString &str )
 {
     OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
-    return PyRef( PyStr_FromString( o.getStr() ), SAL_NO_ACQUIRE );
+    return PyRef( PyUnicode_FromString( o.getStr() ), SAL_NO_ACQUIRE );
 }
 
 OUString pyString2ustring( PyObject *pystr )


More information about the Libreoffice-commits mailing list