gurudesh/pytracebugs huggingface dataset:
# Buggy code
def get_working_dir():
    """General purpose utility to return the absolute path of the working
    directory"""
    return os.path.join(general.get_top_dir(), constants.temp_folder)


# Traceback (subprocess.CalledProcessError)
docker build -t ternd .
./docker_run.sh workdir ternd "report -i golang:alpine"
-------------------------OUTPUT-------------------------
Traceback (most recent call last):
File "/usr/bin/tern", line 8, in <module>
sys.exit(main())
File "/usr/lib/python3.7/site-packages/tern/__main__.py", line 167, in main
do_main(args)
File "/usr/lib/python3.7/site-packages/tern/__main__.py", line 90, in do_main
run.execute_docker_image(args)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/run.py", line 88, in execute_docker_image
analyze(full_image, args)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/run.py", line 67, in analyze
analyze_docker_image(image_obj, args.redo, is_dockerfile)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/analyze.py", line 39, in analyze_docker_image
analyze_subsequent_layers(image_obj, shell, master_list, redo)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/analyze.py", line 133, in analyze_subsequent_layers
target = mount_overlay_fs(image_obj, curr_layer)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/analyze.py", line 178, in mount_overlay_fs
target = rootfs.mount_diff_layers(tar_layers)
File "/usr/lib/python3.7/site-packages/tern/utils/rootfs.py", line 196, in mount_diff_layers
root_command(union_mount, args, merge_dir_path)
File "/usr/lib/python3.7/site-packages/tern/utils/rootfs.py", line 66, in root_command
1, cmd=full_cmd, output=error)
subprocess.CalledProcessError: Command '['mount', '-t', 'overlay', 'overlay', '-o', 'lowerdir=/root/.tern/temp/8e635d6264340a45901f63d2a18ea5bc8c680919e07191e4ef276860952d0399/contents,upperdir=/root/.tern/temp/1332c5c5bbaa26b26053d13d13b81dd2f523a2217e01f6edfe38fe107c091859/contents,workdir=/root/.tern/temp/workdir', '/root/.tern/temp/mergedir']' returned non-zero exit status 1.
Traceback (most recent call last):
File "ci/test_files_touched.py", line 130, in <module>
subprocess.check_output(t, shell=True)  # nosec
File "/opt/circleci/.pyenv/versions/3.6.5/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/opt/circleci/.pyenv/versions/3.6.5/lib/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command './docker_run.sh workdir ternd "report -i golang:alpine"' returned non-zero exit status 1.
Exited with code 1

# Fixed code
def get_working_dir():
    """General purpose utility to return the absolute path of the working
    directory"""
    return os.path.join(mount_dir, constants.temp_folder)
# Buggy code
def root_command(command, *extra):
    """Invoke a shell command as root or using sudo. The command is a
    list of shell command words"""
    full_cmd = []
    sudo = True
    if os.getuid() == 0:
        sudo = False
    if sudo:
        full_cmd.append("sudo")
    full_cmd.extend(command)
    for arg in extra:
        full_cmd.append(arg)
    # invoke
    logger.debug("Running command: %s", " ".join(full_cmd))
    pipes = subprocess.Popen(
        full_cmd,
        stdout=subprocess.PIPE,  # nosec
        stderr=subprocess.PIPE,
    )
    result, error = pipes.communicate()  # nosec
    if error:
        logger.error("Command failed. %s", error.decode())
        raise subprocess.CalledProcessError(1, cmd=full_cmd, output=error)  # nosec
    else:
        return result


# Traceback (PermissionError)
$ python
Python 3.7.3 (default, Apr  3 2019, 19:16:38)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
from tern.classes.docker_image import DockerImage
d = DockerImage('ubuntu:14.04')
d.load_image()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nisha/terndev/tern/tern/classes/docker_image.py", line 161, in load_image
layer.gen_fs_hash()
File "/home/nisha/terndev/tern/tern/classes/image_layer.py", line 176, in gen_fs_hash
rootfs.extract_layer_tar(tar_file, fs_dir)
File "/home/nisha/terndev/tern/tern/utils/rootfs.py", line 90, in extract_layer_tar
tar.extractall(directory_path)
File "/usr/lib/python3.7/tarfile.py", line 2002, in extractall
numeric_owner=numeric_owner)
File "/usr/lib/python3.7/tarfile.py", line 2044, in extract
numeric_owner=numeric_owner)
File "/usr/lib/python3.7/tarfile.py", line 2120, in _extract_member
self.makedev(tarinfo, targetpath)
File "/usr/lib/python3.7/tarfile.py", line 2194, in makedev
os.makedev(tarinfo.devmajor, tarinfo.devminor))
PermissionError: [Errno 1] Operation not permitted

# Fixed code
def root_command(command, *extra):
    """Invoke a shell command as root or using sudo. The command is a
    list of shell command words"""
    full_cmd = []
    sudo = True
    if os.getuid() == 0:
        sudo = False
    if sudo:
        full_cmd.append("sudo")
    full_cmd.extend(command)
    for arg in extra:
        full_cmd.append(arg)
    # invoke
    logger.debug("Running command: %s", " ".join(full_cmd))
    pipes = subprocess.Popen(
        full_cmd,
        stdout=subprocess.PIPE,  # nosec
        stderr=subprocess.PIPE,
    )
    result, error = pipes.communicate()  # nosec
    if error:
        logger.error("Command failed. %s", error.decode())
        raise subprocess.CalledProcessError(1, cmd=full_cmd, output=error)  # nosec
    return result
