win32util.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2001-2007 Twisted Matrix Laboratories.
  2. # Permission is hereby granted, free of charge, to any person obtaining
  3. # a copy of this software and associated documentation files (the
  4. # "Software"), to deal in the Software without restriction, including
  5. # without limitation the rights to use, copy, modify, merge, publish,
  6. # distribute, sublicense, and/or sell copies of the Software, and to
  7. # permit persons to whom the Software is furnished to do so, subject to
  8. # the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be
  11. # included in all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. """Error formatting function for Windows.
  21. The code is taken from twisted.python.win32 module.
  22. """
  23. from __future__ import absolute_import
  24. import os
  25. __all__ = ['formatError']
  26. class _ErrorFormatter(object):
  27. """
  28. Formatter for Windows error messages.
  29. @ivar winError: A callable which takes one integer error number argument
  30. and returns an L{exceptions.WindowsError} instance for that error (like
  31. L{ctypes.WinError}).
  32. @ivar formatMessage: A callable which takes one integer error number
  33. argument and returns a C{str} giving the message for that error (like
  34. L{win32api.FormatMessage}).
  35. @ivar errorTab: A mapping from integer error numbers to C{str} messages
  36. which correspond to those errors (like L{socket.errorTab}).
  37. """
  38. def __init__(self, WinError, FormatMessage, errorTab):
  39. self.winError = WinError
  40. self.formatMessage = FormatMessage
  41. self.errorTab = errorTab
  42. @classmethod
  43. def fromEnvironment(cls):
  44. """
  45. Get as many of the platform-specific error translation objects as
  46. possible and return an instance of C{cls} created with them.
  47. """
  48. try:
  49. from ctypes import WinError
  50. except ImportError:
  51. WinError = None
  52. try:
  53. from win32api import FormatMessage
  54. except ImportError:
  55. FormatMessage = None
  56. try:
  57. from socket import errorTab
  58. except ImportError:
  59. errorTab = None
  60. return cls(WinError, FormatMessage, errorTab)
  61. def formatError(self, errorcode):
  62. """
  63. Returns the string associated with a Windows error message, such as the
  64. ones found in socket.error.
  65. Attempts direct lookup against the win32 API via ctypes and then
  66. pywin32 if available), then in the error table in the socket module,
  67. then finally defaulting to C{os.strerror}.
  68. @param errorcode: the Windows error code
  69. @type errorcode: C{int}
  70. @return: The error message string
  71. @rtype: C{str}
  72. """
  73. if self.winError is not None:
  74. return str(self.winError(errorcode))
  75. if self.formatMessage is not None:
  76. return self.formatMessage(errorcode)
  77. if self.errorTab is not None:
  78. result = self.errorTab.get(errorcode)
  79. if result is not None:
  80. return result
  81. return os.strerror(errorcode)
  82. formatError = _ErrorFormatter.fromEnvironment().formatError