# Buggy code
def to_clipboard(obj, excel=None, sep=None, **kwargs):  # pragma: no cover
    """
    Attempt to write text representation of object to the system clipboard
    The clipboard can be then pasted into Excel for example.

    Parameters
    ----------
    obj : the object to write to the clipboard
    excel : boolean, defaults to True
            if True, use the provided separator, writing in a csv
            format for allowing easy pasting into excel.
            if False, write a string representation of the object
            to the clipboard
    sep : optional, defaults to tab
    other keywords are passed to to_csv

    Notes
    -----
    Requirements for your platform
      - Linux: xclip, or xsel (with gtk or PyQt4 modules)
      - Windows:
      - OS X:
    """
    from pandas.util.clipboard import clipboard_set

    if excel is None:
        excel = True

    if excel:
        try:
            if sep is None:
                sep = "\t"
            buf = StringIO()
            obj.to_csv(buf, sep=sep, **kwargs)
            clipboard_set(buf.getvalue())
            return
        except:
            pass

    if isinstance(obj, DataFrame):
        # str(df) has various unhelpful defaults, like truncation
        with option_context("display.max_colwidth", 999999):
            objstr = obj.to_string(**kwargs)
    else:
        objstr = str(obj)
    clipboard_set(objstr)


# Traceback (OSError)
# Your code here
In [1]: df = pd.DataFrame(np.random.randn(10, 2))

In [2]: df.to_clipboard()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-1f8b11f0ff98> in <module>()
----> 1 df.to_clipboard()

c:\users\chris\documents\python-dev\pandas\pandas\core\generic.py in to_clipboard(self, excel, sep, **kwargs)
1236         """
1237         from pandas.io import clipboard
-> 1238         clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs)
1239
1240     def to_xarray(self):

c:\users\chris\documents\python-dev\pandas\pandas\io\clipboard.py in to_clipboard(obj, excel, sep, **kwargs)
96     else:
97         objstr = str(obj)
---> 98     clipboard_set(objstr)

c:\users\chris\documents\python-dev\pandas\pandas\util\clipboard.py in _copyWindows(text)
83                                  len(text.encode('utf-16-le')) + 2)
84     pchData = d.kernel32.GlobalLock(hCd)
---> 85     ctypes.cdll.msvcrt.wcscpy(ctypes.c_wchar_p(pchData), text)
86     d.kernel32.GlobalUnlock(hCd)
87     d.user32.SetClipboardData(CF_UNICODETEXT, hCd)

OSError: exception: access violation writing 0x0000000000000000

In [4]: pd.read_clipboard()
<segfault>

# Fixed code
def to_clipboard(obj, excel=None, sep=None, **kwargs):  # pragma: no cover
    """
    Attempt to write text representation of object to the system clipboard
    The clipboard can be then pasted into Excel for example.

    Parameters
    ----------
    obj : the object to write to the clipboard
    excel : boolean, defaults to True
            if True, use the provided separator, writing in a csv
            format for allowing easy pasting into excel.
            if False, write a string representation of the object
            to the clipboard
    sep : optional, defaults to tab
    other keywords are passed to to_csv

    Notes
    -----
    Requirements for your platform
      - Linux: xclip, or xsel (with gtk or PyQt4 modules)
      - Windows:
      - OS X:
    """
    encoding = kwargs.pop("encoding", "utf-8")

    # testing if an invalid encoding is passed to clipboard
    if encoding is not None and encoding.lower().replace("-", "") != "utf8":
        raise ValueError("clipboard only supports utf-8 encoding")

    from pandas.util.clipboard import clipboard_set

    if excel is None:
        excel = True

    if excel:
        try:
            if sep is None:
                sep = "\t"
            buf = StringIO()
            # clipboard_set (pyperclip) expects unicode
            obj.to_csv(buf, sep=sep, encoding="utf-8", **kwargs)
            text = buf.getvalue()
            if PY2:
                text = text.decode("utf-8")
            clipboard_set(text)
            return
        except:
            pass

    if isinstance(obj, DataFrame):
        # str(df) has various unhelpful defaults, like truncation
        with option_context("display.max_colwidth", 999999):
            objstr = obj.to_string(**kwargs)
    else:
        objstr = str(obj)
    clipboard_set(objstr)