# Buggy code
def extract_layer_tar(layer_tar_path, directory_path):
    """Assuming all the metadata for an image has been extracted into the
    temp folder, extract the tarfile into the required directory"""
    with tarfile.open(layer_tar_path) as tar:
        tar.extractall(directory_path)


# Traceback (PermissionError)
$ python
Python 3.7.3 (default, Apr  3 2019, 19:16:38)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
from tern.classes.docker_image import DockerImage
d = DockerImage('ubuntu:14.04')
d.load_image()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nisha/terndev/tern/tern/classes/docker_image.py", line 161, in load_image
layer.gen_fs_hash()
File "/home/nisha/terndev/tern/tern/classes/image_layer.py", line 176, in gen_fs_hash
rootfs.extract_layer_tar(tar_file, fs_dir)
File "/home/nisha/terndev/tern/tern/utils/rootfs.py", line 90, in extract_layer_tar
tar.extractall(directory_path)
File "/usr/lib/python3.7/tarfile.py", line 2002, in extractall
numeric_owner=numeric_owner)
File "/usr/lib/python3.7/tarfile.py", line 2044, in extract
numeric_owner=numeric_owner)
File "/usr/lib/python3.7/tarfile.py", line 2120, in _extract_member
self.makedev(tarinfo, targetpath)
File "/usr/lib/python3.7/tarfile.py", line 2194, in makedev
os.makedev(tarinfo.devmajor, tarinfo.devminor))
PermissionError: [Errno 1] Operation not permitted

# Fixed code
def extract_layer_tar(layer_tar_path, directory_path):
    """Assuming all the metadata for an image has been extracted into the
    temp folder, extract the tarfile into the required directory"""
    os.makedirs(directory_path)
    with open(os.devnull, "w") as test:
        result = subprocess.call(
            ["tar", "-tf", layer_tar_path], stdout=test, stderr=test
        )
    if not result:
        root_command(extract_tar, layer_tar_path, "-C", directory_path)
# Buggy code
def execute_docker_image(args):
    """Execution path if given a Docker image"""
    check_docker_daemon()
    logger.debug("Setting up...")
    setup(image_tag_string=args.docker_image)
    # attempt to get built image metadata
    full_image = load_full_image(args.docker_image)
    if full_image.origins.is_empty():
        # image loading was successful
        # Add an image origin here
        full_image.origins.add_notice_origin(
            formats.docker_image.format(imagetag=args.docker_image)
        )
        # analyze image
        analyze_docker_image(full_image)
        # generate report
        generate_report(args, full_image)
    else:
        # we cannot load the full image
        logger.warning("Cannot retrieve full image metadata")
    if not args.keep_working_dir:
        clean_image_tars(full_image)
    logger.debug("Teardown...")
    teardown()
    if not args.keep_working_dir:
        shutil.rmtree(os.path.abspath(constants.temp_folder))


# Traceback (subprocess.CalledProcessError)
2018-11-07 20:44:35,853 - DEBUG - rootfs - Running command: sudo rm -rf temp/mergedir
Traceback (most recent call last):
File "./tern", line 91, in
main(args)
File "./tern", line 47, in main
report.execute_docker_image(args)
File "/home/armijn/git/tern/report/report.py", line 402, in execute_docker_image
teardown()
File "/home/armijn/git/tern/report/report.py", line 75, in teardown
rootfs.clean_up()
File "/home/armijn/git/tern/utils/rootfs.py", line 163, in clean_up
root_command(remove, mergedir_path)
File "/home/armijn/git/tern/utils/rootfs.py", line 57, in root_command
raise subprocess.CalledProcessError(1, cmd=full_cmd, output=error)
subprocess.CalledProcessError: Command '['sudo', 'rm', '-rf', 'temp/mergedir']' returned non-zero exit status 1.

# Fixed code
def execute_docker_image(args):
    """Execution path if given a Docker image"""
    check_docker_daemon()
    logger.debug("Setting up...")
    setup(image_tag_string=args.docker_image)
    # attempt to get built image metadata
    full_image = load_full_image(args.docker_image)
    if full_image.origins.is_empty():
        # image loading was successful
        # Add an image origin here
        full_image.origins.add_notice_origin(
            formats.docker_image.format(imagetag=args.docker_image)
        )
        # analyze image
        analyze_docker_image(full_image)
        # generate report
        generate_report(args, full_image)
    else:
        # we cannot load the full image
        logger.warning("Cannot retrieve full image metadata")
    if not args.keep_working_dir:
        clean_image_tars(full_image)
    logger.debug("Teardown...")
    teardown()
    if not args.keep_working_dir:
        clean_working_dir()
# Buggy code
def to_dict(self):
    layer_dict = {}
    pkg_list = []
    for pkg in self.__packages:
        pkg_list.append(pkg.to_dict())
        layer_dict.update(
            {
                self.fs_hash: {
                    "packages": pkg_list,
                    "tar_file": self.__tar_file,
                    "created_by": self.__created_by,
                }
            }
        )
    return layer_dict


# Traceback (AttributeError)
$ python tests/test_class_image_layer.py

E....E
======================================================================
ERROR: testAddNotice (_main_.TestClassImageLayer)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/test_class_image_layer.py", line 48, in testAddNotice
self.layer.add_notice(n)
AttributeError: 'ImageLayer' object has no attribute 'add_notice'

======================================================================
ERROR: testToDict (_main_.TestClassImageLayer)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/test_class_image_layer.py", line 55, in testToDict
self.assertTrue(a_dict['123abc'])
KeyError: '123abc'

----------------------------------------------------------------------
Ran 6 tests in 0.001s

FAILED (errors=2)

# Fixed code
def to_dict(self):
    layer_dict = {}
    pkg_list = []
    for pkg in self.__packages:
        pkg_list.append(pkg.to_dict())
    layer_dict.update(
        {
            self.fs_hash: {
                "packages": pkg_list,
                "tar_file": self.tar_file,
                "created_by": self.created_by,
            }
        }
    )
    return layer_dict
# Buggy code
def unmount_rootfs(num_layers):
    """Unmount the overlay filesystem"""
    rootfs_path = os.path.join(constants.temp_folder, constants.mergedir)
    for _ in range(num_layers):
        root_command(unmount, "-rl", rootfs_path)


# Traceback (subprocess.CalledProcessError)
./tern report -i ./tern -k report -i docker.io/golang
...
2018-09-10 17:16:35,797 - DEBUG - rootfs - Running command: sudo mount -t overlay overlay -o lowerdir=temp/mergedir,upperdir=temp/6e1bf8ee3b708647f06cac5e20e6998c1f55d1e89b311065c82727c7abbde134/contents,workdir=temp/workdir temp/mergedir
Traceback (most recent call last):
File "./tern", line 94, in <module>
main(args)
File "./tern", line 53, in main
report.execute_docker_image(args)
File "/home/nisha/tern3.7/tern/report/report.py", line 324, in execute_docker_image
analyze_docker_image(full_image)
File "/home/nisha/tern3.7/tern/report/report.py", line 176, in analyze_docker_image
image_obj.layers[index].tar_file)
File "/home/nisha/tern3.7/tern/utils/rootfs.py", line 123, in mount_diff_layer
root_command(union_mount, args, merge_dir_path)
File "/home/nisha/tern3.7/tern/utils/rootfs.py", line 57, in root_command
raise subprocess.CalledProcessError(1, cmd=full_cmd, output=error)
subprocess.CalledProcessError: Command '['sudo', 'mount', '-t', 'overlay', 'overlay', '-o', 'lowerdir=temp/mergedir,upperdir=temp/6e1bf8ee3b708647f06cac5e20e6998c1f55d1e89b311065c82727c7abbde134/contents,workdir=temp/workdir', 'temp/mergedir']' returned non-zero exit status 1.

# Fixed code
def unmount_rootfs():
    """Unmount the filesystem"""
    rootfs_path = os.path.join(constants.temp_folder, constants.mergedir)
    root_command(unmount, "-rl", rootfs_path)
# Buggy code
def calc_fs_hash(fs_path):
    """Given the path to the filesystem, calculate the filesystem hash
    We run a shell script located in the tools directory to get the
    file stats and the file content's sha256sum. We then calculate the
    sha256sum of the contents and write the contents in the layer
    directory.
    Note that this file will be deleted if the -k flag is not given"""
    try:
        hash_contents = subprocess.check_output(
            ["sudo", "./tools/fs_hash.sh", os.path.abspath(fs_path)]
        )
        file_name = hashlib.sha256(hash_contents).hexdigest()
        # write file to an appropriate location
        hash_file = os.path.join(os.path.dirname(fs_path), file_name) + ".txt"
        with open(hash_file, "w") as f:
            f.write(hash_contents.decode("utf-8"))
        return file_name
    except:
        raise


# Traceback (subprocess.CalledProcessError)
./tern report -i ./tern -k report -i docker.io/golang
...
2018-09-10 17:16:35,797 - DEBUG - rootfs - Running command: sudo mount -t overlay overlay -o lowerdir=temp/mergedir,upperdir=temp/6e1bf8ee3b708647f06cac5e20e6998c1f55d1e89b311065c82727c7abbde134/contents,workdir=temp/workdir temp/mergedir
Traceback (most recent call last):
File "./tern", line 94, in <module>
main(args)
File "./tern", line 53, in main
report.execute_docker_image(args)
File "/home/nisha/tern3.7/tern/report/report.py", line 324, in execute_docker_image
analyze_docker_image(full_image)
File "/home/nisha/tern3.7/tern/report/report.py", line 176, in analyze_docker_image
image_obj.layers[index].tar_file)
File "/home/nisha/tern3.7/tern/utils/rootfs.py", line 123, in mount_diff_layer
root_command(union_mount, args, merge_dir_path)
File "/home/nisha/tern3.7/tern/utils/rootfs.py", line 57, in root_command
raise subprocess.CalledProcessError(1, cmd=full_cmd, output=error)
subprocess.CalledProcessError: Command '['sudo', 'mount', '-t', 'overlay', 'overlay', '-o', 'lowerdir=temp/mergedir,upperdir=temp/6e1bf8ee3b708647f06cac5e20e6998c1f55d1e89b311065c82727c7abbde134/contents,workdir=temp/workdir', 'temp/mergedir']' returned non-zero exit status 1.

# Fixed code
def calc_fs_hash(fs_path):
    """Given the path to the filesystem, calculate the filesystem hash
    We run a shell script located in the tools directory to get the
    file stats and the file content's sha256sum. We then calculate the
    sha256sum of the contents and write the contents in the layer
    directory.
    Note that this file will be deleted if the -k flag is not given"""
    try:
        hash_contents = subprocess.check_output(
            ["sudo", "./tools/fs_hash.sh", os.path.abspath(fs_path)]
        )
        file_name = hashlib.sha256(hash_contents).hexdigest()
        # write file to an appropriate location
        hash_file = os.path.join(os.path.dirname(fs_path), file_name) + ".txt"
        with open(hash_file, "w") as f:
            f.write(hash_contents.decode("utf-8"))
        return file_name
    except subprocess.CalledProcessError:
        raise
# Buggy code
def clahe(img, clip_limit=2.0, tile_grid_size=(8, 8)):
    if img.dtype != np.uint8:
        raise TypeError("clahe supports only uint8 inputs")
    img = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
    clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
    img[:, :, 0] = clahe.apply(img[:, :, 0])
    img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
    return img


# Traceback (TypeError)
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-25-6a9d0f41d618> in <module>()
6 img.shape
7 # (256, 256)
----> 8 aug(image=img, p=1)

/usr/local/lib/python3.7/site-packages/albumentations/core/transforms_interface.py in __call__(self, **kwargs)
36                     target_function = self._get_target_function(key)
37                     target_dependencies = {k: kwargs[k] for k in self.target_dependence.get(key, [])}
---> 38                     res[key] = target_function(arg, **dict(params, **target_dependencies))
39                 else:
40                     res[key] = None

/usr/local/lib/python3.7/site-packages/albumentations/augmentations/transforms.py in apply(self, img, clip_limit, **params)
985
986     def apply(self, img, clip_limit=2, **params):
--> 987         return F.clahe(img, clip_limit, self.tile_grid_size)
988
989     def get_params(self):

/usr/local/lib/python3.7/site-packages/albumentations/augmentations/functional.py in clahe(img, clip_limit, tile_grid_size)
274     if img.dtype != np.uint8:
275         raise TypeError('clahe supports only uint8 inputs')
--> 276     img = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
277     clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
278     img[:, :, 0] = clahe.apply(img[:, :, 0])

error: OpenCV(3.4.3) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.hpp:255: error: (-2:Unspecified error) in function 'cv::CvtHelper<cv::Set<3, 4, -1>, cv::Set<3, -1, -1>, cv::Set<0, 5, -1>, cv::SizePolicy::NONE>::CvtHelper(InputArray, OutputArray, int) [VScn = cv::Set<3, 4, -1>, VDcn = cv::Set<3, -1, -1>, VDepth = cv::Set<0, 5, -1>, sizePolicy = cv::SizePolicy::NONE]'
Invalid number of channels in input image:
'VScn::contains(scn)'
where
'scn' is 1

# Fixed code
def clahe(img, clip_limit=2.0, tile_grid_size=(8, 8)):
    if img.dtype != np.uint8:
        raise TypeError("clahe supports only uint8 inputs")

    clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)

    if len(img.shape) == 2:
        img = clahe.apply(img)
    else:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
        img[:, :, 0] = clahe.apply(img[:, :, 0])
        img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)

    return img
# Buggy code
def _add_quant_wrapper(layer):
    # Already annotated layer. No need to wrap.
    if isinstance(layer, quantize_annotate_mod.QuantizeAnnotate):
        return layer

    return quantize_annotate_mod.QuantizeAnnotate(layer)


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-ff8232533be0> in <module>
3 outputs = backbone(input_image)
4 not_working_model = keras.Model(input_image, outputs)
----> 5 q_aware_model = quantize_model(not_working_model)

~/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py in quantize_model(to_quantize)
99         'the `quantize_annotate_layer` API to handle individual layers.')
100
--> 101   annotated_model = quantize_annotate_model(to_quantize)
102   return quantize_apply(annotated_model)
103

~/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py in quantize_annotate_model(to_annotate)
148
149   return keras.models.clone_model(
--> 150       to_annotate, input_tensors=None, clone_function=_add_quant_wrapper)
151
152

~/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow/python/keras/models.py in clone_model(model, input_tensors, clone_function)
425   else:
426     return _clone_functional_model(
--> 427         model, input_tensors=input_tensors, layer_fn=clone_function)
428
429

~/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow/python/keras/models.py in _clone_functional_model(model, input_tensors, layer_fn)
198   input_tensors, output_tensors, created_layers = (
199       network.reconstruct_from_config(model_config,
--> 200                                       created_layers=created_layers))
201   metrics_names = model.metrics_names
202   model = Model(input_tensors, output_tensors, name=model.name)

~/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py in reconstruct_from_config(config, custom_objects, created_layers)
2044     layer = created_layers[layer_name]
2045     node_index = get_node_index(layer, node_index)
-> 2046     layer_output_tensors = layer._inbound_nodes[node_index].output_tensors
2047     output_tensors.append(nest.flatten(layer_output_tensors)[tensor_index])
2048

TypeError: list indices must be integers or slices, not NoneType

# Fixed code
def _add_quant_wrapper(layer):
    """Add annotation wrapper."""
    # Already annotated layer. No need to wrap.
    if isinstance(layer, quantize_annotate_mod.QuantizeAnnotate):
        return layer

    if isinstance(layer, tf.keras.Model):
        raise ValueError(
            "Quantizing a tf.keras Model inside another tf.keras Model is not supported."
        )

    return quantize_annotate_mod.QuantizeAnnotate(layer)
# Buggy code
def _make_attr_tuple_class(cls_name, attr_names):
    """
    Create a tuple subclass to hold `Attribute`s for an `attrs` class.

    The subclass is a bare tuple with properties for names.

    class MyClassAttributes(tuple):
        __slots__ = ()
        x = property(itemgetter(0))
    """
    attr_class_name = "{}Attributes".format(cls_name)
    attr_class_template = [
        "class {}(tuple):".format(attr_class_name),
        "    __slots__ = ()",
    ]
    if attr_names:
        for i, attr_name in enumerate(attr_names):
            attr_class_template.append(
                _tuple_property_pat.format(index=i, attr_name=attr_name)
            )
    else:
        attr_class_template.append("    pass")
    globs = {"_attrs_itemgetter": itemgetter, "_attrs_property": property}
    eval(compile("\n".join(attr_class_template), "", "exec"), globs)

    return globs[attr_class_name]


