PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/interpreter/test/test_error.py

https://bitbucket.org/dac_io/pypy
Python | 99 lines | 97 code | 2 blank | 0 comment | 0 complexity | 9cd937f2be47b7a655695b55a81bf315 MD5 | raw file
  1. import py, os, errno
  2. from pypy.interpreter.error import OperationError, operationerrfmt
  3. from pypy.interpreter.error import decompose_valuefmt, get_operrcls2
  4. from pypy.interpreter.error import wrap_oserror, new_exception_class
  5. def test_decompose_valuefmt():
  6. assert (decompose_valuefmt("abc %s def") ==
  7. (("abc ", " def"), ('s',)))
  8. assert (decompose_valuefmt("%s%d%s") ==
  9. (("", "", "", ""), ('s', 'd', 's')))
  10. assert (decompose_valuefmt("%s%d%%%s") ==
  11. (("", "", "%", ""), ('s', 'd', 's')))
  12. def test_get_operrcls2():
  13. cls, strings = get_operrcls2('abc %s def %d')
  14. assert strings == ("abc ", " def ", "")
  15. assert issubclass(cls, OperationError)
  16. inst = cls("w_type", strings, "hello", 42)
  17. assert inst._compute_value() == "abc hello def 42"
  18. cls2, strings2 = get_operrcls2('a %s b %d c')
  19. assert cls2 is cls # caching
  20. assert strings2 == ("a ", " b ", " c")
  21. def test_operationerrfmt():
  22. operr = operationerrfmt("w_type", "abc %s def %d", "foo", 42)
  23. assert isinstance(operr, OperationError)
  24. assert operr.w_type == "w_type"
  25. assert operr._w_value is None
  26. assert operr._compute_value() == "abc foo def 42"
  27. operr2 = operationerrfmt("w_type2", "a %s b %d c", "bar", 43)
  28. assert operr2.__class__ is operr.__class__
  29. operr3 = operationerrfmt("w_type2", "a %s b %s c", "bar", "4b")
  30. assert operr3.__class__ is not operr.__class__
  31. def test_operationerrfmt_empty():
  32. py.test.raises(AssertionError, operationerrfmt, "w_type", "foobar")
  33. def test_errorstr(space):
  34. operr = OperationError(space.w_ValueError, space.wrap("message"))
  35. assert operr.errorstr(space) == "ValueError: message"
  36. assert operr.errorstr(space, use_repr=True) == "ValueError: 'message'"
  37. def test_wrap_oserror():
  38. class FakeSpace:
  39. w_OSError = [OSError]
  40. w_EnvironmentError = [EnvironmentError]
  41. def wrap(self, obj):
  42. return [obj]
  43. def call_function(self, exc, w_errno, w_msg, w_filename=None):
  44. return (exc, w_errno, w_msg, w_filename)
  45. space = FakeSpace()
  46. #
  47. e = wrap_oserror(space, OSError(errno.EBADF, "foobar"))
  48. assert isinstance(e, OperationError)
  49. assert e.w_type == [OSError]
  50. assert e.get_w_value(space) == ([OSError], [errno.EBADF],
  51. [os.strerror(errno.EBADF)], None)
  52. #
  53. e = wrap_oserror(space, OSError(errno.EBADF, "foobar"),
  54. filename = "test.py",
  55. exception_name = "w_EnvironmentError")
  56. assert isinstance(e, OperationError)
  57. assert e.w_type == [EnvironmentError]
  58. assert e.get_w_value(space) == ([EnvironmentError], [errno.EBADF],
  59. [os.strerror(errno.EBADF)],
  60. ["test.py"])
  61. #
  62. e = wrap_oserror(space, OSError(errno.EBADF, "foobar"),
  63. filename = "test.py",
  64. w_exception_class = [SystemError])
  65. assert isinstance(e, OperationError)
  66. assert e.w_type == [SystemError]
  67. assert e.get_w_value(space) == ([SystemError], [errno.EBADF],
  68. [os.strerror(errno.EBADF)],
  69. ["test.py"])
  70. def test_new_exception(space):
  71. w_error = new_exception_class(space, '_socket.error')
  72. assert w_error.getname(space) == 'error'
  73. assert space.str_w(space.repr(w_error)) == "<class '_socket.error'>"
  74. operr = OperationError(w_error, space.wrap("message"))
  75. assert operr.match(space, w_error)
  76. assert operr.match(space, space.w_Exception)
  77. # subclass of ValueError
  78. w_error = new_exception_class(space, 'error', space.w_ValueError)
  79. operr = OperationError(w_error, space.wrap("message"))
  80. assert operr.match(space, w_error)
  81. assert operr.match(space, space.w_ValueError)
  82. # subclass of (ValueError, TypeError)
  83. w_bases = space.newtuple([space.w_ValueError, space.w_TypeError])
  84. w_error = new_exception_class(space, 'error', w_bases)
  85. operr = OperationError(w_error, space.wrap("message"))
  86. assert operr.match(space, w_error)
  87. assert operr.match(space, space.w_ValueError)
  88. assert operr.match(space, space.w_TypeError)