VirtualBox

Changeset 79442 in vbox


Ignore:
Timestamp:
Jul 1, 2019 3:34:26 PM (6 years ago)
Author:
vboxsync
Message:

Main/glue/vboxapi.py: Added getEnumValueName() to help translating enum values into string names. Also fixed negative hex values in xcptToString() and removed legacy errXxxx methods. bugref:9151

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/vboxapi.py

    r79139 r79442  
    10061006        self._dErrorValToName = None
    10071007
     1008        ## Dictionary for resolving enum values to names, two levels of dictionaries.
     1009        ## First level is indexed by enum name, the next by value.
     1010        self._ddEnumValueToName = {};
     1011
    10081012        ## The exception class for the selected platform.
    10091013        self.oXcptClass = self.platform.xcptGetBaseXcpt()
     
    11461150        return VBoxSdkDir
    11471151
     1152    def getEnumValueName(self, sEnumTypeNm, oEnumValue, fTypePrefix = False):
     1153        """
     1154        Returns the name (string) for the corresponding enum value.
     1155        """
     1156        # Cache lookup:
     1157        dValueNames = self._ddEnumValueToName.get(sEnumTypeNm);
     1158        if dValueNames is not None:
     1159            sValueName = dValueNames.get(oEnumValue);
     1160            if sValueName:
     1161                return sValueName if not fTypePrefix else '%s_%s' % (sEnumTypeNm, sValueName);
     1162        else:
     1163            # Cache miss. Build the reverse lookup dictionary and add it to the cache:
     1164            dNamedValues = self.constants.all_values(sEnumTypeNm);
     1165            if len(dNamedValues) > 0:
     1166
     1167                dValueNames = dict();
     1168                for sName in dNamedValues:
     1169                    dValueNames[dNamedValues[sName]] = sName;
     1170                self._ddEnumValueToName[sEnumTypeNm] = dValueNames;
     1171
     1172                # Lookup the value:
     1173                sValueName = dValueNames.get(oEnumValue);
     1174                if sValueName:
     1175                    return sValueName if not fTypePrefix else '%s_%s' % (sEnumTypeNm, sValueName);
     1176
     1177        # Fallback:
     1178        return '%s_Unknown_%s' % (sEnumTypeNm, oEnumValue);
     1179
    11481180    #
    11491181    # Error code utilities.
     
    12211253                if sKey[0].isupper():
    12221254                    oValue = getattr(self.statuses, sKey)
    1223                     if type(oValue) is int:
    1224                         dErrorValToName[oValue] = sKey
     1255                    if isinstance(oValue, (int, long)):
     1256                        dErrorValToName[int(oValue)] = sKey
    12251257            # Always prefer the COM names (see aliasing in platform specific code):
    12261258            for sKey in ('S_OK', 'E_FAIL', 'E_ABORT', 'E_POINTER', 'E_NOINTERFACE', 'E_INVALIDARG',
     
    12361268        except KeyError:
    12371269            hrLong = long(hrStatus)
    1238             sStr = '%#x (%d)' % (hrLong, hrLong)
     1270            sStr = '%#x (%d)' % (hrLong & 0xffffffff, hrLong)
    12391271        return sStr
    12401272
     
    12511283        return sRet
    12521284
    1253     # Legacy, remove in a day or two.
    1254     errGetStatus = xcptGetStatus
    1255     errIsDeadInterface = xcptIsDeadInterface
    1256     errIsOurXcptKind = xcptIsOurXcptKind
    1257     errGetMessage = xcptGetMessage
    1258 
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