[Python-Dev] Proposal: Registry access for Python on Windows

Guido van Rossum guido@CNRI.Reston.VA.US
Tue, 01 Feb 2000 11:11:03 -0500


> I propose to include functions to access the windows registry
> into the python 1.6 core.
> 
> I have thrown together some code which I will post hopefully
> tomorrow, but I would like to hear some comments before.
> 
> ----------------------------------------------------------------------
> winreg - registry access module
> 
> Constants:
>   HKEY_CLASSES_ROOT, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER,
>   HKEY_USERS - bases of registry
>   KEY_READ, KEY_WRITE, KEY_ALL_ACCESS - access rights
>   REG_SZ, REG_DWORD, REG_BINARY - typecodes for values
> 
> Exception:
>   error - raised when a function fails. Will usually contain
>     a windows error code and a textual description.
> 
> Functions:
>   OpenKey (base, subkey, [, rights=KEY_READ]) -> integer
>     Opens an existing key with the specified access rights
>     and returns an integer handle. The handle must be closed
>     by a call to CloseKey().
> 
>   CreateKey (base, subkey [, sam=KEY_ALL_ACCESS]) -> integer
>     Creates a new subkey or opens an exsiting one
>     and returns an integer handle.
>     base must be one of the HKEY_ constants or an integer handle.
>     The handle must be closed by a call to CloseKey().
> 
>   CloseKey (handle)
>     Closes a key handle.
> 
>   EnumValues (handle, subkey) -> sequence
>     Returns a sequence containing name, value, typecode triples
>     for each existing value.
> 
>   EnumKeys (handle, subkey) -> sequence
>     Returns a sequence containing the names of all subkeys.
> 
>   QueryValue (handle, subkey) -> tuple
>     Returns a tuple containing name, value and typecode.
> 
>   SetValue (handle, name, typecode, value)
>     Sets the value of a name to value.
>     value must be a string for REG_SZ or REG_BINARY,
>     an integer for REG_DWORD.
> 
>   DeleteValue (handle, valuename)
>     Deletes the value.
> 
>   DeleteKey (handle, name [,recursive=0)
>     Deletes the named key if no subkeys exist. If recursive is
>     given as a true value, subkeys are recursively deleted.
>     This is done with Reg* calls, NOT with SHDeleteKey and
>     SHDeleteEmptyKey functions, so should work under any
>     flavor of NT and Win9x.
> 
> Note: To use the SetValue and Delete* functions, the key must have
> been opened with KEY_WRITE_ACCESS or KEY_ALL_ACCESS.
> ----------------------------------------------------------------------
> Open Questions:
>   Is the recursive-flag for DeleteKey() too dangerous?
>   Should I switch from an integer handle to a full blown
>   python-object, which would call CloseKey() automatically in the
> destructor?
>   A higher level interface would be nice, but this should probably
> implemented
>   on top of this module in python.
> 
> Comments appreciated!

As long as we're redesigning the API and not just copying the registry
access functions fromn win32api, shouldn't this be made a little bit
more OO?  Things that return a handle should return an object, and
things taking a handle should be methods of that object.  Also, the
structured return values should probably be turned into objects with
attributes.

It might be acceptable to do this as a Python wrapper; in that case
perhaps the C module should be called _winreg.

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