[Python-Dev] The os module, unix and win32

Guido van Rossum guido at python.org
Fri Jan 9 12:28:36 EST 2004


> There are several ways to fix these problems.  A simple one would be to
> write this instead:
> 
> def CreateKey(baseKey, subKey):
>     'Creates/opens the given key and returns it'
>     RCK = windll.advapi32.RegCreateKeyExA
>     key = c_int(0)
>     result = RCK(str(baseKey), str(subKey), 0, 0,
>                  REG_OPTION_NON_VOLATILE,
>                  KEY_ALL_ACCESS, 0, byref(key), 0):
>         raise WindowsError(result)
>     return key.value
> 
> But ctypes supports a kind of 'function prototypes' (which do automatic
> argument type checking and/or conversion= as well as automatic result
> checking:
> 
> def CheckNonNull(errcode):
>     # if errcode is nonzero, raise a WindowsError
>     if errcode:
>         raise WindowsError(errcode)
> 
> RCK = windll.advapi32.RegCreateKeyExA
> # specify the argument types
> RCK.argtypes = (HKEY, LPCTSTR, DWORD, REGSAM, LPSECURITY_ATTRIBUTES,
> PMKEY, LPDWROD)
> # RegCreateKeyExA returns 0 on success, or a windows error code
> RCK.restype = CheckNonNull
> 
> def CreateKey(baseKey, subKey):
>     'Creates/opens the given key and returns it'
>     key = c_int(0)
>     RCK(baseKey, subKey, 0, 0,
>         REG_OPTION_NON_VOLATILE,
>         KEY_ALL_ACCESS, 0, byref(key), 0)
>     return key.value

If no C code existed, this all sounds great -- but don't underestimate
its obscurity.  For _winreg.c, I don't see a reason to switch.  If
it's missing something you'd like to see, please submit a patch to the
C code.

> > I don't know enough about ctypes and its user community to answer that
> > (I doubt I'd have much direct need for it myself).  But in general I'm
> > biased towards cross-platform tools.
> 
> I had reports that it works on Solaris, Linux, MacOS, BSD.  Maybe more
> systems.
> 
> The problem is that the non-windows version uses libffi, which is
> difficult to find and install - it seems to be maintained now as part of
> gcc, although the license is more BSD like.
> 
> I would like to get rid of libffi - but the only multi-architecture
> alternative I know of is Bruno Haible's ffcall (which is GPL).
> 
> There *may* be other options (including writing assembly code).
> Sam Rushing's calldll did this, AFAIK.
> 
> And, remember: you can write bulletproof code with ctypes, but you can
> also easily crash Python.  Or other weird things.

OK, never mind.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list