Skip to content

Commit

Permalink
Clipboard: Remove NUL char addition for non text formats (#2184)
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiFati authored Mar 14, 2024
1 parent 2dfa2bb commit ad20c9b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
15 changes: 12 additions & 3 deletions win32/src/win32clipboardmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,11 @@ static PyObject *py_register_clipboard_format(PyObject *self, PyObject *args)
// info.
}

static bool isTextFormat(int format)
{
return ((format == CF_TEXT) || (format == CF_UNICODETEXT) || (format == CF_OEMTEXT));
}

//*****************************************************************************
//
// @pymethod int|win32clipboard|SetClipboardData|The SetClipboardData function
Expand Down Expand Up @@ -845,15 +850,19 @@ static PyObject *py_set_clipboard_data(PyObject *self, PyObject *args)
PyWinBufferView pybuf;
// In py3k, unicode no longer supports buffer interface
if (PyUnicode_Check(obhandle)) {
buf = tmpw = obhandle; if (!tmpw) return NULL;
bufSize = (tmpw.length + 1) * sizeof(WCHAR);
buf = tmpw = obhandle;
if (!tmpw)
return NULL;
bufSize = tmpw.length * sizeof(WCHAR);
if (isTextFormat(format))
bufSize += sizeof(WCHAR);
}
else {
if (!pybuf.init(obhandle))
return NULL;
buf = pybuf.ptr();
bufSize = pybuf.len();
if (PyBytes_Check(obhandle))
if ((PyBytes_Check(obhandle)) && (isTextFormat(format)))
bufSize++; // size doesnt include nulls!
// else assume buffer needs no terminator...
}
Expand Down
60 changes: 60 additions & 0 deletions win32/test/test_win32clipboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# tests for win32gui
import unittest

import win32clipboard


class TestGetSetClipboardData(unittest.TestCase):

def copyData(self, data, format_):
win32clipboard.OpenClipboard()
ret = None
try:
win32clipboard.SetClipboardData(format_, data)
ret = win32clipboard.GetClipboardData(format_)
finally:
win32clipboard.CloseClipboard()
return ret

def copyText(self, data, format_):
win32clipboard.OpenClipboard()
ret = None
try:
win32clipboard.SetClipboardText(data, format_)
ret = win32clipboard.GetClipboardData(format_)
finally:
win32clipboard.CloseClipboard()
return ret

def test_data(self):
test_data = {
"Dummy str": win32clipboard.CF_UNICODETEXT,
b"Dummy bytes text": win32clipboard.CF_TEXT,
b"Dummy\x00\xFF bytes": win32clipboard.CF_DIB,
}
for data, fmt in test_data.items():
self.assertEqual(data, self.copyData(data, fmt))
test_data = {
"Dummy str": (win32clipboard.CF_TEXT, win32clipboard.CF_DIB),
b"Dummy\x00\xFF bytes": (win32clipboard.CF_UNICODETEXT,),
}
for data, formats in test_data.items():
for fmt in formats:
self.assertNotEqual(data, self.copyData(data, fmt))

def test_text(self):
test_data = {
"Dummy str": win32clipboard.CF_UNICODETEXT,
b"Dummy bytes": win32clipboard.CF_TEXT,
}
for data, fmt in test_data.items():
self.assertEqual(data, self.copyText(data, fmt))
self.assertRaises(ValueError, self.copyText, data, win32clipboard.CF_DIB)
s = "Dummy str"
self.assertEqual(
s.encode(), self.copyText(s, win32clipboard.CF_TEXT)
) # @TODO - cfati: Do we want this?


if __name__ == "__main__":
unittest.main()

0 comments on commit ad20c9b

Please sign in to comment.