VirtualBox

Changeset 79279 in vbox for trunk/src/VBox/ValidationKit


Ignore:
Timestamp:
Jun 21, 2019 2:24:11 PM (6 years ago)
Author:
vboxsync
Message:

common/utils.py: byte compare optimization for 2.x. bugref:9151 bugref:9320

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/common/utils.py

    r79092 r79279  
    20912091        return oLeft == oRight;
    20922092
    2093     # Convert strings to byte arrays:
     2093    #
     2094    # See if byte/buffer stuff that can be compared directory. If not convert
     2095    # strings to bytes.
     2096    #
     2097    # Note! For 2.x, we must convert both sides to the buffer type or the
     2098    #       comparison may fail despite it working okay in test cases.
     2099    #
    20942100    if sys.version_info[0] >= 3:
     2101        if isinstance(oLeft, (bytearray, memoryview, bytes)) and isinstance(oRight, (bytearray, memoryview, bytes)):  # pylint: disable=undefined-variable
     2102            return oLeft == oRight;
     2103
    20952104        if isString(oLeft):
    20962105            try:    oLeft = bytes(oLeft, 'utf-8');
     
    21002109            except: pass;
    21012110    else:
     2111        if isinstance(oLeft, (bytearray, buffer)) and isinstance(oRight, (bytearray, buffer)): # pylint: disable=undefined-variable
     2112            if isinstance(oLeft, bytearray):
     2113                oLeft  = buffer(oLeft);                     # pylint: disable=redefined-variable-type,undefined-variable
     2114            else:
     2115                oRight = buffer(oRight);                    # pylint: disable=redefined-variable-type,undefined-variable
     2116            #print('buf/byte #1 compare: %s (%s vs %s)' % (oLeft == oRight, type(oLeft), type(oRight),));
     2117            return oLeft == oRight;
     2118
    21022119        if isString(oLeft):
    21032120            try:    oLeft = bytearray(oLeft, 'utf-8');      # pylint: disable=redefined-variable-type
     
    21112128        #print('same type now: %s' % (oLeft == oRight,));
    21122129        return oLeft == oRight;
     2130
     2131    # Check if we now have buffer/memoryview vs bytes/bytesarray again.
     2132    if sys.version_info[0] >= 3:
     2133        if isinstance(oLeft, (bytearray, memoryview, bytes)) and isinstance(oRight, (bytearray, memoryview, bytes)):  # pylint: disable=undefined-variable
     2134            return oLeft == oRight;
     2135    else:
     2136        if isinstance(oLeft, (bytearray, buffer)) and isinstance(oRight, (bytearray, buffer)): # pylint: disable=undefined-variable
     2137            if isinstance(oLeft, bytearray):
     2138                oLeft  = buffer(oLeft);                     # pylint: disable=redefined-variable-type,undefined-variable
     2139            else:
     2140                oRight = buffer(oRight);                    # pylint: disable=redefined-variable-type,undefined-variable
     2141            #print('buf/byte #2 compare: %s (%s vs %s)' % (oLeft == oRight, type(oLeft), type(oRight),));
     2142            return oLeft == oRight;
    21132143
    21142144    # Do item by item comparison:
     
    21862216        else:
    21872217            self.assertEqual(areBytesEqual(buffer(bytearray([0x30,0x31,0x32,0x33,0x34]), 1),
    2188                                            bytearray([0x31,0x32,0x32,0x34])), False);
     2218                                                       bytearray([0x31,0x32,0x33,0x34])), True);
    21892219            self.assertEqual(areBytesEqual(buffer(bytearray([0x30,0x31,0x32,0x33,0x34]), 1),
    2190                                            buffer(bytearray([0x31,0x32,0x33,0x34,0x34]), 0, 4)), True);
     2220                                                       bytearray([0x99,0x32,0x32,0x34])), False);
     2221            self.assertEqual(areBytesEqual(buffer(bytearray([0x30,0x31,0x32,0x33,0x34]), 1),
     2222                                                buffer(bytearray([0x31,0x32,0x33,0x34,0x34]), 0, 4)), True);
     2223            self.assertEqual(areBytesEqual(buffer(bytearray([0x30,0x31,0x32,0x33,0x34]), 1),
     2224                                                buffer(bytearray([0x99,0x32,0x33,0x34,0x34]), 0, 4)), False);
    21912225            self.assertEqual(areBytesEqual(buffer(bytearray([0x30,0x31,0x32,0x33,0x34]), 1), b'1234'), True);
    21922226            self.assertEqual(areBytesEqual(buffer(bytearray([0x30,0x31,0x32,0x33,0x34]), 1),  '1234'), True);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette