gurudesh/pytracebugs huggingface dataset:
# Buggy code
def _find_initial_loop_nodes(self, graph, head):
    # TODO optimize
    latching_nodes = set(
        [s for s, t in dfs_back_edges(graph, self._start_node) if t == head]
    )
    loop_subgraph = self.slice_graph(graph, head, latching_nodes, include_frontier=True)
    return set(loop_subgraph.nodes())


# Traceback (TypeError)
Traceback (most recent call last):
File "$HOME/angr-dev/angr-management/angrmanagement/ui/widgets/qfunction_table.py", line 227, in _on_function_selected
self._selection_callback(selected_func)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/functions_view.py", line 41, in _on_function_selected
self.workspace.on_function_selected(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/workspace.py", line 57, in on_function_selected
self.views_by_category['disassembly'][0].display_function(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/disassembly_view.py", line 171, in display_function
self._display_function(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/disassembly_view.py", line 343, in _display_function
vr = self.workspace.instance.project.analyses.VariableRecoveryFast(the_func)
File "$HOME/angr-dev/angr/angr/analyses/analysis.py", line 108, in __call__
oself.__init__(*args, **kwargs)
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 510, in __init__
self._analyze()
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 557, in _analyze
self._analysis_core_graph()
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 583, in _analysis_core_graph
self._add_input_state(n, output_state)
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 605, in _add_input_state
self._state_map[succ] = self._merge_states(succ, *([ self._state_map[succ], input_state ]))
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 551, in _merge_states
return states[0].merge(states[1], successor=node.addr)
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 448, in merge
processor_state=self.processor_state.copy().merge(other.processor_state),
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 53, in merge
self.bp = max(self.bp, other.bp)
TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'

# Fixed code
def _find_initial_loop_nodes(self, graph, head):
    # TODO optimize
    latching_nodes = set(
        [s for s, t in dfs_back_edges(graph, self._start_node) if t == head]
    )
    loop_subgraph = self.slice_graph(graph, head, latching_nodes, include_frontier=True)
    nodes = set(loop_subgraph.nodes())
    return nodes
# Buggy code
def _ail_handle_Const(self, expr):
    return expr


# Traceback (TypeError)
Traceback (most recent call last):
File "$HOME/angr-dev/angr-management/angrmanagement/ui/widgets/qfunction_table.py", line 227, in _on_function_selected
self._selection_callback(selected_func)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/functions_view.py", line 41, in _on_function_selected
self.workspace.on_function_selected(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/workspace.py", line 57, in on_function_selected
self.views_by_category['disassembly'][0].display_function(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/disassembly_view.py", line 171, in display_function
self._display_function(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/disassembly_view.py", line 343, in _display_function
vr = self.workspace.instance.project.analyses.VariableRecoveryFast(the_func)
File "$HOME/angr-dev/angr/angr/analyses/analysis.py", line 108, in __call__
oself.__init__(*args, **kwargs)
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 510, in __init__
self._analyze()
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 557, in _analyze
self._analysis_core_graph()
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 583, in _analysis_core_graph
self._add_input_state(n, output_state)
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 605, in _add_input_state
self._state_map[succ] = self._merge_states(succ, *([ self._state_map[succ], input_state ]))
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 551, in _merge_states
return states[0].merge(states[1], successor=node.addr)
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 448, in merge
processor_state=self.processor_state.copy().merge(other.processor_state),
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 53, in merge
self.bp = max(self.bp, other.bp)
TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'

# Fixed code
def _ail_handle_Const(self, expr):
    return DataSet(expr, expr.bits)
# Buggy code
def merge(self, other):
    if not self == other:
        l.warning("Inconsistent merge: %s %s ", self, other)

    # FIXME: none of the following logic makes any sense...
    if other.sp_adjusted is True:
        self.sp_adjusted = True
    self.sp_adjustment = max(self.sp_adjustment, other.sp_adjustment)
    if other.bp_as_base is True:
        self.bp_as_base = True
    self.bp = max(self.bp, other.bp)
    return self


# Traceback (TypeError)
Traceback (most recent call last):
File "$HOME/angr-dev/angr-management/angrmanagement/ui/widgets/qfunction_table.py", line 227, in _on_function_selected
self._selection_callback(selected_func)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/functions_view.py", line 41, in _on_function_selected
self.workspace.on_function_selected(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/workspace.py", line 57, in on_function_selected
self.views_by_category['disassembly'][0].display_function(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/disassembly_view.py", line 171, in display_function
self._display_function(function)
File "$HOME/angr-dev/angr-management/angrmanagement/ui/views/disassembly_view.py", line 343, in _display_function
vr = self.workspace.instance.project.analyses.VariableRecoveryFast(the_func)
File "$HOME/angr-dev/angr/angr/analyses/analysis.py", line 108, in __call__
oself.__init__(*args, **kwargs)
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 510, in __init__
self._analyze()
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 557, in _analyze
self._analysis_core_graph()
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 583, in _analysis_core_graph
self._add_input_state(n, output_state)
File "$HOME/angr-dev/angr/angr/analyses/forward_analysis.py", line 605, in _add_input_state
self._state_map[succ] = self._merge_states(succ, *([ self._state_map[succ], input_state ]))
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 551, in _merge_states
return states[0].merge(states[1], successor=node.addr)
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 448, in merge
processor_state=self.processor_state.copy().merge(other.processor_state),
File "$HOME/angr-dev/angr/angr/analyses/variable_recovery/variable_recovery_fast.py", line 53, in merge
self.bp = max(self.bp, other.bp)
TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'

# Fixed code
def merge(self, other):
    if not self == other:
        l.warning("Inconsistent merge: %s %s ", self, other)

    # FIXME: none of the following logic makes any sense...
    if other.sp_adjusted is True:
        self.sp_adjusted = True
    self.sp_adjustment = max(self.sp_adjustment, other.sp_adjustment)
    if other.bp_as_base is True:
        self.bp_as_base = True
    if self.bp is None:
        self.bp = other.bp
    elif other.bp is not None:  # and self.bp is not None
        self.bp = max(self.bp, other.bp)
    return self
# Buggy code
def __init__(self, filename: str, title: str, description: str, source: str):
    self.name = os.path.splitext(filename)[0]
    self.filename = filename
    self.title = title
    self.description = description
    self.source = source
    self.code = highlight(source, py_lexer, html_formatter)
    self.previous_example: Optional[Example] = None
    self.next_example: Optional[Example] = None
    self.process: Optional[asyncio.subprocess.Process] = None
    self.is_app = source.find("@app(") > 0


# Traceback (NotImplementedError)
(.venv) C:\code\python\h2oai\wave-0.10.0-windows-amd64>wave run --no-reload examples.tour
----------------------------------------
Welcome to the H2O Wave Interactive Tour!

Go to http://localhost:10101/tour
----------------------------------------
INFO:     Started server process [22080]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:51930 - "POST / HTTP/1.1" 200 OK
Unhandled exception
Traceback (most recent call last):
File "c:\code\python\h2oai\wave-0.10.0-windows-amd64\.venv\lib\site-packages\h2o_wave\server.py", line 265, in _process
await self._handle(q)
File ".\examples\tour.py", line 197, in serve
await show_example(q, catalog[route])
File ".\examples\tour.py", line 166, in show_example
await active_example.start()
File ".\examples\tour.py", line 48, in start
sys.executable, os.path.join(example_dir, self.filename)
File "C:\Python\Python37\Lib\asyncio\subprocess.py", line 217, in create_subprocess_exec
stderr=stderr, **kwds)
File "C:\Python\Python37\Lib\asyncio\base_events.py", line 1544, in subprocess_exec
bufsize, **kwargs)
File "C:\Python\Python37\Lib\asyncio\base_events.py", line 462, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError

# Fixed code
def __init__(self, filename: str, title: str, description: str, source: str):
    self.name = os.path.splitext(filename)[0]
    self.filename = filename
    self.title = title
    self.description = description
    self.source = source
    self.code = highlight(source, py_lexer, html_formatter)
    self.previous_example: Optional[Example] = None
    self.next_example: Optional[Example] = None
    self.process: Optional[subprocess.Popen] = None
    self.is_app = source.find("@app(") > 0
# Buggy code
async def start(self):
    if self.is_app:
        self.process = await asyncio.create_subprocess_exec(
            sys.executable,
            "-m",
            "uvicorn",
            "--port",
            _app_port,
            f"examples.{self.name}:main",
            env=dict(H2O_WAVE_EXTERNAL_ADDRESS=f"http://{_app_host}:{_app_port}"),
        )
    else:
        self.process = await asyncio.create_subprocess_exec(
            sys.executable, os.path.join(example_dir, self.filename)
        )


# Traceback (NotImplementedError)
(.venv) C:\code\python\h2oai\wave-0.10.0-windows-amd64>wave run --no-reload examples.tour
----------------------------------------
Welcome to the H2O Wave Interactive Tour!

Go to http://localhost:10101/tour
----------------------------------------
INFO:     Started server process [22080]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:51930 - "POST / HTTP/1.1" 200 OK
Unhandled exception
Traceback (most recent call last):
File "c:\code\python\h2oai\wave-0.10.0-windows-amd64\.venv\lib\site-packages\h2o_wave\server.py", line 265, in _process
await self._handle(q)
File ".\examples\tour.py", line 197, in serve
await show_example(q, catalog[route])
File ".\examples\tour.py", line 166, in show_example
await active_example.start()
File ".\examples\tour.py", line 48, in start
sys.executable, os.path.join(example_dir, self.filename)
File "C:\Python\Python37\Lib\asyncio\subprocess.py", line 217, in create_subprocess_exec
stderr=stderr, **kwds)
File "C:\Python\Python37\Lib\asyncio\base_events.py", line 1544, in subprocess_exec
bufsize, **kwargs)
File "C:\Python\Python37\Lib\asyncio\base_events.py", line 462, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError

# Fixed code
async def start(self):
    # The environment passed into Popen must include SYSTEMROOT, otherwise Popen will fail when called
    # inside python during initialization if %PATH% is configured, but without %SYSTEMROOT%.
    env = (
        {"SYSTEMROOT": os.environ["SYSTEMROOT"]}
        if sys.platform.lower().startswith("win")
        else {}
    )
    if self.is_app:
        self.process = subprocess.Popen(
            [
                sys.executable,
                "-m",
                "uvicorn",
                "--port",
                _app_port,
                f"examples.{self.name}:main",
            ],
            env=dict(
                H2O_WAVE_EXTERNAL_ADDRESS=f"http://{_app_host}:{_app_port}", **env
            ),
        )
    else:
        self.process = subprocess.Popen(
            [sys.executable, os.path.join(example_dir, self.filename)], env=env
        )
# Buggy code
async def stop(self):
    if self.process and self.process.returncode is None:
        self.process.terminate()


# Traceback (NotImplementedError)
(.venv) C:\code\python\h2oai\wave-0.10.0-windows-amd64>wave run --no-reload examples.tour
----------------------------------------
Welcome to the H2O Wave Interactive Tour!

Go to http://localhost:10101/tour
----------------------------------------
INFO:     Started server process [22080]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:51930 - "POST / HTTP/1.1" 200 OK
Unhandled exception
Traceback (most recent call last):
File "c:\code\python\h2oai\wave-0.10.0-windows-amd64\.venv\lib\site-packages\h2o_wave\server.py", line 265, in _process
await self._handle(q)
File ".\examples\tour.py", line 197, in serve
await show_example(q, catalog[route])
File ".\examples\tour.py", line 166, in show_example
await active_example.start()
File ".\examples\tour.py", line 48, in start
sys.executable, os.path.join(example_dir, self.filename)
File "C:\Python\Python37\Lib\asyncio\subprocess.py", line 217, in create_subprocess_exec
stderr=stderr, **kwds)
File "C:\Python\Python37\Lib\asyncio\base_events.py", line 1544, in subprocess_exec
bufsize, **kwargs)
File "C:\Python\Python37\Lib\asyncio\base_events.py", line 462, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError

# Fixed code
async def stop(self):
    if self.process and self.process.returncode is None:
        self.process.terminate()
        self.process.wait()
# Buggy code
def node_should_be_modified(self, node):
    """Checks if the import statement imports ``get_image_uri`` from the correct module.

    Args:
        node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement.
            For more, see https://docs.python.org/3/library/ast.html#abstract-grammar.

    Returns:
        bool: If the import statement imports ``get_image_uri`` from the correct module.
    """
    return node.module in GET_IMAGE_URI_NAMESPACES and any(
        name.name == GET_IMAGE_URI_NAME for name in node.names
    )


# Traceback (AttributeError)
❯ cat v1.py
import sagemaker

from sagemaker.predictor import csv_serializer

csv_serializer.__doc___

❯ sagemaker-upgrade-v2 --in-file v1.py --out-file v2.py
Traceback (most recent call last):
File "~/testvenv/bin/sagemaker-upgrade-v2", line 8, in <module>
sys.exit(main())
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/sagemaker_upgrade_v2.py", line 78, in main
_update_file(args.in_file, args.out_file)
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/sagemaker_upgrade_v2.py", line 50, in _update_file
updater_cls(input_path=input_file, output_path=output_file).update()
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/files.py", line 72, in update
output = self._update_ast(self._read_input_file())
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/files.py", line 86, in _update_ast
return ASTTransformer().visit(input_ast)
File "/usr/lib/python3.8/ast.py", line 363, in visit
return visitor(node)
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/ast_transformer.py", line 136, in visit_Module
self.generic_visit(node)
File "/usr/lib/python3.8/ast.py", line 439, in generic_visit
value = self.visit(value)
File "/usr/lib/python3.8/ast.py", line 363, in visit
return visitor(node)
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/ast_transformer.py", line 155, in visit_ImportFrom
node = import_checker.check_and_modify_node(node)
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/modifiers/modifier.py", line 26, in check_and_modify_node
if self.node_should_be_modified(node):
File "~/testvenv/lib/python3.8/site-packages/sagemaker/cli/compatibility/v2/modifiers/image_uris.py", line 115, in node_should_be_modified
return node.module in GET_IMAGE_URI_NAMESPACES and any(
AttributeError: 'NoneType' object has no attribute 'module'

# Fixed code
def node_should_be_modified(self, node):
    """Checks if the import statement imports ``get_image_uri`` from the correct module.

    Args:
        node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement.
            For more, see https://docs.python.org/3/library/ast.html#abstract-grammar.

    Returns:
        bool: If the import statement imports ``get_image_uri`` from the correct module.
    """
    return (
        node is not None
        and node.module in GET_IMAGE_URI_NAMESPACES
        and any(name.name == GET_IMAGE_URI_NAME for name in node.names)
    )
# Buggy code
def _compose(self, detached=False):
    """
    Args:
        detached:
    """
    compose_cmd = "docker-compose"

    command = [
        compose_cmd,
        "-f",
        os.path.join(self.container_root, DOCKER_COMPOSE_FILENAME),
        "up",
        "--build",
        "--abort-on-container-exit",
    ]

    if detached:
        command.append("-d")

    logger.info("docker command: %s", " ".join(command))
    return command


# Traceback (RuntimeError)
Exception in thread Thread-4:
Traceback (most recent call last):
File "/home/user/test/venv/lib/python3.6/site-packages/sagemaker/local/image.py", line 614, in run
_stream_output(self.process)
File "/home/user/test/venv/lib/python3.6/site-packages/sagemaker/local/image.py", line 673, in _stream_output
raise RuntimeError("Process exited with code: %s" % exit_code)
RuntimeError: Process exited with code: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/home/user/test/venv/lib/python3.6/site-packages/sagemaker/local/image.py", line 619, in run
raise RuntimeError(msg)
RuntimeError: Failed to run: ['docker-compose', '-f', '/tmp/tmpxxte1jk0/docker-compose.yaml', 'up', '--build', '--abort-on-container-exit', '-d'], Process exited with code: 1

# Fixed code
def _compose(self, detached=False):
    """
    Args:
        detached:
    """
    compose_cmd = "docker-compose"

    command = [
        compose_cmd,
        "-f",
        os.path.join(self.container_root, DOCKER_COMPOSE_FILENAME),
        "up",
        "--build",
        "--abort-on-container-exit"
        if not detached
        else "--detach",  # mutually exclusive
    ]

    logger.info("docker command: %s", " ".join(command))
    return command
# Buggy code
def __init__(self, training_job_name, metric_names, sagemaker_session=None):
    """Initialize a ``TrainingJobAnalytics`` instance.

    Args:
        training_job_name (str): name of the TrainingJob to analyze.
        metric_names (list): string names of all the metrics to collect for this training job
        sagemaker_session (sagemaker.session.Session): Session object which manages interactions with
            Amazon SageMaker APIs and any other AWS services needed. If not specified, one is specified
            using the default AWS configuration chain.
    """
    sagemaker_session = sagemaker_session or Session()
    self._sage_client = sagemaker_session.sagemaker_client
    self._cloudwatch = sagemaker_session.boto_session.client("cloudwatch")
    self._training_job_name = training_job_name
    self._metric_names = metric_names
    self.clear_cache()


# Traceback (TypeError)
TypeError                                 Traceback (most recent call last)
<ipython-input-145-5976c283bde7> in <module>()
----> 1 estimator.training_job_analytics

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/estimator.py in training_job_analytics(self)
325         if self._current_job_name is None:
326             raise ValueError('Estimator is not associated with a TrainingJob')
--> 327         return TrainingJobAnalytics(self._current_job_name)
328
329

TypeError: __init__() missing 1 required positional argument: 'metric_names'

# Fixed code
def __init__(self, training_job_name, metric_names=None, sagemaker_session=None):
    """Initialize a ``TrainingJobAnalytics`` instance.

    Args:
        training_job_name (str): name of the TrainingJob to analyze.
        metric_names (list, optional): string names of all the metrics to collect for this training job.
            If not specified, then it will use all metric names configured for this job.
        sagemaker_session (sagemaker.session.Session): Session object which manages interactions with
            Amazon SageMaker APIs and any other AWS services needed. If not specified, one is specified
            using the default AWS configuration chain.
    """
    sagemaker_session = sagemaker_session or Session()
    self._sage_client = sagemaker_session.sagemaker_client
    self._cloudwatch = sagemaker_session.boto_session.client("cloudwatch")
    self._training_job_name = training_job_name
    if metric_names:
        self._metric_names = metric_names
    else:
        self._metric_names = self._metric_names_for_training_job()
    self.clear_cache()
# Buggy code
def training_job_analytics(self):
    """Return a ``TrainingJobAnalytics`` object for the current training job."""
    if self._current_job_name is None:
        raise ValueError("Estimator is not associated with a TrainingJob")
    return TrainingJobAnalytics(self._current_job_name)


# Traceback (TypeError)
TypeError                                 Traceback (most recent call last)
<ipython-input-145-5976c283bde7> in <module>()
----> 1 estimator.training_job_analytics

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/estimator.py in training_job_analytics(self)
325         if self._current_job_name is None:
326             raise ValueError('Estimator is not associated with a TrainingJob')
--> 327         return TrainingJobAnalytics(self._current_job_name)
328
329

TypeError: __init__() missing 1 required positional argument: 'metric_names'

# Fixed code
def training_job_analytics(self):
    """Return a ``TrainingJobAnalytics`` object for the current training job."""
    if self._current_job_name is None:
        raise ValueError("Estimator is not associated with a TrainingJob")
    return TrainingJobAnalytics(
        self._current_job_name, sagemaker_session=self.sagemaker_session
    )
# Buggy code
def describe(self, print_response=True):
    """Prints out a response from the DescribeProcessingJob API call."""
    describe_response = self.sagemaker_session.describe_processing_job(self.job_name)
    if print_response:
        print(describe_response)
    return describe_response


# Traceback (OSError)
<sagemaker.tensorflow.estimator.TensorFlow object at 0x107b86610>
INFO:sagemaker:Creating training-job with name: sagemaker-tensorflow-2018-06-21-11-17-58-273
Traceback (most recent call last):
File "test.py", line 28, in <module>
iris_estimator.fit(train_data_location)
File "/Library/Python/2.7/site-packages/sagemaker/tensorflow/estimator.py", line 243, in fit
fit_super()
File "/Library/Python/2.7/site-packages/sagemaker/tensorflow/estimator.py", line 225, in fit_super
super(TensorFlow, self).fit(inputs, wait, logs, job_name)
File "/Library/Python/2.7/site-packages/sagemaker/estimator.py", line 177, in fit
self.latest_training_job = _TrainingJob.start_new(self, inputs)
File "/Library/Python/2.7/site-packages/sagemaker/estimator.py", line 362, in start_new
stop_condition=config['stop_condition'], tags=estimator.tags)
File "/Library/Python/2.7/site-packages/sagemaker/session.py", line 264, in train
self.sagemaker_client.create_training_job(**train_request)
File "/Library/Python/2.7/site-packages/sagemaker/local/local_session.py", line 75, in create_training_job
self.s3_model_artifacts = self.train_container.train(InputDataConfig, HyperParameters)
File "/Library/Python/2.7/site-packages/sagemaker/local/image.py", line 95, in train
volumes = self._prepare_training_volumes(data_dir, input_data_config, hyperparameters)
File "/Library/Python/2.7/site-packages/sagemaker/local/image.py", line 312, in _prepare_training_volumes
self._download_folder(bucket_name, key, channel_dir)
File "/Library/Python/2.7/site-packages/sagemaker/local/image.py", line 275, in _download_folder
obj.download_file(file_path)
File "/Library/Python/2.7/site-packages/boto3/s3/inject.py", line 314, in object_download_file
ExtraArgs=ExtraArgs, Callback=Callback, Config=Config)
File "/Library/Python/2.7/site-packages/boto3/s3/inject.py", line 172, in download_file
extra_args=ExtraArgs, callback=Callback)
File "/Library/Python/2.7/site-packages/boto3/s3/transfer.py", line 307, in download_file
future.result()
File "/Library/Python/2.7/site-packages/s3transfer/futures.py", line 73, in result
return self._coordinator.result()
File "/Library/Python/2.7/site-packages/s3transfer/futures.py", line 233, in result
raise self._exception
OSError: [Errno 21] Is a directory

