[Python-checkins] cpython (2.7): Fix #14420. Check for PyLong as well as PyInt when converting in Py2Reg.

brian.curtin python-checkins at python.org
Thu Dec 27 19:29:20 CET 2012


http://hg.python.org/cpython/rev/ccbb16719540
changeset:   81086:ccbb16719540
branch:      2.7
parent:      81082:44609ff7e53c
user:        Brian Curtin <brian at python.org>
date:        Thu Dec 27 12:28:51 2012 -0600
summary:
  Fix #14420. Check for PyLong as well as PyInt when converting in Py2Reg.

This fixes a ValueError seen in winreg.SetValueEx when passed long
winreg.REG_DWORD values that should be supported by the underlying API.

files:
  Lib/test/test_winreg.py |  12 ++++++++++++
  Misc/NEWS               |   4 ++++
  PC/_winreg.c            |  11 ++++++-----
  3 files changed, 22 insertions(+), 5 deletions(-)


diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py
--- a/Lib/test/test_winreg.py
+++ b/Lib/test/test_winreg.py
@@ -314,6 +314,18 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,10 @@
 Core and Builtins
 -----------------
 
+- Issue #14420: Support the full DWORD (unsigned long) range in Py2Reg
+  when passed a REG_DWORD value. Fixes ValueError in winreg.SetValueEx when
+  given a long.
+
 - Issue #13863: Work around buggy 'fstat' implementation on Windows / NTFS that
   lead to incorrect timestamps (off by one hour) being stored in .pyc files on
   some systems.
diff --git a/PC/_winreg.c b/PC/_winreg.c
--- a/PC/_winreg.c
+++ b/PC/_winreg.c
@@ -753,7 +753,8 @@
     Py_ssize_t i,j;
     switch (typ) {
         case REG_DWORD:
-            if (value != Py_None && !PyInt_Check(value))
+            if (value != Py_None &&
+                !(PyInt_Check(value) || PyLong_Check(value)))
                 return FALSE;
             *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
             if (*retDataBuf==NULL){
@@ -765,10 +766,10 @@
                 DWORD zero = 0;
                 memcpy(*retDataBuf, &zero, sizeof(DWORD));
             }
-            else
-                memcpy(*retDataBuf,
-                       &PyInt_AS_LONG((PyIntObject *)value),
-                       sizeof(DWORD));
+            else {
+                DWORD d = PyLong_AsUnsignedLong(value);
+                memcpy(*retDataBuf, &d, sizeof(DWORD));
+            }
             break;
         case REG_SZ:
         case REG_EXPAND_SZ:

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list