gurudesh/pytracebugs huggingface dataset:
# Buggy code
def random(self, point=None, size=None, repeat=None):
    c = draw_values([self.c], point=point)
    dtype = np.array(c).dtype

    def _random(c, dtype=dtype, size=None):
        return np.full(size, fill_value=c, dtype=dtype)

    return generate_samples(_random, c=c, dist_shape=self.shape, size=size).astype(
        dtype
    )


# Traceback (TypeError)
TypeError Traceback (most recent call last)
in ()
1 ann_input.set_value(X_test)
2 ann_output.set_value(Y_test)
----> 3 ppc = pm.sample_ppc(trace, model=neural_network, samples=500, progressbar=False)
4

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\sampling.py in sample_ppc(trace, samples, model, vars, size, random_seed, progressbar)
526 for var in vars:
527 ppc[var.name].append(var.distribution.random(point=param,
--> 528 size=size))
529
530 return {k: np.asarray(v) for k, v in ppc.items()}

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\discrete.py in random(self, point, size, repeat)
152
153 def random(self, point=None, size=None, repeat=None):
--> 154 p = draw_values([self.p], point=point)
155 return generate_samples(stats.bernoulli.rvs, p,
156 dist_shape=self.shape,

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\distribution.py in draw_values(params, point)
183 if not isinstance(node, (tt.sharedvar.TensorSharedVariable,
184 tt.TensorConstant)):
--> 185 givens[name] = (node, draw_value(node, point=point))
186 values = [None for _ in params]
187 for i, param in enumerate(params):

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\distribution.py in draw_value(param, point, givens)
251 except:
252 shape = param.shape
--> 253 if len(shape) == 0 and len(value) == 1:
254 value = value[0]
255 return value

TypeError: object of type 'TensorVariable' has no len()

# Fixed code
def random(self, point=None, size=None, repeat=None):
    c = draw_values([self.c], point=point)[0]
    dtype = np.array(c).dtype

    def _random(c, dtype=dtype, size=None):
        return np.full(size, fill_value=c, dtype=dtype)

    return generate_samples(_random, c=c, dist_shape=self.shape, size=size).astype(
        dtype
    )
# Buggy code
def random(self, point=None, size=None):
    a = draw_values([self.a], point=point)

    def _random(a, size=None):
        return stats.dirichlet.rvs(a, None if size == a.shape else size)

    samples = generate_samples(_random, a, dist_shape=self.shape, size=size)
    return samples


# Traceback (TypeError)
TypeError Traceback (most recent call last)
in ()
1 ann_input.set_value(X_test)
2 ann_output.set_value(Y_test)
----> 3 ppc = pm.sample_ppc(trace, model=neural_network, samples=500, progressbar=False)
4

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\sampling.py in sample_ppc(trace, samples, model, vars, size, random_seed, progressbar)
526 for var in vars:
527 ppc[var.name].append(var.distribution.random(point=param,
--> 528 size=size))
529
530 return {k: np.asarray(v) for k, v in ppc.items()}

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\discrete.py in random(self, point, size, repeat)
152
153 def random(self, point=None, size=None, repeat=None):
--> 154 p = draw_values([self.p], point=point)
155 return generate_samples(stats.bernoulli.rvs, p,
156 dist_shape=self.shape,

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\distribution.py in draw_values(params, point)
183 if not isinstance(node, (tt.sharedvar.TensorSharedVariable,
184 tt.TensorConstant)):
--> 185 givens[name] = (node, draw_value(node, point=point))
186 values = [None for _ in params]
187 for i, param in enumerate(params):

C:\Users\Nikos\Documents\Lasagne\python-3.4.4.amd64\lib\site-packages\pymc3\distributions\distribution.py in draw_value(param, point, givens)
251 except:
252 shape = param.shape
--> 253 if len(shape) == 0 and len(value) == 1:
254 value = value[0]
255 return value

TypeError: object of type 'TensorVariable' has no len()

# Fixed code
def random(self, point=None, size=None):
    a = draw_values([self.a], point=point)[0]

    def _random(a, size=None):
        return stats.dirichlet.rvs(a, None if size == a.shape else size)

    samples = generate_samples(_random, a, dist_shape=self.shape, size=size)
    return samples
# Buggy code
def __init__(self, lam, *args, **kwargs):
    super(Exponential, self).__init__(*args, **kwargs)
    self.lam = lam = tt.as_tensor_variable(lam)
    self.mean = 1.0 / self.lam
    self.median = self.mean * tt.log(2)
    self.mode = 0

    self.variance = self.lam**-2

    assert_negative_support(lam, "lam", "Exponential")


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-1724aa75761e> in <module>()
11     #arrival time model
12     t = pm.Lognormal('t', 100, 50, shape=cluster_number)
---> 13     t_obs = pm.Mixture('t_obs', w, pm.Exponential.dist(t), observed=time)
14

/opt/conda/lib/python3.5/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
28         if isinstance(name, string_types):
29             data = kwargs.pop('observed', None)
---> 30             dist = cls.dist(*args, **kwargs)
31             return model.Var(name, dist, data)
32         else:

/opt/conda/lib/python3.5/site-packages/pymc3/distributions/distribution.py in dist(cls, *args, **kwargs)
39     def dist(cls, *args, **kwargs):
40         dist = object.__new__(cls)
---> 41         dist.__init__(*args, **kwargs)
42         return dist
43

/opt/conda/lib/python3.5/site-packages/pymc3/distributions/mixture.py in __init__(self, w, comp_dists, *args, **kwargs)
63             comp_modes = self._comp_modes()
64             comp_mode_logps = self.logp(comp_modes)
---> 65             self.mode = comp_modes[tt.argmax(w * comp_mode_logps, axis=-1)]
66
67             if 'mode' not in defaults:

/opt/conda/lib/python3.5/site-packages/theano/tensor/var.py in __getitem__(self, args)
530                     self, *theano.tensor.subtensor.Subtensor.collapse(
531                         args,
--> 532                         lambda entry: isinstance(entry, Variable)))
533
534     def take(self, indices, axis=None, mode='raise'):

/opt/conda/lib/python3.5/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)
609         """
610         return_list = kwargs.pop('return_list', False)
--> 611         node = self.make_node(*inputs, **kwargs)
612
613         if config.compute_test_value != 'off':

/opt/conda/lib/python3.5/site-packages/theano/tensor/subtensor.py in make_node(self, x, *inputs)
482                 len(idx_list), x.type.ndim))
483             exception.subtensor_invalid = True
--> 484             raise exception
485
486         input_types = Subtensor.collapse(idx_list,

ValueError: The index list is longer (size 1) than the number of dimensions of the tensor(namely 0). You are asking for a dimension of the tensor that does not exist! You might need to use dimshuffle to add extra dimension to your tensor.

# Fixed code
def __init__(self, lam, *args, **kwargs):
    super(Exponential, self).__init__(*args, **kwargs)
    self.lam = lam = tt.as_tensor_variable(lam)
    self.mean = 1.0 / self.lam
    self.median = self.mean * tt.log(2)
    self.mode = tt.zeros_like(self.lam)

    self.variance = self.lam**-2

    assert_negative_support(lam, "lam", "Exponential")
# Buggy code
async def get_user_directory_stream_pos(self) -> int:
    return await self.db_pool.simple_select_one_onecol(
        table="user_directory_stream_pos",
        keyvalues={},
        retcol="stream_id",
        desc="get_user_directory_stream_pos",
    )


# Traceback (TypeError)
2021-02-16 17:36:09,420 - synapse.metrics.background_process_metrics - 211 - ERROR - user_directory.notify_new_event-9 - Background process 'user_directory.notify_new_event' threw an exception
Traceback (most recent call last):
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/metrics/background_process_metrics.py", line 208, in run
return await maybe_awaitable(func(*args, **kwargs))
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/handlers/user_directory.py", line 110, in process
await self._unsafe_process()
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/handlers/user_directory.py", line 159, in _unsafe_process
self.pos, room_max_stream_ordering
File "/opt/venvs/matrix-synapse/lib/python3.7/site-packages/synapse/storage/databases/main/state_deltas.py", line 51, in get_current_state_deltas
prev_stream_id = int(prev_stream_id)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType

# Fixed code
async def get_user_directory_stream_pos(self) -> Optional[int]:
    """
    Get the stream ID of the user directory stream.

    Returns:
        The stream token or None if the initial background update hasn't happened yet.
    """
    return await self.db_pool.simple_select_one_onecol(
        table="user_directory_stream_pos",
        keyvalues={},
        retcol="stream_id",
        desc="get_user_directory_stream_pos",
    )
# Buggy code
async def is_ignored_by(
    self, ignored_user_id: str, ignorer_user_id: str, cache_context: _CacheContext
) -> bool:
    ignored_account_data = await self.get_global_account_data_by_type_for_user(
        "m.ignored_user_list",
        ignorer_user_id,
        on_invalidate=cache_context.invalidate,
    )
    if not ignored_account_data:
        return False

    return ignored_user_id in ignored_account_data.get("ignored_users", {})


# Traceback (AttributeError)
2020-09-20 07:53:32,780 - synapse.http.server - 80 - ERROR - GET-8896 - Failed handle request via 'SyncRestServlet': <XForwardedForRequest at 0xffff882be940 method='GET' uri='/_matrix/client/r0/sync?fil>
Traceback (most recent call last):
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/http/server.py", line 229, in _async_render_wrapper
callback_return = await self._async_render(request)
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/http/server.py", line 406, in _async_render
callback_return = await raw_callback_return
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/rest/client/v2_alpha/sync.py", line 174, in on_GET
sync_result = await self.sync_handler.wait_for_sync_for_user(
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/handlers/sync.py", line 278, in wait_for_sync_for_user
res = await self.response_cache.wrap(
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/handlers/sync.py", line 310, in _wait_for_sync_for_user
result = await self.current_sync_for_user(
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/handlers/sync.py", line 342, in current_sync_for_user
return await self.generate_sync_result(sync_config, since_token, full_state)
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/handlers/sync.py", line 998, in generate_sync_result
res = await self._generate_sync_entry_for_rooms(
File "/home/ubuntu/synapse/env/lib/python3.8/site-packages/synapse/handlers/sync.py", line 1395, in _generate_sync_entry_for_rooms
ignored_users = ignored_account_data.get("ignored_users", {}).keys()
AttributeError: 'list' object has no attribute 'keys'

# Fixed code
async def is_ignored_by(
    self, ignored_user_id: str, ignorer_user_id: str, cache_context: _CacheContext
) -> bool:
    ignored_account_data = await self.get_global_account_data_by_type_for_user(
        AccountDataTypes.IGNORED_USER_LIST,
        ignorer_user_id,
        on_invalidate=cache_context.invalidate,
    )
    if not ignored_account_data:
        return False

    try:
        return ignored_user_id in ignored_account_data.get("ignored_users", {})
    except TypeError:
        # The type of the ignored_users field is invalid.
        return False
# Buggy code
def __init__(self, hs):
    super(GenericWorkerReplicationHandler, self).__init__(hs)

    self.store = hs.get_datastore()
    self.typing_handler = hs.get_typing_handler()
    self.presence_handler = hs.get_presence_handler()  # type: GenericWorkerPresence
    self.notifier = hs.get_notifier()

    self.notify_pushers = hs.config.start_pushers
    self.pusher_pool = hs.get_pusherpool()

    if hs.config.send_federation:
        self.send_handler = FederationSenderHandler(hs, self)
    else:
        self.send_handler = None


# Traceback (AttributeError)
synapse.app.generic_worker: [replication-RDATA-federation-3176] Error updating federation stream position
Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/synapse/app/generic_worker.py", line 851, in update_token
self.replication_client.send_federation_ack(
AttributeError: 'GenericWorkerReplicationHandler' object has no attribute 'send_federation_ack'

# Fixed code
def __init__(self, hs):
    super(GenericWorkerReplicationHandler, self).__init__(hs)

    self.store = hs.get_datastore()
    self.typing_handler = hs.get_typing_handler()
    self.presence_handler = hs.get_presence_handler()  # type: GenericWorkerPresence
    self.notifier = hs.get_notifier()

    self.notify_pushers = hs.config.start_pushers
    self.pusher_pool = hs.get_pusherpool()

    self.send_handler = None  # type: Optional[FederationSenderHandler]
    if hs.config.send_federation:
        self.send_handler = FederationSenderHandler(hs)
# Buggy code
def __init__(self, hs: GenericWorkerServer, replication_client):
    self.store = hs.get_datastore()
    self._is_mine_id = hs.is_mine_id
    self.federation_sender = hs.get_federation_sender()
    self.replication_client = replication_client

    self.federation_position = self.store.federation_out_pos_startup
    self._fed_position_linearizer = Linearizer(name="_fed_position_linearizer")

    self._last_ack = self.federation_position

    self._room_serials = {}
    self._room_typing = {}


# Traceback (AttributeError)
synapse.app.generic_worker: [replication-RDATA-federation-3176] Error updating federation stream position
Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/synapse/app/generic_worker.py", line 851, in update_token
self.replication_client.send_federation_ack(
AttributeError: 'GenericWorkerReplicationHandler' object has no attribute 'send_federation_ack'

# Fixed code
def __init__(self, hs: GenericWorkerServer):
    self.store = hs.get_datastore()
    self._is_mine_id = hs.is_mine_id
    self.federation_sender = hs.get_federation_sender()
    self._hs = hs

    # if the worker is restarted, we want to pick up where we left off in
    # the replication stream, so load the position from the database.
    #
    # XXX is this actually worthwhile? Whenever the master is restarted, we'll
    # drop some rows anyway (which is mostly fine because we're only dropping
    # typing and presence notifications). If the replication stream is
    # unreliable, why do we do all this hoop-jumping to store the position in the
    # database? See also https://github.com/matrix-org/synapse/issues/7535.
    #
    self.federation_position = self.store.federation_out_pos_startup

    self._fed_position_linearizer = Linearizer(name="_fed_position_linearizer")
    self._last_ack = self.federation_position
# Buggy code
async def update_token(self, token):
    try:
        self.federation_position = token

        # We linearize here to ensure we don't have races updating the token
        with await self._fed_position_linearizer.queue(None):
            if self._last_ack < self.federation_position:
                await self.store.update_federation_out_pos(
                    "federation", self.federation_position
                )

                # We ACK this token over replication so that the master can drop
                # its in memory queues
                self.replication_client.send_federation_ack(self.federation_position)
                self._last_ack = self.federation_position
    except Exception:
        logger.exception("Error updating federation stream position")


# Traceback (AttributeError)
synapse.app.generic_worker: [replication-RDATA-federation-3176] Error updating federation stream position
Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/lib/synapse/venv/lib/python3.8/site-packages/synapse/app/generic_worker.py", line 851, in update_token
self.replication_client.send_federation_ack(
AttributeError: 'GenericWorkerReplicationHandler' object has no attribute 'send_federation_ack'

# Fixed code
async def update_token(self, token):
    """Update the record of where we have processed to in the federation stream.

    Called after we have processed a an update received over replication. Sends
    a FEDERATION_ACK back to the master, and stores the token that we have processed
     in `federation_stream_position` so that we can restart where we left off.
    """
    try:
        self.federation_position = token

        # We linearize here to ensure we don't have races updating the token
        #
        # XXX this appears to be redundant, since the ReplicationCommandHandler
        # has a linearizer which ensures that we only process one line of
        # replication data at a time. Should we remove it, or is it doing useful
        # service for robustness? Or could we replace it with an assertion that
        # we're not being re-entered?

        with await self._fed_position_linearizer.queue(None):
            await self.store.update_federation_out_pos(
                "federation", self.federation_position
            )

            # We ACK this token over replication so that the master can drop
            # its in memory queues
            self._hs.get_tcp_replication().send_federation_ack(self.federation_position)
            self._last_ack = self.federation_position
    except Exception:
        logger.exception("Error updating federation stream position")
# Buggy code
def set_server_admin(self, user, admin):
    """Sets whether a user is an admin of this homeserver.

    Args:
        user (UserID): user ID of the user to test
        admin (bool): true iff the user is to be a server admin,
            false otherwise.
    """
    return self.db.simple_update_one(
        table="users",
        keyvalues={"name": user.to_string()},
        updatevalues={"admin": 1 if admin else 0},
        desc="set_server_admin",
    )


# Traceback (AttributeError)
2020-02-13 19:15:53,449 - synapse.http.server - 110 - ERROR - PUT-31 - Failed handle request via 'UserRestServletV2': <SynapseRequest at 0x7f6a6035bfd0 method='PUT' uri='/_synapse/admin/v2/users/@mjolnir-dev:redacted' clientproto='HTTP/1.0' site=8008>
Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration: {'displayname': 'mjolnir-dev', 'avatar_url': None}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration: {'displayname': 'mjolnir-dev', 'avatar_url': None}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration: ProfileInfo(avatar_url=None, display_name='mjolnir-dev')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/synapse/http/server.py", line 78, in wrapped_request_handler
await h(self, request)
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/synapse/http/server.py", line 331, in _async_render
callback_return = await callback_return
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/synapse/rest/admin/users.py", line 185, in on_PUT
await self.admin_handler.set_user_server_admin(
AttributeError: 'AdminHandler' object has no attribute 'set_user_server_admin'

# Fixed code
def set_server_admin(self, user, admin):
    """Sets whether a user is an admin of this homeserver.

    Args:
        user (UserID): user ID of the user to test
        admin (bool): true iff the user is to be a server admin,
            false otherwise.
    """

    def set_server_admin_txn(txn):
        self.db.simple_update_one_txn(
            txn, "users", {"name": user.to_string()}, {"admin": 1 if admin else 0}
        )
        self._invalidate_cache_and_stream(txn, self.get_user_by_id, (user.to_string(),))

    return self.db.runInteraction("set_server_admin", set_server_admin_txn)
# Buggy code
def handle_check_result(pdu, deferred):
    try:
        res = yield make_deferred_yieldable(deferred)
    except SynapseError:
        res = None

    if not res:
        # Check local db.
        res = yield self.store.get_event(
            pdu.event_id, allow_rejected=True, allow_none=True
        )

    if not res and pdu.origin != origin:
        try:
            res = yield defer.ensureDeferred(
                self.get_pdu(
                    destinations=[pdu.origin],
                    event_id=pdu.event_id,
                    room_version=room_version,
                    outlier=outlier,
                    timeout=10000,
                )
            )
        except SynapseError:
            pass

    if not res:
        logger.warning("Failed to find copy of %s with valid signature", pdu.event_id)

    return res


# Traceback (AttributeError)
2020-02-24 14:00:10,088 - synapse.federation.federation_client - 421 - WARNING - POST-1957- Failed to send_join via matrix.org
...
Traceback (most recent call last):
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 402, in _try_destination_list
res = await callback(destination)
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in send_request
valid_pdus_map = {p.event_id: p for p in valid_pdus}
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in <dictcomp>
valid_pdus_map = {p.event_id: p for p in valid_pdus}
AttributeError: 'coroutine' object has no attribute 'event_id'
2020-02-24 14:00:10,154 - synapse.http.server - 81 - INFO - POST-1957- <SynapseRequest at 0x7ff1bf3efd30 method='POST' uri='/_matrix/client/r0/join/%23synapse%3Amatrix.org' clientproto='HTTP/1.1' site=8447> SynapseError: 502 - Failed to send_join via any server

# Fixed code
def handle_check_result(pdu: EventBase, deferred: Deferred):
    try:
        res = yield make_deferred_yieldable(deferred)
    except SynapseError:
        res = None

    if not res:
        # Check local db.
        res = yield self.store.get_event(
            pdu.event_id, allow_rejected=True, allow_none=True
        )

    if not res and pdu.origin != origin:
        try:
            # This should not exist in the base implementation, until
            # this is fixed, ignore it for typing. See issue #6997.
            res = yield defer.ensureDeferred(
                self.get_pdu(  # type: ignore
                    destinations=[pdu.origin],
                    event_id=pdu.event_id,
                    room_version=room_version,
                    outlier=outlier,
                    timeout=10000,
                )
            )
        except SynapseError:
            pass

    if not res:
        logger.warning("Failed to find copy of %s with valid signature", pdu.event_id)

    return res
# Buggy code
def _check_sigs_and_hash(self, room_version, pdu):
    return make_deferred_yieldable(self._check_sigs_and_hashes(room_version, [pdu])[0])


# Traceback (AttributeError)
2020-02-24 14:00:10,088 - synapse.federation.federation_client - 421 - WARNING - POST-1957- Failed to send_join via matrix.org
...
Traceback (most recent call last):
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 402, in _try_destination_list
res = await callback(destination)
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in send_request
valid_pdus_map = {p.event_id: p for p in valid_pdus}
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in <dictcomp>
valid_pdus_map = {p.event_id: p for p in valid_pdus}
AttributeError: 'coroutine' object has no attribute 'event_id'
2020-02-24 14:00:10,154 - synapse.http.server - 81 - INFO - POST-1957- <SynapseRequest at 0x7ff1bf3efd30 method='POST' uri='/_matrix/client/r0/join/%23synapse%3Amatrix.org' clientproto='HTTP/1.1' site=8447> SynapseError: 502 - Failed to send_join via any server

# Fixed code
def _check_sigs_and_hash(self, room_version: str, pdu: EventBase) -> Deferred:
    return make_deferred_yieldable(self._check_sigs_and_hashes(room_version, [pdu])[0])
# Buggy code
def errback(failure, pdu):
    failure.trap(SynapseError)
    with PreserveLoggingContext(ctx):
        logger.warning(
            "Signature check failed for %s: %s",
            pdu.event_id,
            failure.getErrorMessage(),
        )
    return failure


# Traceback (AttributeError)
2020-02-24 14:00:10,088 - synapse.federation.federation_client - 421 - WARNING - POST-1957- Failed to send_join via matrix.org
...
Traceback (most recent call last):
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 402, in _try_destination_list
res = await callback(destination)
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in send_request
valid_pdus_map = {p.event_id: p for p in valid_pdus}
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in <dictcomp>
valid_pdus_map = {p.event_id: p for p in valid_pdus}
AttributeError: 'coroutine' object has no attribute 'event_id'
2020-02-24 14:00:10,154 - synapse.http.server - 81 - INFO - POST-1957- <SynapseRequest at 0x7ff1bf3efd30 method='POST' uri='/_matrix/client/r0/join/%23synapse%3Amatrix.org' clientproto='HTTP/1.1' site=8447> SynapseError: 502 - Failed to send_join via any server

# Fixed code
def errback(failure: Failure, pdu: EventBase):
    failure.trap(SynapseError)
    with PreserveLoggingContext(ctx):
        logger.warning(
            "Signature check failed for %s: %s",
            pdu.event_id,
            failure.getErrorMessage(),
        )
    return failure
# Buggy code
def _flatten_deferred_list(deferreds):
    """Given a list of deferreds, either return the single deferred,
    combine into a DeferredList, or return an already resolved deferred.
    """
    if len(deferreds) > 1:
        return DeferredList(deferreds, fireOnOneErrback=True, consumeErrors=True)
    elif len(deferreds) == 1:
        return deferreds[0]
    else:
        return defer.succeed(None)


# Traceback (AttributeError)
2020-02-24 14:00:10,088 - synapse.federation.federation_client - 421 - WARNING - POST-1957- Failed to send_join via matrix.org
...
Traceback (most recent call last):
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 402, in _try_destination_list
res = await callback(destination)
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in send_request
valid_pdus_map = {p.event_id: p for p in valid_pdus}
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in <dictcomp>
valid_pdus_map = {p.event_id: p for p in valid_pdus}
AttributeError: 'coroutine' object has no attribute 'event_id'
2020-02-24 14:00:10,154 - synapse.http.server - 81 - INFO - POST-1957- <SynapseRequest at 0x7ff1bf3efd30 method='POST' uri='/_matrix/client/r0/join/%23synapse%3Amatrix.org' clientproto='HTTP/1.1' site=8447> SynapseError: 502 - Failed to send_join via any server

# Fixed code
def _flatten_deferred_list(deferreds: List[Deferred]) -> Deferred:
    """Given a list of deferreds, either return the single deferred,
    combine into a DeferredList, or return an already resolved deferred.
    """
    if len(deferreds) > 1:
        return DeferredList(deferreds, fireOnOneErrback=True, consumeErrors=True)
    elif len(deferreds) == 1:
        return deferreds[0]
    else:
        return defer.succeed(None)
# Buggy code
def _is_invite_via_3pid(event):
    return (
        event.type == EventTypes.Member
        and event.membership == Membership.INVITE
        and "third_party_invite" in event.content
    )


# Traceback (AttributeError)
2020-02-24 14:00:10,088 - synapse.federation.federation_client - 421 - WARNING - POST-1957- Failed to send_join via matrix.org
...
Traceback (most recent call last):
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 402, in _try_destination_list
res = await callback(destination)
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in send_request
valid_pdus_map = {p.event_id: p for p in valid_pdus}
File "/home/synapse/matrixtest/synapse/federation/federation_client.py", line 589, in <dictcomp>
valid_pdus_map = {p.event_id: p for p in valid_pdus}
AttributeError: 'coroutine' object has no attribute 'event_id'
2020-02-24 14:00:10,154 - synapse.http.server - 81 - INFO - POST-1957- <SynapseRequest at 0x7ff1bf3efd30 method='POST' uri='/_matrix/client/r0/join/%23synapse%3Amatrix.org' clientproto='HTTP/1.1' site=8447> SynapseError: 502 - Failed to send_join via any server

# Fixed code
def _is_invite_via_3pid(event: EventBase) -> bool:
    return (
        event.type == EventTypes.Member
        and event.membership == Membership.INVITE
        and "third_party_invite" in event.content
    )
# Buggy code
def get_options(self, host):
    # Check if certificate verification has been enabled
    should_verify = self._config.federation_verify_certificates

    # Check if we've disabled certificate verification for this host
    if should_verify:
        for regex in self._config.federation_certificate_verification_whitelist:
            if regex.match(host):
                should_verify = False
                break

    ssl_context = (
        self._verify_ssl_context if should_verify else self._no_verify_ssl_context
    )

    return SSLClientConnectionCreator(host, ssl_context, should_verify)


# Traceback (TypeError)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/synapse/http/federation/well_known_resolver.py", line 114, in _do_get_well_known
self._well_known_agent.request(b"GET", uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 2126, in request
deferred = self._agent.request(method, uri, headers, bodyProducer)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1732, in request
endpoint = self._getEndpoint(parsedURI)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1715, in _getEndpoint
return self._endpointFactory.endpointForURI(uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1590, in endpointForURI
uri.port)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 155, in creatorForNetloc
return self.get_options(hostname)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 124, in get_options
if regex.match(host):
TypeError: cannot use a string pattern on a bytes-like object

# Fixed code
def get_options(self, host: bytes):
    # IPolicyForHTTPS.get_options takes bytes, but we want to compare
    # against the str whitelist. The hostnames in the whitelist are already
    # IDNA-encoded like the hosts will be here.
    ascii_host = host.decode("ascii")

    # Check if certificate verification has been enabled
    should_verify = self._config.federation_verify_certificates

    # Check if we've disabled certificate verification for this host
    if should_verify:
        for regex in self._config.federation_certificate_verification_whitelist:
            if regex.match(ascii_host):
                should_verify = False
                break

    ssl_context = (
        self._verify_ssl_context if should_verify else self._no_verify_ssl_context
    )

    return SSLClientConnectionCreator(host, ssl_context, should_verify)
# Buggy code
def __init__(self, hostname, ctx, verify_certs):
    self._ctx = ctx
    self._verifier = ConnectionVerifier(hostname, verify_certs)


# Traceback (TypeError)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/synapse/http/federation/well_known_resolver.py", line 114, in _do_get_well_known
self._well_known_agent.request(b"GET", uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 2126, in request
deferred = self._agent.request(method, uri, headers, bodyProducer)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1732, in request
endpoint = self._getEndpoint(parsedURI)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1715, in _getEndpoint
return self._endpointFactory.endpointForURI(uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1590, in endpointForURI
uri.port)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 155, in creatorForNetloc
return self.get_options(hostname)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 124, in get_options
if regex.match(host):
TypeError: cannot use a string pattern on a bytes-like object

# Fixed code
def __init__(self, hostname: bytes, ctx, verify_certs: bool):
    self._ctx = ctx
    self._verifier = ConnectionVerifier(hostname, verify_certs)
# Buggy code
def __init__(self, hostname, verify_certs):
    self._verify_certs = verify_certs

    if isIPAddress(hostname) or isIPv6Address(hostname):
        self._hostnameBytes = hostname.encode("ascii")
        self._is_ip_address = True
    else:
        # twisted's ClientTLSOptions falls back to the stdlib impl here if
        # idna is not installed, but points out that lacks support for
        # IDNA2008 (http://bugs.python.org/issue17305).
        #
        # We can rely on having idna.
        self._hostnameBytes = idna.encode(hostname)
        self._is_ip_address = False

    self._hostnameASCII = self._hostnameBytes.decode("ascii")


# Traceback (TypeError)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/synapse/http/federation/well_known_resolver.py", line 114, in _do_get_well_known
self._well_known_agent.request(b"GET", uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 2126, in request
deferred = self._agent.request(method, uri, headers, bodyProducer)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1732, in request
endpoint = self._getEndpoint(parsedURI)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1715, in _getEndpoint
return self._endpointFactory.endpointForURI(uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1590, in endpointForURI
uri.port)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 155, in creatorForNetloc
return self.get_options(hostname)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 124, in get_options
if regex.match(host):
TypeError: cannot use a string pattern on a bytes-like object

# Fixed code
def __init__(self, hostname: bytes, verify_certs):
    self._verify_certs = verify_certs

    _decoded = hostname.decode("ascii")
    if isIPAddress(_decoded) or isIPv6Address(_decoded):
        self._is_ip_address = True
    else:
        self._is_ip_address = False

    self._hostnameBytes = hostname
    self._hostnameASCII = self._hostnameBytes.decode("ascii")
# Buggy code
def __init__(self, reactor, tls_client_options_factory, srv_resolver, parsed_uri):
    self._reactor = reactor

    self._parsed_uri = parsed_uri

    # set up the TLS connection params
    #
    # XXX disabling TLS is really only supported here for the benefit of the
    # unit tests. We should make the UTs cope with TLS rather than having to make
    # the code support the unit tests.

    if tls_client_options_factory is None:
        self._tls_options = None
    else:
        self._tls_options = tls_client_options_factory.get_options(
            self._parsed_uri.host.decode("ascii")
        )

    self._srv_resolver = srv_resolver


# Traceback (TypeError)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/synapse/http/federation/well_known_resolver.py", line 114, in _do_get_well_known
self._well_known_agent.request(b"GET", uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 2126, in request
deferred = self._agent.request(method, uri, headers, bodyProducer)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1732, in request
endpoint = self._getEndpoint(parsedURI)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1715, in _getEndpoint
return self._endpointFactory.endpointForURI(uri)
File "/usr/local/lib/python3.7/site-packages/twisted/web/client.py", line 1590, in endpointForURI
uri.port)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 155, in creatorForNetloc
return self.get_options(hostname)
File "/usr/local/lib/python3.7/site-packages/synapse/crypto/context_factory.py", line 124, in get_options
if regex.match(host):
TypeError: cannot use a string pattern on a bytes-like object

# Fixed code
def __init__(self, reactor, tls_client_options_factory, srv_resolver, parsed_uri):
    self._reactor = reactor

    self._parsed_uri = parsed_uri

    # set up the TLS connection params
    #
    # XXX disabling TLS is really only supported here for the benefit of the
    # unit tests. We should make the UTs cope with TLS rather than having to make
    # the code support the unit tests.

    if tls_client_options_factory is None:
        self._tls_options = None
    else:
        self._tls_options = tls_client_options_factory.get_options(
            self._parsed_uri.host
        )

    self._srv_resolver = srv_resolver
# Buggy code
def _job_to_response(job):
    if not job:
        return {
            "type": None,
            "started_by": None,
            "status": State.SCHEDULED,
            "percentage": 0,
            "progress": [],
            "id": None,
            "cancellable": False,
        }
    else:
        return {
            "type": job.extra_metadata.get("type"),
            "started_by": job.extra_metadata.get("started_by"),
            "status": job.state,
            "exception": str(job.exception),
            "traceback": str(job.traceback),
            "percentage": job.percentage_progress,
            "id": job.job_id,
            "cancellable": job.cancellable,
        }


# Traceback (AttributeError)
ERROR    Internal Server Error: /api/tasks/
Traceback (most recent call last):
File "/Users/d/PythonEnvs/le2/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/d/PythonEnvs/le2/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/d/PythonEnvs/le2/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/d/PythonEnvs/le2/lib/python2.7/site-packages/rest_framework/viewsets.py", line 87, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/d/PythonEnvs/le2/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/Users/d/PythonEnvs/le2/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "kolibri/tasks/api.py", line 42, in list
jobs_response = [_job_to_response(j) for j in get_client().all_jobs()]
File "kolibri/tasks/api.py", line 427, in _job_to_response
"type": job.extra_metadata.get("type"),
AttributeError: 'Job' object has no attribute 'extra_metadata'

# Fixed code
def _job_to_response(job):
    if not job:
        return {
            "type": None,
            "started_by": None,
            "status": State.SCHEDULED,
            "percentage": 0,
            "progress": [],
            "id": None,
            "cancellable": False,
        }
    else:
        return {
            "type": getattr(job, "extra_metadata", {}).get("type"),
            "started_by": getattr(job, "extra_metadata", {}).get("started_by"),
            "status": job.state,
            "exception": str(job.exception),
            "traceback": str(job.traceback),
            "percentage": job.percentage_progress,
            "id": job.job_id,
            "cancellable": job.cancellable,
        }
# Buggy code
def error_received(self, exc):  # pragma: no cover
    if self.recvfrom:
        self.recvfrom.set_exception(exc)


# Traceback (asyncio.base_futures.InvalidStateError)
this assertion passes:
this assertion passes, but it's noisy:
Exception in callback _SelectorTransport._call_connection_lost(None)
handle: <Handle _SelectorTransport._call_connection_lost(None)>
Traceback (most recent call last):
File "C:\Miniconda3\envs\py37\lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Miniconda3\envs\py37\lib\asyncio\selector_events.py", line 700, in _call_connection_lost
self._protocol.connection_lost(exc)
File "C:\Miniconda3\envs\py37\lib\site-packages\dns\_asyncio_backend.py", line 38, in connection_lost
self.recvfrom.set_exception(exc)
asyncio.base_futures.InvalidStateError: invalid state
Exception in callback _SelectorTransport._call_connection_lost(None)
handle: <Handle _SelectorTransport._call_connection_lost(None)>
Traceback (most recent call last):
File "C:\Miniconda3\envs\py37\lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Miniconda3\envs\py37\lib\asyncio\selector_events.py", line 700, in _call_connection_lost
self._protocol.connection_lost(exc)
File "C:\Miniconda3\envs\py37\lib\site-packages\dns\_asyncio_backend.py", line 38, in connection_lost
self.recvfrom.set_exception(exc)
asyncio.base_futures.InvalidStateError: invalid state
Exception in callback _SelectorTransport._call_connection_lost(None)
handle: <Handle _SelectorTransport._call_connection_lost(None)>
Traceback (most recent call last):
File "C:\Miniconda3\envs\py37\lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Miniconda3\envs\py37\lib\asyncio\selector_events.py", line 700, in _call_connection_lost
self._protocol.connection_lost(exc)
File "C:\Miniconda3\envs\py37\lib\site-packages\dns\_asyncio_backend.py", line 38, in connection_lost
self.recvfrom.set_exception(exc)
asyncio.base_futures.InvalidStateError: invalid state
passed

# Fixed code
def error_received(self, exc):  # pragma: no cover
    if self.recvfrom and not self.recvfrom.done():
        self.recvfrom.set_exception(exc)
# Buggy code
def connection_lost(self, exc):
    if self.recvfrom:
        self.recvfrom.set_exception(exc)


# Traceback (asyncio.base_futures.InvalidStateError)
this assertion passes:
this assertion passes, but it's noisy:
Exception in callback _SelectorTransport._call_connection_lost(None)
handle: <Handle _SelectorTransport._call_connection_lost(None)>
Traceback (most recent call last):
File "C:\Miniconda3\envs\py37\lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Miniconda3\envs\py37\lib\asyncio\selector_events.py", line 700, in _call_connection_lost
self._protocol.connection_lost(exc)
File "C:\Miniconda3\envs\py37\lib\site-packages\dns\_asyncio_backend.py", line 38, in connection_lost
self.recvfrom.set_exception(exc)
asyncio.base_futures.InvalidStateError: invalid state
Exception in callback _SelectorTransport._call_connection_lost(None)
handle: <Handle _SelectorTransport._call_connection_lost(None)>
Traceback (most recent call last):
File "C:\Miniconda3\envs\py37\lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Miniconda3\envs\py37\lib\asyncio\selector_events.py", line 700, in _call_connection_lost
self._protocol.connection_lost(exc)
File "C:\Miniconda3\envs\py37\lib\site-packages\dns\_asyncio_backend.py", line 38, in connection_lost
self.recvfrom.set_exception(exc)
asyncio.base_futures.InvalidStateError: invalid state
Exception in callback _SelectorTransport._call_connection_lost(None)
handle: <Handle _SelectorTransport._call_connection_lost(None)>
Traceback (most recent call last):
File "C:\Miniconda3\envs\py37\lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Miniconda3\envs\py37\lib\asyncio\selector_events.py", line 700, in _call_connection_lost
self._protocol.connection_lost(exc)
File "C:\Miniconda3\envs\py37\lib\site-packages\dns\_asyncio_backend.py", line 38, in connection_lost
self.recvfrom.set_exception(exc)
asyncio.base_futures.InvalidStateError: invalid state
passed

# Fixed code
def connection_lost(self, exc):
    if self.recvfrom and not self.recvfrom.done():
        self.recvfrom.set_exception(exc)