0001 import pythoncom 0002 import win32com.server.util 0003 import win32com.test.util 0004 0005 import unittest 0006 0007 class Persists: 0008 _public_methods_ = [ 'GetClassID', 'IsDirty', 'Load', 'Save', 0009 'GetSizeMax', 'InitNew' ] 0010 _com_interfaces_ = [ pythoncom.IID_IPersistStreamInit ] 0011 def __init__(self): 0012 self.data = "abcdefg" 0013 self.dirty = 1 0014 def GetClassID(self): 0015 return pythoncom.IID_NULL 0016 def IsDirty(self): 0017 return self.dirty 0018 def Load(self, stream): 0019 self.data = stream.Read(26) 0020 def Save(self, stream, clearDirty): 0021 stream.Write(self.data) 0022 if clearDirty: 0023 self.dirty = 0 0024 def GetSizeMax(self): 0025 return 1024 0026 0027 def InitNew(self): 0028 pass 0029 0030 0031 class Stream: 0032 _public_methods_ = [ 'Read', 'Write' ] 0033 _com_interfaces_ = [ pythoncom.IID_IStream ] 0034 0035 def __init__(self, data): 0036 self.data = data 0037 self.index = 0 0038 0039 def Read(self, amount): 0040 result = self.data[self.index : self.index + amount] 0041 self.index = self.index + amount 0042 return result 0043 0044 def Write(self, data): 0045 self.data = data 0046 self.index = 0 0047 return len(data) 0048 0049 0050 class StreamTest(win32com.test.util.TestCase): 0051 def _readWrite(self, data, write_stream, read_stream = None): 0052 if read_stream is None: read_stream = write_stream 0053 write_stream.Write(data) 0054 got = read_stream.Read(len(data)) 0055 self.assertEqual(data, got) 0056 0057 def testit(self): 0058 mydata = 'abcdefghijklmnopqrstuvwxyz' 0059 0060 # First test the objects just as Python objects... 0061 s = Stream(mydata) 0062 p = Persists() 0063 0064 p.Load(s) 0065 p.Save(s, 0) 0066 self.assertEqual(s.data, mydata) 0067 0068 # Wrap the Python objects as COM objects, and make the calls as if 0069 # they were non-Python COM objects. 0070 s2 = win32com.server.util.wrap(s, pythoncom.IID_IStream) 0071 p2 = win32com.server.util.wrap(p, pythoncom.IID_IPersistStreamInit) 0072 0073 self._readWrite(mydata, s, s) 0074 self._readWrite(mydata, s, s2) 0075 self._readWrite(mydata, s2, s) 0076 self._readWrite(mydata, s2, s2) 0077 0078 self._readWrite("string with\0a NULL", s2, s2) 0079 # reset the stream 0080 s.Write(mydata) 0081 p2.Load(s2) 0082 p2.Save(s2, 0) 0083 self.assertEqual(s.data, mydata) 0084 0085 if __name__=='__main__': 0086 unittest.main() 0087
Generated by PyXR 0.9.4