# Buggy code
def osx_clipboard_get():
    """Get the clipboard's text on OS X."""
    p = subprocess.Popen(["pbpaste", "-Prefer", "ascii"], stdout=subprocess.PIPE)
    text, stderr = p.communicate()
    # Text comes in with old Mac \r line endings. Change them to \n.
    text = text.replace("\r", "\n")
    text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
    return text


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

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/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     #-------------------------------------------------------------------------

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/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

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/IPython/frontend/terminal/interactiveshell.py in paste(self, parameter_s)

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/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):

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/IPython/frontend/terminal/interactiveshell.py in paste(self, parameter_s)
270             return
271         try:
--> 272             block = self.shell.hooks.clipboard_get()
273         except TryNext as clipboard_exc:
274             message = getattr(clipboard_exc, 'args')

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/IPython/core/hooks.py in __call__(self, *args, **kw)
136             #print "prio",prio,"cmd",cmd #dbg
137             try:
--> 138                 return cmd(*args, **kw)
139             except TryNext as exc:
140                 last_exc = exc

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/IPython/core/hooks.py in clipboard_get(self)
227     for func in chain:
228         dispatcher.add(func)
--> 229     text = dispatcher()
230     return text

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/IPython/core/hooks.py in __call__(self, *args, **kw)
136             #print "prio",prio,"cmd",cmd #dbg
137             try:
--> 138                 return cmd(*args, **kw)
139             except TryNext as exc:
140                 last_exc = exc

/sw/lib/python3.3/site-packages/ipython-0.13.1-py3.3.egg/IPython/lib/clipboard.py in osx_clipboard_get()
31     text, stderr = p.communicate()
32     # Text comes in with old Mac \r line endings. Change them to \n.
---> 33     text = text.replace('\r', '\n')
34     return text
35

TypeError: expected bytes, bytearray or buffer compatible object

# Fixed code
def osx_clipboard_get():
    """Get the clipboard's text on OS X."""
    p = subprocess.Popen(["pbpaste", "-Prefer", "ascii"], stdout=subprocess.PIPE)
    text, stderr = p.communicate()
    # Text comes in with old Mac \r line endings. Change them to \n.
    text = text.replace(b"\r", b"\n")
    text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
    return text