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.

2088 lines
64 KiB

Merged revisions 55631-55794 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ................ r55636 | neal.norwitz | 2007-05-29 00:06:39 -0700 (Tue, 29 May 2007) | 149 lines Merged revisions 55506-55635 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r55507 | georg.brandl | 2007-05-22 07:28:17 -0700 (Tue, 22 May 2007) | 2 lines Remove the "panel" module doc file which has been ignored since 1994. ........ r55522 | mark.hammond | 2007-05-22 19:04:28 -0700 (Tue, 22 May 2007) | 4 lines Remove definition of PY_UNICODE_TYPE from pyconfig.h, allowing the definition in unicodeobject.h to be used, giving us the desired wchar_t in place of 'unsigned short'. As discussed on python-dev. ........ r55525 | neal.norwitz | 2007-05-22 23:35:32 -0700 (Tue, 22 May 2007) | 6 lines Add -3 option to the interpreter to warn about features that are deprecated and will be changed/removed in Python 3.0. This patch is mostly from Anthony. I tweaked some format and added a little doc. ........ r55527 | neal.norwitz | 2007-05-22 23:57:35 -0700 (Tue, 22 May 2007) | 1 line Whitespace cleanup ........ r55528 | neal.norwitz | 2007-05-22 23:58:36 -0700 (Tue, 22 May 2007) | 1 line Add a bunch more deprecation warnings for builtins that are going away in 3.0 ........ r55549 | georg.brandl | 2007-05-24 09:49:29 -0700 (Thu, 24 May 2007) | 2 lines shlex.split() now has an optional "posix" parameter. ........ r55550 | georg.brandl | 2007-05-24 10:33:33 -0700 (Thu, 24 May 2007) | 2 lines Fix parameter passing. ........ r55555 | facundo.batista | 2007-05-24 10:50:54 -0700 (Thu, 24 May 2007) | 6 lines Added an optional timeout parameter to urllib.ftpwrapper, with tests (for this and a basic one, because there weren't any). Changed also NEWS, but didn't find documentation for this function, assumed it wasn't public... ........ r55563 | facundo.batista | 2007-05-24 13:01:59 -0700 (Thu, 24 May 2007) | 4 lines Removed the .recv() in the test, is not necessary, and was causing problems that didn't have anything to do with was actually being tested... ........ r55564 | facundo.batista | 2007-05-24 13:51:19 -0700 (Thu, 24 May 2007) | 5 lines Let's see if reading exactly what is written allow this live test to pass (now I know why there were so few tests in ftp, http, etc, :( ). ........ r55567 | facundo.batista | 2007-05-24 20:10:28 -0700 (Thu, 24 May 2007) | 4 lines Trying to make the tests work in Windows and Solaris, everywhere else just works ........ r55568 | facundo.batista | 2007-05-24 20:47:19 -0700 (Thu, 24 May 2007) | 4 lines Fixing stupid error, and introducing a sleep, to see if the other thread is awakened and finish sending data. ........ r55569 | facundo.batista | 2007-05-24 21:20:22 -0700 (Thu, 24 May 2007) | 4 lines Commenting out the tests until find out who can test them in one of the problematic enviroments. ........ r55570 | neal.norwitz | 2007-05-24 22:13:40 -0700 (Thu, 24 May 2007) | 2 lines Get test passing again by commenting out the reference to the test class. ........ r55575 | vinay.sajip | 2007-05-25 00:05:59 -0700 (Fri, 25 May 2007) | 1 line Updated docstring for SysLogHandler (#1720726). ........ r55576 | vinay.sajip | 2007-05-25 00:06:55 -0700 (Fri, 25 May 2007) | 1 line Updated documentation for SysLogHandler (#1720726). ........ r55592 | brett.cannon | 2007-05-25 13:17:15 -0700 (Fri, 25 May 2007) | 3 lines Remove direct call's to file's constructor and replace them with calls to open() as ths is considered best practice. ........ r55601 | kristjan.jonsson | 2007-05-26 12:19:50 -0700 (Sat, 26 May 2007) | 1 line Remove the rgbimgmodule from PCBuild8 ........ r55602 | kristjan.jonsson | 2007-05-26 12:31:39 -0700 (Sat, 26 May 2007) | 1 line Include <windows.h> after python.h, so that WINNT is properly set before windows.h is included. Fixes warnings in PC builds. ........ r55603 | walter.doerwald | 2007-05-26 14:04:13 -0700 (Sat, 26 May 2007) | 2 lines Fix typo. ........ r55604 | peter.astrand | 2007-05-26 15:18:20 -0700 (Sat, 26 May 2007) | 1 line Applied patch 1669481, slightly modified: Support close_fds on Win32 ........ r55606 | neal.norwitz | 2007-05-26 21:08:54 -0700 (Sat, 26 May 2007) | 2 lines Add the new function object attribute names from py3k. ........ r55617 | lars.gustaebel | 2007-05-27 12:49:30 -0700 (Sun, 27 May 2007) | 20 lines Added errors argument to TarFile class that allows the user to specify an error handling scheme for character conversion. Additional scheme "utf-8" in read mode. Unicode input filenames are now supported by design. The values of the pax_headers dictionary are now limited to unicode objects. Fixed: The prefix field is no longer used in PAX_FORMAT (in conformance with POSIX). Fixed: In read mode use a possible pax header size field. Fixed: Strip trailing slashes from pax header name values. Fixed: Give values in user-specified pax_headers precedence when writing. Added unicode tests. Added pax/regtype4 member to testtar.tar all possible number fields in a pax header. Added two chapters to the documentation about the different formats tarfile.py supports and how unicode issues are handled. ........ r55618 | raymond.hettinger | 2007-05-27 22:23:22 -0700 (Sun, 27 May 2007) | 1 line Explain when groupby() issues a new group. ........ r55634 | martin.v.loewis | 2007-05-28 21:01:29 -0700 (Mon, 28 May 2007) | 2 lines Test pre-commit hook for a link to a .py file. ........ r55635 | martin.v.loewis | 2007-05-28 21:02:03 -0700 (Mon, 28 May 2007) | 2 lines Revert 55634. ........ ................ r55639 | neal.norwitz | 2007-05-29 00:58:11 -0700 (Tue, 29 May 2007) | 1 line Remove sys.exc_{type,exc_value,exc_traceback} ................ r55641 | neal.norwitz | 2007-05-29 01:03:50 -0700 (Tue, 29 May 2007) | 1 line Missed one sys.exc_type. I wonder why exc_{value,traceback} were already gone ................ r55642 | neal.norwitz | 2007-05-29 01:08:33 -0700 (Tue, 29 May 2007) | 1 line Missed more doc for sys.exc_* attrs. ................ r55643 | neal.norwitz | 2007-05-29 01:18:19 -0700 (Tue, 29 May 2007) | 1 line Remove sys.exc_clear() ................ r55665 | guido.van.rossum | 2007-05-29 19:45:43 -0700 (Tue, 29 May 2007) | 4 lines Make None, True, False keywords. We can now also delete all the other places that explicitly forbid assignment to None, but I'm not going to bother right now. ................ r55666 | guido.van.rossum | 2007-05-29 20:01:51 -0700 (Tue, 29 May 2007) | 3 lines Found another place that needs check for forbidden names. Fixed test_syntax.py accordingly (it helped me find that one). ................ r55668 | guido.van.rossum | 2007-05-29 20:41:48 -0700 (Tue, 29 May 2007) | 2 lines Mark None, True, False as keywords. ................ r55673 | neal.norwitz | 2007-05-29 23:28:25 -0700 (Tue, 29 May 2007) | 3 lines Get the dis module working on modules again after changing dicts to not return lists and also new-style classes. Add a test. ................ r55674 | neal.norwitz | 2007-05-29 23:35:45 -0700 (Tue, 29 May 2007) | 1 line Umm, it helps to add the module that the test uses ................ r55675 | neal.norwitz | 2007-05-29 23:53:05 -0700 (Tue, 29 May 2007) | 4 lines Try to fix up all the other places that were assigning to True/False. There's at least one more problem in test.test_xmlrpc. I have other changes in that file and that should be fixed soon (I hope). ................ r55679 | neal.norwitz | 2007-05-30 00:31:55 -0700 (Wed, 30 May 2007) | 1 line Fix up another place that was assigning to True/False. ................ r55688 | brett.cannon | 2007-05-30 14:19:47 -0700 (Wed, 30 May 2007) | 2 lines Ditch MimeWriter. ................ r55692 | brett.cannon | 2007-05-30 14:52:00 -0700 (Wed, 30 May 2007) | 2 lines Remove the mimify module. ................ r55707 | guido.van.rossum | 2007-05-31 05:08:45 -0700 (Thu, 31 May 2007) | 2 lines Backport the addition of show_code() to dis.py -- it's too handy. ................ r55708 | guido.van.rossum | 2007-05-31 06:22:57 -0700 (Thu, 31 May 2007) | 7 lines Fix a fairly long-standing bug in the check for assignment to None (and other keywords, these days). In 2.5, you could write foo(None=1) without getting a SyntaxError (although foo()'s definition would have to use **kwds to avoid getting a runtime error complaining about an unknown keyword of course). This ought to be backported to 2.5.2 or at least 2.6. ................ r55724 | brett.cannon | 2007-05-31 19:32:41 -0700 (Thu, 31 May 2007) | 2 lines Remove the cfmfile. ................ r55727 | neal.norwitz | 2007-05-31 22:19:44 -0700 (Thu, 31 May 2007) | 1 line Remove reload() builtin. ................ r55729 | neal.norwitz | 2007-05-31 22:51:30 -0700 (Thu, 31 May 2007) | 59 lines Merged revisions 55636-55728 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r55637 | georg.brandl | 2007-05-29 00:16:47 -0700 (Tue, 29 May 2007) | 2 lines Fix rst markup. ........ r55638 | neal.norwitz | 2007-05-29 00:51:39 -0700 (Tue, 29 May 2007) | 1 line Fix typo in doc ........ r55671 | neal.norwitz | 2007-05-29 21:53:41 -0700 (Tue, 29 May 2007) | 1 line Fix indentation (whitespace only). ........ r55676 | thomas.heller | 2007-05-29 23:58:30 -0700 (Tue, 29 May 2007) | 1 line Fix compiler warnings. ........ r55677 | thomas.heller | 2007-05-30 00:01:25 -0700 (Wed, 30 May 2007) | 2 lines Correct the name of a field in the WIN32_FIND_DATAA and WIN32_FIND_DATAW structures. Closes bug #1726026. ........ r55686 | brett.cannon | 2007-05-30 13:46:26 -0700 (Wed, 30 May 2007) | 2 lines Have MimeWriter raise a DeprecationWarning as per PEP 4 and its documentation. ........ r55690 | brett.cannon | 2007-05-30 14:48:58 -0700 (Wed, 30 May 2007) | 3 lines Have mimify raise a DeprecationWarning. The docs and PEP 4 have listed the module as deprecated for a while. ........ r55696 | brett.cannon | 2007-05-30 15:24:28 -0700 (Wed, 30 May 2007) | 2 lines Have md5 raise a DeprecationWarning as per PEP 4. ........ r55705 | neal.norwitz | 2007-05-30 21:14:22 -0700 (Wed, 30 May 2007) | 1 line Add some spaces in the example code. ........ r55716 | brett.cannon | 2007-05-31 12:20:00 -0700 (Thu, 31 May 2007) | 2 lines Have the sha module raise a DeprecationWarning as specified in PEP 4. ........ r55719 | brett.cannon | 2007-05-31 12:40:42 -0700 (Thu, 31 May 2007) | 2 lines Cause buildtools to raise a DeprecationWarning. ........ r55721 | brett.cannon | 2007-05-31 13:01:11 -0700 (Thu, 31 May 2007) | 2 lines Have cfmfile raise a DeprecationWarning as per PEP 4. ........ r55726 | neal.norwitz | 2007-05-31 21:56:47 -0700 (Thu, 31 May 2007) | 1 line Mail if there is an installation failure. ........ ................ r55730 | neal.norwitz | 2007-05-31 23:22:07 -0700 (Thu, 31 May 2007) | 2 lines Remove the code that was missed in rev 55303. ................ r55738 | neal.norwitz | 2007-06-01 19:10:43 -0700 (Fri, 01 Jun 2007) | 1 line Fix doc breakage ................ r55741 | neal.norwitz | 2007-06-02 00:41:58 -0700 (Sat, 02 Jun 2007) | 1 line Remove timing module (plus some remnants of other modules). ................ r55742 | neal.norwitz | 2007-06-02 00:51:44 -0700 (Sat, 02 Jun 2007) | 1 line Remove posixfile module (plus some remnants of other modules). ................ r55744 | neal.norwitz | 2007-06-02 10:18:56 -0700 (Sat, 02 Jun 2007) | 1 line Fix doc breakage. ................ r55745 | neal.norwitz | 2007-06-02 11:32:16 -0700 (Sat, 02 Jun 2007) | 1 line Make a whatsnew 3.0 template. ................ r55754 | neal.norwitz | 2007-06-03 23:24:18 -0700 (Sun, 03 Jun 2007) | 1 line SF #1730441, os._execvpe raises UnboundLocal due to new try/except semantics ................ r55755 | neal.norwitz | 2007-06-03 23:26:00 -0700 (Sun, 03 Jun 2007) | 1 line Get rid of extra whitespace ................ r55794 | guido.van.rossum | 2007-06-06 15:29:22 -0700 (Wed, 06 Jun 2007) | 3 lines Make this compile in GCC 2.96, which does not allow interspersing declarations and code. ................
19 years ago
Merged revisions 59843-59863 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59844 | raymond.hettinger | 2008-01-07 21:56:05 +0100 (Mon, 07 Jan 2008) | 1 line Use get() instead of pop() for the optimized version of _replace(). ........ r59847 | raymond.hettinger | 2008-01-07 22:33:51 +0100 (Mon, 07 Jan 2008) | 1 line Documentation nits. ........ r59849 | raymond.hettinger | 2008-01-08 03:02:05 +0100 (Tue, 08 Jan 2008) | 1 line Expand comment. ........ r59850 | raymond.hettinger | 2008-01-08 03:24:15 +0100 (Tue, 08 Jan 2008) | 1 line Docs on named tuple's naming conventions and limits of subclassing ........ r59851 | christian.heimes | 2008-01-08 04:40:04 +0100 (Tue, 08 Jan 2008) | 1 line It's verbose, not debug ........ r59852 | facundo.batista | 2008-01-08 13:25:20 +0100 (Tue, 08 Jan 2008) | 4 lines Issue #1757: The hash of a Decimal instance is no longer affected by the current context. Thanks Mark Dickinson. ........ r59853 | andrew.kuchling | 2008-01-08 15:30:55 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1137: allow assigning to .buffer_size attribute of PyExpat.parser objects ........ r59854 | andrew.kuchling | 2008-01-08 15:56:02 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1114: fix compilation of curses module on 64-bit AIX, and any other LP64 platforms where attr_t isn't a C long ........ r59856 | thomas.heller | 2008-01-08 16:15:09 +0100 (Tue, 08 Jan 2008) | 5 lines Use relative instead of absolute filenames in the C-level tracebacks. This prevents traceback prints pointing to files in this way: File "\loewis\25\python\Modules\_ctypes\callbacks.c", line 206, in 'calling callback function' ........ r59857 | christian.heimes | 2008-01-08 16:46:10 +0100 (Tue, 08 Jan 2008) | 2 lines Added __enter__ and __exit__ functions to HKEY object Added ExpandEnvironmentStrings to the _winreg module. ........ r59858 | georg.brandl | 2008-01-08 17:18:26 +0100 (Tue, 08 Jan 2008) | 2 lines Fix markup errors from r59857 and clarify key.__enter__/__exit__ docs ........ r59860 | georg.brandl | 2008-01-08 20:42:30 +0100 (Tue, 08 Jan 2008) | 2 lines Better method for associating .py files with the interpreter. ........ r59862 | facundo.batista | 2008-01-08 22:10:12 +0100 (Tue, 08 Jan 2008) | 9 lines Issue 846388. Adds a call to PyErr_CheckSignals to SRE_MATCH so that signal handlers can be invoked during long regular expression matches. It also adds a new error return value indicating that an exception occurred in a signal handler during the match, allowing exceptions in the signal handler to propagate up to the main loop. Thanks Josh Hoyt and Ralf Schmitt. ........
19 years ago
Merged revisions 59843-59863 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59844 | raymond.hettinger | 2008-01-07 21:56:05 +0100 (Mon, 07 Jan 2008) | 1 line Use get() instead of pop() for the optimized version of _replace(). ........ r59847 | raymond.hettinger | 2008-01-07 22:33:51 +0100 (Mon, 07 Jan 2008) | 1 line Documentation nits. ........ r59849 | raymond.hettinger | 2008-01-08 03:02:05 +0100 (Tue, 08 Jan 2008) | 1 line Expand comment. ........ r59850 | raymond.hettinger | 2008-01-08 03:24:15 +0100 (Tue, 08 Jan 2008) | 1 line Docs on named tuple's naming conventions and limits of subclassing ........ r59851 | christian.heimes | 2008-01-08 04:40:04 +0100 (Tue, 08 Jan 2008) | 1 line It's verbose, not debug ........ r59852 | facundo.batista | 2008-01-08 13:25:20 +0100 (Tue, 08 Jan 2008) | 4 lines Issue #1757: The hash of a Decimal instance is no longer affected by the current context. Thanks Mark Dickinson. ........ r59853 | andrew.kuchling | 2008-01-08 15:30:55 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1137: allow assigning to .buffer_size attribute of PyExpat.parser objects ........ r59854 | andrew.kuchling | 2008-01-08 15:56:02 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1114: fix compilation of curses module on 64-bit AIX, and any other LP64 platforms where attr_t isn't a C long ........ r59856 | thomas.heller | 2008-01-08 16:15:09 +0100 (Tue, 08 Jan 2008) | 5 lines Use relative instead of absolute filenames in the C-level tracebacks. This prevents traceback prints pointing to files in this way: File "\loewis\25\python\Modules\_ctypes\callbacks.c", line 206, in 'calling callback function' ........ r59857 | christian.heimes | 2008-01-08 16:46:10 +0100 (Tue, 08 Jan 2008) | 2 lines Added __enter__ and __exit__ functions to HKEY object Added ExpandEnvironmentStrings to the _winreg module. ........ r59858 | georg.brandl | 2008-01-08 17:18:26 +0100 (Tue, 08 Jan 2008) | 2 lines Fix markup errors from r59857 and clarify key.__enter__/__exit__ docs ........ r59860 | georg.brandl | 2008-01-08 20:42:30 +0100 (Tue, 08 Jan 2008) | 2 lines Better method for associating .py files with the interpreter. ........ r59862 | facundo.batista | 2008-01-08 22:10:12 +0100 (Tue, 08 Jan 2008) | 9 lines Issue 846388. Adds a call to PyErr_CheckSignals to SRE_MATCH so that signal handlers can be invoked during long regular expression matches. It also adds a new error return value indicating that an exception occurred in a signal handler during the match, allowing exceptions in the signal handler to propagate up to the main loop. Thanks Josh Hoyt and Ralf Schmitt. ........
19 years ago
Merged revisions 59843-59863 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59844 | raymond.hettinger | 2008-01-07 21:56:05 +0100 (Mon, 07 Jan 2008) | 1 line Use get() instead of pop() for the optimized version of _replace(). ........ r59847 | raymond.hettinger | 2008-01-07 22:33:51 +0100 (Mon, 07 Jan 2008) | 1 line Documentation nits. ........ r59849 | raymond.hettinger | 2008-01-08 03:02:05 +0100 (Tue, 08 Jan 2008) | 1 line Expand comment. ........ r59850 | raymond.hettinger | 2008-01-08 03:24:15 +0100 (Tue, 08 Jan 2008) | 1 line Docs on named tuple's naming conventions and limits of subclassing ........ r59851 | christian.heimes | 2008-01-08 04:40:04 +0100 (Tue, 08 Jan 2008) | 1 line It's verbose, not debug ........ r59852 | facundo.batista | 2008-01-08 13:25:20 +0100 (Tue, 08 Jan 2008) | 4 lines Issue #1757: The hash of a Decimal instance is no longer affected by the current context. Thanks Mark Dickinson. ........ r59853 | andrew.kuchling | 2008-01-08 15:30:55 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1137: allow assigning to .buffer_size attribute of PyExpat.parser objects ........ r59854 | andrew.kuchling | 2008-01-08 15:56:02 +0100 (Tue, 08 Jan 2008) | 1 line Patch 1114: fix compilation of curses module on 64-bit AIX, and any other LP64 platforms where attr_t isn't a C long ........ r59856 | thomas.heller | 2008-01-08 16:15:09 +0100 (Tue, 08 Jan 2008) | 5 lines Use relative instead of absolute filenames in the C-level tracebacks. This prevents traceback prints pointing to files in this way: File "\loewis\25\python\Modules\_ctypes\callbacks.c", line 206, in 'calling callback function' ........ r59857 | christian.heimes | 2008-01-08 16:46:10 +0100 (Tue, 08 Jan 2008) | 2 lines Added __enter__ and __exit__ functions to HKEY object Added ExpandEnvironmentStrings to the _winreg module. ........ r59858 | georg.brandl | 2008-01-08 17:18:26 +0100 (Tue, 08 Jan 2008) | 2 lines Fix markup errors from r59857 and clarify key.__enter__/__exit__ docs ........ r59860 | georg.brandl | 2008-01-08 20:42:30 +0100 (Tue, 08 Jan 2008) | 2 lines Better method for associating .py files with the interpreter. ........ r59862 | facundo.batista | 2008-01-08 22:10:12 +0100 (Tue, 08 Jan 2008) | 9 lines Issue 846388. Adds a call to PyErr_CheckSignals to SRE_MATCH so that signal handlers can be invoked during long regular expression matches. It also adds a new error return value indicating that an exception occurred in a signal handler during the match, allowing exceptions in the signal handler to propagate up to the main loop. Thanks Josh Hoyt and Ralf Schmitt. ........
19 years ago
  1. /*
  2. winreg.c
  3. Windows Registry access module for Python.
  4. * Simple registry access written by Mark Hammond in win32api
  5. module circa 1995.
  6. * Bill Tutt expanded the support significantly not long after.
  7. * Numerous other people have submitted patches since then.
  8. * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
  9. basic Unicode support added.
  10. */
  11. #define PY_SSIZE_T_CLEAN
  12. #include "Python.h"
  13. #include "structmember.h"
  14. #include "windows.h"
  15. static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
  16. static BOOL clinic_HKEY_converter(PyObject *ob, void *p);
  17. static PyObject *PyHKEY_FromHKEY(HKEY h);
  18. static BOOL PyHKEY_Close(PyObject *obHandle);
  19. static char errNotAHandle[] = "Object is not a handle";
  20. /* The win32api module reports the function name that failed,
  21. but this concept is not in the Python core.
  22. Hopefully it will one day, and in the meantime I don't
  23. want to lose this info...
  24. */
  25. #define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
  26. PyErr_SetFromWindowsErr(rc)
  27. /* Forward declares */
  28. /* Doc strings */
  29. PyDoc_STRVAR(module_doc,
  30. "This module provides access to the Windows registry API.\n"
  31. "\n"
  32. "Functions:\n"
  33. "\n"
  34. "CloseKey() - Closes a registry key.\n"
  35. "ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
  36. " on another computer.\n"
  37. "CreateKey() - Creates the specified key, or opens it if it already exists.\n"
  38. "DeleteKey() - Deletes the specified key.\n"
  39. "DeleteValue() - Removes a named value from the specified registry key.\n"
  40. "EnumKey() - Enumerates subkeys of the specified open registry key.\n"
  41. "EnumValue() - Enumerates values of the specified open registry key.\n"
  42. "ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n"
  43. " string.\n"
  44. "FlushKey() - Writes all the attributes of the specified key to the registry.\n"
  45. "LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n"
  46. " stores registration information from a specified file into that\n"
  47. " subkey.\n"
  48. "OpenKey() - Opens the specified key.\n"
  49. "OpenKeyEx() - Alias of OpenKey().\n"
  50. "QueryValue() - Retrieves the value associated with the unnamed value for a\n"
  51. " specified key in the registry.\n"
  52. "QueryValueEx() - Retrieves the type and data for a specified value name\n"
  53. " associated with an open registry key.\n"
  54. "QueryInfoKey() - Returns information about the specified key.\n"
  55. "SaveKey() - Saves the specified key, and all its subkeys a file.\n"
  56. "SetValue() - Associates a value with a specified key.\n"
  57. "SetValueEx() - Stores data in the value field of an open registry key.\n"
  58. "\n"
  59. "Special objects:\n"
  60. "\n"
  61. "HKEYType -- type object for HKEY objects\n"
  62. "error -- exception raised for Win32 errors\n"
  63. "\n"
  64. "Integer constants:\n"
  65. "Many constants are defined - see the documentation for each function\n"
  66. "to see what constants are used, and where.");
  67. /* PyHKEY docstrings */
  68. PyDoc_STRVAR(PyHKEY_doc,
  69. "PyHKEY Object - A Python object, representing a win32 registry key.\n"
  70. "\n"
  71. "This object wraps a Windows HKEY object, automatically closing it when\n"
  72. "the object is destroyed. To guarantee cleanup, you can call either\n"
  73. "the Close() method on the PyHKEY, or the CloseKey() method.\n"
  74. "\n"
  75. "All functions which accept a handle object also accept an integer --\n"
  76. "however, use of the handle object is encouraged.\n"
  77. "\n"
  78. "Functions:\n"
  79. "Close() - Closes the underlying handle.\n"
  80. "Detach() - Returns the integer Win32 handle, detaching it from the object\n"
  81. "\n"
  82. "Properties:\n"
  83. "handle - The integer Win32 handle.\n"
  84. "\n"
  85. "Operations:\n"
  86. "__bool__ - Handles with an open object return true, otherwise false.\n"
  87. "__int__ - Converting a handle to an integer returns the Win32 handle.\n"
  88. "rich comparison - Handle objects are compared using the handle value.");
  89. /************************************************************************
  90. The PyHKEY object definition
  91. ************************************************************************/
  92. typedef struct {
  93. PyObject_VAR_HEAD
  94. HKEY hkey;
  95. } PyHKEYObject;
  96. #define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
  97. static char *failMsg = "bad operand type";
  98. static PyObject *
  99. PyHKEY_unaryFailureFunc(PyObject *ob)
  100. {
  101. PyErr_SetString(PyExc_TypeError, failMsg);
  102. return NULL;
  103. }
  104. static PyObject *
  105. PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
  106. {
  107. PyErr_SetString(PyExc_TypeError, failMsg);
  108. return NULL;
  109. }
  110. static PyObject *
  111. PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
  112. {
  113. PyErr_SetString(PyExc_TypeError, failMsg);
  114. return NULL;
  115. }
  116. static void
  117. PyHKEY_deallocFunc(PyObject *ob)
  118. {
  119. /* Can not call PyHKEY_Close, as the ob->tp_type
  120. has already been cleared, thus causing the type
  121. check to fail!
  122. */
  123. PyHKEYObject *obkey = (PyHKEYObject *)ob;
  124. if (obkey->hkey)
  125. RegCloseKey((HKEY)obkey->hkey);
  126. PyObject_DEL(ob);
  127. }
  128. static int
  129. PyHKEY_boolFunc(PyObject *ob)
  130. {
  131. return ((PyHKEYObject *)ob)->hkey != 0;
  132. }
  133. static PyObject *
  134. PyHKEY_intFunc(PyObject *ob)
  135. {
  136. PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
  137. return PyLong_FromVoidPtr(pyhkey->hkey);
  138. }
  139. static PyObject *
  140. PyHKEY_strFunc(PyObject *ob)
  141. {
  142. PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
  143. return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
  144. }
  145. static int
  146. PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
  147. {
  148. PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
  149. PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
  150. return pyhkey1 == pyhkey2 ? 0 :
  151. (pyhkey1 < pyhkey2 ? -1 : 1);
  152. }
  153. static Py_hash_t
  154. PyHKEY_hashFunc(PyObject *ob)
  155. {
  156. /* Just use the address.
  157. XXX - should we use the handle value?
  158. */
  159. return _Py_HashPointer(ob);
  160. }
  161. static PyNumberMethods PyHKEY_NumberMethods =
  162. {
  163. PyHKEY_binaryFailureFunc, /* nb_add */
  164. PyHKEY_binaryFailureFunc, /* nb_subtract */
  165. PyHKEY_binaryFailureFunc, /* nb_multiply */
  166. PyHKEY_binaryFailureFunc, /* nb_remainder */
  167. PyHKEY_binaryFailureFunc, /* nb_divmod */
  168. PyHKEY_ternaryFailureFunc, /* nb_power */
  169. PyHKEY_unaryFailureFunc, /* nb_negative */
  170. PyHKEY_unaryFailureFunc, /* nb_positive */
  171. PyHKEY_unaryFailureFunc, /* nb_absolute */
  172. PyHKEY_boolFunc, /* nb_bool */
  173. PyHKEY_unaryFailureFunc, /* nb_invert */
  174. PyHKEY_binaryFailureFunc, /* nb_lshift */
  175. PyHKEY_binaryFailureFunc, /* nb_rshift */
  176. PyHKEY_binaryFailureFunc, /* nb_and */
  177. PyHKEY_binaryFailureFunc, /* nb_xor */
  178. PyHKEY_binaryFailureFunc, /* nb_or */
  179. PyHKEY_intFunc, /* nb_int */
  180. 0, /* nb_reserved */
  181. PyHKEY_unaryFailureFunc, /* nb_float */
  182. };
  183. /*[clinic input]
  184. module winreg
  185. class winreg.HKEYType "PyHKEYObject *" "&PyHKEY_Type"
  186. [clinic start generated code]*/
  187. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4c964eba3bf914d6]*/
  188. /*[python input]
  189. class REGSAM_converter(CConverter):
  190. type = 'REGSAM'
  191. format_unit = 'i'
  192. class DWORD_converter(CConverter):
  193. type = 'DWORD'
  194. format_unit = 'k'
  195. class HKEY_converter(CConverter):
  196. type = 'HKEY'
  197. converter = 'clinic_HKEY_converter'
  198. class HKEY_return_converter(CReturnConverter):
  199. type = 'HKEY'
  200. def render(self, function, data):
  201. self.declare(data)
  202. self.err_occurred_if_null_pointer("_return_value", data)
  203. data.return_conversion.append(
  204. 'return_value = PyHKEY_FromHKEY(_return_value);\n')
  205. # HACK: this only works for PyHKEYObjects, nothing else.
  206. # Should this be generalized and enshrined in clinic.py,
  207. # destroy this converter with prejudice.
  208. class self_return_converter(CReturnConverter):
  209. type = 'PyHKEYObject *'
  210. def render(self, function, data):
  211. self.declare(data)
  212. data.return_conversion.append(
  213. 'return_value = (PyObject *)_return_value;\n')
  214. [python start generated code]*/
  215. /*[python end generated code: output=da39a3ee5e6b4b0d input=22f7aedc6d68e80e]*/
  216. #include "clinic/winreg.c.h"
  217. /************************************************************************
  218. The PyHKEY object methods
  219. ************************************************************************/
  220. /*[clinic input]
  221. winreg.HKEYType.Close
  222. Closes the underlying Windows handle.
  223. If the handle is already closed, no error is raised.
  224. [clinic start generated code]*/
  225. static PyObject *
  226. winreg_HKEYType_Close_impl(PyHKEYObject *self)
  227. /*[clinic end generated code: output=fced3a624fb0c344 input=6786ac75f6b89de6]*/
  228. {
  229. if (!PyHKEY_Close((PyObject *)self))
  230. return NULL;
  231. Py_RETURN_NONE;
  232. }
  233. /*[clinic input]
  234. winreg.HKEYType.Detach
  235. Detaches the Windows handle from the handle object.
  236. The result is the value of the handle before it is detached. If the
  237. handle is already detached, this will return zero.
  238. After calling this function, the handle is effectively invalidated,
  239. but the handle is not closed. You would call this function when you
  240. need the underlying win32 handle to exist beyond the lifetime of the
  241. handle object.
  242. [clinic start generated code]*/
  243. static PyObject *
  244. winreg_HKEYType_Detach_impl(PyHKEYObject *self)
  245. /*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/
  246. {
  247. void* ret;
  248. if (PySys_Audit("winreg.PyHKEY.Detach", "n", (Py_ssize_t)self->hkey) < 0) {
  249. return NULL;
  250. }
  251. ret = (void*)self->hkey;
  252. self->hkey = 0;
  253. return PyLong_FromVoidPtr(ret);
  254. }
  255. /*[clinic input]
  256. winreg.HKEYType.__enter__ -> self
  257. [clinic start generated code]*/
  258. static PyHKEYObject *
  259. winreg_HKEYType___enter___impl(PyHKEYObject *self)
  260. /*[clinic end generated code: output=52c34986dab28990 input=c40fab1f0690a8e2]*/
  261. {
  262. Py_XINCREF(self);
  263. return self;
  264. }
  265. /*[clinic input]
  266. winreg.HKEYType.__exit__
  267. exc_type: object
  268. exc_value: object
  269. traceback: object
  270. [clinic start generated code]*/
  271. static PyObject *
  272. winreg_HKEYType___exit___impl(PyHKEYObject *self, PyObject *exc_type,
  273. PyObject *exc_value, PyObject *traceback)
  274. /*[clinic end generated code: output=923ebe7389e6a263 input=fb32489ee92403c7]*/
  275. {
  276. if (!PyHKEY_Close((PyObject *)self))
  277. return NULL;
  278. Py_RETURN_NONE;
  279. }
  280. /*[clinic input]
  281. [clinic start generated code]*/
  282. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
  283. static struct PyMethodDef PyHKEY_methods[] = {
  284. WINREG_HKEYTYPE_CLOSE_METHODDEF
  285. WINREG_HKEYTYPE_DETACH_METHODDEF
  286. WINREG_HKEYTYPE___ENTER___METHODDEF
  287. WINREG_HKEYTYPE___EXIT___METHODDEF
  288. {NULL}
  289. };
  290. #define OFF(e) offsetof(PyHKEYObject, e)
  291. static PyMemberDef PyHKEY_memberlist[] = {
  292. {"handle", T_INT, OFF(hkey), READONLY},
  293. {NULL} /* Sentinel */
  294. };
  295. /* The type itself */
  296. PyTypeObject PyHKEY_Type =
  297. {
  298. PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
  299. "PyHKEY",
  300. sizeof(PyHKEYObject),
  301. 0,
  302. PyHKEY_deallocFunc, /* tp_dealloc */
  303. 0, /* tp_vectorcall_offset */
  304. 0, /* tp_getattr */
  305. 0, /* tp_setattr */
  306. 0, /* tp_as_async */
  307. 0, /* tp_repr */
  308. &PyHKEY_NumberMethods, /* tp_as_number */
  309. 0, /* tp_as_sequence */
  310. 0, /* tp_as_mapping */
  311. PyHKEY_hashFunc, /* tp_hash */
  312. 0, /* tp_call */
  313. PyHKEY_strFunc, /* tp_str */
  314. 0, /* tp_getattro */
  315. 0, /* tp_setattro */
  316. 0, /* tp_as_buffer */
  317. 0, /* tp_flags */
  318. PyHKEY_doc, /* tp_doc */
  319. 0, /*tp_traverse*/
  320. 0, /*tp_clear*/
  321. 0, /*tp_richcompare*/
  322. 0, /*tp_weaklistoffset*/
  323. 0, /*tp_iter*/
  324. 0, /*tp_iternext*/
  325. PyHKEY_methods, /*tp_methods*/
  326. PyHKEY_memberlist, /*tp_members*/
  327. };
  328. /************************************************************************
  329. The public PyHKEY API (well, not public yet :-)
  330. ************************************************************************/
  331. PyObject *
  332. PyHKEY_New(HKEY hInit)
  333. {
  334. PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
  335. if (key)
  336. key->hkey = hInit;
  337. return (PyObject *)key;
  338. }
  339. BOOL
  340. PyHKEY_Close(PyObject *ob_handle)
  341. {
  342. LONG rc;
  343. HKEY key;
  344. if (!PyHKEY_AsHKEY(ob_handle, &key, TRUE)) {
  345. return FALSE;
  346. }
  347. if (PyHKEY_Check(ob_handle)) {
  348. ((PyHKEYObject*)ob_handle)->hkey = 0;
  349. }
  350. rc = key ? RegCloseKey(key) : ERROR_SUCCESS;
  351. if (rc != ERROR_SUCCESS)
  352. PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
  353. return rc == ERROR_SUCCESS;
  354. }
  355. BOOL
  356. PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
  357. {
  358. if (ob == Py_None) {
  359. if (!bNoneOK) {
  360. PyErr_SetString(
  361. PyExc_TypeError,
  362. "None is not a valid HKEY in this context");
  363. return FALSE;
  364. }
  365. *pHANDLE = (HKEY)0;
  366. }
  367. else if (PyHKEY_Check(ob)) {
  368. PyHKEYObject *pH = (PyHKEYObject *)ob;
  369. *pHANDLE = pH->hkey;
  370. }
  371. else if (PyLong_Check(ob)) {
  372. /* We also support integers */
  373. PyErr_Clear();
  374. *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
  375. if (PyErr_Occurred())
  376. return FALSE;
  377. }
  378. else {
  379. PyErr_SetString(
  380. PyExc_TypeError,
  381. "The object is not a PyHKEY object");
  382. return FALSE;
  383. }
  384. return TRUE;
  385. }
  386. BOOL
  387. clinic_HKEY_converter(PyObject *ob, void *p)
  388. {
  389. if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE))
  390. return FALSE;
  391. return TRUE;
  392. }
  393. PyObject *
  394. PyHKEY_FromHKEY(HKEY h)
  395. {
  396. PyHKEYObject *op;
  397. /* Inline PyObject_New */
  398. op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
  399. if (op == NULL)
  400. return PyErr_NoMemory();
  401. PyObject_INIT(op, &PyHKEY_Type);
  402. op->hkey = h;
  403. return (PyObject *)op;
  404. }
  405. /************************************************************************
  406. The module methods
  407. ************************************************************************/
  408. BOOL
  409. PyWinObject_CloseHKEY(PyObject *obHandle)
  410. {
  411. BOOL ok;
  412. if (PyHKEY_Check(obHandle)) {
  413. ok = PyHKEY_Close(obHandle);
  414. }
  415. #if SIZEOF_LONG >= SIZEOF_HKEY
  416. else if (PyLong_Check(obHandle)) {
  417. long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
  418. ok = (rc == ERROR_SUCCESS);
  419. if (!ok)
  420. PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
  421. }
  422. #else
  423. else if (PyLong_Check(obHandle)) {
  424. long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
  425. ok = (rc == ERROR_SUCCESS);
  426. if (!ok)
  427. PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
  428. }
  429. #endif
  430. else {
  431. PyErr_SetString(
  432. PyExc_TypeError,
  433. "A handle must be a HKEY object or an integer");
  434. return FALSE;
  435. }
  436. return ok;
  437. }
  438. /*
  439. Private Helper functions for the registry interfaces
  440. ** Note that fixupMultiSZ and countString have both had changes
  441. ** made to support "incorrect strings". The registry specification
  442. ** calls for strings to be terminated with 2 null bytes. It seems
  443. ** some commercial packages install strings which don't conform,
  444. ** causing this code to fail - however, "regedit" etc still work
  445. ** with these strings (ie only we don't!).
  446. */
  447. static void
  448. fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
  449. {
  450. wchar_t *P;
  451. int i;
  452. wchar_t *Q;
  453. if (len > 0 && data[len - 1] == '\0') {
  454. Q = data + len - 1;
  455. }
  456. else {
  457. Q = data + len;
  458. }
  459. for (P = data, i = 0; P < Q; P++, i++) {
  460. str[i] = P;
  461. for (; P < Q && *P != '\0'; P++) {
  462. ;
  463. }
  464. }
  465. }
  466. static int
  467. countStrings(wchar_t *data, int len)
  468. {
  469. int strings;
  470. wchar_t *P, *Q;
  471. if (len > 0 && data[len - 1] == '\0') {
  472. Q = data + len - 1;
  473. }
  474. else {
  475. Q = data + len;
  476. }
  477. for (P = data, strings = 0; P < Q; P++, strings++) {
  478. for (; P < Q && *P != '\0'; P++) {
  479. ;
  480. }
  481. }
  482. return strings;
  483. }
  484. /* Convert PyObject into Registry data.
  485. Allocates space as needed. */
  486. static BOOL
  487. Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
  488. {
  489. Py_ssize_t i,j;
  490. switch (typ) {
  491. case REG_DWORD:
  492. if (value != Py_None && !PyLong_Check(value))
  493. return FALSE;
  494. *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
  495. if (*retDataBuf == NULL){
  496. PyErr_NoMemory();
  497. return FALSE;
  498. }
  499. *retDataSize = sizeof(DWORD);
  500. if (value == Py_None) {
  501. DWORD zero = 0;
  502. memcpy(*retDataBuf, &zero, sizeof(DWORD));
  503. }
  504. else {
  505. DWORD d = PyLong_AsUnsignedLong(value);
  506. memcpy(*retDataBuf, &d, sizeof(DWORD));
  507. }
  508. break;
  509. case REG_QWORD:
  510. if (value != Py_None && !PyLong_Check(value))
  511. return FALSE;
  512. *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1);
  513. if (*retDataBuf == NULL){
  514. PyErr_NoMemory();
  515. return FALSE;
  516. }
  517. *retDataSize = sizeof(DWORD64);
  518. if (value == Py_None) {
  519. DWORD64 zero = 0;
  520. memcpy(*retDataBuf, &zero, sizeof(DWORD64));
  521. }
  522. else {
  523. DWORD64 d = PyLong_AsUnsignedLongLong(value);
  524. memcpy(*retDataBuf, &d, sizeof(DWORD64));
  525. }
  526. break;
  527. case REG_SZ:
  528. case REG_EXPAND_SZ:
  529. {
  530. if (value != Py_None) {
  531. Py_ssize_t len;
  532. if (!PyUnicode_Check(value))
  533. return FALSE;
  534. *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
  535. if (*retDataBuf == NULL)
  536. return FALSE;
  537. *retDataSize = Py_SAFE_DOWNCAST(
  538. (len + 1) * sizeof(wchar_t),
  539. Py_ssize_t, DWORD);
  540. }
  541. else {
  542. *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
  543. if (*retDataBuf == NULL) {
  544. PyErr_NoMemory();
  545. return FALSE;
  546. }
  547. ((wchar_t *)*retDataBuf)[0] = L'\0';
  548. *retDataSize = 1 * sizeof(wchar_t);
  549. }
  550. break;
  551. }
  552. case REG_MULTI_SZ:
  553. {
  554. DWORD size = 0;
  555. wchar_t *P;
  556. if (value == Py_None)
  557. i = 0;
  558. else {
  559. if (!PyList_Check(value))
  560. return FALSE;
  561. i = PyList_Size(value);
  562. }
  563. for (j = 0; j < i; j++)
  564. {
  565. PyObject *t;
  566. wchar_t *wstr;
  567. Py_ssize_t len;
  568. t = PyList_GET_ITEM(value, j);
  569. if (!PyUnicode_Check(t))
  570. return FALSE;
  571. wstr = PyUnicode_AsUnicodeAndSize(t, &len);
  572. if (wstr == NULL)
  573. return FALSE;
  574. size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
  575. size_t, DWORD);
  576. }
  577. *retDataSize = size + 2;
  578. *retDataBuf = (BYTE *)PyMem_NEW(char,
  579. *retDataSize);
  580. if (*retDataBuf == NULL){
  581. PyErr_NoMemory();
  582. return FALSE;
  583. }
  584. P = (wchar_t *)*retDataBuf;
  585. for (j = 0; j < i; j++)
  586. {
  587. PyObject *t;
  588. wchar_t *wstr;
  589. Py_ssize_t len;
  590. t = PyList_GET_ITEM(value, j);
  591. wstr = PyUnicode_AsUnicodeAndSize(t, &len);
  592. assert(wstr);
  593. wcscpy(P, wstr);
  594. P += (len + 1);
  595. }
  596. /* And doubly-terminate the list... */
  597. *P = '\0';
  598. break;
  599. }
  600. case REG_BINARY:
  601. /* ALSO handle ALL unknown data types here. Even if we can't
  602. support it natively, we should handle the bits. */
  603. default:
  604. if (value == Py_None) {
  605. *retDataSize = 0;
  606. *retDataBuf = NULL;
  607. }
  608. else {
  609. Py_buffer view;
  610. if (!PyObject_CheckBuffer(value)) {
  611. PyErr_Format(PyExc_TypeError,
  612. "Objects of type '%s' can not "
  613. "be used as binary registry values",
  614. value->ob_type->tp_name);
  615. return FALSE;
  616. }
  617. if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
  618. return FALSE;
  619. *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
  620. if (*retDataBuf == NULL){
  621. PyBuffer_Release(&view);
  622. PyErr_NoMemory();
  623. return FALSE;
  624. }
  625. *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
  626. memcpy(*retDataBuf, view.buf, view.len);
  627. PyBuffer_Release(&view);
  628. }
  629. break;
  630. }
  631. return TRUE;
  632. }
  633. /* Convert Registry data into PyObject*/
  634. static PyObject *
  635. Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
  636. {
  637. PyObject *obData;
  638. switch (typ) {
  639. case REG_DWORD:
  640. if (retDataSize == 0)
  641. obData = PyLong_FromUnsignedLong(0);
  642. else
  643. obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf);
  644. break;
  645. case REG_QWORD:
  646. if (retDataSize == 0)
  647. obData = PyLong_FromUnsignedLongLong(0);
  648. else
  649. obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf);
  650. break;
  651. case REG_SZ:
  652. case REG_EXPAND_SZ:
  653. {
  654. /* REG_SZ should be a NUL terminated string, but only by
  655. * convention. The buffer may have been saved without a NUL
  656. * or with embedded NULs. To be consistent with reg.exe and
  657. * regedit.exe, consume only up to the first NUL. */
  658. wchar_t *data = (wchar_t *)retDataBuf;
  659. size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
  660. obData = PyUnicode_FromWideChar(data, len);
  661. break;
  662. }
  663. case REG_MULTI_SZ:
  664. if (retDataSize == 0)
  665. obData = PyList_New(0);
  666. else
  667. {
  668. int index = 0;
  669. wchar_t *data = (wchar_t *)retDataBuf;
  670. int len = retDataSize / 2;
  671. int s = countStrings(data, len);
  672. wchar_t **str = PyMem_New(wchar_t *, s);
  673. if (str == NULL)
  674. return PyErr_NoMemory();
  675. fixupMultiSZ(str, data, len);
  676. obData = PyList_New(s);
  677. if (obData == NULL) {
  678. PyMem_Free(str);
  679. return NULL;
  680. }
  681. for (index = 0; index < s; index++)
  682. {
  683. size_t slen = wcsnlen(str[index], len);
  684. PyObject *uni = PyUnicode_FromWideChar(str[index], slen);
  685. if (uni == NULL) {
  686. Py_DECREF(obData);
  687. PyMem_Free(str);
  688. return NULL;
  689. }
  690. PyList_SET_ITEM(obData, index, uni);
  691. len -= Py_SAFE_DOWNCAST(slen + 1, size_t, int);
  692. }
  693. PyMem_Free(str);
  694. break;
  695. }
  696. case REG_BINARY:
  697. /* ALSO handle ALL unknown data types here. Even if we can't
  698. support it natively, we should handle the bits. */
  699. default:
  700. if (retDataSize == 0) {
  701. Py_INCREF(Py_None);
  702. obData = Py_None;
  703. }
  704. else
  705. obData = PyBytes_FromStringAndSize(
  706. (char *)retDataBuf, retDataSize);
  707. break;
  708. }
  709. return obData;
  710. }
  711. /* The Python methods */
  712. /*[clinic input]
  713. winreg.CloseKey
  714. hkey: object
  715. A previously opened key.
  716. /
  717. Closes a previously opened registry key.
  718. Note that if the key is not closed using this method, it will be
  719. closed when the hkey object is destroyed by Python.
  720. [clinic start generated code]*/
  721. static PyObject *
  722. winreg_CloseKey(PyObject *module, PyObject *hkey)
  723. /*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/
  724. {
  725. if (!PyHKEY_Close(hkey))
  726. return NULL;
  727. Py_RETURN_NONE;
  728. }
  729. /*[clinic input]
  730. winreg.ConnectRegistry -> HKEY
  731. computer_name: Py_UNICODE(accept={str, NoneType})
  732. The name of the remote computer, of the form r"\\computername". If
  733. None, the local computer is used.
  734. key: HKEY
  735. The predefined key to connect to.
  736. /
  737. Establishes a connection to the registry on another computer.
  738. The return value is the handle of the opened key.
  739. If the function fails, an OSError exception is raised.
  740. [clinic start generated code]*/
  741. static HKEY
  742. winreg_ConnectRegistry_impl(PyObject *module,
  743. const Py_UNICODE *computer_name, HKEY key)
  744. /*[clinic end generated code: output=cd4f70fb9ec901fb input=5f98a891a347e68e]*/
  745. {
  746. HKEY retKey;
  747. long rc;
  748. if (PySys_Audit("winreg.ConnectRegistry", "un",
  749. computer_name, (Py_ssize_t)key) < 0) {
  750. return NULL;
  751. }
  752. Py_BEGIN_ALLOW_THREADS
  753. rc = RegConnectRegistryW(computer_name, key, &retKey);
  754. Py_END_ALLOW_THREADS
  755. if (rc != ERROR_SUCCESS) {
  756. PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry");
  757. return NULL;
  758. }
  759. return retKey;
  760. }
  761. /*[clinic input]
  762. winreg.CreateKey -> HKEY
  763. key: HKEY
  764. An already open key, or one of the predefined HKEY_* constants.
  765. sub_key: Py_UNICODE(accept={str, NoneType})
  766. The name of the key this method opens or creates.
  767. /
  768. Creates or opens the specified key.
  769. If key is one of the predefined keys, sub_key may be None. In that case,
  770. the handle returned is the same key handle passed in to the function.
  771. If the key already exists, this function opens the existing key.
  772. The return value is the handle of the opened key.
  773. If the function fails, an OSError exception is raised.
  774. [clinic start generated code]*/
  775. static HKEY
  776. winreg_CreateKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
  777. /*[clinic end generated code: output=2af13910d56eae26 input=3cdd1622488acea2]*/
  778. {
  779. HKEY retKey;
  780. long rc;
  781. if (PySys_Audit("winreg.CreateKey", "nun",
  782. (Py_ssize_t)key, sub_key,
  783. (Py_ssize_t)KEY_WRITE) < 0) {
  784. return NULL;
  785. }
  786. rc = RegCreateKeyW(key, sub_key, &retKey);
  787. if (rc != ERROR_SUCCESS) {
  788. PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
  789. return NULL;
  790. }
  791. if (PySys_Audit("winreg.OpenKey/result", "n",
  792. (Py_ssize_t)retKey) < 0) {
  793. return NULL;
  794. }
  795. return retKey;
  796. }
  797. /*[clinic input]
  798. winreg.CreateKeyEx -> HKEY
  799. key: HKEY
  800. An already open key, or one of the predefined HKEY_* constants.
  801. sub_key: Py_UNICODE(accept={str, NoneType})
  802. The name of the key this method opens or creates.
  803. reserved: int = 0
  804. A reserved integer, and must be zero. Default is zero.
  805. access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE
  806. An integer that specifies an access mask that describes the
  807. desired security access for the key. Default is KEY_WRITE.
  808. Creates or opens the specified key.
  809. If key is one of the predefined keys, sub_key may be None. In that case,
  810. the handle returned is the same key handle passed in to the function.
  811. If the key already exists, this function opens the existing key
  812. The return value is the handle of the opened key.
  813. If the function fails, an OSError exception is raised.
  814. [clinic start generated code]*/
  815. static HKEY
  816. winreg_CreateKeyEx_impl(PyObject *module, HKEY key,
  817. const Py_UNICODE *sub_key, int reserved,
  818. REGSAM access)
  819. /*[clinic end generated code: output=643a70ad6a361a97 input=42c2b03f98406b66]*/
  820. {
  821. HKEY retKey;
  822. long rc;
  823. if (PySys_Audit("winreg.CreateKey", "nun",
  824. (Py_ssize_t)key, sub_key,
  825. (Py_ssize_t)access) < 0) {
  826. return NULL;
  827. }
  828. rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
  829. access, NULL, &retKey, NULL);
  830. if (rc != ERROR_SUCCESS) {
  831. PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
  832. return NULL;
  833. }
  834. if (PySys_Audit("winreg.OpenKey/result", "n",
  835. (Py_ssize_t)retKey) < 0) {
  836. return NULL;
  837. }
  838. return retKey;
  839. }
  840. /*[clinic input]
  841. winreg.DeleteKey
  842. key: HKEY
  843. An already open key, or any one of the predefined HKEY_* constants.
  844. sub_key: Py_UNICODE
  845. A string that must be the name of a subkey of the key identified by
  846. the key parameter. This value must not be None, and the key may not
  847. have subkeys.
  848. /
  849. Deletes the specified key.
  850. This method can not delete keys with subkeys.
  851. If the function succeeds, the entire key, including all of its values,
  852. is removed. If the function fails, an OSError exception is raised.
  853. [clinic start generated code]*/
  854. static PyObject *
  855. winreg_DeleteKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
  856. /*[clinic end generated code: output=d2652a84f70e0862 input=b31d225b935e4211]*/
  857. {
  858. long rc;
  859. if (PySys_Audit("winreg.DeleteKey", "nun",
  860. (Py_ssize_t)key, sub_key,
  861. (Py_ssize_t)0) < 0) {
  862. return NULL;
  863. }
  864. rc = RegDeleteKeyW(key, sub_key );
  865. if (rc != ERROR_SUCCESS)
  866. return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
  867. Py_RETURN_NONE;
  868. }
  869. /*[clinic input]
  870. winreg.DeleteKeyEx
  871. key: HKEY
  872. An already open key, or any one of the predefined HKEY_* constants.
  873. sub_key: Py_UNICODE
  874. A string that must be the name of a subkey of the key identified by
  875. the key parameter. This value must not be None, and the key may not
  876. have subkeys.
  877. access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
  878. An integer that specifies an access mask that describes the
  879. desired security access for the key. Default is KEY_WOW64_64KEY.
  880. reserved: int = 0
  881. A reserved integer, and must be zero. Default is zero.
  882. Deletes the specified key (64-bit OS only).
  883. This method can not delete keys with subkeys.
  884. If the function succeeds, the entire key, including all of its values,
  885. is removed. If the function fails, an OSError exception is raised.
  886. On unsupported Windows versions, NotImplementedError is raised.
  887. [clinic start generated code]*/
  888. static PyObject *
  889. winreg_DeleteKeyEx_impl(PyObject *module, HKEY key,
  890. const Py_UNICODE *sub_key, REGSAM access,
  891. int reserved)
  892. /*[clinic end generated code: output=52a1c8b374ebc003 input=711d9d89e7ecbed7]*/
  893. {
  894. HMODULE hMod;
  895. typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
  896. RDKEFunc pfn = NULL;
  897. long rc;
  898. if (PySys_Audit("winreg.DeleteKey", "nun",
  899. (Py_ssize_t)key, sub_key,
  900. (Py_ssize_t)access) < 0) {
  901. return NULL;
  902. }
  903. /* Only available on 64bit platforms, so we must load it
  904. dynamically. */
  905. Py_BEGIN_ALLOW_THREADS
  906. hMod = GetModuleHandleW(L"advapi32.dll");
  907. if (hMod)
  908. pfn = (RDKEFunc)GetProcAddress(hMod, "RegDeleteKeyExW");
  909. Py_END_ALLOW_THREADS
  910. if (!pfn) {
  911. PyErr_SetString(PyExc_NotImplementedError,
  912. "not implemented on this platform");
  913. return NULL;
  914. }
  915. Py_BEGIN_ALLOW_THREADS
  916. rc = (*pfn)(key, sub_key, access, reserved);
  917. Py_END_ALLOW_THREADS
  918. if (rc != ERROR_SUCCESS)
  919. return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
  920. Py_RETURN_NONE;
  921. }
  922. /*[clinic input]
  923. winreg.DeleteValue
  924. key: HKEY
  925. An already open key, or any one of the predefined HKEY_* constants.
  926. value: Py_UNICODE(accept={str, NoneType})
  927. A string that identifies the value to remove.
  928. /
  929. Removes a named value from a registry key.
  930. [clinic start generated code]*/
  931. static PyObject *
  932. winreg_DeleteValue_impl(PyObject *module, HKEY key, const Py_UNICODE *value)
  933. /*[clinic end generated code: output=56fa9d21f3a54371 input=a78d3407a4197b21]*/
  934. {
  935. long rc;
  936. if (PySys_Audit("winreg.DeleteValue", "nu",
  937. (Py_ssize_t)key, value) < 0) {
  938. return NULL;
  939. }
  940. Py_BEGIN_ALLOW_THREADS
  941. rc = RegDeleteValueW(key, value);
  942. Py_END_ALLOW_THREADS
  943. if (rc !=ERROR_SUCCESS)
  944. return PyErr_SetFromWindowsErrWithFunction(rc,
  945. "RegDeleteValue");
  946. Py_RETURN_NONE;
  947. }
  948. /*[clinic input]
  949. winreg.EnumKey
  950. key: HKEY
  951. An already open key, or any one of the predefined HKEY_* constants.
  952. index: int
  953. An integer that identifies the index of the key to retrieve.
  954. /
  955. Enumerates subkeys of an open registry key.
  956. The function retrieves the name of one subkey each time it is called.
  957. It is typically called repeatedly until an OSError exception is
  958. raised, indicating no more values are available.
  959. [clinic start generated code]*/
  960. static PyObject *
  961. winreg_EnumKey_impl(PyObject *module, HKEY key, int index)
  962. /*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
  963. {
  964. long rc;
  965. PyObject *retStr;
  966. if (PySys_Audit("winreg.EnumKey", "ni",
  967. (Py_ssize_t)key, index) < 0) {
  968. return NULL;
  969. }
  970. /* The Windows docs claim that the max key name length is 255
  971. * characters, plus a terminating nul character. However,
  972. * empirical testing demonstrates that it is possible to
  973. * create a 256 character key that is missing the terminating
  974. * nul. RegEnumKeyEx requires a 257 character buffer to
  975. * retrieve such a key name. */
  976. wchar_t tmpbuf[257];
  977. DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
  978. Py_BEGIN_ALLOW_THREADS
  979. rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
  980. Py_END_ALLOW_THREADS
  981. if (rc != ERROR_SUCCESS)
  982. return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
  983. retStr = PyUnicode_FromWideChar(tmpbuf, len);
  984. return retStr; /* can be NULL */
  985. }
  986. /*[clinic input]
  987. winreg.EnumValue
  988. key: HKEY
  989. An already open key, or any one of the predefined HKEY_* constants.
  990. index: int
  991. An integer that identifies the index of the value to retrieve.
  992. /
  993. Enumerates values of an open registry key.
  994. The function retrieves the name of one subkey each time it is called.
  995. It is typically called repeatedly, until an OSError exception
  996. is raised, indicating no more values.
  997. The result is a tuple of 3 items:
  998. value_name
  999. A string that identifies the value.
  1000. value_data
  1001. An object that holds the value data, and whose type depends
  1002. on the underlying registry type.
  1003. data_type
  1004. An integer that identifies the type of the value data.
  1005. [clinic start generated code]*/
  1006. static PyObject *
  1007. winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
  1008. /*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
  1009. {
  1010. long rc;
  1011. wchar_t *retValueBuf;
  1012. BYTE *tmpBuf;
  1013. BYTE *retDataBuf;
  1014. DWORD retValueSize, bufValueSize;
  1015. DWORD retDataSize, bufDataSize;
  1016. DWORD typ;
  1017. PyObject *obData;
  1018. PyObject *retVal;
  1019. if (PySys_Audit("winreg.EnumValue", "ni",
  1020. (Py_ssize_t)key, index) < 0) {
  1021. return NULL;
  1022. }
  1023. if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
  1024. NULL,
  1025. &retValueSize, &retDataSize, NULL, NULL))
  1026. != ERROR_SUCCESS)
  1027. return PyErr_SetFromWindowsErrWithFunction(rc,
  1028. "RegQueryInfoKey");
  1029. ++retValueSize; /* include null terminators */
  1030. ++retDataSize;
  1031. bufDataSize = retDataSize;
  1032. bufValueSize = retValueSize;
  1033. retValueBuf = PyMem_New(wchar_t, retValueSize);
  1034. if (retValueBuf == NULL)
  1035. return PyErr_NoMemory();
  1036. retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
  1037. if (retDataBuf == NULL) {
  1038. PyMem_Free(retValueBuf);
  1039. return PyErr_NoMemory();
  1040. }
  1041. while (1) {
  1042. Py_BEGIN_ALLOW_THREADS
  1043. rc = RegEnumValueW(key,
  1044. index,
  1045. retValueBuf,
  1046. &retValueSize,
  1047. NULL,
  1048. &typ,
  1049. (BYTE *)retDataBuf,
  1050. &retDataSize);
  1051. Py_END_ALLOW_THREADS
  1052. if (rc != ERROR_MORE_DATA)
  1053. break;
  1054. bufDataSize *= 2;
  1055. tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
  1056. if (tmpBuf == NULL) {
  1057. PyErr_NoMemory();
  1058. retVal = NULL;
  1059. goto fail;
  1060. }
  1061. retDataBuf = tmpBuf;
  1062. retDataSize = bufDataSize;
  1063. retValueSize = bufValueSize;
  1064. }
  1065. if (rc != ERROR_SUCCESS) {
  1066. retVal = PyErr_SetFromWindowsErrWithFunction(rc,
  1067. "PyRegEnumValue");
  1068. goto fail;
  1069. }
  1070. obData = Reg2Py(retDataBuf, retDataSize, typ);
  1071. if (obData == NULL) {
  1072. retVal = NULL;
  1073. goto fail;
  1074. }
  1075. retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
  1076. Py_DECREF(obData);
  1077. fail:
  1078. PyMem_Free(retValueBuf);
  1079. PyMem_Free(retDataBuf);
  1080. return retVal;
  1081. }
  1082. /*[clinic input]
  1083. winreg.ExpandEnvironmentStrings
  1084. string: Py_UNICODE
  1085. /
  1086. Expand environment vars.
  1087. [clinic start generated code]*/
  1088. static PyObject *
  1089. winreg_ExpandEnvironmentStrings_impl(PyObject *module,
  1090. const Py_UNICODE *string)
  1091. /*[clinic end generated code: output=8fa4e959747a7312 input=b2a9714d2b751aa6]*/
  1092. {
  1093. wchar_t *retValue = NULL;
  1094. DWORD retValueSize;
  1095. DWORD rc;
  1096. PyObject *o;
  1097. if (PySys_Audit("winreg.ExpandEnvironmentStrings", "u",
  1098. string) < 0) {
  1099. return NULL;
  1100. }
  1101. retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
  1102. if (retValueSize == 0) {
  1103. return PyErr_SetFromWindowsErrWithFunction(retValueSize,
  1104. "ExpandEnvironmentStrings");
  1105. }
  1106. retValue = PyMem_New(wchar_t, retValueSize);
  1107. if (retValue == NULL) {
  1108. return PyErr_NoMemory();
  1109. }
  1110. rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
  1111. if (rc == 0) {
  1112. PyMem_Free(retValue);
  1113. return PyErr_SetFromWindowsErrWithFunction(retValueSize,
  1114. "ExpandEnvironmentStrings");
  1115. }
  1116. o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
  1117. PyMem_Free(retValue);
  1118. return o;
  1119. }
  1120. /*[clinic input]
  1121. winreg.FlushKey
  1122. key: HKEY
  1123. An already open key, or any one of the predefined HKEY_* constants.
  1124. /
  1125. Writes all the attributes of a key to the registry.
  1126. It is not necessary to call FlushKey to change a key. Registry changes
  1127. are flushed to disk by the registry using its lazy flusher. Registry
  1128. changes are also flushed to disk at system shutdown. Unlike
  1129. CloseKey(), the FlushKey() method returns only when all the data has
  1130. been written to the registry.
  1131. An application should only call FlushKey() if it requires absolute
  1132. certainty that registry changes are on disk. If you don't know whether
  1133. a FlushKey() call is required, it probably isn't.
  1134. [clinic start generated code]*/
  1135. static PyObject *
  1136. winreg_FlushKey_impl(PyObject *module, HKEY key)
  1137. /*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
  1138. {
  1139. long rc;
  1140. Py_BEGIN_ALLOW_THREADS
  1141. rc = RegFlushKey(key);
  1142. Py_END_ALLOW_THREADS
  1143. if (rc != ERROR_SUCCESS)
  1144. return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
  1145. Py_RETURN_NONE;
  1146. }
  1147. /*[clinic input]
  1148. winreg.LoadKey
  1149. key: HKEY
  1150. An already open key, or any one of the predefined HKEY_* constants.
  1151. sub_key: Py_UNICODE
  1152. A string that identifies the sub-key to load.
  1153. file_name: Py_UNICODE
  1154. The name of the file to load registry data from. This file must
  1155. have been created with the SaveKey() function. Under the file
  1156. allocation table (FAT) file system, the filename may not have an
  1157. extension.
  1158. /
  1159. Insert data into the registry from a file.
  1160. Creates a subkey under the specified key and stores registration
  1161. information from a specified file into that subkey.
  1162. A call to LoadKey() fails if the calling process does not have the
  1163. SE_RESTORE_PRIVILEGE privilege.
  1164. If key is a handle returned by ConnectRegistry(), then the path
  1165. specified in fileName is relative to the remote computer.
  1166. The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
  1167. tree.
  1168. [clinic start generated code]*/
  1169. static PyObject *
  1170. winreg_LoadKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
  1171. const Py_UNICODE *file_name)
  1172. /*[clinic end generated code: output=65f89f2548cb27c7 input=e3b5b45ade311582]*/
  1173. {
  1174. long rc;
  1175. if (PySys_Audit("winreg.LoadKey", "nuu",
  1176. (Py_ssize_t)key, sub_key, file_name) < 0) {
  1177. return NULL;
  1178. }
  1179. Py_BEGIN_ALLOW_THREADS
  1180. rc = RegLoadKeyW(key, sub_key, file_name );
  1181. Py_END_ALLOW_THREADS
  1182. if (rc != ERROR_SUCCESS)
  1183. return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
  1184. Py_RETURN_NONE;
  1185. }
  1186. /*[clinic input]
  1187. winreg.OpenKey -> HKEY
  1188. key: HKEY
  1189. An already open key, or any one of the predefined HKEY_* constants.
  1190. sub_key: Py_UNICODE(accept={str, NoneType})
  1191. A string that identifies the sub_key to open.
  1192. reserved: int = 0
  1193. A reserved integer that must be zero. Default is zero.
  1194. access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
  1195. An integer that specifies an access mask that describes the desired
  1196. security access for the key. Default is KEY_READ.
  1197. Opens the specified key.
  1198. The result is a new handle to the specified key.
  1199. If the function fails, an OSError exception is raised.
  1200. [clinic start generated code]*/
  1201. static HKEY
  1202. winreg_OpenKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
  1203. int reserved, REGSAM access)
  1204. /*[clinic end generated code: output=8849bff2c30104ad input=098505ac36a9ae28]*/
  1205. {
  1206. HKEY retKey;
  1207. long rc;
  1208. if (PySys_Audit("winreg.OpenKey", "nun",
  1209. (Py_ssize_t)key, sub_key,
  1210. (Py_ssize_t)access) < 0) {
  1211. return NULL;
  1212. }
  1213. Py_BEGIN_ALLOW_THREADS
  1214. rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
  1215. Py_END_ALLOW_THREADS
  1216. if (rc != ERROR_SUCCESS) {
  1217. PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
  1218. return NULL;
  1219. }
  1220. if (PySys_Audit("winreg.OpenKey/result", "n",
  1221. (Py_ssize_t)retKey) < 0) {
  1222. return NULL;
  1223. }
  1224. return retKey;
  1225. }
  1226. /*[clinic input]
  1227. winreg.OpenKeyEx = winreg.OpenKey
  1228. Opens the specified key.
  1229. The result is a new handle to the specified key.
  1230. If the function fails, an OSError exception is raised.
  1231. [clinic start generated code]*/
  1232. static HKEY
  1233. winreg_OpenKeyEx_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
  1234. int reserved, REGSAM access)
  1235. /*[clinic end generated code: output=81bc2bd684bc77ae input=c6c4972af8622959]*/
  1236. {
  1237. return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
  1238. }
  1239. /*[clinic input]
  1240. winreg.QueryInfoKey
  1241. key: HKEY
  1242. An already open key, or any one of the predefined HKEY_* constants.
  1243. /
  1244. Returns information about a key.
  1245. The result is a tuple of 3 items:
  1246. An integer that identifies the number of sub keys this key has.
  1247. An integer that identifies the number of values this key has.
  1248. An integer that identifies when the key was last modified (if available)
  1249. as 100's of nanoseconds since Jan 1, 1600.
  1250. [clinic start generated code]*/
  1251. static PyObject *
  1252. winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
  1253. /*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
  1254. {
  1255. long rc;
  1256. DWORD nSubKeys, nValues;
  1257. FILETIME ft;
  1258. LARGE_INTEGER li;
  1259. PyObject *l;
  1260. PyObject *ret;
  1261. if (PySys_Audit("winreg.QueryInfoKey", "n", (Py_ssize_t)key) < 0) {
  1262. return NULL;
  1263. }
  1264. if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
  1265. &nValues, NULL, NULL, NULL, &ft))
  1266. != ERROR_SUCCESS) {
  1267. return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
  1268. }
  1269. li.LowPart = ft.dwLowDateTime;
  1270. li.HighPart = ft.dwHighDateTime;
  1271. l = PyLong_FromLongLong(li.QuadPart);
  1272. if (l == NULL) {
  1273. return NULL;
  1274. }
  1275. ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
  1276. Py_DECREF(l);
  1277. return ret;
  1278. }
  1279. /*[clinic input]
  1280. winreg.QueryValue
  1281. key: HKEY
  1282. An already open key, or any one of the predefined HKEY_* constants.
  1283. sub_key: Py_UNICODE(accept={str, NoneType})
  1284. A string that holds the name of the subkey with which the value
  1285. is associated. If this parameter is None or empty, the function
  1286. retrieves the value set by the SetValue() method for the key
  1287. identified by key.
  1288. /
  1289. Retrieves the unnamed value for a key.
  1290. Values in the registry have name, type, and data components. This method
  1291. retrieves the data for a key's first value that has a NULL name.
  1292. But since the underlying API call doesn't return the type, you'll
  1293. probably be happier using QueryValueEx; this function is just here for
  1294. completeness.
  1295. [clinic start generated code]*/
  1296. static PyObject *
  1297. winreg_QueryValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
  1298. /*[clinic end generated code: output=c655810ae50c63a9 input=41cafbbf423b21d6]*/
  1299. {
  1300. long rc;
  1301. PyObject *retStr;
  1302. wchar_t *retBuf;
  1303. DWORD bufSize = 0;
  1304. DWORD retSize = 0;
  1305. wchar_t *tmp;
  1306. if (PySys_Audit("winreg.QueryValue", "nuu",
  1307. (Py_ssize_t)key, sub_key, NULL) < 0) {
  1308. return NULL;
  1309. }
  1310. rc = RegQueryValueW(key, sub_key, NULL, &retSize);
  1311. if (rc == ERROR_MORE_DATA)
  1312. retSize = 256;
  1313. else if (rc != ERROR_SUCCESS)
  1314. return PyErr_SetFromWindowsErrWithFunction(rc,
  1315. "RegQueryValue");
  1316. bufSize = retSize;
  1317. retBuf = (wchar_t *) PyMem_Malloc(bufSize);
  1318. if (retBuf == NULL)
  1319. return PyErr_NoMemory();
  1320. while (1) {
  1321. retSize = bufSize;
  1322. rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
  1323. if (rc != ERROR_MORE_DATA)
  1324. break;
  1325. bufSize *= 2;
  1326. tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
  1327. if (tmp == NULL) {
  1328. PyMem_Free(retBuf);
  1329. return PyErr_NoMemory();
  1330. }
  1331. retBuf = tmp;
  1332. }
  1333. if (rc != ERROR_SUCCESS) {
  1334. PyMem_Free(retBuf);
  1335. return PyErr_SetFromWindowsErrWithFunction(rc,
  1336. "RegQueryValue");
  1337. }
  1338. retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
  1339. PyMem_Free(retBuf);
  1340. return retStr;
  1341. }
  1342. /*[clinic input]
  1343. winreg.QueryValueEx
  1344. key: HKEY
  1345. An already open key, or any one of the predefined HKEY_* constants.
  1346. name: Py_UNICODE(accept={str, NoneType})
  1347. A string indicating the value to query.
  1348. /
  1349. Retrieves the type and value of a specified sub-key.
  1350. Behaves mostly like QueryValue(), but also returns the type of the
  1351. specified value name associated with the given open registry key.
  1352. The return value is a tuple of the value and the type_id.
  1353. [clinic start generated code]*/
  1354. static PyObject *
  1355. winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name)
  1356. /*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/
  1357. {
  1358. long rc;
  1359. BYTE *retBuf, *tmp;
  1360. DWORD bufSize = 0, retSize;
  1361. DWORD typ;
  1362. PyObject *obData;
  1363. PyObject *result;
  1364. if (PySys_Audit("winreg.QueryValue", "nuu",
  1365. (Py_ssize_t)key, NULL, name) < 0) {
  1366. return NULL;
  1367. }
  1368. rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
  1369. if (rc == ERROR_MORE_DATA)
  1370. bufSize = 256;
  1371. else if (rc != ERROR_SUCCESS)
  1372. return PyErr_SetFromWindowsErrWithFunction(rc,
  1373. "RegQueryValueEx");
  1374. retBuf = (BYTE *)PyMem_Malloc(bufSize);
  1375. if (retBuf == NULL)
  1376. return PyErr_NoMemory();
  1377. while (1) {
  1378. retSize = bufSize;
  1379. rc = RegQueryValueExW(key, name, NULL, &typ,
  1380. (BYTE *)retBuf, &retSize);
  1381. if (rc != ERROR_MORE_DATA)
  1382. break;
  1383. bufSize *= 2;
  1384. tmp = (char *) PyMem_Realloc(retBuf, bufSize);
  1385. if (tmp == NULL) {
  1386. PyMem_Free(retBuf);
  1387. return PyErr_NoMemory();
  1388. }
  1389. retBuf = tmp;
  1390. }
  1391. if (rc != ERROR_SUCCESS) {
  1392. PyMem_Free(retBuf);
  1393. return PyErr_SetFromWindowsErrWithFunction(rc,
  1394. "RegQueryValueEx");
  1395. }
  1396. obData = Reg2Py(retBuf, bufSize, typ);
  1397. PyMem_Free(retBuf);
  1398. if (obData == NULL)
  1399. return NULL;
  1400. result = Py_BuildValue("Oi", obData, typ);
  1401. Py_DECREF(obData);
  1402. return result;
  1403. }
  1404. /*[clinic input]
  1405. winreg.SaveKey
  1406. key: HKEY
  1407. An already open key, or any one of the predefined HKEY_* constants.
  1408. file_name: Py_UNICODE
  1409. The name of the file to save registry data to. This file cannot
  1410. already exist. If this filename includes an extension, it cannot be
  1411. used on file allocation table (FAT) file systems by the LoadKey(),
  1412. ReplaceKey() or RestoreKey() methods.
  1413. /
  1414. Saves the specified key, and all its subkeys to the specified file.
  1415. If key represents a key on a remote computer, the path described by
  1416. file_name is relative to the remote computer.
  1417. The caller of this method must possess the SeBackupPrivilege
  1418. security privilege. This function passes NULL for security_attributes
  1419. to the API.
  1420. [clinic start generated code]*/
  1421. static PyObject *
  1422. winreg_SaveKey_impl(PyObject *module, HKEY key, const Py_UNICODE *file_name)
  1423. /*[clinic end generated code: output=ca94b835c88f112b input=da735241f91ac7a2]*/
  1424. {
  1425. LPSECURITY_ATTRIBUTES pSA = NULL;
  1426. long rc;
  1427. /* One day we may get security into the core?
  1428. if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
  1429. return NULL;
  1430. */
  1431. if (PySys_Audit("winreg.SaveKey", "nu",
  1432. (Py_ssize_t)key, file_name) < 0) {
  1433. return NULL;
  1434. }
  1435. Py_BEGIN_ALLOW_THREADS
  1436. rc = RegSaveKeyW(key, file_name, pSA );
  1437. Py_END_ALLOW_THREADS
  1438. if (rc != ERROR_SUCCESS)
  1439. return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
  1440. Py_RETURN_NONE;
  1441. }
  1442. /*[clinic input]
  1443. winreg.SetValue
  1444. key: HKEY
  1445. An already open key, or any one of the predefined HKEY_* constants.
  1446. sub_key: Py_UNICODE(accept={str, NoneType})
  1447. A string that names the subkey with which the value is associated.
  1448. type: DWORD
  1449. An integer that specifies the type of the data. Currently this must
  1450. be REG_SZ, meaning only strings are supported.
  1451. value: Py_UNICODE(zeroes=True)
  1452. A string that specifies the new value.
  1453. /
  1454. Associates a value with a specified key.
  1455. If the key specified by the sub_key parameter does not exist, the
  1456. SetValue function creates it.
  1457. Value lengths are limited by available memory. Long values (more than
  1458. 2048 bytes) should be stored as files with the filenames stored in
  1459. the configuration registry to help the registry perform efficiently.
  1460. The key identified by the key parameter must have been opened with
  1461. KEY_SET_VALUE access.
  1462. [clinic start generated code]*/
  1463. static PyObject *
  1464. winreg_SetValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
  1465. DWORD type, const Py_UNICODE *value,
  1466. Py_ssize_clean_t value_length)
  1467. /*[clinic end generated code: output=686bedb1cbb4367b input=2cd2adab79339c53]*/
  1468. {
  1469. long rc;
  1470. if (type != REG_SZ) {
  1471. PyErr_SetString(PyExc_TypeError, "type must be winreg.REG_SZ");
  1472. return NULL;
  1473. }
  1474. if ((size_t)value_length >= PY_DWORD_MAX) {
  1475. PyErr_SetString(PyExc_OverflowError, "value is too long");
  1476. return NULL;
  1477. }
  1478. if (PySys_Audit("winreg.SetValue", "nunu#",
  1479. (Py_ssize_t)key, sub_key, (Py_ssize_t)type,
  1480. value, value_length) < 0) {
  1481. return NULL;
  1482. }
  1483. Py_BEGIN_ALLOW_THREADS
  1484. rc = RegSetValueW(key, sub_key, REG_SZ, value, (DWORD)(value_length + 1));
  1485. Py_END_ALLOW_THREADS
  1486. if (rc != ERROR_SUCCESS)
  1487. return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
  1488. Py_RETURN_NONE;
  1489. }
  1490. /*[clinic input]
  1491. winreg.SetValueEx
  1492. key: HKEY
  1493. An already open key, or any one of the predefined HKEY_* constants.
  1494. value_name: Py_UNICODE(accept={str, NoneType})
  1495. A string containing the name of the value to set, or None.
  1496. reserved: object
  1497. Can be anything - zero is always passed to the API.
  1498. type: DWORD
  1499. An integer that specifies the type of the data, one of:
  1500. REG_BINARY -- Binary data in any form.
  1501. REG_DWORD -- A 32-bit number.
  1502. REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
  1503. REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
  1504. REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
  1505. references to environment variables (for example,
  1506. %PATH%).
  1507. REG_LINK -- A Unicode symbolic link.
  1508. REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
  1509. by two null characters. Note that Python handles
  1510. this termination automatically.
  1511. REG_NONE -- No defined value type.
  1512. REG_QWORD -- A 64-bit number.
  1513. REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
  1514. REG_RESOURCE_LIST -- A device-driver resource list.
  1515. REG_SZ -- A null-terminated string.
  1516. value: object
  1517. A string that specifies the new value.
  1518. /
  1519. Stores data in the value field of an open registry key.
  1520. This method can also set additional value and type information for the
  1521. specified key. The key identified by the key parameter must have been
  1522. opened with KEY_SET_VALUE access.
  1523. To open the key, use the CreateKeyEx() or OpenKeyEx() methods.
  1524. Value lengths are limited by available memory. Long values (more than
  1525. 2048 bytes) should be stored as files with the filenames stored in
  1526. the configuration registry to help the registry perform efficiently.
  1527. [clinic start generated code]*/
  1528. static PyObject *
  1529. winreg_SetValueEx_impl(PyObject *module, HKEY key,
  1530. const Py_UNICODE *value_name, PyObject *reserved,
  1531. DWORD type, PyObject *value)
  1532. /*[clinic end generated code: output=811b769a66ae11b7 input=900a9e3990bfb196]*/
  1533. {
  1534. BYTE *data;
  1535. DWORD len;
  1536. LONG rc;
  1537. if (!Py2Reg(value, type, &data, &len))
  1538. {
  1539. if (!PyErr_Occurred())
  1540. PyErr_SetString(PyExc_ValueError,
  1541. "Could not convert the data to the specified type.");
  1542. return NULL;
  1543. }
  1544. if (PySys_Audit("winreg.SetValue", "nunO",
  1545. (Py_ssize_t)key, value_name, (Py_ssize_t)type,
  1546. value) < 0) {
  1547. return NULL;
  1548. }
  1549. Py_BEGIN_ALLOW_THREADS
  1550. rc = RegSetValueExW(key, value_name, 0, type, data, len);
  1551. Py_END_ALLOW_THREADS
  1552. PyMem_DEL(data);
  1553. if (rc != ERROR_SUCCESS)
  1554. return PyErr_SetFromWindowsErrWithFunction(rc,
  1555. "RegSetValueEx");
  1556. Py_RETURN_NONE;
  1557. }
  1558. /*[clinic input]
  1559. winreg.DisableReflectionKey
  1560. key: HKEY
  1561. An already open key, or any one of the predefined HKEY_* constants.
  1562. /
  1563. Disables registry reflection for 32bit processes running on a 64bit OS.
  1564. Will generally raise NotImplementedError if executed on a 32bit OS.
  1565. If the key is not on the reflection list, the function succeeds but has
  1566. no effect. Disabling reflection for a key does not affect reflection
  1567. of any subkeys.
  1568. [clinic start generated code]*/
  1569. static PyObject *
  1570. winreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
  1571. /*[clinic end generated code: output=830cce504cc764b4 input=70bece2dee02e073]*/
  1572. {
  1573. HMODULE hMod;
  1574. typedef LONG (WINAPI *RDRKFunc)(HKEY);
  1575. RDRKFunc pfn = NULL;
  1576. LONG rc;
  1577. if (PySys_Audit("winreg.DisableReflectionKey", "n", (Py_ssize_t)key) < 0) {
  1578. return NULL;
  1579. }
  1580. /* Only available on 64bit platforms, so we must load it
  1581. dynamically.*/
  1582. Py_BEGIN_ALLOW_THREADS
  1583. hMod = GetModuleHandleW(L"advapi32.dll");
  1584. if (hMod)
  1585. pfn = (RDRKFunc)GetProcAddress(hMod,
  1586. "RegDisableReflectionKey");
  1587. Py_END_ALLOW_THREADS
  1588. if (!pfn) {
  1589. PyErr_SetString(PyExc_NotImplementedError,
  1590. "not implemented on this platform");
  1591. return NULL;
  1592. }
  1593. Py_BEGIN_ALLOW_THREADS
  1594. rc = (*pfn)(key);
  1595. Py_END_ALLOW_THREADS
  1596. if (rc != ERROR_SUCCESS)
  1597. return PyErr_SetFromWindowsErrWithFunction(rc,
  1598. "RegDisableReflectionKey");
  1599. Py_RETURN_NONE;
  1600. }
  1601. /*[clinic input]
  1602. winreg.EnableReflectionKey
  1603. key: HKEY
  1604. An already open key, or any one of the predefined HKEY_* constants.
  1605. /
  1606. Restores registry reflection for the specified disabled key.
  1607. Will generally raise NotImplementedError if executed on a 32bit OS.
  1608. Restoring reflection for a key does not affect reflection of any
  1609. subkeys.
  1610. [clinic start generated code]*/
  1611. static PyObject *
  1612. winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
  1613. /*[clinic end generated code: output=86fa1385fdd9ce57 input=eeae770c6eb9f559]*/
  1614. {
  1615. HMODULE hMod;
  1616. typedef LONG (WINAPI *RERKFunc)(HKEY);
  1617. RERKFunc pfn = NULL;
  1618. LONG rc;
  1619. if (PySys_Audit("winreg.EnableReflectionKey", "n", (Py_ssize_t)key) < 0) {
  1620. return NULL;
  1621. }
  1622. /* Only available on 64bit platforms, so we must load it
  1623. dynamically.*/
  1624. Py_BEGIN_ALLOW_THREADS
  1625. hMod = GetModuleHandleW(L"advapi32.dll");
  1626. if (hMod)
  1627. pfn = (RERKFunc)GetProcAddress(hMod,
  1628. "RegEnableReflectionKey");
  1629. Py_END_ALLOW_THREADS
  1630. if (!pfn) {
  1631. PyErr_SetString(PyExc_NotImplementedError,
  1632. "not implemented on this platform");
  1633. return NULL;
  1634. }
  1635. Py_BEGIN_ALLOW_THREADS
  1636. rc = (*pfn)(key);
  1637. Py_END_ALLOW_THREADS
  1638. if (rc != ERROR_SUCCESS)
  1639. return PyErr_SetFromWindowsErrWithFunction(rc,
  1640. "RegEnableReflectionKey");
  1641. Py_RETURN_NONE;
  1642. }
  1643. /*[clinic input]
  1644. winreg.QueryReflectionKey
  1645. key: HKEY
  1646. An already open key, or any one of the predefined HKEY_* constants.
  1647. /
  1648. Returns the reflection state for the specified key as a bool.
  1649. Will generally raise NotImplementedError if executed on a 32bit OS.
  1650. [clinic start generated code]*/
  1651. static PyObject *
  1652. winreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
  1653. /*[clinic end generated code: output=4e774af288c3ebb9 input=a98fa51d55ade186]*/
  1654. {
  1655. HMODULE hMod;
  1656. typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
  1657. RQRKFunc pfn = NULL;
  1658. BOOL result;
  1659. LONG rc;
  1660. if (PySys_Audit("winreg.QueryReflectionKey", "n", (Py_ssize_t)key) < 0) {
  1661. return NULL;
  1662. }
  1663. /* Only available on 64bit platforms, so we must load it
  1664. dynamically.*/
  1665. Py_BEGIN_ALLOW_THREADS
  1666. hMod = GetModuleHandleW(L"advapi32.dll");
  1667. if (hMod)
  1668. pfn = (RQRKFunc)GetProcAddress(hMod,
  1669. "RegQueryReflectionKey");
  1670. Py_END_ALLOW_THREADS
  1671. if (!pfn) {
  1672. PyErr_SetString(PyExc_NotImplementedError,
  1673. "not implemented on this platform");
  1674. return NULL;
  1675. }
  1676. Py_BEGIN_ALLOW_THREADS
  1677. rc = (*pfn)(key, &result);
  1678. Py_END_ALLOW_THREADS
  1679. if (rc != ERROR_SUCCESS)
  1680. return PyErr_SetFromWindowsErrWithFunction(rc,
  1681. "RegQueryReflectionKey");
  1682. return PyBool_FromLong(result);
  1683. }
  1684. static struct PyMethodDef winreg_methods[] = {
  1685. WINREG_CLOSEKEY_METHODDEF
  1686. WINREG_CONNECTREGISTRY_METHODDEF
  1687. WINREG_CREATEKEY_METHODDEF
  1688. WINREG_CREATEKEYEX_METHODDEF
  1689. WINREG_DELETEKEY_METHODDEF
  1690. WINREG_DELETEKEYEX_METHODDEF
  1691. WINREG_DELETEVALUE_METHODDEF
  1692. WINREG_DISABLEREFLECTIONKEY_METHODDEF
  1693. WINREG_ENABLEREFLECTIONKEY_METHODDEF
  1694. WINREG_ENUMKEY_METHODDEF
  1695. WINREG_ENUMVALUE_METHODDEF
  1696. WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
  1697. WINREG_FLUSHKEY_METHODDEF
  1698. WINREG_LOADKEY_METHODDEF
  1699. WINREG_OPENKEY_METHODDEF
  1700. WINREG_OPENKEYEX_METHODDEF
  1701. WINREG_QUERYVALUE_METHODDEF
  1702. WINREG_QUERYVALUEEX_METHODDEF
  1703. WINREG_QUERYINFOKEY_METHODDEF
  1704. WINREG_QUERYREFLECTIONKEY_METHODDEF
  1705. WINREG_SAVEKEY_METHODDEF
  1706. WINREG_SETVALUE_METHODDEF
  1707. WINREG_SETVALUEEX_METHODDEF
  1708. NULL,
  1709. };
  1710. static void
  1711. insint(PyObject * d, char * name, long value)
  1712. {
  1713. PyObject *v = PyLong_FromLong(value);
  1714. if (!v || PyDict_SetItemString(d, name, v))
  1715. PyErr_Clear();
  1716. Py_XDECREF(v);
  1717. }
  1718. #define ADD_INT(val) insint(d, #val, val)
  1719. static void
  1720. inskey(PyObject * d, char * name, HKEY key)
  1721. {
  1722. PyObject *v = PyLong_FromVoidPtr(key);
  1723. if (!v || PyDict_SetItemString(d, name, v))
  1724. PyErr_Clear();
  1725. Py_XDECREF(v);
  1726. }
  1727. #define ADD_KEY(val) inskey(d, #val, val)
  1728. static struct PyModuleDef winregmodule = {
  1729. PyModuleDef_HEAD_INIT,
  1730. "winreg",
  1731. module_doc,
  1732. -1,
  1733. winreg_methods,
  1734. NULL,
  1735. NULL,
  1736. NULL,
  1737. NULL
  1738. };
  1739. PyMODINIT_FUNC PyInit_winreg(void)
  1740. {
  1741. PyObject *m, *d;
  1742. m = PyModule_Create(&winregmodule);
  1743. if (m == NULL)
  1744. return NULL;
  1745. d = PyModule_GetDict(m);
  1746. PyHKEY_Type.tp_doc = PyHKEY_doc;
  1747. if (PyType_Ready(&PyHKEY_Type) < 0)
  1748. return NULL;
  1749. Py_INCREF(&PyHKEY_Type);
  1750. if (PyDict_SetItemString(d, "HKEYType",
  1751. (PyObject *)&PyHKEY_Type) != 0)
  1752. return NULL;
  1753. Py_INCREF(PyExc_OSError);
  1754. if (PyDict_SetItemString(d, "error",
  1755. PyExc_OSError) != 0)
  1756. return NULL;
  1757. /* Add the relevant constants */
  1758. ADD_KEY(HKEY_CLASSES_ROOT);
  1759. ADD_KEY(HKEY_CURRENT_USER);
  1760. ADD_KEY(HKEY_LOCAL_MACHINE);
  1761. ADD_KEY(HKEY_USERS);
  1762. ADD_KEY(HKEY_PERFORMANCE_DATA);
  1763. #ifdef HKEY_CURRENT_CONFIG
  1764. ADD_KEY(HKEY_CURRENT_CONFIG);
  1765. #endif
  1766. #ifdef HKEY_DYN_DATA
  1767. ADD_KEY(HKEY_DYN_DATA);
  1768. #endif
  1769. ADD_INT(KEY_QUERY_VALUE);
  1770. ADD_INT(KEY_SET_VALUE);
  1771. ADD_INT(KEY_CREATE_SUB_KEY);
  1772. ADD_INT(KEY_ENUMERATE_SUB_KEYS);
  1773. ADD_INT(KEY_NOTIFY);
  1774. ADD_INT(KEY_CREATE_LINK);
  1775. ADD_INT(KEY_READ);
  1776. ADD_INT(KEY_WRITE);
  1777. ADD_INT(KEY_EXECUTE);
  1778. ADD_INT(KEY_ALL_ACCESS);
  1779. #ifdef KEY_WOW64_64KEY
  1780. ADD_INT(KEY_WOW64_64KEY);
  1781. #endif
  1782. #ifdef KEY_WOW64_32KEY
  1783. ADD_INT(KEY_WOW64_32KEY);
  1784. #endif
  1785. ADD_INT(REG_OPTION_RESERVED);
  1786. ADD_INT(REG_OPTION_NON_VOLATILE);
  1787. ADD_INT(REG_OPTION_VOLATILE);
  1788. ADD_INT(REG_OPTION_CREATE_LINK);
  1789. ADD_INT(REG_OPTION_BACKUP_RESTORE);
  1790. ADD_INT(REG_OPTION_OPEN_LINK);
  1791. ADD_INT(REG_LEGAL_OPTION);
  1792. ADD_INT(REG_CREATED_NEW_KEY);
  1793. ADD_INT(REG_OPENED_EXISTING_KEY);
  1794. ADD_INT(REG_WHOLE_HIVE_VOLATILE);
  1795. ADD_INT(REG_REFRESH_HIVE);
  1796. ADD_INT(REG_NO_LAZY_FLUSH);
  1797. ADD_INT(REG_NOTIFY_CHANGE_NAME);
  1798. ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
  1799. ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
  1800. ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
  1801. ADD_INT(REG_LEGAL_CHANGE_FILTER);
  1802. ADD_INT(REG_NONE);
  1803. ADD_INT(REG_SZ);
  1804. ADD_INT(REG_EXPAND_SZ);
  1805. ADD_INT(REG_BINARY);
  1806. ADD_INT(REG_DWORD);
  1807. ADD_INT(REG_DWORD_LITTLE_ENDIAN);
  1808. ADD_INT(REG_DWORD_BIG_ENDIAN);
  1809. ADD_INT(REG_QWORD);
  1810. ADD_INT(REG_QWORD_LITTLE_ENDIAN);
  1811. ADD_INT(REG_LINK);
  1812. ADD_INT(REG_MULTI_SZ);
  1813. ADD_INT(REG_RESOURCE_LIST);
  1814. ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
  1815. ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
  1816. return m;
  1817. }