# Buggy code
def _make_single_record(self, package_filename):
    if not package_filename.endswith(CONDA_TARBALL_EXTENSION):
        package_filename += CONDA_TARBALL_EXTENSION

    package_tarball_full_path = join(self.pkgs_dir, package_filename)
    log.trace("adding to package cache %s", package_tarball_full_path)
    extracted_package_dir = package_tarball_full_path[: -len(CONDA_TARBALL_EXTENSION)]

    # try reading info/repodata_record.json
    try:
        repodata_record = read_repodata_json(extracted_package_dir)
        package_cache_record = PackageCacheRecord.from_objects(
            repodata_record,
            package_tarball_full_path=package_tarball_full_path,
            extracted_package_dir=extracted_package_dir,
        )
        return package_cache_record
    except (IOError, OSError):
        # no info/repodata_record.json exists
        # try reading info/index.json
        try:
            index_json_record = read_index_json(extracted_package_dir)
        except (IOError, OSError):
            # info/index.json doesn't exist either
            if isdir(extracted_package_dir) and not isfile(package_tarball_full_path):
                # We have a directory that looks like a conda package, but without
                # (1) info/repodata_record.json or info/index.json, and (2) a conda package
                # tarball, there's not much we can do.  We'll just ignore it.
                return None

            try:
                if self.is_writable:
                    if isdir(extracted_package_dir):
                        # We have a partially unpacked conda package directory. Best thing
                        # to do is remove it and try extracting.
                        rm_rf(extracted_package_dir)
                    extract_tarball(package_tarball_full_path, extracted_package_dir)
                    index_json_record = read_index_json(extracted_package_dir)
                else:
                    index_json_record = read_index_json_from_tarball(
                        package_tarball_full_path
                    )
            except (EOFError, ReadError):
                # EOFError: Compressed file ended before the end-of-stream marker was reached
                # tarfile.ReadError: file could not be opened successfully
                rm_rf(package_tarball_full_path)
                return None

        if isfile(package_tarball_full_path):
            md5 = compute_md5sum(package_tarball_full_path)
        else:
            md5 = None

        url = first(self._urls_data, lambda x: basename(x) == package_filename)
        package_cache_record = PackageCacheRecord.from_objects(
            index_json_record,
            url=url,
            md5=md5,
            package_tarball_full_path=package_tarball_full_path,
            extracted_package_dir=extracted_package_dir,
        )

        # write the info/repodata_record.json file so we can short-circuit this next time
        if self.is_writable:
            repodata_record = PackageRecord.from_objects(package_cache_record)
            repodata_record_path = join(
                extracted_package_dir, "info", "repodata_record.json"
            )
            write_as_json_to_file(repodata_record_path, repodata_record)

        return package_cache_record


# Traceback (ValueError)
Traceback (most recent call last):
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/exceptions.py", line 722, in __call__
return func(*args, **kwargs)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 78, in _main
exit_code = do_call(args, p)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/cli/conda_argparse.py", line 75, in do_call
exit_code = getattr(module, func_name)(args, parser)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/cli/main_install.py", line 11, in execute
install(args, parser, 'install')
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 239, in install
handle_txn(progressive_fetch_extract, unlink_link_transaction, prefix, args, newenv)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 256, in handle_txn
unlink_link_transaction.display_actions(progressive_fetch_extract)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/link.py", line 706, in display_actions
legacy_action_groups = self.make_legacy_action_groups(pfe)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/link.py", line 689, in make_legacy_action_groups
pfe.prepare()
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/common/io.py", line 402, in decorated
return f(*args, **kwds)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 508, in prepare
for prec in self.link_precs)
File "/home/pablo/anaconda/lib/python2.7/_abcoll.py", line 571, in update
for key, value in other:
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 508, in <genexpr>
for prec in self.link_precs)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 402, in make_actions_for_record
), None)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 399, in <genexpr>
pcrec for pcrec in concat(PackageCache(pkgs_dir).query(pref_or_spec)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 400, in <genexpr>
for pkgs_dir in context.pkgs_dirs)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 116, in query
return (pcrec for pcrec in itervalues(self._package_cache_records) if pcrec == param)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 209, in _package_cache_records
self.load()
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 88, in load
package_cache_record = self._make_single_record(base_name)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/core/package_cache.py", line 279, in _make_single_record
index_json_record = read_index_json(extracted_package_dir)
File "/home/pablo/anaconda/lib/python2.7/site-packages/conda/gateways/disk/read.py", line 112, in read_index_json
record = IndexJsonRecord(**json.load(fi))
File "/home/pablo/anaconda/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/home/pablo/anaconda/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/home/pablo/anaconda/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/pablo/anaconda/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

