pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.5k stars 17.87k forks source link

to_clipboard() locks clipboard system-wide on exception #8304

Closed klonuo closed 8 years ago

klonuo commented 10 years ago

Exception in the following code locks clipboard system-wide until I exit my interactive Python session:

import pandas as pd
df = pd.DataFrame.from_dict({1: u' \u2014'}, 'index')
df.to_clipboard()
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-58-1f8b11f0ff98> in <module>()
----> 1 df.to_clipboard()

C:\Python27\lib\site-packages\pandas\core\generic.pyc in to_clipboard(self, excel, sep, **kwargs)
   1000         from pandas.io import clipboard
-> 1001         clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs)
   1002 
   1003     #----------------------------------------------------------------------

C:\Python27\lib\site-packages\pandas\io\clipboard.pyc in to_clipboard(obj, excel, sep, **kwargs)
     95     else:
     96         objstr = str(obj)
---> 97     clipboard_set(objstr)

C:\Python27\lib\site-packages\pandas\util\clipboard.pyc in winSetClipboard(text)
     63     try:
     64         # works on Python 2 (bytes() only takes one argument)
---> 65         hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
     66     except TypeError:
     67         # works on Python 3 (bytes() requires an encoding)

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2014' in position 10: ordinal not in range(128)

pandas 0.14.1

jreback commented 10 years ago

where its failing is copied from pyperclip. (though its included in pandas under license). So it is possible to modify.

I suppose you can just do those windows locks in a with. Though I am not really familiar with how windows works in that regard.

jreback commented 8 years ago

closing as stale. pls reopen if still an issue.