# Traceback (NameError)
Traceback (most recent call last):
File "example.py", line 8, in <module>
print(get_type_hints(C.__init__))
File "C:\Python37\lib\typing.py", line 1001, in get_type_hints
value = _eval_type(value, globalns, localns)
File "C:\Python37\lib\typing.py", line 260, in _eval_type
return t._evaluate(globalns, localns)
File "C:\Python37\lib\typing.py", line 464, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'List' is not defined

# Fixed code
def _make_attr_tuple_class(cls_name, attr_names):
    """
    Create a tuple subclass to hold `Attribute`s for an `attrs` class.

    The subclass is a bare tuple with properties for names.

    class MyClassAttributes(tuple):
        __slots__ = ()
        x = property(itemgetter(0))
    """
    attr_class_name = "{}Attributes".format(cls_name)
    attr_class_template = [
        "class {}(tuple):".format(attr_class_name),
        "    __slots__ = ()",
    ]
    if attr_names:
        for i, attr_name in enumerate(attr_names):
            attr_class_template.append(
                _tuple_property_pat.format(index=i, attr_name=attr_name)
            )
    else:
        attr_class_template.append("    pass")
    globs = {"_attrs_itemgetter": itemgetter, "_attrs_property": property}
    _compile_and_eval("\n".join(attr_class_template), globs)
    return globs[attr_class_name]
# Buggy code
def _make_eq(cls, attrs):
    """
    Create __eq__ method for *cls* with *attrs*.
    """
    attrs = [a for a in attrs if a.eq]

    unique_filename = _generate_unique_filename(cls, "eq")
    lines = [
        "def __eq__(self, other):",
        "    if other.__class__ is not self.__class__:",
        "        return NotImplemented",
    ]
    # We can't just do a big self.x = other.x and... clause due to
    # irregularities like nan == nan is false but (nan,) == (nan,) is true.
    if attrs:
        lines.append("    return  (")
        others = ["    ) == ("]
        for a in attrs:
            lines.append("        self.%s," % (a.name,))
            others.append("        other.%s," % (a.name,))

        lines += others + ["    )"]
    else:
        lines.append("    return True")

    script = "\n".join(lines)
    globs = {}
    locs = {}
    bytecode = compile(script, unique_filename, "exec")
    eval(bytecode, globs, locs)

    # In order of debuggers like PDB being able to step through the code,
    # we add a fake linecache entry.
    linecache.cache[unique_filename] = (
        len(script),
        None,
        script.splitlines(True),
        unique_filename,
    )
    return locs["__eq__"]


# Traceback (NameError)
Traceback (most recent call last):
File "example.py", line 8, in <module>
print(get_type_hints(C.__init__))
File "C:\Python37\lib\typing.py", line 1001, in get_type_hints
value = _eval_type(value, globalns, localns)
File "C:\Python37\lib\typing.py", line 260, in _eval_type
return t._evaluate(globalns, localns)
File "C:\Python37\lib\typing.py", line 464, in _evaluate
eval(self.__forward_code__, globalns, localns),
File "<string>", line 1, in <module>
NameError: name 'List' is not defined

# Fixed code
def _make_eq(cls, attrs):
    """
    Create __eq__ method for *cls* with *attrs*.
    """
    attrs = [a for a in attrs if a.eq]

    unique_filename = _generate_unique_filename(cls, "eq")
    lines = [
        "def __eq__(self, other):",
        "    if other.__class__ is not self.__class__:",
        "        return NotImplemented",
    ]
    # We can't just do a big self.x = other.x and... clause due to
    # irregularities like nan == nan is false but (nan,) == (nan,) is true.
    if attrs:
        lines.append("    return  (")
        others = ["    ) == ("]
        for a in attrs:
            lines.append("        self.%s," % (a.name,))
            others.append("        other.%s," % (a.name,))

        lines += others + ["    )"]
    else:
        lines.append("    return True")

    script = "\n".join(lines)
    return _make_method("__eq__", script, unique_filename)
# Buggy code
def stop_httptools(port):
    """Kill httptools."""
    # Invoke HTTPtools UI Kill Request
    try:
        requests.get("http://127.0.0.1:" + str(port) + "/kill", timeout=5)
        logger.info("Killing httptools UI")
    except Exception:
        pass

    # Inkoke HTTPtools Proxy Kill Request
    try:
        http_proxy = "http://127.0.0.1:" + str(port)
        headers = {"httptools": "kill"}
        url = "http://127.0.0.1"
        requests.get(url, headers=headers, proxies={"http": http_proxy})
        logger.info("Killing httptools Proxy")
    except Exception:
        pass


# Traceback (AttributeError)
[ERROR] 05/Feb/2021 02:43:41 - Internal Server Error: /search
Traceback (most recent call last):
File "\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\mobsf\MobSF\views\home.py", line 227, in search
return HttpResponseRedirect('/' + db_obj[0].URL)
AttributeError: 'RecentScansDB' object has no attribute 'URL'
[ERROR] 05/Feb/2021 02:43:41 - "GET /search?md5=<the file md5> HTTP/1.1" 500 80105

