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

Skip to content

Commit 4e80bb5

Browse files
committed
Allow any object supporting the buffer protocol to be written as a binary object.
1 parent abfeff7 commit 4e80bb5

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

PC/_winreg.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -830,19 +830,23 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
830830
if (value == Py_None)
831831
*retDataSize = 0;
832832
else {
833-
if (!PyString_Check(value))
834-
return 0;
835-
*retDataSize = PyString_Size(value);
833+
void *src_buf;
834+
PyBufferProcs *pb = value->ob_type->tp_as_buffer;
835+
if (pb==NULL) {
836+
PyErr_Format(PyExc_TypeError,
837+
"Objects of type '%s' can not "
838+
"be used as binary registry values",
839+
value->ob_type->tp_name);
840+
return FALSE;
841+
}
842+
*retDataSize = (*pb->bf_getreadbuffer)(value, 0, &src_buf);
836843
*retDataBuf = (BYTE *)PyMem_NEW(char,
837844
*retDataSize);
838845
if (*retDataBuf==NULL){
839846
PyErr_NoMemory();
840847
return FALSE;
841848
}
842-
memcpy(*retDataBuf,
843-
PyString_AS_STRING(
844-
(PyStringObject *)value),
845-
*retDataSize);
849+
memcpy(*retDataBuf, src_buf, *retDataSize);
846850
}
847851
break;
848852
}

0 commit comments

Comments
 (0)