gurudesh/pytracebugs huggingface dataset:
# Buggy code
async def handle_websocket_message(
    self,
    message: dict,
    websocket: WebSocket,
    subscriptions: Dict[str, AsyncGenerator],
):
    operation_id = cast(str, message.get("id"))
    message_type = cast(str, message.get("type"))

    if message_type == GQL_CONNECTION_INIT:
        await websocket.send_json({"type": GQL_CONNECTION_ACK})
        asyncio.ensure_future(self.keep_websocket_alive(websocket))
    elif message_type == GQL_CONNECTION_TERMINATE:
        await websocket.close()
    elif message_type == GQL_START:
        await self.process_single_message(websocket, subscriptions, message)
    elif message_type == GQL_STOP:
        if operation_id in subscriptions:
            await subscriptions[operation_id].aclose()
            del subscriptions[operation_id]


# Traceback (TypeError)
Traceback (most recent call last):",
File \"/Users/...REDACTED.../virtualenvs/graphql-Pfr5HTvn/lib/python3.7/site-packages/graphql/execution/execute.py\", line 625, in resolve_field_value_or_error",
result = resolve_fn(source, info, **args)",
File \"/Users/...REDACTED.../app/__init__.py\", line 26, in counter_resolver",
return count + 1",
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'"

# Fixed code
async def handle_websocket_message(
    self,
    message: dict,
    websocket: WebSocket,
    subscriptions: Dict[str, AsyncGenerator],
):
    operation_id = cast(str, message.get("id"))
    message_type = cast(str, message.get("type"))

    if message_type == GQL_CONNECTION_INIT:
        await websocket.send_json({"type": GQL_CONNECTION_ACK})
        asyncio.ensure_future(self.keep_websocket_alive(websocket))
    elif message_type == GQL_CONNECTION_TERMINATE:
        await websocket.close()
    elif message_type == GQL_START:
        await self.start_websocket_subscription(
            message.get("payload"), operation_id, websocket, subscriptions
        )
    elif message_type == GQL_STOP:
        if operation_id in subscriptions:
            await subscriptions[operation_id].aclose()
            del subscriptions[operation_id]
# Buggy code
async def disconnect(self, reason: DisconnectReason) -> None:
    """
    On completion of this method, the peer will be disconnected
    and not in the peer pool anymore.
    """
    self.disconnect_nowait(reason)

    await self.manager.stop()