# Fixed code
def stop_httptools(url):
    """Kill httptools."""
    # Invoke HTTPtools UI Kill Request
    try:
        requests.get(f"{url}/kill", timeout=5)
        logger.info("Killing httptools UI")
    except Exception:
        pass

    # Invoke HTTPtools Proxy Kill Request
    try:
        http_proxy = url.replace("https://", "http://")
        headers = {"httptools": "kill"}
        url = "http://127.0.0.1"
        requests.get(url, headers=headers, proxies={"http": http_proxy})
        logger.info("Killing httptools Proxy")
    except Exception:
        pass
# Buggy code
def httptools_start(request):
    """Start httprools UI."""
    logger.info("Starting httptools Web UI")
    try:
        stop_httptools(settings.PROXY_PORT)
        start_httptools_ui(settings.PROXY_PORT)
        time.sleep(3)
        logger.info("httptools UI started")
        if request.GET["project"]:
            project = request.GET["project"]
        else:
            project = ""
        url = "http://localhost:{}/dashboard/{}".format(
            str(settings.PROXY_PORT), project
        )
        return HttpResponseRedirect(url)  # lgtm [py/reflective-xss]
    except Exception:
        logger.exception("Starting httptools Web UI")
        err = "Error Starting httptools UI"
        return print_n_send_error_response(request, err)


# Traceback (AttributeError)
[ERROR] 05/Feb/2021 02:43:41 - Internal Server Error: /search
Traceback (most recent call last):
File "\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\mobsf\MobSF\views\home.py", line 227, in search
return HttpResponseRedirect('/' + db_obj[0].URL)
AttributeError: 'RecentScansDB' object has no attribute 'URL'
[ERROR] 05/Feb/2021 02:43:41 - "GET /search?md5=<the file md5> HTTP/1.1" 500 80105

# Fixed code
def httptools_start(request):
    """Start httprools UI."""
    logger.info("Starting httptools Web UI")
    try:
        httptools_url = get_http_tools_url(request)
        stop_httptools(httptools_url)
        start_httptools_ui(settings.PROXY_PORT)
        time.sleep(3)
        logger.info("httptools UI started")
        if request.GET["project"]:
            project = request.GET["project"]
        else:
            project = ""
        url = f"{httptools_url}/dashboard/{project}"
        return HttpResponseRedirect(url)  # lgtm [py/reflective-xss]
    except Exception:
        logger.exception("Starting httptools Web UI")
        err = "Error Starting httptools UI"
        return print_n_send_error_response(request, err)
# Buggy code
def configure_proxy(self, project):
    """HTTPS Proxy."""
    self.install_mobsf_ca("install")
    proxy_port = settings.PROXY_PORT
    logger.info("Starting HTTPs Proxy on %s", proxy_port)
    stop_httptools(proxy_port)
    start_proxy(proxy_port, project)


# Traceback (AttributeError)
[ERROR] 05/Feb/2021 02:43:41 - Internal Server Error: /search
Traceback (most recent call last):
File "\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\mobsf\MobSF\views\home.py", line 227, in search
return HttpResponseRedirect('/' + db_obj[0].URL)
AttributeError: 'RecentScansDB' object has no attribute 'URL'
[ERROR] 05/Feb/2021 02:43:41 - "GET /search?md5=<the file md5> HTTP/1.1" 500 80105

# Fixed code
def configure_proxy(self, project, request):
    """HTTPS Proxy."""
    self.install_mobsf_ca("install")
    proxy_port = settings.PROXY_PORT
    logger.info("Starting HTTPs Proxy on %s", proxy_port)
    httptools_url = get_http_tools_url(request)
    stop_httptools(httptools_url)
    start_proxy(proxy_port, project)
# Buggy code
def install_mobsf_ca(self, action):
    """Install or Remove MobSF Root CA."""
    mobsf_ca = get_ca_file()
    ca_file = None
    if is_file_exists(mobsf_ca):
        ca_construct = "{}.0"
        pem = open(mobsf_ca, "rb").read()
        ca_obj = crypto.load_certificate(crypto.FILETYPE_PEM, pem)
        ca_file_hash = hex(ca_obj.subject_name_hash()).lstrip("0x")
        ca_file = os.path.join(
            "/system/etc/security/cacerts/", ca_construct.format(ca_file_hash)
        )
    else:
        logger.warning("mitmproxy root CA is not generated yet.")
        return
    if action == "install":
        logger.info("Installing MobSF RootCA")
        self.adb_command(["push", mobsf_ca, ca_file])
        self.adb_command(["chmod", "644", ca_file], True)
    elif action == "remove":
        logger.info("Removing MobSF RootCA")
        self.adb_command(["rm", ca_file], True)


# Traceback (AttributeError)
[ERROR] 05/Feb/2021 02:43:41 - Internal Server Error: /search
Traceback (most recent call last):
File "\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\mobsf\MobSF\views\home.py", line 227, in search
return HttpResponseRedirect('/' + db_obj[0].URL)
AttributeError: 'RecentScansDB' object has no attribute 'URL'
[ERROR] 05/Feb/2021 02:43:41 - "GET /search?md5=<the file md5> HTTP/1.1" 500 80105

# Fixed code
def install_mobsf_ca(self, action):
    """Install or Remove MobSF Root CA."""
    mobsf_ca = get_ca_file()
    ca_file = None
    if is_file_exists(mobsf_ca):
        ca_construct = "{}.0"
        pem = open(mobsf_ca, "rb")
        ca_obj = crypto.load_certificate(crypto.FILETYPE_PEM, pem.read())
        md = md5(ca_obj.get_subject().der()).digest()
        ret = md[0] | (md[1] << 8) | (md[2] << 16) | md[3] << 24
        ca_file_hash = hex(ret).lstrip("0x")
        ca_file = os.path.join(
            "/system/etc/security/cacerts/", ca_construct.format(ca_file_hash)
        )
        pem.close()
    else:
        logger.warning("mitmproxy root CA is not generated yet.")
        return
    if action == "install":
        logger.info("Installing MobSF RootCA")
        self.adb_command(["push", mobsf_ca, ca_file])
        self.adb_command(["chmod", "644", ca_file], True)
    elif action == "remove":
        logger.info("Removing MobSF RootCA")
        self.adb_command(["rm", ca_file], True)
