我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用win32api.FormatMessage()。
def fromEnvironment(cls): """ Get as many of the platform-specific error translation objects as possible and return an instance of C{cls} created with them. """ try: from ctypes import WinError except ImportError: WinError = None try: from win32api import FormatMessage except ImportError: FormatMessage = None try: from socket import errorTab except ImportError: errorTab = None return cls(WinError, FormatMessage, errorTab)
def test_correctLookups(self): """ Given a known-good errno, make sure that formatMessage gives results matching either C{socket.errorTab}, C{ctypes.WinError}, or C{win32api.FormatMessage}. """ acceptable = [socket.errorTab[ECONNABORTED]] try: from ctypes import WinError acceptable.append(WinError(ECONNABORTED).strerror) except ImportError: pass try: from win32api import FormatMessage acceptable.append(FormatMessage(ECONNABORTED)) except ImportError: pass self.assertIn(formatError(ECONNABORTED), acceptable)
def WaitForServiceStatus(serviceName, status, waitSecs, machine=None): """Waits for the service to return the specified status. You should have already requested the service to enter that state""" for i in range(waitSecs*4): now_status = QueryServiceStatus(serviceName, machine)[1] if now_status == status: break win32api.Sleep(250) else: raise pywintypes.error(winerror.ERROR_SERVICE_REQUEST_TIMEOUT, "QueryServiceStatus", win32api.FormatMessage(winerror.ERROR_SERVICE_REQUEST_TIMEOUT)[:-2])
def __StopServiceWithTimeout(hs, waitSecs = 30): try: status = win32service.ControlService(hs, win32service.SERVICE_CONTROL_STOP) except pywintypes.error, exc: if exc.winerror!=winerror.ERROR_SERVICE_NOT_ACTIVE: raise for i in range(waitSecs): status = win32service.QueryServiceStatus(hs) if status[1] == win32service.SERVICE_STOPPED: break win32api.Sleep(1000) else: raise pywintypes.error(winerror.ERROR_SERVICE_REQUEST_TIMEOUT, "ControlService", win32api.FormatMessage(winerror.ERROR_SERVICE_REQUEST_TIMEOUT)[:-2])
def __init__(self, WinError, FormatMessage, errorTab): self.winError = WinError self.formatMessage = FormatMessage self.errorTab = errorTab
def _print_error(self, err): ctx, hresult = err.GetError() try: hresult_msg = win32api.FormatMessage(hresult) except win32api.error: hresult_msg = "" print "Context=0x%x, hresult=0x%x (%s)" % (ctx, hresult, hresult_msg) print err.GetErrorDescription()
def test_FromString(self): msg = "Hello %1, how are you %2?" inserts = ["Mark", "today"] result = win32api.FormatMessage(win32con.FORMAT_MESSAGE_FROM_STRING, msg, # source 0, # ID 0, # LangID inserts) self.assertEqual(result, "Hello Mark, how are you today?")
def testMessageIndex(self): exc = self._getInvalidHandleException() expected = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip() self._testExceptionIndex(exc, 2, expected)
def testUnpack(self): try: win32api.CloseHandle(1) self.fail("expected exception!") except win32api.error, exc: self.failUnlessEqual(exc.winerror, winerror.ERROR_INVALID_HANDLE) self.failUnlessEqual(exc.funcname, "CloseHandle") expected_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip() self.failUnlessEqual(exc.strerror, expected_msg)
def testAsStr(self): exc = self._getInvalidHandleException() err_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip() # early on the result actually *was* a tuple - it must always look like one err_tuple = (winerror.ERROR_INVALID_HANDLE, 'CloseHandle', err_msg) self.failUnlessEqual(str(exc), str(err_tuple))
def testAttributes(self): exc = self._getInvalidHandleException() err_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip() self.failUnlessEqual(exc.winerror, winerror.ERROR_INVALID_HANDLE) self.failUnlessEqual(exc.strerror, err_msg) self.failUnlessEqual(exc.funcname, 'CloseHandle') # some tests for 'insane' args.
def testMessageIndex(self): exc = self._getException() expected = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip() self._testExceptionIndex(exc, 1, expected)
def testAsStr(self): exc = self._getException() err_msg = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip() # early on the result actually *was* a tuple - it must always look like one err_tuple = (winerror.STG_E_INVALIDFLAG, err_msg, None, None) self.failUnlessEqual(str(exc), str(err_tuple))
def testAsTuple(self): exc = self._getException() err_msg = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip() # early on the result actually *was* a tuple - it must be able to be one err_tuple = (winerror.STG_E_INVALIDFLAG, err_msg, None, None) if sys.version_info < (3,): self.failUnlessEqual(tuple(exc), err_tuple) else: self.failUnlessEqual(exc.args, err_tuple)
def testAttributes(self): exc = self._getException() err_msg = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip() self.failUnlessEqual(exc.hresult, winerror.STG_E_INVALIDFLAG) self.failUnlessEqual(exc.strerror, err_msg) self.failUnlessEqual(exc.argerror, None) self.failUnlessEqual(exc.excepinfo, None)
def __str__(self): if self.strerror is None: try: import win32api self.strerror = win32api.FormatMessage(self.errno).strip() except: self.strerror = "no error message is available" # str() looks like a win32api error. return str( (self.errno, self.strerror, self.funcname) )
def _print_error(self, err): ctx, hresult = err.GetError() try: hresult_msg = win32api.FormatMessage(hresult) except win32api.error: hresult_msg = "" print("Context=0x%x, hresult=0x%x (%s)" % (ctx, hresult, hresult_msg)) print(err.GetErrorDescription())
def io_callback(ecb, url, cbIO, errcode): # Get the status of our ExecURL httpstatus, substatus, win32 = ecb.GetExecURLStatus() print("ExecURL of %r finished with http status %d.%d, win32 status %d (%s)" % ( url, httpstatus, substatus, win32, win32api.FormatMessage(win32).strip())) # nothing more to do! ecb.DoneWithSession() # The ISAPI extension - handles all requests in the site.
def __StopServiceWithTimeout(hs, waitSecs = 30): try: status = win32service.ControlService(hs, win32service.SERVICE_CONTROL_STOP) except pywintypes.error as exc: if exc.winerror!=winerror.ERROR_SERVICE_NOT_ACTIVE: raise for i in range(waitSecs): status = win32service.QueryServiceStatus(hs) if status[1] == win32service.SERVICE_STOPPED: break win32api.Sleep(1000) else: raise pywintypes.error(winerror.ERROR_SERVICE_REQUEST_TIMEOUT, "ControlService", win32api.FormatMessage(winerror.ERROR_SERVICE_REQUEST_TIMEOUT)[:-2])
def testUnpack(self): try: win32api.CloseHandle(1) self.fail("expected exception!") except win32api.error as exc: self.failUnlessEqual(exc.winerror, winerror.ERROR_INVALID_HANDLE) self.failUnlessEqual(exc.funcname, "CloseHandle") expected_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip() self.failUnlessEqual(exc.strerror, expected_msg)
def testAsTuple(self): exc = self._getInvalidHandleException() err_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip() # early on the result actually *was* a tuple - it must be able to be one err_tuple = (winerror.ERROR_INVALID_HANDLE, 'CloseHandle', err_msg) if sys.version_info < (3,): self.failUnlessEqual(tuple(exc), err_tuple) else: self.failUnlessEqual(exc.args, err_tuple)