# Fixed code
def describe(self):
    """Prints out a response from the DescribeProcessingJob API call."""
    return self.sagemaker_session.describe_processing_job(self.job_name)
# Buggy code
def get_all_submissions():
    submissions = Submission.query.all()
    return jsonify(
        {"submissions": [submission.to_json() for submission in submissions]}
    ), 200


# Traceback (AttributeError)
Traceback (most recent call last):
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc1/securedrop/securedrop/journalist_app/api.py", line 48, in decorated_function
return f(*args, **kwargs)
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc1/securedrop/securedrop/journalist_app/api.py", line 303, in get_all_submissions
submission in submissions]}), 200
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc1/securedrop/securedrop/journalist_app/api.py", line 303, in <listcomp>
submission in submissions]}), 200
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc1/securedrop/securedrop/models.py", line 218, in to_json
source_uuid=self.source.uuid),
AttributeError: 'NoneType' object has no attribute 'uuid'

# Fixed code
def get_all_submissions():
    submissions = Submission.query.all()
    return jsonify(
        {
            "submissions": [
                submission.to_json() for submission in submissions if submission.source
            ]
        }
    ), 200
# Buggy code
def token_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        try:
            auth_header = request.headers["Authorization"]
        except KeyError:
            return abort(403, "API token not found in Authorization header.")

        if auth_header:
            auth_token = auth_header.split(" ")[1]
        else:
            auth_token = ""
        if not Journalist.validate_api_token_and_get_user(auth_token):
            return abort(403, "API token is invalid or expired.")
        return f(*args, **kwargs)

    return decorated_function