# Buggy code
def search(request):
    """Search Scan by MD5 Route."""
    md5 = request.GET["md5"]
    if re.match("[0-9a-f]{32}", md5):
        db_obj = RecentScansDB.objects.filter(MD5=md5)
        if db_obj.exists():
            return HttpResponseRedirect("/" + db_obj[0].URL)
        else:
            return HttpResponseRedirect("/not_found/")
    return print_n_send_error_response(request, "Invalid Scan Hash")


# Traceback (AttributeError)
[ERROR] 05/Feb/2021 02:43:41 - Internal Server Error: /search
Traceback (most recent call last):
File "\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\mobsf\MobSF\views\home.py", line 227, in search
return HttpResponseRedirect('/' + db_obj[0].URL)
AttributeError: 'RecentScansDB' object has no attribute 'URL'
[ERROR] 05/Feb/2021 02:43:41 - "GET /search?md5=<the file md5> HTTP/1.1" 500 80105

# Fixed code
def search(request):
    """Search Scan by MD5 Route."""
    md5 = request.GET["md5"]
    if re.match("[0-9a-f]{32}", md5):
        db_obj = RecentScansDB.objects.filter(MD5=md5)
        if db_obj.exists():
            e = db_obj[0]
            url = (
                f"/{e.ANALYZER}/?name={e.FILE_NAME}&amp;"
                f"checksum={e.MD5}&amp;type={e.SCAN_TYPE}"
            )
            return HttpResponseRedirect(url)
        else:
            return HttpResponseRedirect("/not_found/")
    return print_n_send_error_response(request, "Invalid Scan Hash")
# Buggy code
def is_package_installed(self, package):
    """Check if package is installed."""
    out = self.adb_command(["pm", "list", "packages"], True)
    pkg = f"{package}".encode("utf-8")
    if pkg + b"\n" in out or pkg + b"\r\n" in out:
        # Windows uses \r\n
        return True
    return False


# Traceback (TypeError)
1. This is the first step
upload the specific app to do the static analysis, the mobsf will be killed for no reason
2. This is the second step
restart the mobsf ,Traceback (most recent call last):                                                                                                                                 'security_score'] = score(context['code_analysis'])
TypeError: 'NoneType' object is not subscriptable
[ERROR] 07/Nov/2020 21:42:48 - 'NoneType' object is not subscriptable                                                                                           [ERROR] 07/Nov/2020 21:42:48 - Internal Server Error: /StaticAnalyzer/
3. Further steps, etc.

# Fixed code
def is_package_installed(self, package, extra):
    """Check if package is installed."""
    success = "\nSuccess" in extra
    out = self.adb_command(["pm", "list", "packages"], True)
    pkg = f"{package}".encode("utf-8")
    pkg_fmts = [pkg + b"\n", pkg + b"\r\n", pkg + b"\r\r\n"]
    if any(pkg in out for pkg in pkg_fmts):
        # Windows uses \r\n and \r\r\n
        return True
    if success:
        # Fallback check
        return True
    return False
# Buggy code
def install_apk(self, apk_path, package):
    """Install APK and Verify Installation."""
    if self.is_package_installed(package):
        logger.info("Removing existing installation")
        # Remove existing installation'
        self.adb_command(["uninstall", package], False, True)
    # Disable install verification
    self.adb_command(
        [
            "settings",
            "put",
            "global",
            "verifier_verify_adb_installs",
            "0",
        ],
        True,
    )
    logger.info("Installing APK")
    # Install APK
    out = self.adb_command(["install", "-r", "-t", "-d", apk_path], False, True)
    if not out:
        return False, "adb install failed"
    out = out.decode("utf-8", "ignore")
    # Verify Installation
    return self.is_package_installed(package), out


# Traceback (TypeError)
1. This is the first step
upload the specific app to do the static analysis, the mobsf will be killed for no reason
2. This is the second step
restart the mobsf ,Traceback (most recent call last):                                                                                                                                 'security_score'] = score(context['code_analysis'])
TypeError: 'NoneType' object is not subscriptable
[ERROR] 07/Nov/2020 21:42:48 - 'NoneType' object is not subscriptable                                                                                           [ERROR] 07/Nov/2020 21:42:48 - Internal Server Error: /StaticAnalyzer/
3. Further steps, etc.

# Fixed code
def install_apk(self, apk_path, package):
    """Install APK and Verify Installation."""
    if self.is_package_installed(package, ""):
        logger.info("Removing existing installation")
        # Remove existing installation'
        self.adb_command(["uninstall", package], False, True)
    # Disable install verification
    self.adb_command(
        [
            "settings",
            "put",
            "global",
            "verifier_verify_adb_installs",
            "0",
        ],
        True,
    )
    logger.info("Installing APK")
    # Install APK
    out = self.adb_command(["install", "-r", "-t", "-d", apk_path], False, True)
    if not out:
        return False, "adb install failed"
    out = out.decode("utf-8", "ignore")
    # Verify Installation
    return self.is_package_installed(package, out), out
