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
This commit is contained in:
Michael Stahl 2012-11-25 15:57:38 +01:00
parent 87c923be60
commit a38b59265c
2 changed files with 11 additions and 5 deletions

View File

@ -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"

View File

@ -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" )