# Traceback (IndexError)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/heartsucker/code/freedomofpress/securedrop/securedrop/journalist_app/api.py", line 40, in decorated_function
auth_token = auth_header.split(" ")[1]
IndexError: list index out of range

# Fixed code
def token_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        try:
            auth_header = request.headers["Authorization"]
        except KeyError:
            return abort(403, "API token not found in Authorization header.")

        if auth_header:
            split = auth_header.split(" ")
            if len(split) != 2 or split[0] != "Token":
                abort(403, "Malformed authorization header.")
            auth_token = split[1]
        else:
            auth_token = ""
        if not Journalist.validate_api_token_and_get_user(auth_token):
            return abort(403, "API token is invalid or expired.")
        return f(*args, **kwargs)

    return decorated_function
# Buggy code
def decorated_function(*args, **kwargs):
    try:
        auth_header = request.headers["Authorization"]
    except KeyError:
        return abort(403, "API token not found in Authorization header.")

    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        auth_token = ""
    if not Journalist.validate_api_token_and_get_user(auth_token):
        return abort(403, "API token is invalid or expired.")
    return f(*args, **kwargs)


# Traceback (IndexError)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/heartsucker/code/freedomofpress/securedrop/securedrop/journalist_app/api.py", line 40, in decorated_function
auth_token = auth_header.split(" ")[1]
IndexError: list index out of range