# Traceback (AttributeError)
�[1m�[31m   ERROR  2020-09-10 09:35:40,267           ETHPeerPool  unexpected error during peer connection
Traceback (most recent call last):
File "/usr/src/app/trinity/trinity/protocol/common/peer.py", line 228, in maybe_connect_more_peers
for backend in self.peer_backends
File "/usr/src/app/trinity/p2p/peer_pool.py", line 223, in _add_peers_from_backend
await self.connect_to_nodes(candidates)
File "/usr/src/app/trinity/p2p/peer_pool.py", line 452, in connect_to_nodes
await asyncio.gather(*(self.connect_to_node(node) for node in batch))
File "/usr/src/app/trinity/p2p/peer_pool.py", line 498, in connect_to_node
await peer.disconnect(DisconnectReason.TOO_MANY_PEERS)
File "/usr/src/app/trinity/p2p/peer.py", line 338, in disconnect
self.disconnect_nowait(reason)
File "/usr/src/app/trinity/p2p/peer.py", line 349, in disconnect_nowait
self._send_disconnect(reason)
File "/usr/src/app/trinity/p2p/peer.py", line 353, in _send_disconnect
self._p2p_api.disconnect(reason)
AttributeError: 'ETHPeer' object has no attribute '_p2p_api'�[0m

# Fixed code
async def disconnect(self, reason: DisconnectReason) -> None:
    """
    Send a Disconnect msg to the remote peer and stop ourselves.
    """
    self.disconnect_nowait(reason)

    await self.manager.stop()
# Buggy code
async def add_outbound_peer(self, peer: BasePeer) -> None:
    try:
        await self._start_peer(peer)
    except asyncio.TimeoutError as err:
        self.logger.debug("Timeout waiting for %s to start: %s", peer, err)
        return

    await self._add_peer_and_bootstrap(peer)


# Traceback (AttributeError)
�[1m�[31m   ERROR  2020-09-10 09:35:40,267           ETHPeerPool  unexpected error during peer connection
Traceback (most recent call last):
File "/usr/src/app/trinity/trinity/protocol/common/peer.py", line 228, in maybe_connect_more_peers
for backend in self.peer_backends
File "/usr/src/app/trinity/p2p/peer_pool.py", line 223, in _add_peers_from_backend
await self.connect_to_nodes(candidates)
File "/usr/src/app/trinity/p2p/peer_pool.py", line 452, in connect_to_nodes
await asyncio.gather(*(self.connect_to_node(node) for node in batch))
File "/usr/src/app/trinity/p2p/peer_pool.py", line 498, in connect_to_node
await peer.disconnect(DisconnectReason.TOO_MANY_PEERS)
File "/usr/src/app/trinity/p2p/peer.py", line 338, in disconnect
self.disconnect_nowait(reason)
File "/usr/src/app/trinity/p2p/peer.py", line 349, in disconnect_nowait
self._send_disconnect(reason)
File "/usr/src/app/trinity/p2p/peer.py", line 353, in _send_disconnect
self._p2p_api.disconnect(reason)
AttributeError: 'ETHPeer' object has no attribute '_p2p_api'�[0m

# Fixed code
async def add_outbound_peer(self, peer: BasePeer) -> None:
    try:
        await self._start_peer(peer)
    except asyncio.TimeoutError as err:
        self.logger.debug("Timeout waiting for %s to start: %s", peer, err)
        return

    # Check again to see if we have *become* full since the previous check.
    if self.is_full:
        self.logger.debug(
            "Successfully connected to %s but peer pool is full.  Disconnecting.",
            peer,
        )
        await peer.disconnect(DisconnectReason.TOO_MANY_PEERS)
        return
    elif not self.manager.is_running:
        self.logger.debug(
            "Successfully connected to %s but peer pool is closing.  Disconnecting.",
            peer,
        )
        await peer.disconnect(DisconnectReason.CLIENT_QUITTING)
        return

    await self._add_peer_and_bootstrap(peer)
# Buggy code
async def continuously_report(self) -> None:
    while self.manager.is_running:
        self._reporter.report_now()
        await asyncio.sleep(self._reporting_frequency)


# Traceback (combined_error)
Traceback (most recent call last):
File "/usr/src/app/trinity/trinity/extensibility/trio.py", line 50, in run_process
await self.do_run(event_bus)
File "/usr/src/app/trinity/trinity/components/builtin/metrics/component.py", line 171, in do_run
await managers[0].wait_finished()
File "/usr/local/lib/python3.7/contextlib.py", line 678, in __aexit__
raise exc_details[1]
File "/usr/local/lib/python3.7/contextlib.py", line 661, in __aexit__
cb_suppress = await cb(*exc_details)
File "/usr/local/lib/python3.7/contextlib.py", line 552, in _exit_wrapper
return await cm_exit(cm, exc_type, exc, tb)
File "/usr/local/lib/python3.7/site-packages/async_generator/_util.py", line 53, in __aexit__
await self._agen.athrow(type, value, traceback)
File "/usr/local/lib/python3.7/site-packages/async_service/trio.py", line 411, in background_trio_service
await manager.stop()
File "/usr/local/lib/python3.7/site-packages/trio/_core/_run.py", line 730, in __aexit__
raise combined_error_from_nursery
File "/usr/local/lib/python3.7/site-packages/async_service/trio.py", line 208, in run
for _, exc_value, exc_tb in self._errors
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 300, in _run_and_manage_task
await task.run()
File "/usr/local/lib/python3.7/site-packages/async_service/trio.py", line 76, in run
await self._async_fn(*self._async_fn_args)
File "/usr/src/app/trinity/trinity/components/builtin/metrics/service/trio.py", line 10, in continuously_report
self._reporter.report_now()
File "/usr/local/lib/python3.7/site-packages/pyformance/reporters/influx.py", line 88, in report_now
response = urlopen(request)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/usr/local/lib/python3.7/urllib/request.py", line 543, in _open
'_open', req)
File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/local/lib/python3.7/urllib/request.py", line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/local/lib/python3.7/urllib/request.py", line 1320, in do_open
r = h.getresponse()
File "/usr/local/lib/python3.7/http/client.py", line 1336, in getresponse
response.begin()
File "/usr/local/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/usr/local/lib/python3.7/http/client.py", line 275, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

# Fixed code
async def continuously_report(self) -> None:
    while self.manager.is_running:
        super().report_now()
        await asyncio.sleep(self._reporting_frequency)
# Buggy code
async def continuously_report(self) -> None:
    async for _ in trio_utils.every(self._reporting_frequency):
        self._reporter.report_now()


# Traceback (combined_error)
Traceback (most recent call last):
File "/usr/src/app/trinity/trinity/extensibility/trio.py", line 50, in run_process
await self.do_run(event_bus)
File "/usr/src/app/trinity/trinity/components/builtin/metrics/component.py", line 171, in do_run
await managers[0].wait_finished()
File "/usr/local/lib/python3.7/contextlib.py", line 678, in __aexit__
raise exc_details[1]
File "/usr/local/lib/python3.7/contextlib.py", line 661, in __aexit__
cb_suppress = await cb(*exc_details)
File "/usr/local/lib/python3.7/contextlib.py", line 552, in _exit_wrapper
return await cm_exit(cm, exc_type, exc, tb)
File "/usr/local/lib/python3.7/site-packages/async_generator/_util.py", line 53, in __aexit__
await self._agen.athrow(type, value, traceback)
File "/usr/local/lib/python3.7/site-packages/async_service/trio.py", line 411, in background_trio_service
await manager.stop()
File "/usr/local/lib/python3.7/site-packages/trio/_core/_run.py", line 730, in __aexit__
raise combined_error_from_nursery
File "/usr/local/lib/python3.7/site-packages/async_service/trio.py", line 208, in run
for _, exc_value, exc_tb in self._errors
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 300, in _run_and_manage_task
await task.run()
File "/usr/local/lib/python3.7/site-packages/async_service/trio.py", line 76, in run
await self._async_fn(*self._async_fn_args)
File "/usr/src/app/trinity/trinity/components/builtin/metrics/service/trio.py", line 10, in continuously_report
self._reporter.report_now()
File "/usr/local/lib/python3.7/site-packages/pyformance/reporters/influx.py", line 88, in report_now
response = urlopen(request)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/usr/local/lib/python3.7/urllib/request.py", line 543, in _open
'_open', req)
File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/local/lib/python3.7/urllib/request.py", line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/local/lib/python3.7/urllib/request.py", line 1320, in do_open
r = h.getresponse()
File "/usr/local/lib/python3.7/http/client.py", line 1336, in getresponse
response.begin()
File "/usr/local/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/usr/local/lib/python3.7/http/client.py", line 275, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

# Fixed code
async def continuously_report(self) -> None:
    async for _ in trio_utils.every(self._reporting_frequency):
        super().report_now()
# Buggy code
def lock_node_for_handshake(self, node: NodeAPI) -> asyncio.Lock:
    return self._handshake_locks.lock(node)


# Traceback (KeyError)
DEBUG  12-18 15:21:47  async_process_runner.py  b'\x1b[1m\x1b[31m   ERROR  2019-12-18 15:21:47,268           LESPeerPool  unexpected error during peer connection\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'Traceback (most recent call last):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 209, in maybe_connect_more_peers\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    for backend in self.peer_backends\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 191, in _add_peers_from_backend\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    await self.connect_to_nodes(iter(candidates))\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 419, in connect_to_nodes\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    loop=self.get_event_loop(),\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 440, in connect_to_node\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    async with self.lock_node_for_handshake(node):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 423, in lock_node_for_handshake\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self._handshake_locks.lock(node)\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/resource_lock.py", line 18, in lock\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    lock = self._locks[resource]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/.tox/py36-wheel-cli/lib/python3.6/weakref.py", line 394, in __getitem__\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self.data[ref(key)]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b"KeyError: <weakref at 0x7f48b9d9cf48; to 'Node' at 0x7f48b83175c0>\x1b[0m\n"

# Fixed code
def lock_node_for_handshake(self, node: NodeAPI) -> AsyncContextManager[None]:
    return self._handshake_locks.lock(node)
# Buggy code
def __init__(self) -> None:
    self._locks = weakref.WeakKeyDictionary()


# Traceback (KeyError)
DEBUG  12-18 15:21:47  async_process_runner.py  b'\x1b[1m\x1b[31m   ERROR  2019-12-18 15:21:47,268           LESPeerPool  unexpected error during peer connection\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'Traceback (most recent call last):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 209, in maybe_connect_more_peers\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    for backend in self.peer_backends\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 191, in _add_peers_from_backend\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    await self.connect_to_nodes(iter(candidates))\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 419, in connect_to_nodes\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    loop=self.get_event_loop(),\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 440, in connect_to_node\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    async with self.lock_node_for_handshake(node):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 423, in lock_node_for_handshake\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self._handshake_locks.lock(node)\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/resource_lock.py", line 18, in lock\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    lock = self._locks[resource]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/.tox/py36-wheel-cli/lib/python3.6/weakref.py", line 394, in __getitem__\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self.data[ref(key)]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b"KeyError: <weakref at 0x7f48b9d9cf48; to 'Node' at 0x7f48b83175c0>\x1b[0m\n"

# Fixed code
def __init__(self) -> None:
    self._locks = {}
    self._reference_counts = defaultdict(int)
# Buggy code
def lock(self, resource: Hashable) -> asyncio.Lock:
    if resource not in self._locks:
        self._locks[resource] = asyncio.Lock()
    lock = self._locks[resource]
    return lock


# Traceback (KeyError)
DEBUG  12-18 15:21:47  async_process_runner.py  b'\x1b[1m\x1b[31m   ERROR  2019-12-18 15:21:47,268           LESPeerPool  unexpected error during peer connection\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'Traceback (most recent call last):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 209, in maybe_connect_more_peers\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    for backend in self.peer_backends\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 191, in _add_peers_from_backend\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    await self.connect_to_nodes(iter(candidates))\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 419, in connect_to_nodes\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    loop=self.get_event_loop(),\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 440, in connect_to_node\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    async with self.lock_node_for_handshake(node):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 423, in lock_node_for_handshake\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self._handshake_locks.lock(node)\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/resource_lock.py", line 18, in lock\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    lock = self._locks[resource]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/.tox/py36-wheel-cli/lib/python3.6/weakref.py", line 394, in __getitem__\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self.data[ref(key)]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b"KeyError: <weakref at 0x7f48b9d9cf48; to 'Node' at 0x7f48b83175c0>\x1b[0m\n"

# Fixed code
async def lock(self, resource: TResource) -> AsyncIterator[None]:
    if resource not in self._locks:
        self._locks[resource] = asyncio.Lock()

    try:
        self._reference_counts[resource] += 1
        async with self._locks[resource]:
            yield
    finally:
        self._reference_counts[resource] -= 1
        if self._reference_counts[resource] <= 0:
            del self._reference_counts[resource]
            del self._locks[resource]
# Buggy code
def is_locked(self, resource: Hashable) -> bool:
    if resource not in self._locks:
        return False
    else:
        return self._locks[resource].locked()


# Traceback (KeyError)
DEBUG  12-18 15:21:47  async_process_runner.py  b'\x1b[1m\x1b[31m   ERROR  2019-12-18 15:21:47,268           LESPeerPool  unexpected error during peer connection\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'Traceback (most recent call last):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 209, in maybe_connect_more_peers\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    for backend in self.peer_backends\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 191, in _add_peers_from_backend\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    await self.connect_to_nodes(iter(candidates))\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 419, in connect_to_nodes\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    loop=self.get_event_loop(),\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 440, in connect_to_node\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    async with self.lock_node_for_handshake(node):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 423, in lock_node_for_handshake\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self._handshake_locks.lock(node)\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/resource_lock.py", line 18, in lock\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    lock = self._locks[resource]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/.tox/py36-wheel-cli/lib/python3.6/weakref.py", line 394, in __getitem__\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self.data[ref(key)]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b"KeyError: <weakref at 0x7f48b9d9cf48; to 'Node' at 0x7f48b83175c0>\x1b[0m\n"

# Fixed code
def is_locked(self, resource: TResource) -> bool:
    if resource not in self._locks:
        return False
    else:
        return self._locks[resource].locked()
# Buggy code
def _unpack_v4(
    message: bytes,
) -> Tuple[datatypes.PublicKey, int, Tuple[Any, ...], Hash32]:
    """Unpack a discovery v4 UDP message received from a remote node.

    Returns the public key used to sign the message, the cmd ID, payload and hash.
    """
    message_hash = Hash32(message[:MAC_SIZE])
    if message_hash != keccak(message[MAC_SIZE:]):
        raise WrongMAC("Wrong msg mac")
    signature = keys.Signature(message[MAC_SIZE:HEAD_SIZE])
    signed_data = message[HEAD_SIZE:]
    remote_pubkey = signature.recover_public_key_from_msg(signed_data)
    cmd_id = message[HEAD_SIZE]
    cmd = CMD_ID_MAP[cmd_id]
    payload = tuple(rlp.decode(message[HEAD_SIZE + 1 :], strict=False))
    # Ignore excessive list elements as required by EIP-8.
    payload = payload[: cmd.elem_count]
    return remote_pubkey, cmd_id, payload, message_hash


# Traceback (KeyError)
ERROR  09-05 09:51:49               asyncio  Exception in callback UDPTransport._on_read_ready
handle: <Handle UDPTransport._on_read_ready>
Traceback (most recent call last):
File "uvloop/cbhandles.pyx", line 69, in uvloop.loop.Handle._run
File "uvloop/handles/udp.pyx", line 64, in uvloop.loop.UDPTransport._on_read_ready
File "/home/ubuntu/trinity/p2p/discovery.py", line 448, in datagram_received
self.receive(address, cast(bytes, data))
File "/home/ubuntu/trinity/p2p/discovery.py", line 463, in receive
remote_pubkey, cmd_id, payload, message_hash = _unpack_v4(message)
File "/home/ubuntu/trinity/p2p/discovery.py", line 1261, in _unpack_v4
cmd = CMD_ID_MAP[cmd_id]
KeyError: 9

# Fixed code
def _unpack_v4(
    message: bytes,
) -> Tuple[datatypes.PublicKey, int, Tuple[Any, ...], Hash32]:
    """Unpack a discovery v4 UDP message received from a remote node.

    Returns the public key used to sign the message, the cmd ID, payload and hash.
    """
    message_hash = Hash32(message[:MAC_SIZE])
    if message_hash != keccak(message[MAC_SIZE:]):
        raise WrongMAC("Wrong msg mac")
    signature = keys.Signature(message[MAC_SIZE:HEAD_SIZE])
    signed_data = message[HEAD_SIZE:]
    remote_pubkey = signature.recover_public_key_from_msg(signed_data)
    cmd_id = message[HEAD_SIZE]
    try:
        cmd = CMD_ID_MAP[cmd_id]
    except KeyError as e:
        raise UnknownCommand(f"Invalid Command ID {cmd_id}") from e
    payload = tuple(rlp.decode(message[HEAD_SIZE + 1 :], strict=False))
    # Ignore excessive list elements as required by EIP-8.
    payload = payload[: cmd.elem_count]
    return remote_pubkey, cmd_id, payload, message_hash
# Buggy code
async def receive_handshake(
    self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
) -> None:
    expected_exceptions = (
        TimeoutError,
        PeerConnectionLost,
        HandshakeFailure,
        NoMatchingPeerCapabilities,
        asyncio.IncompleteReadError,
    )

    def _cleanup_reader_and_writer() -> None:
        if not reader.at_eof():
            reader.feed_eof()
        writer.close()

    try:
        await self._receive_handshake(reader, writer)
    except expected_exceptions as e:
        self.logger.debug("Could not complete handshake: %s", e)
        _cleanup_reader_and_writer()
    except OperationCancelled:
        pass
    except Exception as e:
        self.logger.exception("Unexpected error handling handshake")
        _cleanup_reader_and_writer()


# Traceback (p2p.exceptions.NoMatchingPeerCapabilities)
ERROR  09-02 13:55:52            FullServer  Unexpected error handling handshake
Traceback (most recent call last):
File "/home/ubuntu/trinity/trinity/server.py", line 156, in receive_handshake
await self._receive_handshake(reader, writer)
File "/home/ubuntu/trinity/trinity/server.py", line 176, in _receive_handshake
token=self.cancel_token,
File "/home/ubuntu/trinity/p2p/peer.py", line 133, in receive_handshake
token=token,
File "/home/ubuntu/trinity/p2p/handshake.py", line 268, in negotiate_protocol_handshakes
"Found no matching capabilities between self and peer:\n"
p2p.exceptions.NoMatchingPeerCapabilities: Found no matching capabilities between self and peer:
- local : (('eth', 63),)
- remote: (('pip', 1),)

# Fixed code
async def receive_handshake(
    self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
) -> None:
    def _cleanup_reader_and_writer() -> None:
        if not reader.at_eof():
            reader.feed_eof()
        writer.close()

    try:
        await self._receive_handshake(reader, writer)
    except COMMON_RECEIVE_HANDSHAKE_EXCEPTIONS as e:
        self.logger.debug("Could not complete handshake: %s", e)
        _cleanup_reader_and_writer()
    except OperationCancelled:
        pass
    except Exception as e:
        self.logger.exception("Unexpected error handling handshake")
        _cleanup_reader_and_writer()
# Buggy code
async def receive_handshake(
    self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
) -> None:
    expected_exceptions = (
        TimeoutError,
        PeerConnectionLost,
        HandshakeFailure,
        asyncio.IncompleteReadError,
    )

    def _cleanup_reader_and_writer() -> None:
        if not reader.at_eof():
            reader.feed_eof()
        writer.close()

    try:
        await self._receive_handshake(reader, writer)
    except expected_exceptions as e:
        self.logger.debug("Could not complete handshake: %s", e)
        _cleanup_reader_and_writer()
    except OperationCancelled:
        pass
    except Exception as e:
        self.logger.exception("Unexpected error handling handshake")
        _cleanup_reader_and_writer()


# Traceback (p2p.exceptions.NoMatchingPeerCapabilities)
ERROR  09-02 13:55:52            FullServer  Unexpected error handling handshake
Traceback (most recent call last):
File "/home/ubuntu/trinity/trinity/server.py", line 156, in receive_handshake
await self._receive_handshake(reader, writer)
File "/home/ubuntu/trinity/trinity/server.py", line 176, in _receive_handshake
token=self.cancel_token,
File "/home/ubuntu/trinity/p2p/peer.py", line 133, in receive_handshake
token=token,
File "/home/ubuntu/trinity/p2p/handshake.py", line 268, in negotiate_protocol_handshakes
"Found no matching capabilities between self and peer:\n"
p2p.exceptions.NoMatchingPeerCapabilities: Found no matching capabilities between self and peer:
- local : (('eth', 63),)
- remote: (('pip', 1),)

# Fixed code
async def receive_handshake(
    self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
) -> None:
    expected_exceptions = (
        TimeoutError,
        PeerConnectionLost,
        HandshakeFailure,
        NoMatchingPeerCapabilities,
        asyncio.IncompleteReadError,
    )

    def _cleanup_reader_and_writer() -> None:
        if not reader.at_eof():
            reader.feed_eof()
        writer.close()

    try:
        await self._receive_handshake(reader, writer)
    except expected_exceptions as e:
        self.logger.debug("Could not complete handshake: %s", e)
        _cleanup_reader_and_writer()
    except OperationCancelled:
        pass
    except Exception as e:
        self.logger.exception("Unexpected error handling handshake")
        _cleanup_reader_and_writer()
# Buggy code
def decompress_payload(self, raw_payload: bytes) -> bytes:
    # Do the Snappy Decompression only if Snappy Compression is supported by the protocol
    if self.snappy_support:
        return snappy.decompress(raw_payload)
    else:
        return raw_payload


# Traceback (snappy.CompressedLengthError)
b'\x1b[1m\x1b[31m   ERROR  02-19 09:50:08           ETHPeerPool  Unexpected error during auth/p2p handshake with <Node(0x865a63255b3bb68023b6bffd5095118fcc13e79dcf014fe4e47e065c350c7cc72af2e53eff895f11ba1bbb6a2b33271c1116ee870f266618eadfc2e78aa7349c@52.176.100.77:30303)>\n'
b'Traceback (most recent call last):\n'
b'  File "/home/circleci/repo/p2p/peer_pool.py", line 255, in connect\n'
b'    peer = await self.wait(handshake(remote, self.get_peer_factory()))\n'
b'  File "/home/circleci/repo/p2p/cancellable.py", line 20, in wait\n'
b'    return await self.wait_first(awaitable, token=token, timeout=timeout)\n'
b'  File "/home/circleci/repo/p2p/cancellable.py", line 42, in wait_first\n'
b'    return await token_chain.cancellable_wait(*awaitables, timeout=timeout)\n'
b'  File "/home/circleci/repo/.tox/py36-long_run_integration/lib/python3.6/site-packages/cancel_token/token.py", line 152, in cancellable_wait\n'
b'    return done.pop().result()\n'
b'  File "/home/circleci/repo/p2p/peer.py", line 128, in handshake\n'
b'    await peer.do_sub_proto_handshake()\n'
b'  File "/home/circleci/repo/p2p/peer.py", line 321, in do_sub_proto_handshake\n'
b'    cmd, msg = await self.read_msg()\n'
b'  File "/home/circleci/repo/p2p/peer.py", line 460, in read_msg\n'
b'    decoded_msg = cast(Dict[str, Any], cmd.decode(msg))\n'
b'  File "/home/circleci/repo/p2p/p2p_proto.py", line 78, in decode\n'
b'    raw_decoded = cast(Dict[str, int], super().decode(data))\n'
b'  File "/home/circleci/repo/p2p/protocol.py", line 123, in decode\n'
b'    encoded_payload = self.decompress_payload(compressed_payload)\n'
b'  File "/home/circleci/repo/p2p/protocol.py", line 130, in decompress_payload\n'
b'    return snappy.decompress(raw_payload)\n'
b'  File "/home/circleci/repo/.tox/py36-long_run_integration/lib/python3.6/site-packages/snappy/snappy.py", line 92, in uncompress\n'
b'    return _uncompress(data)\n'
b'snappy.CompressedLengthError: Can not calculate uncompressed length\x1b[0m\n'

# Fixed code
def decompress_payload(self, raw_payload: bytes) -> bytes:
    # Do the Snappy Decompression only if Snappy Compression is supported by the protocol
    if self.snappy_support:
        try:
            return snappy.decompress(raw_payload)
        except Exception as err:
            # log this just in case it's a library error of some kind on valid messages.
            self.logger.debug(
                "Snappy decompression error on payload: %s", raw_payload.hex()
            )
            raise MalformedMessage from err
    else:
        return raw_payload
# Buggy code
def box(self, left_edge, right_edge, **kwargs):
    """
    box is a wrapper to the Region object for creating a region
    without having to specify a *center* value.  It assumes the center
    is the midpoint between the left_edge and right_edge.
    """
    # we handle units in the region data object
    # but need to check if left_edge or right_edge is a
    # list or other non-array iterable before calculating
    # the center
    if not isinstance(left_edge, np.ndarray):
        left_edge = np.array(left_edge, dtype="float64")
    if not isinstance(right_edge, np.ndarray):
        right_edge = np.array(right_edge, dtype="float64")
    c = (left_edge + right_edge) / 2.0
    return self.region(c, left_edge, right_edge, **kwargs)


# Traceback (yt.utilities.exceptions.YTUnitOperationError)
$ python3 test_region.py
yt : [INFO     ] 2020-04-25 14:16:34,930 Parameters: current_time              = 7.24422756567562
yt : [INFO     ] 2020-04-25 14:16:34,930 Parameters: domain_dimensions         = [512 512 512]
yt : [INFO     ] 2020-04-25 14:16:34,930 Parameters: domain_left_edge          = [-5.12e+09 -5.12e+09 -5.12e+09]
yt : [INFO     ] 2020-04-25 14:16:34,930 Parameters: domain_right_edge         = [5.12e+09 5.12e+09 5.12e+09]
YTRegion (smallplt00595): , center=[-2.56e+09  0.00e+00  0.00e+00] cm, left_edge=[-5.12e+09 -5.12e+09 -5.12e+09] cm, right_edge=[0.00e+00 5.12e+09 5.12e+09] cm
Traceback (most recent call last):
File "test_region.py", line 22, in <module>
region2 = ds.box(ll, rr)
File "/raid/zingale/development/yt/yt/data_objects/static_output.py", line 854, in box
c = (left_edge + right_edge)/2.0
File "/raid/zingale/development/yt/yt/units/yt_array.py", line 1414, in __array_ufunc__
inps, units, ufunc, ret_class)
File "/raid/zingale/development/yt/yt/units/yt_array.py", line 174, in handle_preserve_units
raise YTUnitOperationError(ufunc, *units)
yt.utilities.exceptions.YTUnitOperationError: The <ufunc 'add'> operator for YTArrays with units (code_length) and (dimensionless) is not well defined.

# Fixed code
def box(self, left_edge, right_edge, **kwargs):
    """
    box is a wrapper to the Region object for creating a region
    without having to specify a *center* value.  It assumes the center
    is the midpoint between the left_edge and right_edge.
    """
    # we handle units in the region data object
    # but need to check if left_edge or right_edge is a
    # list or other non-array iterable before calculating
    # the center
    if isinstance(left_edge[0], YTQuantity):
        left_edge = YTArray(left_edge)
        right_edge = YTArray(right_edge)

    left_edge = np.asanyarray(left_edge, dtype="float64")
    right_edge = np.asanyarray(right_edge, dtype="float64")
    c = (left_edge + right_edge) / 2.0
    return self.region(c, left_edge, right_edge, **kwargs)
# Buggy code
def _get_gadget_format(filename):
    # check and return gadget binary format with file endianness
    ff = open(filename, "rb")
    (rhead,) = struct.unpack("<I", ff.read(4))
    ff.close()
    if rhead == 134217728:
        return 2, ">"
    elif rhead == 8:
        return 2, "<"
    elif rhead == 65536:
        return 1, ">"
    elif rhead == 256:
        return 1, "<"
    else:
        raise RuntimeError("Incorrect Gadget format %s!" % str(rhead))


# Traceback (TypeError)
Traceback (most recent call last):
File "./plot_solution.py", line 519, in <module>
pf.add_field(("gas", "density_squared"), function=_density_squared, units="g**2/cm**6")
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 1191, in add_field
self.index
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 504, in index
self, dataset_type=self.dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 39, in __init__
super(ParticleIndex, self).__init__(ds, dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/geometry_handler.py", line 50, in __init__
self._setup_geometry()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 50, in _setup_geometry
self._initialize_particle_handler()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 104, in _initialize_particle_handler
self._initialize_indices()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 134, in _initialize_indices
self.io._initialize_index(data_file, self.regions)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 351, in _initialize_index
data_file, count, 0, regions, DLE, DRE)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 334, in _get_morton_from_position
pp = np.fromfile(f, dtype=self._endian + self._float_type,
TypeError: cannot concatenate 'str' and 'int' objects

# Fixed code
def _get_gadget_format(filename, header_size):
    # check and return gadget binary format with file endianness
    ff = open(filename, "rb")
    (rhead,) = struct.unpack("<I", ff.read(4))
    ff.close()
    if rhead == _byte_swap_32(8):
        return 2, ">"
    elif rhead == 8:
        return 2, "<"
    elif rhead == _byte_swap_32(header_size):
        return 1, ">"
    elif rhead == header_size:
        return 1, "<"
    else:
        raise RuntimeError("Incorrect Gadget format %s!" % str(rhead))
# Buggy code
def __init__(self, ds, io, filename, file_id):
    gformat = _get_gadget_format(filename)
    with open(filename, "rb") as f:
        if gformat[0] == 2:
            f.seek(f.tell() + SNAP_FORMAT_2_OFFSET)
        self.header = read_record(f, ds._header_spec, endian=gformat[1])
        if gformat[0] == 2:
            f.seek(f.tell() + SNAP_FORMAT_2_OFFSET)
        self._position_offset = f.tell()
        f.seek(0, os.SEEK_END)
        self._file_size = f.tell()

    super(GadgetBinaryFile, self).__init__(ds, io, filename, file_id)


# Traceback (TypeError)
Traceback (most recent call last):
File "./plot_solution.py", line 519, in <module>
pf.add_field(("gas", "density_squared"), function=_density_squared, units="g**2/cm**6")
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 1191, in add_field
self.index
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 504, in index
self, dataset_type=self.dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 39, in __init__
super(ParticleIndex, self).__init__(ds, dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/geometry_handler.py", line 50, in __init__
self._setup_geometry()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 50, in _setup_geometry
self._initialize_particle_handler()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 104, in _initialize_particle_handler
self._initialize_indices()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 134, in _initialize_indices
self.io._initialize_index(data_file, self.regions)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 351, in _initialize_index
data_file, count, 0, regions, DLE, DRE)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 334, in _get_morton_from_position
pp = np.fromfile(f, dtype=self._endian + self._float_type,
TypeError: cannot concatenate 'str' and 'int' objects

# Fixed code
def __init__(self, ds, io, filename, file_id):
    gformat = _get_gadget_format(filename, ds._header_size)
    with open(filename, "rb") as f:
        if gformat[0] == 2:
            f.seek(f.tell() + SNAP_FORMAT_2_OFFSET)
        self.header = read_record(f, ds._header_spec, endian=gformat[1])
        if gformat[0] == 2:
            f.seek(f.tell() + SNAP_FORMAT_2_OFFSET)
        self._position_offset = f.tell()
        f.seek(0, os.SEEK_END)
        self._file_size = f.tell()

    super(GadgetBinaryFile, self).__init__(ds, io, filename, file_id)
# Buggy code
def _setup_binary_spec(self, spec, spec_dict):
    if isinstance(spec, str):
        _hs = ()
        for hs in spec.split("+"):
            _hs += spec_dict[hs]
        spec = _hs
    return spec


# Traceback (TypeError)
Traceback (most recent call last):
File "./plot_solution.py", line 519, in <module>
pf.add_field(("gas", "density_squared"), function=_density_squared, units="g**2/cm**6")
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 1191, in add_field
self.index
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 504, in index
self, dataset_type=self.dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 39, in __init__
super(ParticleIndex, self).__init__(ds, dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/geometry_handler.py", line 50, in __init__
self._setup_geometry()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 50, in _setup_geometry
self._initialize_particle_handler()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 104, in _initialize_particle_handler
self._initialize_indices()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 134, in _initialize_indices
self.io._initialize_index(data_file, self.regions)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 351, in _initialize_index
data_file, count, 0, regions, DLE, DRE)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 334, in _get_morton_from_position
pp = np.fromfile(f, dtype=self._endian + self._float_type,
TypeError: cannot concatenate 'str' and 'int' objects

# Fixed code
def _setup_binary_spec(cls, spec, spec_dict):
    if isinstance(spec, str):
        _hs = ()
        for hs in spec.split("+"):
            _hs += spec_dict[hs]
        spec = _hs
    return spec
# Buggy code
def _get_hvals(self):
    # The entries in this header are capitalized and named to match Table 4
    # in the GADGET-2 user guide.
    gformat = _get_gadget_format(self.parameter_filename)
    f = open(self.parameter_filename, "rb")
    if gformat[0] == 2:
        f.seek(f.tell() + SNAP_FORMAT_2_OFFSET)
    hvals = read_record(f, self._header_spec, endian=gformat[1])
    for i in hvals:
        if len(hvals[i]) == 1:
            hvals[i] = hvals[i][0]
    return hvals


# Traceback (TypeError)
Traceback (most recent call last):
File "./plot_solution.py", line 519, in <module>
pf.add_field(("gas", "density_squared"), function=_density_squared, units="g**2/cm**6")
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 1191, in add_field
self.index
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 504, in index
self, dataset_type=self.dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 39, in __init__
super(ParticleIndex, self).__init__(ds, dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/geometry_handler.py", line 50, in __init__
self._setup_geometry()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 50, in _setup_geometry
self._initialize_particle_handler()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 104, in _initialize_particle_handler
self._initialize_indices()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 134, in _initialize_indices
self.io._initialize_index(data_file, self.regions)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 351, in _initialize_index
data_file, count, 0, regions, DLE, DRE)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 334, in _get_morton_from_position
pp = np.fromfile(f, dtype=self._endian + self._float_type,
TypeError: cannot concatenate 'str' and 'int' objects

# Fixed code
def _get_hvals(self):
    # The entries in this header are capitalized and named to match Table 4
    # in the GADGET-2 user guide.
    gformat = _get_gadget_format(self.parameter_filename, self._header_size)
    f = open(self.parameter_filename, "rb")
    if gformat[0] == 2:
        f.seek(f.tell() + SNAP_FORMAT_2_OFFSET)
    hvals = read_record(f, self._header_spec, endian=gformat[1])
    for i in hvals:
        if len(hvals[i]) == 1:
            hvals[i] = hvals[i][0]
    return hvals
# Buggy code
def _is_valid(self, *args, **kwargs):
    # First 4 bytes used to check load
    return GadgetDataset._validate_header(args[0])[0]


# Traceback (TypeError)
Traceback (most recent call last):
File "./plot_solution.py", line 519, in <module>
pf.add_field(("gas", "density_squared"), function=_density_squared, units="g**2/cm**6")
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 1191, in add_field
self.index
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 504, in index
self, dataset_type=self.dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 39, in __init__
super(ParticleIndex, self).__init__(ds, dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/geometry_handler.py", line 50, in __init__
self._setup_geometry()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 50, in _setup_geometry
self._initialize_particle_handler()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 104, in _initialize_particle_handler
self._initialize_indices()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 134, in _initialize_indices
self.io._initialize_index(data_file, self.regions)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 351, in _initialize_index
data_file, count, 0, regions, DLE, DRE)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 334, in _get_morton_from_position
pp = np.fromfile(f, dtype=self._endian + self._float_type,
TypeError: cannot concatenate 'str' and 'int' objects

# Fixed code
def _is_valid(cls, *args, **kwargs):
    if "header_spec" in kwargs:
        # Compute header size if header is customized
        header_spec = cls._setup_binary_spec(kwargs["header_spec"], gadget_header_specs)
        header_size = _compute_header_size(header_spec)
    else:
        header_size = 256
    # First 4 bytes used to check load
    return GadgetDataset._validate_header(args[0], header_size)[0]
# Buggy code
def __init__(self, ds, *args, **kwargs):
    self._vector_fields = dict(self._vector_fields)
    self._fields = ds._field_spec
    self._ptypes = ds._ptype_spec
    self.data_files = set([])
    gformat = _get_gadget_format(ds.parameter_filename)
    # gadget format 1 original, 2 with block name
    self._format = gformat[0]
    self._endian = gformat[1]
    super(IOHandlerGadgetBinary, self).__init__(ds, *args, **kwargs)


# Traceback (TypeError)
Traceback (most recent call last):
File "./plot_solution.py", line 519, in <module>
pf.add_field(("gas", "density_squared"), function=_density_squared, units="g**2/cm**6")
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 1191, in add_field
self.index
File "/usr/local/lib/python2.7/dist-packages/yt/data_objects/static_output.py", line 504, in index
self, dataset_type=self.dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 39, in __init__
super(ParticleIndex, self).__init__(ds, dataset_type)
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/geometry_handler.py", line 50, in __init__
self._setup_geometry()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 50, in _setup_geometry
self._initialize_particle_handler()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 104, in _initialize_particle_handler
self._initialize_indices()
File "/usr/local/lib/python2.7/dist-packages/yt/geometry/particle_geometry_handler.py", line 134, in _initialize_indices
self.io._initialize_index(data_file, self.regions)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 351, in _initialize_index
data_file, count, 0, regions, DLE, DRE)
File "/usr/local/lib/python2.7/dist-packages/yt/frontends/gadget/io.py", line 334, in _get_morton_from_position
pp = np.fromfile(f, dtype=self._endian + self._float_type,
TypeError: cannot concatenate 'str' and 'int' objects

# Fixed code
def __init__(self, ds, *args, **kwargs):
    self._vector_fields = dict(self._vector_fields)
    self._fields = ds._field_spec
    self._ptypes = ds._ptype_spec
    self.data_files = set([])
    gformat = _get_gadget_format(ds.parameter_filename, ds._header_size)
    # gadget format 1 original, 2 with block name
    self._format = gformat[0]
    self._endian = gformat[1]
    super(IOHandlerGadgetBinary, self).__init__(ds, *args, **kwargs)