# Fixed code
def _make_single_record(self, package_filename):
    if not package_filename.endswith(CONDA_TARBALL_EXTENSION):
        package_filename += CONDA_TARBALL_EXTENSION

    package_tarball_full_path = join(self.pkgs_dir, package_filename)
    log.trace("adding to package cache %s", package_tarball_full_path)
    extracted_package_dir = package_tarball_full_path[: -len(CONDA_TARBALL_EXTENSION)]

    # try reading info/repodata_record.json
    try:
        repodata_record = read_repodata_json(extracted_package_dir)
        package_cache_record = PackageCacheRecord.from_objects(
            repodata_record,
            package_tarball_full_path=package_tarball_full_path,
            extracted_package_dir=extracted_package_dir,
        )
        return package_cache_record
    except (IOError, OSError, JSONDecodeError) as e:
        # IOError / OSError if info/repodata_record.json doesn't exists
        # JsonDecodeError if info/repodata_record.json is partially extracted or corrupted
        #   python 2.7 raises ValueError instead of JsonDecodeError
        #   ValueError("No JSON object could be decoded")
        log.debug(
            "unable to read %s\n  because %r",
            join(extracted_package_dir, "info", "repodata_record.json"),
            e,
        )

        # try reading info/index.json
        try:
            index_json_record = read_index_json(extracted_package_dir)
        except (IOError, OSError, JSONDecodeError) as e:
            # IOError / OSError if info/index.json doesn't exist
            # JsonDecodeError if info/index.json is partially extracted or corrupted
            #   python 2.7 raises ValueError instead of JsonDecodeError
            #   ValueError("No JSON object could be decoded")
            log.debug(
                "unable to read %s\n  because",
                join(extracted_package_dir, "info", "index.json"),
                e,
            )

            if isdir(extracted_package_dir) and not isfile(package_tarball_full_path):
                # We have a directory that looks like a conda package, but without
                # (1) info/repodata_record.json or info/index.json, and (2) a conda package
                # tarball, there's not much we can do.  We'll just ignore it.
                return None

            try:
                if self.is_writable:
                    if isdir(extracted_package_dir):
                        # We have a partially unpacked conda package directory. Best thing
                        # to do is remove it and try extracting.
                        rm_rf(extracted_package_dir)
                    extract_tarball(package_tarball_full_path, extracted_package_dir)
                    index_json_record = read_index_json(extracted_package_dir)
                else:
                    index_json_record = read_index_json_from_tarball(
                        package_tarball_full_path
                    )
            except (EOFError, ReadError) as e:
                # EOFError: Compressed file ended before the end-of-stream marker was reached
                # tarfile.ReadError: file could not be opened successfully
                # We have a corrupted tarball. Remove the tarball so it doesn't affect
                # anything, and move on.
                log.debug(
                    "unable to extract info/index.json from %s\n  because %r",
                    package_tarball_full_path,
                    e,
                )
                rm_rf(package_tarball_full_path)
                return None

        # we were able to read info/index.json, so let's continue
        if isfile(package_tarball_full_path):
            md5 = compute_md5sum(package_tarball_full_path)
        else:
            md5 = None

        url = first(self._urls_data, lambda x: basename(x) == package_filename)
        package_cache_record = PackageCacheRecord.from_objects(
            index_json_record,
            url=url,
            md5=md5,
            package_tarball_full_path=package_tarball_full_path,
            extracted_package_dir=extracted_package_dir,
        )

        # write the info/repodata_record.json file so we can short-circuit this next time
        if self.is_writable:
            repodata_record = PackageRecord.from_objects(package_cache_record)
            repodata_record_path = join(
                extracted_package_dir, "info", "repodata_record.json"
            )
            write_as_json_to_file(repodata_record_path, repodata_record)

        return package_cache_record