# Buggy code
def win32_clipboard_get():
    """Get the current clipboard's text on Windows.

    Requires Mark Hammond's pywin32 extensions.
    """
    try:
        import win32clipboard
    except ImportError:
        raise TryNext(
            "Getting text from the clipboard requires the pywin32 "
            "extensions: http://sourceforge.net/projects/pywin32/"
        )
    win32clipboard.OpenClipboard()
    text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
    # FIXME: convert \r\n to \n?
    win32clipboard.CloseClipboard()
    return text


# Traceback (TypeError)
In [1]: %paste
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-86b9186405a3> in <module>()
----> 1 get_ipython().magic('paste')

/usr/local/lib/python3.3/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
2135         magic_name, _, magic_arg_s = arg_s.partition(' ')
2136         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2137         return self.run_line_magic(magic_name, magic_arg_s)
2138
2139     #-------------------------------------------------------------------------

/usr/local/lib/python3.3/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
2061                 args.append(sys._getframe(stack_depth).f_locals)
2062             with self.builtin_trap:
-> 2063                 result = fn(*args)
2064             return result
2065

/usr/local/lib/python3.3/site-packages/IPython/frontend/terminal/interactiveshell.py in paste(self, parameter_s)

/usr/local/lib/python3.3/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
190     # but it's overkill for just that one bit of state.
191     def magic_deco(arg):
--> 192         call = lambda f, *a, **k: f(*a, **k)
193
194         if isinstance(arg, collections.Callable):

/usr/local/lib/python3.3/site-packages/IPython/frontend/terminal/interactiveshell.py in paste(self, parameter_s)
282         if 'q' not in opts:
283             write = self.shell.write
--> 284             write(self.shell.pycolorize(block))
285             if not block.endswith('\n'):
286                 write('\n')

/usr/local/lib/python3.3/site-packages/IPython/core/interactiveshell.py in <lambda>(src)
575         # Python source parser/formatter for syntax highlighting
576         pyformat = PyColorize.Parser().format
--> 577         self.pycolorize = lambda src: pyformat(src,'str',self.colors)
578
579     def init_pushd_popd_magic(self):

/usr/local/lib/python3.3/site-packages/IPython/utils/PyColorize.py in format(self, raw, out, scheme)
127
128     def format(self, raw, out = None, scheme = ''):
--> 129         return self.format2(raw, out, scheme)[0]
130
131     def format2(self, raw, out = None, scheme = ''):

/usr/local/lib/python3.3/site-packages/IPython/utils/PyColorize.py in format2(self, raw, out, scheme)
174         lines_append = self.lines.append
175         while 1:
--> 176             pos = raw_find('\n', pos) + 1
177             if not pos: break
178             lines_append(pos)

TypeError: Type str doesn't support the buffer API

# Fixed code
def win32_clipboard_get():
    """Get the current clipboard's text on Windows.

    Requires Mark Hammond's pywin32 extensions.
    """
    try:
        import win32clipboard
    except ImportError:
        raise TryNext(
            "Getting text from the clipboard requires the pywin32 "
            "extensions: http://sourceforge.net/projects/pywin32/"
        )
    win32clipboard.OpenClipboard()
    try:
        text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
    except TypeError:
        try:
            text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
            text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
        except TypeError:
            raise ClipboardEmpty
    finally:
        win32clipboard.CloseClipboard()
    return text