Tilting at Windmills with ctypes and cygwinreg

  • 2,050 views
Uploaded on

In this presentation, I take a brisk walk through a small problem in automated browser testing, take a quick right at Windows, and dash through implementing a new Python module replacing …

In this presentation, I take a brisk walk through a small problem in automated browser testing, take a quick right at Windows, and dash through implementing a new Python module replacing winreg.

http://pypi.python.org/pypi/cygwinreg

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,050
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
5
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Tilting at Windmills with ctypes and cygwinreg Simon Law simon@akoha.com
  • 2. def introduction():
  • 3. social reality game
  • 4. def problem():
  • 5. website
  • 6. testing
  • 7. click wait look
  • 8. click wait look
  • 9. click wait look
  • 10. slow
  • 11. laborious
  • 12. boring
  • 13. automated testing
  • 14. def test_browser():
  • 15. © Andy Heatwole Creative Commons Attribution-NonCommercial-NoDerivs 2.5. http://blog.bluehourphoto.com/?showimage=224
  • 16. getwindmill.com
  • 17. one script
  • 18. all browsers
  • 19. from windmill.authoring import * def setup_module(module): client = WindmillTestClient(__name__) client.click(id=u'home') client.waits.forPageLoad(timeout=u'60000') client.asserts.assertNode(id='LoginPage')
  • 20. from windmill.authoring import * def setup_module(module): client = WindmillTestClient(__name__) client.click(id=u'home') client.waits.forPageLoad(timeout=u'60000') client.asserts.assertNode(id='LoginPage')
  • 21. from windmill.authoring import * def setup_module(module): client = WindmillTestClient(__name__) client.click(id=u'home') client.waits.forPageLoad(timeout=u'60000') client.asserts.assertNode(id='LoginPage')
  • 22. from windmill.authoring import * def setup_module(module): client = WindmillTestClient(__name__) client.click(id=u'home') client.waits.forPageLoad(timeout=u'60000') client.asserts.assertNode(id='LoginPage')
  • 23. semi-automated testing
  • 24. def continuous_integration():
  • 25. test automatically
  • 26. buildbot.net
  • 27. ~/akoha$ make check
  • 28. C:>make check 'make' is not recognized as an internal or external command, operable program or batch file.
  • 29. cygwin.com
  • 30. ~/akoha$ make check
  • 31. Traceback (most recent call last): ... ImportError: No module named _winreg
  • 32. def windows_registry():
  • 33. proxy settings
  • 34. import _winreg
  • 35. import winreg
  • 36. 32.3. winreg Windows registry access Platforms: Windows
  • 37. 32.3. winreg Windows registry access Platforms: Windows
  • 38. 3 choices
  • 39. 1
  • 40. give up
  • 41. 2
  • 42. recompile Python
  • 43. 3
  • 44. cygwinreg
  • 45. like hurting myself
  • 46. def call_system_libraries():
  • 47. import ctypes
  • 48. foreign function interface
  • 49. call functions
  • 50. shared libraries
  • 51. libiberty.so
  • 52. libgcc.dynlib
  • 53. advapi32.dll
  • 54. >>> from ctypes import cdll
  • 55. >>> cdll.advapi32 <CDLL 'advapi32', ... at ...>
  • 56. >>> cdll.advapi32.RegCloseKey <_FuncPtr object at 0x...>
  • 57. >>> a = cdll.advapi32 >>> RegCloseKey = a.RegCloseKey >>> RegCloseKey(0) 6
  • 58. no introspection
  • 59. >>> RegCloseKey(0) 6 >>> RegCloseKey() 6
  • 60. return value
  • 61. function arguments
  • 62. from ctypes import cdll from ctypes import c_long, c_ulong # WINADVAPI LONG WINAPI RegCloseKey(HKEY); RegCloseKey = cdll.advapi32.RegCloseKey RegCloseKey.restype = c_long RegCloseKey.argtypes = [c_ulong]
  • 63. from ctypes import cdll from ctypes import c_long, c_ulong # WINADVAPI LONG WINAPI RegCloseKey(HKEY); RegCloseKey = cdll.advapi32.RegCloseKey RegCloseKey.restype = c_long RegCloseKey.argtypes = [c_ulong]
  • 64. from ctypes import cdll from ctypes import c_long, c_ulong # WINADVAPI LONG WINAPI RegCloseKey(HKEY); RegCloseKey = cdll.advapi32.RegCloseKey RegCloseKey.restype = c_long RegCloseKey.argtypes = [c_ulong]
  • 65. from ctypes import cdll from ctypes import c_long, c_ulong LONG = c_long HANDLE = c_ulong # in header files: void * HKEY = HANDLE # WINADVAPI LONG WINAPI RegCloseKey(HKEY); RegCloseKey = cdll.advapi32.RegCloseKey RegCloseKey.restype = LONG RegCloseKey.argtypes = [HKEY]
  • 66. >>> RegCloseKey(0) 6 >>> RegCloseKey() Traceback (most recent call last): ... TypeError: this function takes at least 1 argument (0 given)
  • 67. not Pythonic
  • 68. PyHKEY.Close() Closes the underlying Windows handle.
  • 69. class PyHKEY(object): def __init__(self, hkey): self.hkey = hkey def Close(self): if RegCloseKey(self.hkey) != 0: raise WindowsError() self.hkey = 0 class WindowsError(OSError): ...
  • 70. class PyHKEY(object): def __init__(self, hkey): self.hkey = hkey def Close(self): RegCloseKey(self.hkey) raise WindowsError() self.hkey = 0 class WindowsError(OSError): ...
  • 71. class PyHKEY(object): def __init__(self, hkey): self.hkey = hkey def Close(self): if RegCloseKey(self.hkey) != 0: raise WindowsError() self.hkey = 0 class WindowsError(OSError): ...
  • 72. >>> from cygwinreg import PyHKEY >>> hkey = PyHKEY(0) >>> hkey.Close()
  • 73. RegDeleteKeyW = cdll.advapi32.RegDeleteKeyW RegDeleteKeyW.restype = LONG RegDeleteKeyW.argtypes = [HKEY, LPCWSTR]
  • 74. def DeleteKey(key, sub_key): from cygwinreg.w32api import RegDeleteKeyW wincall(RegDeleteKeyW(PyHKEY.make(key), sub_key))
  • 75. hours later
  • 76. >>> try: ... import winreg ... except ImportError: ... import cygwinreg as winreg
  • 77. def tradeoffs():
  • 78. Python only
  • 79. no compilation
  • 80. slower
  • 81. no parsing
  • 82. simple
  • 83. not automatic
  • 84. standard
  • 85. portable
  • 86. good enough
  • 87. def conclusion():
  • 88. ~/akoha$ make check
  • 89. continuous integration
  • 90. browser tests
  • 91. pypi.python.org/pypi/cygwinreg
  • 92. no compilation
  • 93. no installation
  • 94. no :'-(
  • 95. cygwinreg pypi.python.org/pypi/cygwinreg PSF License this presentation: Attribution-Share Alike 3.0