Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8c360e

Browse files
committed
SF patch# 1755229 by Amaury Forgeot d'Arc: fix _winreg module and tests.
Untested.
1 parent 52b8976 commit a8c360e

2 files changed

Lines changed: 24 additions & 34 deletions

File tree

Lib/test/test_winreg.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from _winreg import *
55
import os, sys
66

7-
from test.test_support import verify, have_unicode
7+
from test.test_support import verify
88

99
test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
1010

@@ -13,17 +13,10 @@
1313
("String Val", "A string value", REG_SZ),
1414
("StringExpand", "The path is %path%", REG_EXPAND_SZ),
1515
("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
16-
("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
16+
("Raw Data", bytes("binary"+chr(0)+"data"), REG_BINARY),
1717
("Big String", "x"*(2**14-1), REG_SZ),
18-
("Big Binary", "x"*(2**14), REG_BINARY),
18+
("Big Binary", b"x"*(2**14), REG_BINARY),
1919
]
20-
if have_unicode:
21-
test_data+=[
22-
(str("Unicode Val"), str("A Unicode value"), REG_SZ,),
23-
("UnicodeExpand", str("The path is %path%"), REG_EXPAND_SZ),
24-
("Multi-unicode", [str("Lots"), str("of"), str("unicode"), str("values")], REG_MULTI_SZ),
25-
("Multi-mixed", [str("Unicode"), str("and"), "string", "values"],REG_MULTI_SZ),
26-
]
2720

2821
def WriteTestData(root_key):
2922
# Set the default value for this key.
@@ -65,7 +58,7 @@ def WriteTestData(root_key):
6558
def ReadTestData(root_key):
6659
# Check we can get default value for this key.
6760
val = QueryValue(root_key, test_key_name)
68-
verify(val=="Default value", "Registry didn't give back the correct value")
61+
verify(type(val) is str and val=="Default value", "Registry didn't give back the correct value")
6962

7063
key = OpenKey(root_key, test_key_name)
7164
# Read the sub-keys

PC/_winreg.c

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ PyHKEY_strFunc(PyObject *ob)
404404
PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
405405
char resBuf[160];
406406
wsprintf(resBuf, "<PyHKEY:%p>", pyhkey->hkey);
407-
return PyString_FromString(resBuf);
407+
return PyUnicode_FromString(resBuf);
408408
}
409409

410410
static int
@@ -444,6 +444,7 @@ static PyNumberMethods PyHKEY_NumberMethods =
444444
PyHKEY_binaryFailureFunc, /* nb_and */
445445
PyHKEY_binaryFailureFunc, /* nb_xor */
446446
PyHKEY_binaryFailureFunc, /* nb_or */
447+
NULL, /* nb_coerce */
447448
PyHKEY_intFunc, /* nb_int */
448449
PyHKEY_unaryFailureFunc, /* nb_long */
449450
PyHKEY_unaryFailureFunc, /* nb_float */
@@ -729,11 +730,10 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
729730
return FALSE;
730731
need_decref = 1;
731732
}
732-
if (!PyString_Check(value))
733+
if (!PyBytes_Check(value))
733734
return FALSE;
734735
*retDataSize = 1 + strlen(
735-
PyString_AS_STRING(
736-
(PyStringObject *)value));
736+
PyBytes_AS_STRING(value));
737737
}
738738
*retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
739739
if (*retDataBuf==NULL){
@@ -744,8 +744,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
744744
strcpy((char *)*retDataBuf, "");
745745
else
746746
strcpy((char *)*retDataBuf,
747-
PyString_AS_STRING(
748-
(PyStringObject *)value));
747+
PyBytes_AS_STRING(value));
749748
if (need_decref)
750749
Py_DECREF(value);
751750
break;
@@ -770,7 +769,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
770769
PyObject *t;
771770
t = PyList_GET_ITEM(
772771
(PyListObject *)value,j);
773-
if (PyString_Check(t)) {
772+
if (PyBytes_Check(t)) {
774773
obs[j] = t;
775774
Py_INCREF(t);
776775
} else if (PyUnicode_Check(t)) {
@@ -783,8 +782,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
783782
} else
784783
goto reg_multi_fail;
785784
size += 1 + strlen(
786-
PyString_AS_STRING(
787-
(PyStringObject *)obs[j]));
785+
PyBytes_AS_STRING(obs[j]));
788786
}
789787

790788
*retDataSize = size + 1;
@@ -800,12 +798,9 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
800798
{
801799
PyObject *t;
802800
t = obs[j];
803-
strcpy(P,
804-
PyString_AS_STRING(
805-
(PyStringObject *)t));
801+
strcpy(P, PyBytes_AS_STRING(t));
806802
P += 1 + strlen(
807-
PyString_AS_STRING(
808-
(PyStringObject *)t));
803+
PyBytes_AS_STRING(t));
809804
Py_DECREF(obs[j]);
810805
}
811806
/* And doubly-terminate the list... */
@@ -922,7 +917,7 @@ Reg2Py(char *retDataBuf, DWORD retDataSize, DWORD typ)
922917
obData = Py_None;
923918
}
924919
else
925-
obData = Py_BuildValue("s#",
920+
obData = Py_BuildValue("y#",
926921
(char *)retDataBuf,
927922
retDataSize);
928923
break;
@@ -1047,7 +1042,7 @@ PyEnumKey(PyObject *self, PyObject *args)
10471042
if (rc != ERROR_SUCCESS)
10481043
return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
10491044

1050-
retStr = PyString_FromStringAndSize(tmpbuf, len);
1045+
retStr = PyUnicode_FromStringAndSize(tmpbuf, len);
10511046
return retStr; /* can be NULL */
10521047
}
10531048

@@ -1109,7 +1104,7 @@ PyEnumValue(PyObject *self, PyObject *args)
11091104
retVal = NULL;
11101105
goto fail;
11111106
}
1112-
retVal = Py_BuildValue("sOi", retValueBuf, obData, typ);
1107+
retVal = Py_BuildValue("UOi", retValueBuf, obData, typ);
11131108
Py_DECREF(obData);
11141109
fail:
11151110
PyMem_Free(retValueBuf);
@@ -1232,17 +1227,19 @@ PyQueryValue(PyObject *self, PyObject *args)
12321227
!= ERROR_SUCCESS)
12331228
return PyErr_SetFromWindowsErrWithFunction(rc,
12341229
"RegQueryValue");
1235-
retStr = PyString_FromStringAndSize(NULL, bufSize);
1236-
if (retStr == NULL)
1237-
return NULL;
1238-
retBuf = PyString_AS_STRING(retStr);
1230+
retBuf = (char *)PyMem_Malloc(bufSize);
1231+
if (retBuf == NULL)
1232+
return PyErr_NoMemory();
1233+
12391234
if ((rc = RegQueryValue(hKey, subKey, retBuf, &bufSize))
12401235
!= ERROR_SUCCESS) {
1241-
Py_DECREF(retStr);
1236+
PyMem_Free(retBuf);
12421237
return PyErr_SetFromWindowsErrWithFunction(rc,
12431238
"RegQueryValue");
12441239
}
1245-
_PyString_Resize(&retStr, strlen(retBuf));
1240+
1241+
retStr = PyUnicode_DecodeMBCS(retBuf, strlen(retBuf), NULL);
1242+
PyMem_Free(retBuf);
12461243
return retStr;
12471244
}
12481245

0 commit comments

Comments
 (0)