Update of /cvsroot/pywin32/pywin32/win32/Demos
In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv4367/win32/Demos
Modified Files:
BackupRead_BackupWrite.py CreateFileTransacted_MiniVersion.py
eventLogDemo.py mmapfile_demo.py win32clipboardDemo.py
win32fileDemo.py
Log Message:
Move most tests and demos to using pywin32_testutil helpers, particularly
for tests involving bytes and buffers. Some tests raise TestSkipped when
appropriate and other minor test fixes.
Index: CreateFileTransacted_MiniVersion.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/Demos/CreateFileTransacted_MiniVersion.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** CreateFileTransacted_MiniVersion.py 8 Nov 2007 20:02:30 -0000 1.2
--- CreateFileTransacted_MiniVersion.py 7 Jan 2009 06:03:29 -0000 1.3
***************
*** 8,11 ****
--- 8,13 ----
import win32con, winioctlcon
import struct
+ import os
+ from pywin32_testutil import str2bytes # py3k-friendly helper
"""
***************
*** 22,26 ****
tempdir=win32api.GetTempPath()
tempfile=win32api.GetTempFileName(tempdir,'cft')[0]
! print tempfile
f=open(tempfile,'w')
f.write('This is original file.\n')
--- 24,28 ----
tempdir=win32api.GetTempPath()
tempfile=win32api.GetTempFileName(tempdir,'cft')[0]
! print "Demonstrating transactions on tempfile", tempfile
f=open(tempfile,'w')
f.write('This is original file.\n')
***************
*** 32,42 ****
None, win32con.OPEN_EXISTING, 0 , None, Transaction=trans)
! win32file.WriteFile(hfile, 'This is first miniversion.\n')
! buf=win32file.DeviceIoControl(hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION,'',buf_size,None)
struct_ver, struct_len, base_ver, ver_1=struct.unpack(buf_fmt, buf)
win32file.SetFilePointer(hfile, 0, win32con.FILE_BEGIN)
! win32file.WriteFile(hfile, 'This is second miniversion!\n')
! buf=win32file.DeviceIoControl(hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION,'',buf_size,None)
struct_ver, struct_len, base_ver, ver_2=struct.unpack(buf_fmt, buf)
hfile.Close()
--- 34,44 ----
None, win32con.OPEN_EXISTING, 0 , None, Transaction=trans)
! win32file.WriteFile(hfile, str2bytes('This is first miniversion.\n'))
! buf=win32file.DeviceIoControl(hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION,None,buf_size,None)
struct_ver, struct_len, base_ver, ver_1=struct.unpack(buf_fmt, buf)
win32file.SetFilePointer(hfile, 0, win32con.FILE_BEGIN)
! win32file.WriteFile(hfile, str2bytes('This is second miniversion!\n'))
! buf=win32file.DeviceIoControl(hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION,None,buf_size,None)
struct_ver, struct_len, base_ver, ver_2=struct.unpack(buf_fmt, buf)
hfile.Close()
***************
*** 63,64 ****
--- 65,68 ----
## MiniVersions are destroyed when transaction is committed or rolled back
win32transaction.CommitTransaction(trans)
+
+ os.unlink(tempfile)
Index: win32fileDemo.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/Demos/win32fileDemo.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** win32fileDemo.py 11 Dec 2008 06:58:21 -0000 1.7
--- win32fileDemo.py 7 Jan 2009 06:03:29 -0000 1.8
***************
*** 21,26 ****
0,
None)
! test_data = "Hello\0there"
! win32file.WriteFile(handle, test_data.encode('ascii'))
handle.Close()
# Open it for reading.
--- 21,26 ----
0,
None)
! test_data = "Hello\0there".encode("ascii")
! win32file.WriteFile(handle, test_data)
handle.Close()
# Open it for reading.
***************
*** 28,33 ****
rc, data = win32file.ReadFile(handle, 1024)
handle.Close()
! if data.decode('ascii') == test_data:
print "Successfully wrote and read a file"
os.unlink(testName)
--- 28,35 ----
rc, data = win32file.ReadFile(handle, 1024)
handle.Close()
! if data == test_data:
print "Successfully wrote and read a file"
+ else:
+ raise Exception("Got different data back???")
os.unlink(testName)
Index: mmapfile_demo.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/Demos/mmapfile_demo.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** mmapfile_demo.py 28 Jun 2007 08:28:21 -0000 1.1
--- mmapfile_demo.py 7 Jan 2009 06:03:29 -0000 1.2
***************
*** 1,4 ****
--- 1,7 ----
import win32api, mmapfile
+ import winerror
import tempfile, os
+ from pywin32_testutil import str2bytes
+
system_info=win32api.GetSystemInfo()
page_size=system_info[1]
***************
*** 12,18 ****
m1=mmapfile.mmapfile(File=fname, Name=mapping_name, MaximumSize=fsize)
m1.seek(100)
! m1.write_byte('?')
m1.seek(-1,1)
! assert m1.read_byte()=='?'
## A reopened named mapping should have exact same size as original mapping
--- 15,21 ----
m1=mmapfile.mmapfile(File=fname, Name=mapping_name, MaximumSize=fsize)
m1.seek(100)
! m1.write_byte(str2bytes('?'))
m1.seek(-1,1)
! assert m1.read_byte()==str2bytes('?')
## A reopened named mapping should have exact same size as original mapping
***************
*** 20,25 ****
assert m2.size()==m1.size()
m1.seek(0,0)
! m1.write(fsize*'s')
! assert m2.read(fsize)==fsize*'s'
move_src=100
--- 23,28 ----
assert m2.size()==m1.size()
m1.seek(0,0)
! m1.write(fsize*str2bytes('s'))
! assert m2.read(fsize)==fsize*str2bytes('s')
move_src=100
***************
*** 29,36 ****
m2.seek(move_src,0)
assert m2.tell()==move_src
! m2.write('m'*move_size)
m2.move(move_dest, move_src, move_size)
m2.seek(move_dest, 0)
! assert m2.read(move_size) == 'm' * move_size
## m2.write('x'* (fsize+1))
--- 32,39 ----
m2.seek(move_src,0)
assert m2.tell()==move_src
! m2.write(str2bytes('m')*move_size)
m2.move(move_dest, move_src, move_size)
m2.seek(move_dest, 0)
! assert m2.read(move_size) == str2bytes('m') * move_size
## m2.write('x'* (fsize+1))
***************
*** 39,43 ****
assert m1.size()==fsize * 2
m1.seek(fsize)
! m1.write('w' * fsize)
m1.flush()
m1.close()
--- 42,46 ----
assert m1.size()==fsize * 2
m1.seek(fsize)
! m1.write(str2bytes('w') * fsize)
m1.flush()
m1.close()
***************
*** 50,54 ****
fname_large=tempfile.mktemp()
mapping_name='Pywin32_large_mmap'
! offsetdata='This is start of offset'
## Deliberately use odd numbers to test rounding logic
--- 53,57 ----
fname_large=tempfile.mktemp()
mapping_name='Pywin32_large_mmap'
! offsetdata=str2bytes('This is start of offset')
## Deliberately use odd numbers to test rounding logic
***************
*** 68,80 ****
m2=None
try:
! m1=mmapfile.mmapfile(fname_large, mapping_name, fsize, 0, offset*2)
! m1.seek(offset)
! m1.write(offsetdata)
!
! ## When reopening an existing mapping without passing a file handle, you have
! ## to specify a positive size even though it's ignored
! m2=mmapfile.mmapfile(File=None, Name=mapping_name, MaximumSize=1,
! FileOffset=offset, NumberOfBytesToMap=view_size)
! assert m2.read(len(offsetdata))==offsetdata
finally:
if m1 is not None:
--- 71,90 ----
m2=None
try:
! try:
! m1=mmapfile.mmapfile(fname_large, mapping_name, fsize, 0, offset*2)
! except mmapfile.error, exc:
! # if we don't have enough disk-space, that's OK.
! if exc.winerror!=winerror.ERROR_DISK_FULL:
! raise
! print "skipping large file test - need", fsize, "available bytes."
! else:
! m1.seek(offset)
! m1.write(offsetdata)
!
! ## When reopening an existing mapping without passing a file handle, you have
! ## to specify a positive size even though it's ignored
! m2=mmapfile.mmapfile(File=None, Name=mapping_name, MaximumSize=1,
! FileOffset=offset, NumberOfBytesToMap=view_size)
! assert m2.read(len(offsetdata))==offsetdata
finally:
if m1 is not None:
Index: BackupRead_BackupWrite.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/Demos/BackupRead_BackupWrite.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** BackupRead_BackupWrite.py 7 Jan 2005 00:20:24 -0000 1.1
--- BackupRead_BackupWrite.py 7 Jan 2009 06:03:29 -0000 1.2
***************
*** 5,8 ****
--- 5,9 ----
import pythoncom, pywintypes
import struct, traceback
+ from pywin32_testutil import str2bytes, ob2memory
all_sd_info=win32security.DACL_SECURITY_INFORMATION|win32security.DACL_SECURITY_INFORMATION| \
***************
*** 69,73 ****
print 'Written:',bytes_written,'Context:',outctxt
win32file.BackupRead(h, 0, buf, True, True, ctxt)
! win32file.BackupWrite(outh, 0, '', True, True, outctxt)
win32file.CloseHandle(h)
win32file.CloseHandle(outh)
--- 70,74 ----
print 'Written:',bytes_written,'Context:',outctxt
win32file.BackupRead(h, 0, buf, True, True, ctxt)
! win32file.BackupWrite(outh, 0, str2bytes(''), True, True, outctxt)
win32file.CloseHandle(h)
win32file.CloseHandle(outh)
***************
*** 76,80 ****
assert open(tempfile+':streamdata').read()==open(outfile+':streamdata').read(),"streamdata contents differ !"
assert open(tempfile+':anotherstream').read()==open(outfile+':anotherstream').read(),"anotherstream contents differ !"
! assert buffer(win32security.GetFileSecurity(tempfile,all_sd_info))[:]== \
! buffer(win32security.GetFileSecurity(outfile, all_sd_info))[:], "Security descriptors are different !"
## also should check Summary Info programatically
--- 77,81 ----
assert open(tempfile+':streamdata').read()==open(outfile+':streamdata').read(),"streamdata contents differ !"
assert open(tempfile+':anotherstream').read()==open(outfile+':anotherstream').read(),"anotherstream contents differ !"
! assert ob2memory(win32security.GetFileSecurity(tempfile,all_sd_info))[:]== \
! ob2memory(win32security.GetFileSecurity(outfile, all_sd_info))[:], "Security descriptors are different !"
## also should check Summary Info programatically
Index: eventLogDemo.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/Demos/eventLogDemo.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** eventLogDemo.py 11 Dec 2008 06:57:50 -0000 1.7
--- eventLogDemo.py 7 Jan 2009 06:03:29 -0000 1.8
***************
*** 90,100 ****
win32evtlogutil.ReportEvent(logType, 2,
strings=["The message text for event 2","Another insert"],
! data = "Raw\0Data", sid = my_sid)
win32evtlogutil.ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_WARNING_TYPE,
strings=["A warning","An even more dire warning"],
! data = "Raw\0Data", sid = my_sid)
win32evtlogutil.ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
strings=["An info","Too much info"],
! data = "Raw\0Data", sid = my_sid)
print("Successfully wrote 3 records to the log")
--- 90,100 ----
win32evtlogutil.ReportEvent(logType, 2,
strings=["The message text for event 2","Another insert"],
! data = "Raw\0Data".encode("ascii"), sid = my_sid)
win32evtlogutil.ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_WARNING_TYPE,
strings=["A warning","An even more dire warning"],
! data = "Raw\0Data".encode("ascii"), sid = my_sid)
win32evtlogutil.ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
strings=["An info","Too much info"],
! data = "Raw\0Data".encode("ascii"), sid = my_sid)
print("Successfully wrote 3 records to the log")
Index: win32clipboardDemo.py
===================================================================
RCS file: /cvsroot/pywin32/pywin32/win32/Demos/win32clipboardDemo.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** win32clipboardDemo.py 3 Feb 2003 00:30:29 -0000 1.2
--- win32clipboardDemo.py 7 Jan 2009 06:03:29 -0000 1.3
***************
*** 3,6 ****
--- 3,7 ----
# Demo/test of the win32clipboard module.
from win32clipboard import *
+ from pywin32_testutil import str2bytes # py3k-friendly helper
import win32con
import types
***************
*** 28,37 ****
try:
text = "Hello from Python"
SetClipboardText(text)
got = GetClipboardData(win32con.CF_TEXT)
! assert got == text, "Didnt get the correct result back - '%r'." % (got,)
! # Win32 documentation says I can get the result back as CF_UNICODE or CF_OEMTEXT.
! # But it appears I need to close the clipboard for this to kick-in.
! # but if I attempt to, it fails!
finally:
CloseClipboard()
--- 29,37 ----
try:
text = "Hello from Python"
+ text_bytes = str2bytes(text)
SetClipboardText(text)
got = GetClipboardData(win32con.CF_TEXT)
! # CF_TEXT always gives us 'bytes' back .
! assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
finally:
CloseClipboard()
***************
*** 39,52 ****
OpenClipboard()
try:
got = GetClipboardData(win32con.CF_UNICODETEXT)
assert got == text, "Didnt get the correct result back - '%r'." % (got,)
assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
got = GetClipboardData(win32con.CF_OEMTEXT)
! assert got == text, "Didnt get the correct result back - '%r'." % (got,)
# Unicode tests
EmptyClipboard()
text = u"Hello from Python unicode"
# Now set the Unicode value
SetClipboardData(win32con.CF_UNICODETEXT, text)
--- 39,55 ----
OpenClipboard()
try:
+ # CF_UNICODE text always gives unicode objects back.
got = GetClipboardData(win32con.CF_UNICODETEXT)
assert got == text, "Didnt get the correct result back - '%r'." % (got,)
assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
+ # CF_OEMTEXT is a bytes-based format.
got = GetClipboardData(win32con.CF_OEMTEXT)
! assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
# Unicode tests
EmptyClipboard()
text = u"Hello from Python unicode"
+ text_bytes = str2bytes(text)
# Now set the Unicode value
SetClipboardData(win32con.CF_UNICODETEXT, text)
***************
*** 63,74 ****
try:
! # Make sure I can still get the text.
got = GetClipboardData(win32con.CF_TEXT)
! assert got == text, "Didnt get the correct result back - '%r'." % (got,)
# Make sure we get back the correct types.
got = GetClipboardData(win32con.CF_UNICODETEXT)
assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
got = GetClipboardData(win32con.CF_OEMTEXT)
! assert got == text, "Didnt get the correct result back - '%r'." % (got,)
print "Clipboard text tests worked correctly"
finally:
--- 66,77 ----
try:
! # Make sure I can still get the text as bytes
got = GetClipboardData(win32con.CF_TEXT)
! assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
# Make sure we get back the correct types.
got = GetClipboardData(win32con.CF_UNICODETEXT)
assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
got = GetClipboardData(win32con.CF_OEMTEXT)
! assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
print "Clipboard text tests worked correctly"
finally:
***************
*** 102,105 ****
--- 105,110 ----
def __cmp__(self, other):
return cmp(self.__dict__, other.__dict__)
+ def __eq__(self, other):
+ return self.__dict__==other.__dict__
def TestCustomFormat():
|