From 12706f20825f78afa8305c1cbeb47900a790c21a Mon Sep 17 00:00:00 2001
From: Brian Curtin <brian@python.org>
Date: Thu, 27 Dec 2012 10:12:45 -0600
Subject: Fix #14420. Use PyLong_AsUnsignedLong to support the full range of
 DWORD.

This fixes an OverflowError seen in winreg.SetValueEx when passed
winreg.REG_DWORD values that should be supported by the underlying API.
---
 Lib/test/test_winreg.py | 12 ++++++++++++
 Misc/NEWS               |  3 +++
 PC/winreg.c             |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py
index c7b50dd..68c12eb 100644
--- a/Lib/test/test_winreg.py
+++ b/Lib/test/test_winreg.py
@@ -323,6 +323,18 @@ class LocalWinregTests(BaseWinregTests):
         finally:
             DeleteKey(HKEY_CURRENT_USER, test_key_name)
 
+    def test_setvalueex_value_range(self):
+        # Test for Issue #14420, accept proper ranges for SetValueEx.
+        # Py2Reg, which gets called by SetValueEx, was using PyLong_AsLong,
+        # thus raising OverflowError. The implementation now uses
+        # PyLong_AsUnsignedLong to match DWORD's size.
+        try:
+            with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
+                self.assertNotEqual(ck.handle, 0)
+                SetValueEx(ck, "test_name", None, REG_DWORD, 0x80000000)
+        finally:
+            DeleteKey(HKEY_CURRENT_USER, test_key_name)
+
 
 @unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
 class RemoteWinregTests(BaseWinregTests):
diff --git a/Misc/NEWS b/Misc/NEWS
index 4514739..08d3e36 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
 Core and Builtins
 -----------------
 
+- Issue #14420: Support the full DWORD (unsigned long) range in Py2Reg
+  when passed a REG_DWORD value. Fixes OverflowError in winreg.SetValueEx.
+
 - Issue #16602: When a weakref's target was part of a long deallocation
   chain, the object could remain reachable through its weakref even though
   its refcount had dropped to zero.
diff --git a/PC/winreg.c b/PC/winreg.c
index 454ea53..523ad17 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -785,7 +785,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
                 memcpy(*retDataBuf, &zero, sizeof(DWORD));
             }
             else {
-                DWORD d = PyLong_AsLong(value);
+                DWORD d = PyLong_AsUnsignedLong(value);
                 memcpy(*retDataBuf, &d, sizeof(DWORD));
             }
             break;
-- 
cgit v0.12

