Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: to_clipboard text truncated for Python 3 on Windows for UTF-16 text #25040

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions pandas/io/clipboard/windows.py
Expand Up @@ -3,7 +3,7 @@
"""
import contextlib
import ctypes
from ctypes import c_size_t, c_wchar, c_wchar_p, get_errno, sizeof
from ctypes import c_char_p, c_size_t, c_wchar, c_wchar_p, get_errno, sizeof
import time

from .exceptions import PyperclipWindowsException
Expand Down Expand Up @@ -129,13 +129,15 @@ def copy_windows(text):
# If the hMem parameter identifies a memory object,
# the object must have been allocated using the
# function with the GMEM_MOVEABLE flag.
count = len(text) + 1
text = text.encode('utf-16LE')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a vendored copy of pyperclip, is this changed in the upstream?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. They had a different fix in their release. Should I copy their code over and update my pr?

mem_size = len(text) + sizeof(c_wchar)
handle = safeGlobalAlloc(GMEM_MOVEABLE,
count * sizeof(c_wchar))
mem_size)

locked_handle = safeGlobalLock(handle)

ctypes.memmove(c_wchar_p(locked_handle),
c_wchar_p(text), count * sizeof(c_wchar))
ctypes.memmove(c_char_p(locked_handle),
c_char_p(text), mem_size)

safeGlobalUnlock(handle)
safeSetClipboardData(CF_UNICODETEXT, handle)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/io/test_clipboard.py
Expand Up @@ -225,3 +225,16 @@ def test_invalid_encoding(self, df):
@pytest.mark.parametrize('enc', ['UTF-8', 'utf-8', 'utf8'])
def test_round_trip_valid_encodings(self, enc, df):
self.check_round_trip_frame(df, encoding=enc)


@pytest.mark.single
@pytest.mark.clipboard
@pytest.mark.skipif(not _DEPS_INSTALLED,
reason="clipboard primitives not installed")
class TestRawClipboard(object):

@pytest.mark.parametrize('data', [u'\U0001f44d...', u'Ωœ∑´...', 'abcd...'])
def test_raw_roundtrip(self, data):
import pandas.io.clipboard
pandas.io.clipboard.clipboard_set(data)
assert data == pandas.io.clipboard.clipboard_get()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can you just import clipboard_set directly (and move it to the top)
  • Let's just use test functions (instead of a class). We are trying to be more pytest-idiomatic.
  • Can you add your original issue example as a test to this file?