# Fixed code
def decorated_function(*args, **kwargs):
    try:
        auth_header = request.headers["Authorization"]
    except KeyError:
        return abort(403, "API token not found in Authorization header.")

    if auth_header:
        split = auth_header.split(" ")
        if len(split) != 2 or split[0] != "Token":
            abort(403, "Malformed authorization header.")
        auth_token = split[1]
    else:
        auth_token = ""
    if not Journalist.validate_api_token_and_get_user(auth_token):
        return abort(403, "API token is invalid or expired.")
    return f(*args, **kwargs)
# Buggy code
def export_pubkey(self, name):
    fingerprint = self.getkey(name)
    return self.gpg.export_keys(fingerprint)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/heartsucker/code/freedomofpress/securedrop-client/securedrop_client/logic.py", line 189, in <lambda>
lambda: self.completed_api_call(new_thread_id, callback))
File "/home/heartsucker/code/freedomofpress/securedrop-client/securedrop_client/logic.py", line 242, in completed_api_call
user_callback(result_data)
File "/home/heartsucker/code/freedomofpress/securedrop-client/securedrop_client/logic.py", line 419, in on_synced
self.gpg.import_key(source.uuid, pub_key)
File "/home/heartsucker/code/freedomofpress/securedrop-client/securedrop_client/crypto.py", line 116, in import_key
raise RuntimeError('Expected exactly one fingerprint. Found: {}'
RuntimeError: Expected exactly one fingerprint.

# Fixed code
def export_pubkey(self, name):
    fingerprint = self.getkey(name)
    if fingerprint:
        return self.gpg.export_keys(fingerprint)
    else:
        return None
# Buggy code
def login():
    if request.method == "POST":
        codename = request.form["codename"]
        if valid_codename(codename):
            session.update(codename=codename, logged_in=True)
            return redirect(url_for("lookup"))
        else:
            flash("Sorry, that is not a recognized codename.", "error")
    return render_template("login.html")


# Traceback (KeyError)
[Mon Dec 02 21:49:44 2013] [error] ERROR:source:Exception on / [GET]
[Mon Dec 02 21:49:44 2013] [error] Traceback (most recent call last):
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in
wsgi_app
[Mon Dec 02 21:49:44 2013] [error]     response =
self.full_dispatch_request()
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in
full_dispatch_request
[Mon Dec 02 21:49:44 2013] [error]     rv = self.handle_user_exception(e)
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in
handle_user_exception
[Mon Dec 02 21:49:44 2013] [error]     reraise(exc_type, exc_value, tb)
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1473, in
full_dispatch_request
[Mon Dec 02 21:49:44 2013] [error]     rv = self.preprocess_request()
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1666, in
preprocess_request
[Mon Dec 02 21:49:44 2013] [error]     rv = func()
[Mon Dec 02 21:49:44 2013] [error]   File
"/var/www/securedrop/source.py", line 52, in decorated_function
[Mon Dec 02 21:49:44 2013] [error]     return f(*args, **kwargs)
[Mon Dec 02 21:49:44 2013] [error]   File
"/var/www/securedrop/source.py", line 64, in setup_g
[Mon Dec 02 21:49:44 2013] [error]     g.flagged = session['flagged']
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 368, in
<lambda>
[Mon Dec 02 21:49:44 2013] [error]     __getitem__ = lambda x, i:
x._get_current_object()[i]
[Mon Dec 02 21:49:44 2013] [error] KeyError: 'flagged'

# Fixed code
def login():
    if request.method == "POST":
        codename = request.form["codename"]
        if valid_codename(codename):
            flagged = check_flagged(codename)
            session.update(codename=codename, flagged=flagged, logged_in=True)
            return redirect(url_for("lookup"))
        else:
            flash("Sorry, that is not a recognized codename.", "error")
    return render_template("login.html")
# Buggy code
def setup_g():
    """Store commonly used values in Flask's special g object"""
    # ignore_static here because `crypto_util.shash` is bcrypt (very time consuming),
    # and we don't need to waste time running if we're just serving a static
    # resource that won't need to access these common values.
    if logged_in():
        g.flagged = session["flagged"]
        g.codename = session["codename"]
        g.sid = crypto_util.shash(g.codename)
        g.loc = store.path(g.sid)


# Traceback (KeyError)
[Mon Dec 02 21:49:44 2013] [error] ERROR:source:Exception on / [GET]
[Mon Dec 02 21:49:44 2013] [error] Traceback (most recent call last):
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in
wsgi_app
[Mon Dec 02 21:49:44 2013] [error]     response =
self.full_dispatch_request()
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in
full_dispatch_request
[Mon Dec 02 21:49:44 2013] [error]     rv = self.handle_user_exception(e)
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in
handle_user_exception
[Mon Dec 02 21:49:44 2013] [error]     reraise(exc_type, exc_value, tb)
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1473, in
full_dispatch_request
[Mon Dec 02 21:49:44 2013] [error]     rv = self.preprocess_request()
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1666, in
preprocess_request
[Mon Dec 02 21:49:44 2013] [error]     rv = func()
[Mon Dec 02 21:49:44 2013] [error]   File
"/var/www/securedrop/source.py", line 52, in decorated_function
[Mon Dec 02 21:49:44 2013] [error]     return f(*args, **kwargs)
[Mon Dec 02 21:49:44 2013] [error]   File
"/var/www/securedrop/source.py", line 64, in setup_g
[Mon Dec 02 21:49:44 2013] [error]     g.flagged = session['flagged']
[Mon Dec 02 21:49:44 2013] [error]   File
"/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 368, in
<lambda>
[Mon Dec 02 21:49:44 2013] [error]     __getitem__ = lambda x, i:
x._get_current_object()[i]
[Mon Dec 02 21:49:44 2013] [error] KeyError: 'flagged'

# Fixed code
def setup_g():
    """Store commonly used values in Flask's special g object"""
    # ignore_static here because `crypto_util.shash` is bcrypt (very time consuming),
    # and we don't need to waste time running if we're just serving a static
    # resource that won't need to access these common values.
    if logged_in():
        # We use session.get (which defaults to None if 'flagged' is not in the
        # session) to avoid a KeyError on the redirect from login/ to lookup/
        g.flagged = session.get("flagged")
        g.codename = session["codename"]
        g.sid = crypto_util.shash(g.codename)
        g.loc = store.path(g.sid)
# Buggy code
def __getattr__(self, function_name: str) -> "ContractFunction":
    if self.abi is None:
        raise NoABIFound(
            "There is no ABI found for this contract.",
        )
    if "_functions" not in self.__dict__:
        raise NoABIFunctionsFound(
            "The abi for this contract contains no function definitions. ",
            "Are you sure you provided the correct contract abi?",
        )
    elif function_name not in self.__dict__["_functions"]:
        raise MismatchedABI(
            "The function '{}' was not found in this contract's abi. ".format(
                function_name
            ),
            "Are you sure you provided the correct contract abi?",
        )
    else:
        return super().__getattribute__(function_name)


# Traceback (web3.exceptions.MismatchedABI)
Traceback (most recent call last):
File "tools/get_names.py", line 101, in <module>
main(parser.parse_args())
File "tools/get_names.py", line 89, in main
if hasattr(registrar.events, 'BidRevealed'):
File "/home/user/.local/lib/python3.7/site-packages/web3/contract.py", line 200, in __getattr__
"Are you sure you provided the correct contract abi?"
web3.exceptions.MismatchedABI: ("The event 'BidRevealed' was not found in this contract's abi. ", 'Are you sure you provided the correct contract abi?')

# Fixed code
def __getattr__(self, function_name: str) -> "ContractFunction":
    if self.abi is None:
        raise NoABIFound(
            "There is no ABI found for this contract.",
        )
    if "_functions" not in self.__dict__:
        raise NoABIFunctionsFound(
            "The abi for this contract contains no function definitions. ",
            "Are you sure you provided the correct contract abi?",
        )
    elif function_name not in self.__dict__["_functions"]:
        raise ABIFunctionNotFound(
            "The function '{}' was not found in this contract's abi. ".format(
                function_name
            ),
            "Are you sure you provided the correct contract abi?",
        )
    else:
        return super().__getattribute__(function_name)
# Buggy code
def __getattr__(self, event_name: str) -> "ContractEvent":
    if "_events" not in self.__dict__:
        raise NoABIEventsFound(
            "The abi for this contract contains no event definitions. ",
            "Are you sure you provided the correct contract abi?",
        )
    elif event_name not in self.__dict__["_events"]:
        raise MismatchedABI(
            "The event '{}' was not found in this contract's abi. ".format(event_name),
            "Are you sure you provided the correct contract abi?",
        )
    else:
        return super().__getattribute__(event_name)


# Traceback (web3.exceptions.MismatchedABI)
Traceback (most recent call last):
File "tools/get_names.py", line 101, in <module>
main(parser.parse_args())
File "tools/get_names.py", line 89, in main
if hasattr(registrar.events, 'BidRevealed'):
File "/home/user/.local/lib/python3.7/site-packages/web3/contract.py", line 200, in __getattr__
"Are you sure you provided the correct contract abi?"
web3.exceptions.MismatchedABI: ("The event 'BidRevealed' was not found in this contract's abi. ", 'Are you sure you provided the correct contract abi?')

# Fixed code
def __getattr__(self, event_name: str) -> "ContractEvent":
    if "_events" not in self.__dict__:
        raise NoABIEventsFound(
            "The abi for this contract contains no event definitions. ",
            "Are you sure you provided the correct contract abi?",
        )
    elif event_name not in self.__dict__["_events"]:
        raise ABIEventFunctionNotFound(
            "The event '{}' was not found in this contract's abi. ".format(event_name),
            "Are you sure you provided the correct contract abi?",
        )
    else:
        return super().__getattribute__(event_name)
# Buggy code
def __getattr__(self, function_name: str) -> Any:
    if self.abi is None:
        raise NoABIFound(
            "There is no ABI found for this contract.",
        )
    elif not self._functions or len(self._functions) == 0:
        raise NoABIFunctionsFound(
            "The ABI for this contract contains no function definitions. ",
            "Are you sure you provided the correct contract ABI?",
        )
    elif function_name not in set(fn["name"] for fn in self._functions):
        functions_available = ", ".join([fn["name"] for fn in self._functions])
        raise MismatchedABI(
            "The function '{}' was not found in this contract's ABI. ".format(
                function_name
            ),
            "Here is a list of all of the function names found: ",
            "{}. ".format(functions_available),
            "Did you mean to call one of those functions?",
        )
    else:
        return super().__getattribute__(function_name)


# Traceback (web3.exceptions.MismatchedABI)
Traceback (most recent call last):
File "tools/get_names.py", line 101, in <module>
main(parser.parse_args())
File "tools/get_names.py", line 89, in main
if hasattr(registrar.events, 'BidRevealed'):
File "/home/user/.local/lib/python3.7/site-packages/web3/contract.py", line 200, in __getattr__
"Are you sure you provided the correct contract abi?"
web3.exceptions.MismatchedABI: ("The event 'BidRevealed' was not found in this contract's abi. ", 'Are you sure you provided the correct contract abi?')

# Fixed code
def __getattr__(self, function_name: str) -> Any:
    if self.abi is None:
        raise NoABIFound(
            "There is no ABI found for this contract.",
        )
    elif not self._functions or len(self._functions) == 0:
        raise NoABIFunctionsFound(
            "The ABI for this contract contains no function definitions. ",
            "Are you sure you provided the correct contract ABI?",
        )
    elif function_name not in set(fn["name"] for fn in self._functions):
        functions_available = ", ".join([fn["name"] for fn in self._functions])
        raise ABIFunctionNotFound(
            "The function '{}' was not found in this contract's ABI. ".format(
                function_name
            ),
            "Here is a list of all of the function names found: ",
            "{}. ".format(functions_available),
            "Did you mean to call one of those functions?",
        )
    else:
        return super().__getattribute__(function_name)