Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win32clipboardDemo.py 4.6KB

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