VirtualBox

Changeset 48043 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Aug 25, 2013 7:12:55 PM (11 years ago)
Author:
vboxsync
Message:

vboxapi.py: err -> xcpt, allow all oXcpt parameters to be None (simplifies exception handling both for COM/XPCOM portability as well as python 2 <-> 3, since we don't have to specify any specitic exception class to catch). Fixes.

File:
1 edited

Legend:

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

    r47984 r48043  
    297297    #
    298298
    299     def errGetStatus(self, oXcpt):
     299    def xcptGetStatus(self, oXcpt):
    300300        """
    301301        Returns the COM status code from the VBox API given exception.
     
    303303        return None;
    304304
    305     def errIsDeadInterface(self, oXcpt):
     305    def xcptIsDeadInterface(self, oXcpt):
    306306        """
    307307        Returns True if the exception indicates that the interface is dead, False if not.
     
    309309        return False;
    310310
    311     def errIsEqual(oXcpt, hrStatus):
     311    def xcptIsEqual(self, oXcpt, hrStatus):
    312312        """
    313313        Checks if the exception oXcpt is equal to the COM/XPCOM status code
     
    320320        """
    321321        try:
    322             hrXcpt = self.errGetStatus(oXcpt);
     322            hrXcpt = self.xcptGetStatus(oXcpt);
    323323        except AttributeError:
    324324            return False;
    325         return hrXcpt == hrStatus;
    326 
    327     def errGetMessage(self, oXcpt):
     325        if hrXcpt == hrStatus:
     326            return True;
     327
     328        # Fudge for 32-bit signed int conversion.
     329        if hrStatus > 0x7fffffff and hrStatus <= 0xffffffff and hrXcpt < 0:
     330            if (hrStatus - 0x100000000) == hrXcpt:
     331                return True;
     332        return False;
     333
     334    def xcptGetMessage(self, oXcpt):
    328335        """
    329336        Returns the best error message found in the COM-like exception.
    330         Returns None to fall back on errToString.
     337        Returns None to fall back on xcptToString.
    331338        Raises exception if oXcpt isn't our kind of exception object.
    332339        """
    333340        return None;
    334341
    335     def errGetBaseXcpt(self):
     342    def xcptGetBaseXcpt(self):
    336343        """
    337344        Returns the base exception class.
     
    339346        return None;
    340347
    341     def errSetupConstants(self, oDst):
     348    def xcptSetupConstants(self, oDst):
    342349        """
    343350        Copy/whatever all error constants onto oDst.
     
    346353
    347354    @staticmethod
    348     def errCopyErrorConstants(oDst, oSrc):
     355    def xcptCopyErrorConstants(oDst, oSrc):
    349356        """
    350357        Copy everything that looks like error constants from oDst to oSrc.
     
    612619        return CastTo(oIUnknown, sClassName)
    613620
    614     def errGetStatus(self, oXcpt):
     621    def xcptGetStatus(self, oXcpt):
    615622        # The DISP_E_EXCEPTION + excptinfo fun needs checking up, only
    616623        # empirical info on it so far.
     
    621628        return hrXcpt;
    622629
    623     def errIsDeadInterface(self, oXcpt):
    624         return self.errGetStatus(oXcpt) in [
     630    def xcptIsDeadInterface(self, oXcpt):
     631        return self.xcptGetStatus(oXcpt) in [
    625632            0x800706ba, -2147023174, # RPC_S_SERVER_UNAVAILABLE.
    626633            0x800706be, -2147023170, # RPC_S_CALL_FAILED.
    627634            0x800706bf, -2147023169, # RPC_S_CALL_FAILED_DNE.
    628635            0x80010108, -2147417848, # RPC_E_DISCONNECTED.
     636            0x800706b5, -2147023179, # RPC_S_UNKNOWN_IF
    629637        ];
    630638
    631639
    632     def errGetMessage(self, oXcpt):
     640    def xcptGetMessage(self, oXcpt):
    633641        if hasattr(oXcpt, 'excepinfo'):
    634642            try:
     
    648656        return None;
    649657
    650     def errGetBaseXcpt(self):
     658    def xcptGetBaseXcpt(self):
    651659        import pythoncom;
    652660        return pythoncom.com_error;
    653661
    654     def errSetupConstants(self, oDst):
     662    def xcptSetupConstants(self, oDst):
    655663        import winerror;
    656         oDst = self.errCopyErrorConstants(oDst, winerror);
     664        oDst = self.xcptCopyErrorConstants(oDst, winerror);
    657665
    658666        # XPCOM compatability constants.
     
    734742        return oIUnknown.queryInterface(getattr(xpcom.components.interfaces, sClassName))
    735743
    736     def errGetStatus(self, oXcpt):
     744    def xcptGetStatus(self, oXcpt):
    737745        return oXcpt.errno;
    738746
    739     def errIsDeadInterface(self, oXcpt):
    740         return self.errGetStatus(oXcpt) in [
     747    def xcptIsDeadInterface(self, oXcpt):
     748        return self.xcptGetStatus(oXcpt) in [
    741749            0x80004004, -2147467260, # NS_ERROR_ABORT
    742750            0x800706be, -2147023170, # NS_ERROR_CALL_FAILED (RPC_S_CALL_FAILED)
    743751        ];
    744752
    745     def errGetMessage(self, oXcpt):
     753    def xcptGetMessage(self, oXcpt):
    746754        if hasattr(oXcpt, 'msg'):
    747755            try:
     
    753761        return None;
    754762
    755     def errGetBaseXcpt(self):
     763    def xcptGetBaseXcpt(self):
    756764        import xpcom;
    757765        return xpcom.Exception;
    758766
    759     def errSetupConstants(self, oDst):
     767    def xcptSetupConstants(self, oDst):
    760768        import xpcom;
    761         oDst = self.errCopyErrorConstants(oDst, xpcom.nsError);
     769        oDst = self.xcptCopyErrorConstants(oDst, xpcom.nsError);
    762770
    763771        # COM compatability constants.
     
    923931
    924932        ## Status constants.
    925         self.statuses  = self.platform.errSetupConstants(VirtualBoxManager.Statuses());
     933        self.statuses  = self.platform.xcptSetupConstants(VirtualBoxManager.Statuses());
    926934        ## @todo Add VBOX_E_XXX to statuses? They're already in constants...
    927935        ## Dictionary for errToString, built on demand.
     
    929937
    930938        ## The exception class for the selected platform.
    931         self.oXcptClass = self.platform.errGetBaseXcpt();
     939        self.oXcptClass = self.platform.xcptGetBaseXcpt();
    932940        global CurXcptClass;
    933941        CurXcptClass = self.oXcptClass;
     
    10771085    ## @todo port to webservices!
    10781086
    1079     def errGetStatus(self, oXcpt):
    1080         """
    1081         Gets the status code from an exception.
    1082         """
    1083         return self.platform.errGetStatus(oXcpt);
    1084 
    1085     def errIsDeadInterface(self, oXcpt):
    1086         """
    1087         Returns True if the exception indicates that the interface is dead, False if not.
    1088         """
    1089         return self.platform.errIsDeadInterface(oXcpt);
    1090 
    1091     def errIsOurXcptKind(self, oXcpt):
    1092         """
    1093         Checks if the exception is one that could come from the VBox API.
     1087    def xcptGetStatus(self, oXcpt = None):
     1088        """
     1089        Gets the status code from an exception.  If the exception parameter
     1090        isn't specified, the current exception is examined.
     1091        """
     1092        if oXcpt is None:
     1093            oXcpt = sys.exc_info()[1];
     1094        return self.platform.xcptGetStatus(oXcpt);
     1095
     1096    def xcptIsDeadInterface(self, oXcpt = None):
     1097        """
     1098        Returns True if the exception indicates that the interface is dead,
     1099        False if not.  If the exception parameter isn't specified, the current
     1100        exception is examined.
     1101        """
     1102        if oXcpt is None:
     1103            oXcpt = sys.exc_info()[1];
     1104        return self.platform.xcptIsDeadInterface(oXcpt);
     1105
     1106    def xcptIsOurXcptKind(self, oXcpt = None):
     1107        """
     1108        Checks if the exception is one that could come from the VBox API. If
     1109        the exception parameter isn't specified, the current exception is
     1110        examined.
    10941111        """
    10951112        if self.oXcptClass is None: ## @todo find the exception class for web services!
    10961113            return False;
     1114        if oXcpt is None:
     1115            oXcpt = sys.exc_info()[1];
    10971116        return isinstance(oXcpt, self.oXcptClass);
    10981117
    1099     def errIsEqual(self, oXcpt, hrStatus):
    1100         """ See PlatformBase::errIsEqual(). """
    1101         return self.platform.errIsEqual(oXcpt, hrStatus);
    1102 
    1103     def errIsNotEqual(self, oXcpt, hrStatus):
    1104         """
    1105         Negated errIsEqual.
    1106         """
    1107         return self.errIsEqual(oXcpt, hrStatus);
    1108 
    1109     def errToString(self, hrStatusOrXcpt):
     1118    def xcptIsEqual(self, oXcpt, hrStatus):
     1119        """
     1120        Checks if the exception oXcpt is equal to the COM/XPCOM status code
     1121        hrStatus.
     1122
     1123        The oXcpt parameter can be any kind of object, we'll just return True
     1124        if it doesn't behave like a our exception class.  If it's None, we'll
     1125        query the current exception and examine that.
     1126
     1127        Will not raise any exception as long as hrStatus and self are not bad.
     1128        """
     1129        if oXcpt is None:
     1130            oXcpt = sys.exc_info()[1];
     1131        return self.platform.xcptIsEqual(oXcpt, hrStatus);
     1132
     1133    def xcptIsNotEqual(self, oXcpt, hrStatus):
     1134        """
     1135        Negated xcptIsEqual.
     1136        """
     1137        return not self.xcptIsEqual(oXcpt, hrStatus);
     1138
     1139    def xcptToString(self, hrStatusOrXcpt = None):
    11101140        """
    11111141        Converts the specified COM status code, or the status code of the
    1112         specified exception, to a C constant string.
     1142        specified exception, to a C constant string.  If the parameter isn't
     1143        specified (is None), the current exception is examined.
    11131144        """
    11141145
    11151146        # Deal with exceptions.
    1116         if self.errIsOurXcptKind(hrStatusOrXcpt):
    1117             hrStatus = self.errGetStatus(hrStatusOrXcpt);
     1147        if hrStatusOrXcpt is None  or  self.xcptIsOurXcptKind(hrStatusOrXcpt):
     1148            hrStatus = self.xcptGetStatus(hrStatusOrXcpt);
    11181149        else:
    11191150            hrStatus = hrStatusOrXcpt;
     
    11371168        return sStr;
    11381169
    1139     def errGetMessage(self, oXcpt):
    1140         """
    1141         Returns the best error message found in the COM-like exception.
    1142         """
    1143         sRet = self.platform.errGetMessage(oXcpt);
     1170    def xcptGetMessage(self, oXcpt = None):
     1171        """
     1172        Returns the best error message found in the COM-like exception. If the
     1173        exception parameter isn't specified, the current exception is examined.
     1174        """
     1175        if oXcpt is None:
     1176            oXcpt = sys.exc_info()[1];
     1177        sRet = self.platform.xcptGetMessage(oXcpt);
    11441178        if sRet is None:
    1145             sRet = self.errToString(oXcpt);
     1179            sRet = self.xcptToString(oXcpt);
    11461180        return sRet;
    11471181
     1182    # Legacy, remove in a day or two.
     1183    errGetStatus       = xcptGetStatus
     1184    errIsDeadInterface = xcptIsDeadInterface
     1185    errIsOurXcptKind   = xcptIsOurXcptKind
     1186    errGetMessage      = xcptGetMessage
     1187
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