win32clipboardDemo.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # win32clipboardDemo.py
  2. #
  3. # Demo/test of the win32clipboard module.
  4. from win32clipboard import *
  5. from pywin32_testutil import str2bytes # py3k-friendly helper
  6. import win32con
  7. import types
  8. if not __debug__:
  9. print("WARNING: The test code in this module uses assert")
  10. print("This instance of Python has asserts disabled, so many tests will be skipped")
  11. cf_names = {}
  12. # Build map of CF_* constants to names.
  13. for name, val in list(win32con.__dict__.items()):
  14. if name[:3]=="CF_" and name != "CF_SCREENFONTS": # CF_SCREEN_FONTS==CF_TEXT!?!?
  15. cf_names[val] = name
  16. def TestEmptyClipboard():
  17. OpenClipboard()
  18. try:
  19. EmptyClipboard()
  20. assert EnumClipboardFormats(0)==0, "Clipboard formats were available after emptying it!"
  21. finally:
  22. CloseClipboard()
  23. def TestText():
  24. OpenClipboard()
  25. try:
  26. text = "Hello from Python"
  27. text_bytes = str2bytes(text)
  28. SetClipboardText(text)
  29. got = GetClipboardData(win32con.CF_TEXT)
  30. # CF_TEXT always gives us 'bytes' back .
  31. assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
  32. finally:
  33. CloseClipboard()
  34. OpenClipboard()
  35. try:
  36. # CF_UNICODE text always gives unicode objects back.
  37. got = GetClipboardData(win32con.CF_UNICODETEXT)
  38. assert got == text, "Didnt get the correct result back - '%r'." % (got,)
  39. assert type(got)==str, "Didnt get the correct result back - '%r'." % (got,)
  40. # CF_OEMTEXT is a bytes-based format.
  41. got = GetClipboardData(win32con.CF_OEMTEXT)
  42. assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
  43. # Unicode tests
  44. EmptyClipboard()
  45. text = "Hello from Python unicode"
  46. text_bytes = str2bytes(text)
  47. # Now set the Unicode value
  48. SetClipboardData(win32con.CF_UNICODETEXT, text)
  49. # Get it in Unicode.
  50. got = GetClipboardData(win32con.CF_UNICODETEXT)
  51. assert got == text, "Didnt get the correct result back - '%r'." % (got,)
  52. assert type(got)==str, "Didnt get the correct result back - '%r'." % (got,)
  53. # Close and open the clipboard to ensure auto-conversions take place.
  54. finally:
  55. CloseClipboard()
  56. OpenClipboard()
  57. try:
  58. # Make sure I can still get the text as bytes
  59. got = GetClipboardData(win32con.CF_TEXT)
  60. assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
  61. # Make sure we get back the correct types.
  62. got = GetClipboardData(win32con.CF_UNICODETEXT)
  63. assert type(got)==str, "Didnt get the correct result back - '%r'." % (got,)
  64. got = GetClipboardData(win32con.CF_OEMTEXT)
  65. assert got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
  66. print("Clipboard text tests worked correctly")
  67. finally:
  68. CloseClipboard()
  69. def TestClipboardEnum():
  70. OpenClipboard()
  71. try:
  72. # Enumerate over the clipboard types
  73. enum = 0
  74. while 1:
  75. enum = EnumClipboardFormats(enum)
  76. if enum==0:
  77. break
  78. assert IsClipboardFormatAvailable(enum), "Have format, but clipboard says it is not available!"
  79. n = cf_names.get(enum,"")
  80. if not n:
  81. try:
  82. n = GetClipboardFormatName(enum)
  83. except error:
  84. n = "unknown (%s)" % (enum,)
  85. print("Have format", n)
  86. print("Clipboard enumerator tests worked correctly")
  87. finally:
  88. CloseClipboard()
  89. class Foo:
  90. def __init__(self, **kw):
  91. self.__dict__.update(kw)
  92. def __cmp__(self, other):
  93. return cmp(self.__dict__, other.__dict__)
  94. def __eq__(self, other):
  95. return self.__dict__==other.__dict__
  96. def TestCustomFormat():
  97. OpenClipboard()
  98. try:
  99. # Just for the fun of it pickle Python objects through the clipboard
  100. fmt = RegisterClipboardFormat("Python Pickle Format")
  101. import pickle
  102. pickled_object = Foo(a=1, b=2, Hi=3)
  103. SetClipboardData(fmt, pickle.dumps( pickled_object ) )
  104. # Now read it back.
  105. data = GetClipboardData(fmt)
  106. loaded_object = pickle.loads(data)
  107. assert pickle.loads(data) == pickled_object, "Didnt get the correct data!"
  108. print("Clipboard custom format tests worked correctly")
  109. finally:
  110. CloseClipboard()
  111. if __name__=='__main__':
  112. TestEmptyClipboard()
  113. TestText()
  114. TestCustomFormat()
  115. TestClipboardEnum()
  116. # And leave it empty at the end!
  117. TestEmptyClipboard()