Tilting at Windmills with ctypes and cygwinreg

2,355 views

Published 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 winreg.

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,355
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Tilting at Windmills with ctypes and cygwinreg

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

×