# Buggy code
def httptools_start(request):
    """Start httprools UI."""
    logger.info("Starting httptools Web UI")
    try:
        stop_httptools(settings.PROXY_PORT)
        start_httptools_ui(settings.PROXY_PORT)
        time.sleep(3)
        logger.info("httptools UI started")
        if request.GET["project"]:
            project = request.GET["project"]
        else:
            project = ""
        url = "http://localhost:{}/dashboard/{}".format(
            str(settings.PROXY_PORT), project
        )
        return HttpResponseRedirect(url)
    except Exception:
        logger.exception("Starting httptools Web UI")
        err = "Error Starting httptools UI"
        return print_n_send_error_response(request, err)


# Traceback (FileNotFoundError)
MobSFy Android Instance Traceback (most recent call last): File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 408, in mobsfy_init self.mobsf_agents_setup('frida') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 418, in mobsf_agents_setup self.install_mobsf_ca('install') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 140, in install_mobsf_ca pem = open(get_ca_dir(), 'rb').read() FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Lijoy KFH\\.mitmproxy\\mitmproxy-ca-cert.pem' [ERROR] 12/Apr/2020 18:53:11 – Failed to MobSFy the instance [ERROR] 12/Apr/2020 18:53:11 – Internal Server Error: /android_dynamic/"

# Fixed code
def httptools_start(request):
    """Start httprools UI."""
    logger.info("Starting httptools Web UI")
    try:
        stop_httptools(settings.PROXY_PORT)
        start_httptools_ui(settings.PROXY_PORT)
        time.sleep(3)
        logger.info("httptools UI started")
        if request.GET["project"]:
            project = request.GET["project"]
        else:
            project = ""
        url = "http://localhost:{}/dashboard/{}".format(
            str(settings.PROXY_PORT), project
        )
        return HttpResponseRedirect(url)  # lgtm [py/reflective-xss]
    except Exception:
        logger.exception("Starting httptools Web UI")
        err = "Error Starting httptools UI"
        return print_n_send_error_response(request, err)
# Buggy code
def configure_proxy(self, project):
    """HTTPS Proxy."""
    proxy_port = settings.PROXY_PORT
    logger.info("Starting HTTPs Proxy on %s", proxy_port)
    stop_httptools(proxy_port)
    start_proxy(proxy_port, project)


# Traceback (FileNotFoundError)
MobSFy Android Instance Traceback (most recent call last): File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 408, in mobsfy_init self.mobsf_agents_setup('frida') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 418, in mobsf_agents_setup self.install_mobsf_ca('install') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 140, in install_mobsf_ca pem = open(get_ca_dir(), 'rb').read() FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Lijoy KFH\\.mitmproxy\\mitmproxy-ca-cert.pem' [ERROR] 12/Apr/2020 18:53:11 – Failed to MobSFy the instance [ERROR] 12/Apr/2020 18:53:11 – Internal Server Error: /android_dynamic/"

# Fixed code
def configure_proxy(self, project):
    """HTTPS Proxy."""
    self.install_mobsf_ca("install")
    proxy_port = settings.PROXY_PORT
    logger.info("Starting HTTPs Proxy on %s", proxy_port)
    stop_httptools(proxy_port)
    start_proxy(proxy_port, project)
# Buggy code
def install_mobsf_ca(self, action):
    """Install or Remove MobSF Root CA."""
    ca_construct = "{}.0"
    pem = open(get_ca_dir(), "rb").read()
    ca_file = crypto.load_certificate(crypto.FILETYPE_PEM, pem)
    ca_file_hash = hex(ca_file.subject_name_hash()).lstrip("0x")
    ca_file = os.path.join(
        "/system/etc/security/cacerts/", ca_construct.format(ca_file_hash)
    )
    if action == "install":
        logger.info("Installing MobSF RootCA")
        self.adb_command(["push", get_ca_dir(), ca_file])
        self.adb_command(["chmod", "644", ca_file], True)
    elif action == "remove":
        logger.info("Removing MobSF RootCA")
        self.adb_command(["rm", ca_file], True)


# Traceback (FileNotFoundError)
MobSFy Android Instance Traceback (most recent call last): File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 408, in mobsfy_init self.mobsf_agents_setup('frida') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 418, in mobsf_agents_setup self.install_mobsf_ca('install') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 140, in install_mobsf_ca pem = open(get_ca_dir(), 'rb').read() FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Lijoy KFH\\.mitmproxy\\mitmproxy-ca-cert.pem' [ERROR] 12/Apr/2020 18:53:11 – Failed to MobSFy the instance [ERROR] 12/Apr/2020 18:53:11 – Internal Server Error: /android_dynamic/"

# Fixed code
def install_mobsf_ca(self, action):
    """Install or Remove MobSF Root CA."""
    mobsf_ca = get_ca_file()
    ca_file = None
    if is_file_exists(mobsf_ca):
        ca_construct = "{}.0"
        pem = open(mobsf_ca, "rb").read()
        ca_obj = crypto.load_certificate(crypto.FILETYPE_PEM, pem)
        ca_file_hash = hex(ca_obj.subject_name_hash()).lstrip("0x")
        ca_file = os.path.join(
            "/system/etc/security/cacerts/", ca_construct.format(ca_file_hash)
        )
    else:
        logger.warning("mitmproxy root CA is not generated yet.")
        return
    if action == "install":
        logger.info("Installing MobSF RootCA")
        self.adb_command(["push", mobsf_ca, ca_file])
        self.adb_command(["chmod", "644", ca_file], True)
    elif action == "remove":
        logger.info("Removing MobSF RootCA")
        self.adb_command(["rm", ca_file], True)