# Buggy code
def add_cached_package(pdir, url, overwrite=False, urlstxt=False):
    """
    Adds a new package to the cache. The URL is used to determine the
    package filename and channel, and the directory pdir is scanned for
    both a compressed and an extracted version of that package. If
    urlstxt=True, this URL will be appended to the urls.txt file in the
    cache, so that subsequent runs will correctly identify the package.
    """
    package_cache()
    if "/" in url:
        dist = url.rsplit("/", 1)[-1]
    else:
        dist = url
        url = None
    if dist.endswith(".tar.bz2"):
        fname = dist
        dist = dist[:-8]
    else:
        fname = dist + ".tar.bz2"
    xpkg = join(pdir, fname)
    if not overwrite and xpkg in fname_table_:
        return
    if not isfile(xpkg):
        xpkg = None
    xdir = join(pdir, dist)
    if not (
        isdir(xdir)
        and isfile(join(xdir, "info", "files"))
        and isfile(join(xdir, "info", "index.json"))
    ):
        xdir = None
    if not (xpkg or xdir):
        return
    url = remove_binstar_tokens(url)
    _, schannel = url_channel(url)
    prefix = "" if schannel == "defaults" else schannel + "::"
    xkey = xpkg or (xdir + ".tar.bz2")
    fname_table_[xkey] = fname_table_[url_path(xkey)] = prefix
    fkey = prefix + dist
    rec = package_cache_.get(fkey)
    if rec is None:
        rec = package_cache_[fkey] = dict(files=[], dirs=[], urls=[])
    if url and url not in rec["urls"]:
        rec["urls"].append(url)
    if xpkg and xpkg not in rec["files"]:
        rec["files"].append(xpkg)
    if xdir and xdir not in rec["dirs"]:
        rec["dirs"].append(xdir)
    if urlstxt:
        try:
            with open(join(pdir, "urls.txt"), "a") as fa:
                fa.write("%s\n" % url)
        except IOError:
            pass


# Traceback (TypeError)
$ conda search
...
Traceback (most recent call last):
File "/Users/ilan/python/bin/conda", line 6, in <module>
sys.exit(conda.cli.main())
File "/Users/ilan/conda/conda/cli/main.py", line 120, in main
args_func(args, p)
File "/Users/ilan/conda/conda/cli/main.py", line 127, in args_func
args.func(args, p)
File "/Users/ilan/conda/conda/cli/main_search.py", line 118, in execute
execute_search(args, parser)
File "/Users/ilan/conda/conda/cli/main_search.py", line 155, in execute_search
extracted = conda.install.extracted()
File "/Users/ilan/conda/conda/install.py", line 749, in extracted
return set(dist for dist, rec in package_cache().items() if rec['dirs'])
File "/Users/ilan/conda/conda/install.py", line 673, in package_cache
add_cached_package(pdir, fn)
File "/Users/ilan/conda/conda/install.py", line 627, in add_cached_package
url = remove_binstar_tokens(url)
File "/Users/ilan/conda/conda/config.py", line 258, in remove_binstar_tokens
return BINSTAR_TOKEN_PAT.sub(r'\1', url)
TypeError: expected string or buffer

# Fixed code
def add_cached_package(pdir, url, overwrite=False, urlstxt=False):
    """
    Adds a new package to the cache. The URL is used to determine the
    package filename and channel, and the directory pdir is scanned for
    both a compressed and an extracted version of that package. If
    urlstxt=True, this URL will be appended to the urls.txt file in the
    cache, so that subsequent runs will correctly identify the package.
    """
    package_cache()
    if "/" in url:
        dist = url.rsplit("/", 1)[-1]
    else:
        dist = url
        url = None
    if dist.endswith(".tar.bz2"):
        fname = dist
        dist = dist[:-8]
    else:
        fname = dist + ".tar.bz2"
    xpkg = join(pdir, fname)
    if not overwrite and xpkg in fname_table_:
        return
    if not isfile(xpkg):
        xpkg = None
    xdir = join(pdir, dist)
    if not (
        isdir(xdir)
        and isfile(join(xdir, "info", "files"))
        and isfile(join(xdir, "info", "index.json"))
    ):
        xdir = None
    if not (xpkg or xdir):
        return
    if url:
        url = remove_binstar_tokens(url)
    _, schannel = url_channel(url)
    prefix = "" if schannel == "defaults" else schannel + "::"
    xkey = xpkg or (xdir + ".tar.bz2")
    fname_table_[xkey] = fname_table_[url_path(xkey)] = prefix
    fkey = prefix + dist
    rec = package_cache_.get(fkey)
    if rec is None:
        rec = package_cache_[fkey] = dict(files=[], dirs=[], urls=[])
    if url and url not in rec["urls"]:
        rec["urls"].append(url)
    if xpkg and xpkg not in rec["files"]:
        rec["files"].append(xpkg)
    if xdir and xdir not in rec["dirs"]:
        rec["dirs"].append(xdir)
    if urlstxt:
        try:
            with open(join(pdir, "urls.txt"), "a") as fa:
                fa.write("%s\n" % url)
        except IOError:
            pass