test_styles.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.persisted.styles}.
  5. """
  6. import pickle
  7. from twisted.trial import unittest
  8. from twisted.persisted.styles import unpickleMethod, _UniversalPicklingError
  9. class Foo:
  10. """
  11. Helper class.
  12. """
  13. def method(self):
  14. """
  15. Helper method.
  16. """
  17. class Bar:
  18. """
  19. Helper class.
  20. """
  21. def sampleFunction():
  22. """
  23. A sample function for pickling.
  24. """
  25. lambdaExample = lambda x: x
  26. class UniversalPicklingErrorTests(unittest.TestCase):
  27. """
  28. Tests the L{_UniversalPicklingError} exception.
  29. """
  30. def raise_UniversalPicklingError(self):
  31. """
  32. Raise L{UniversalPicklingError}.
  33. """
  34. raise _UniversalPicklingError
  35. def test_handledByPickleModule(self):
  36. """
  37. Handling L{pickle.PicklingError} handles
  38. L{_UniversalPicklingError}.
  39. """
  40. self.assertRaises(pickle.PicklingError,
  41. self.raise_UniversalPicklingError)
  42. def test_handledBycPickleModule(self):
  43. """
  44. Handling L{cPickle.PicklingError} handles
  45. L{_UniversalPicklingError}.
  46. """
  47. try:
  48. import cPickle
  49. except ImportError:
  50. raise unittest.SkipTest("cPickle not available.")
  51. else:
  52. self.assertRaises(cPickle.PicklingError,
  53. self.raise_UniversalPicklingError)
  54. class UnpickleMethodTests(unittest.TestCase):
  55. """
  56. Tests for the unpickleMethod function.
  57. """
  58. def test_instanceBuildingNamePresent(self):
  59. """
  60. L{unpickleMethod} returns an instance method bound to the
  61. instance passed to it.
  62. """
  63. foo = Foo()
  64. m = unpickleMethod('method', foo, Foo)
  65. self.assertEqual(m, foo.method)
  66. self.assertIsNot(m, foo.method)
  67. def test_instanceBuildingNameNotPresent(self):
  68. """
  69. If the named method is not present in the class,
  70. L{unpickleMethod} finds a method on the class of the instance
  71. and returns a bound method from there.
  72. """
  73. foo = Foo()
  74. m = unpickleMethod('method', foo, Bar)
  75. self.assertEqual(m, foo.method)
  76. self.assertIsNot(m, foo.method)
  77. def test_primeDirective(self):
  78. """
  79. We do not contaminate normal function pickling with concerns from
  80. Twisted.
  81. """
  82. def expected(n):
  83. return "\n".join([
  84. "c" + __name__,
  85. sampleFunction.__name__, "p" + n, "."
  86. ]).encode("ascii")
  87. self.assertEqual(pickle.dumps(sampleFunction, protocol=0),
  88. expected("0"))
  89. try:
  90. import cPickle
  91. except:
  92. pass
  93. else:
  94. self.assertEqual(
  95. cPickle.dumps(sampleFunction, protocol=0),
  96. expected("1")
  97. )
  98. def test_lambdaRaisesPicklingError(self):
  99. """
  100. Pickling a C{lambda} function ought to raise a L{pickle.PicklingError}.
  101. """
  102. self.assertRaises(pickle.PicklingError, pickle.dumps, lambdaExample)
  103. try:
  104. import cPickle
  105. except:
  106. pass
  107. else:
  108. self.assertRaises(cPickle.PicklingError, cPickle.dumps,
  109. lambdaExample)