]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix #14420. Check for PyLong as well as PyInt when converting in Py2Reg.
authorBrian Curtin <brian@python.org>
Thu, 27 Dec 2012 18:28:51 +0000 (12:28 -0600)
committerBrian Curtin <brian@python.org>
Thu, 27 Dec 2012 18:28:51 +0000 (12:28 -0600)
This fixes a ValueError seen in winreg.SetValueEx when passed long
winreg.REG_DWORD values that should be supported by the underlying API.

Lib/test/test_winreg.py
Misc/NEWS
PC/_winreg.c

index a741b65e07f3fa55e05978a96d198abd82792ef2..c4ae73746cab50dc14810e293fe0ab725014ccdc 100644 (file)
@@ -314,6 +314,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):
index 7642e6067a17d8293e76a2e587dd3ce2faba4433..ea9e6bf0528191f2deecc558cec63fad6854dae6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,10 @@ What's New in Python 2.7.4
 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.
index 445c3edd76e04c2b2942436d5dd0404de4cdda14..813e99383adc1290a1675e595ae8b2d52307a39a 100644 (file)
@@ -753,7 +753,8 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
     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 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
                 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: