home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_new.py < prev    next >
Text File  |  1997-01-27  |  1KB  |  64 lines

  1. from test_support import verbose
  2. import sys
  3. import new
  4.  
  5. class Eggs:
  6.     def get_yolks(self):
  7.     return self.yolks
  8.  
  9. print 'new.module()'
  10. m = new.module('Spam')
  11. if verbose:
  12.     print m
  13. m.Eggs = Eggs
  14. sys.modules['Spam'] = m
  15. import Spam
  16.  
  17. def get_more_yolks(self):
  18.     return self.yolks + 3
  19.  
  20. print 'new.classobj()'
  21. C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
  22. if verbose:
  23.     print C
  24. print 'new.instance()'
  25. c = new.instance(C, {'yolks': 3})
  26. if verbose:
  27.     print c
  28.  
  29. def break_yolks(self):
  30.     self.yolks = self.yolks - 2
  31. print 'new.instancemethod()'
  32. im = new.instancemethod(break_yolks, c, C)
  33. if verbose:
  34.     print im
  35.  
  36. if c.get_yolks() <> 3 and c.get_more_yolks() <> 6:
  37.     print 'Broken call of hand-crafted class instance'
  38. im()
  39. if c.get_yolks() <> 1 and c.get_more_yolks() <> 4:
  40.     print 'Broken call of hand-crafted instance method'
  41.  
  42. codestr = '''
  43. a = 1
  44. b = 2
  45. c = a + b
  46. '''
  47.  
  48. ccode = compile(codestr, '<string>', 'exec')
  49. g = {'c': 0, '__builtins__': __builtins__}
  50. # this test could be more robust
  51. print 'new.function()'
  52. func = new.function(ccode, g)
  53. if verbose:
  54.     print func
  55. func()
  56. if g['c'] <> 3:
  57.     print 'Could not create a proper function object'
  58.  
  59. # bogus test of new.code()
  60. print 'new.code()'
  61. d = new.code(3, 3, 3, 3, codestr, (), (), (), "<string>", "<name>", 1, "")
  62. if verbose:
  63.     print d
  64.