gurudesh/pytracebugs huggingface dataset:
# Buggy code
def toBox(self, name, strings, objects, proto):
    """
    Convert from data to box. We handled too-long
    batched data and put it together here.
    """
    value = StringIO(objects[name])
    strings[name] = value.read(AMP_MAXLEN)
    for counter in count(2):
        chunk = value.read(AMP_MAXLEN)
        if not chunk:
            break
        strings["%s.%d" % (name, counter)] = chunk


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/src/evennia/evennia/commands/cmdhandler.py", line 591, in _run_command
ret = cmd.func()
File "/usr/src/evennia/evennia/commands/default/system.py", line 769, in func
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0  # resident
memory
ValueError: could not convert string to float:

An untrapped error occurred.
(Traceback was logged 2018-07-17 01:10:38-0000).

# Fixed code
def toBox(self, name, strings, objects, proto):
    """
    Convert from python object to string box representation.
    we break up too-long data snippets into multiple batches here.

    """
    value = StringIO(objects[name])
    strings[name] = self.toStringProto(value.read(AMP_MAXLEN), proto)
    for counter in count(2):
        chunk = value.read(AMP_MAXLEN)
        if not chunk:
            break
        strings["%s.%d" % (name, counter)] = self.toStringProto(chunk, proto)
# Buggy code
def toString(self, inObject):
    """
    Convert to send on the wire, with compression.
    """
    return zlib.compress(inObject, 9)


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/src/evennia/evennia/commands/cmdhandler.py", line 591, in _run_command
ret = cmd.func()
File "/usr/src/evennia/evennia/commands/default/system.py", line 769, in func
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0  # resident
memory
ValueError: could not convert string to float:

An untrapped error occurred.
(Traceback was logged 2018-07-17 01:10:38-0000).

# Fixed code
def toString(self, inObject):
    """
    Convert to send as a string on the wire, with compression.
    """
    return zlib.compress(super(Compressed, self).toString(inObject), 9)
# Buggy code
def fromString(self, inString):
    """
    Convert (decompress) from the wire to Python.
    """
    return zlib.decompress(inString)


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/src/evennia/evennia/commands/cmdhandler.py", line 591, in _run_command
ret = cmd.func()
File "/usr/src/evennia/evennia/commands/default/system.py", line 769, in func
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0  # resident
memory
ValueError: could not convert string to float:

An untrapped error occurred.
(Traceback was logged 2018-07-17 01:10:38-0000).

# Fixed code
def fromString(self, inString):
    """
    Convert (decompress) from the string-representation on the wire to Python.
    """
    return super(Compressed, self).fromString(zlib.decompress(inString))
# Buggy code
def __init__(self, *args, **kwargs):
    """
    Initialize protocol with some things that need to be in place
    already before connecting both on portal and server.

    """
    self.send_batch_counter = 0
    self.send_reset_time = time.time()
    self.send_mode = True
    self.send_task = None


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/src/evennia/evennia/commands/cmdhandler.py", line 591, in _run_command
ret = cmd.func()
File "/usr/src/evennia/evennia/commands/default/system.py", line 769, in func
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0  # resident
memory
ValueError: could not convert string to float:

An untrapped error occurred.
(Traceback was logged 2018-07-17 01:10:38-0000).

# Fixed code
def __init__(self, *args, **kwargs):
    """
    Initialize protocol with some things that need to be in place
    already before connecting both on portal and server.

    """
    self.send_batch_counter = 0
    self.send_reset_time = time.time()
    self.send_mode = True
    self.send_task = None
    self.multibatches = 0
# Buggy code
def dataReceived(self, data):
    """
    Handle non-AMP messages, such as HTTP communication.
    """
    if data[0] != b"\0":
        self.transport.write(_HTTP_WARNING)
        self.transport.loseConnection()
    else:
        super(AMPMultiConnectionProtocol, self).dataReceived(data)


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/src/evennia/evennia/commands/cmdhandler.py", line 591, in _run_command
ret = cmd.func()
File "/usr/src/evennia/evennia/commands/default/system.py", line 769, in func
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0  # resident
memory
ValueError: could not convert string to float:

An untrapped error occurred.
(Traceback was logged 2018-07-17 01:10:38-0000).

# Fixed code
def dataReceived(self, data):
    """
    Handle non-AMP messages, such as HTTP communication.
    """
    if data[0] == NUL:
        # an AMP communication
        if data[-2:] != NULNUL:
            # an incomplete AMP box means more batches are forthcoming.
            self.multibatches += 1
        super(AMPMultiConnectionProtocol, self).dataReceived(data)
    elif self.multibatches:
        # invalid AMP, but we have a pending multi-batch that is not yet complete
        if data[-2:] == NULNUL:
            # end of existing multibatch
            self.multibatches = max(0, self.multibatches - 1)
        super(AMPMultiConnectionProtocol, self).dataReceived(data)
    else:
        # not an AMP communication, return warning
        self.transport.write(_HTTP_WARNING)
        self.transport.loseConnection()
        print("HTML received: %s" % data)
# Buggy code
def portal_receive_server2portal(self, packed_data):
    """
    Receives message arriving to Portal from Server.
    This method is executed on the Portal.

    Args:
        packed_data (str): Pickled data (sessid, kwargs) coming over the wire.

    """
    sessid, kwargs = self.data_in(packed_data)
    session = self.factory.portal.sessions.get(sessid, None)
    if session:
        self.factory.portal.sessions.data_out(session, **kwargs)
    return {}


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/src/evennia/evennia/commands/cmdhandler.py", line 591, in _run_command
ret = cmd.func()
File "/usr/src/evennia/evennia/commands/default/system.py", line 769, in func
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0  # resident
memory
ValueError: could not convert string to float:

An untrapped error occurred.
(Traceback was logged 2018-07-17 01:10:38-0000).

# Fixed code
def portal_receive_server2portal(self, packed_data):
    """
    Receives message arriving to Portal from Server.
    This method is executed on the Portal.

    Args:
        packed_data (str): Pickled data (sessid, kwargs) coming over the wire.

    """
    try:
        sessid, kwargs = self.data_in(packed_data)
        session = self.factory.portal.sessions.get(sessid, None)
        if session:
            self.factory.portal.sessions.data_out(session, **kwargs)
    except Exception:
        logger.log_trace("packed_data len {}".format(len(packed_data)))
    return {}
# Buggy code
def msg(self, text=None, **kwargs):
    """
    Takes text from connected channel (only).

    Args:
        text (str, optional): Incoming text from channel.

    Kwargs:
        options (dict): Options dict with the following allowed keys:
            - from_channel (str): dbid of a channel this text originated from.
            - from_obj (list): list of objects this text.

    """
    from_obj = kwargs.get("from_obj", None)
    options = kwargs.get("options", None) or {}
    if not self.ndb.ev_channel and self.db.ev_channel:
        # cache channel lookup
        self.ndb.ev_channel = self.db.ev_channel
    if (
        "from_channel" in options
        and text
        and self.ndb.ev_channel.dbid == options["from_channel"]
    ):
        if not from_obj or from_obj != [self.id]:
            super(IRCBot, self).msg(channel=text)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/home/avenew/evennia/evennia/objects/objects.py", line 511, in msg
from_obj.at_msg_send(text=text, to_obj=self, **kwargs)
AttributeError: 'list' object has no attribute 'at_msg_send'

# Fixed code
def msg(self, text=None, **kwargs):
    """
    Takes text from connected channel (only).

    Args:
        text (str, optional): Incoming text from channel.

    Kwargs:
        options (dict): Options dict with the following allowed keys:
            - from_channel (str): dbid of a channel this text originated from.
            - from_obj (list): list of objects sending this text.

    """
    from_obj = kwargs.get("from_obj", None)
    options = kwargs.get("options", None) or {}
    if not self.ndb.ev_channel and self.db.ev_channel:
        # cache channel lookup
        self.ndb.ev_channel = self.db.ev_channel
    if (
        "from_channel" in options
        and text
        and self.ndb.ev_channel.dbid == options["from_channel"]
    ):
        if not from_obj or from_obj != [self]:
            super(IRCBot, self).msg(channel=text)
# Buggy code
def dataReceived(self, data):
    """
    Handle non-AMP messages, such as HTTP communication.
    """
    if data[0] == NUL:
        # an AMP communication
        if data[-2:] != NULNUL:
            # an incomplete AMP box means more batches are forthcoming.
            self.multibatches += 1
        super(AMPMultiConnectionProtocol, self).dataReceived(data)
    elif self.multibatches:
        # invalid AMP, but we have a pending multi-batch that is not yet complete
        if data[-2:] == NULNUL:
            # end of existing multibatch
            self.multibatches = max(0, self.multibatches - 1)
        super(AMPMultiConnectionProtocol, self).dataReceived(data)
    else:
        # not an AMP communication, return warning
        self.transport.write(_HTTP_WARNING)
        self.transport.loseConnection()
        print("HTML received: %s" % data)


# Traceback (IndexError)
2017-04-14T10:44:37-0400 [AMPProtocol,client] [::] Traceback (most recent call last):
2017-04-14T10:44:37-0400 [AMPProtocol,client] [::]   File "/home/tehom/arx/evennia/evennia/server/portal/portalsessionhandler.py", line 420, in data_out
2017-04-14T10:44:37-0400 [AMPProtocol,client] [::]     getattr(session, funcname)(*cmdargs, **cmdkwargs)
2017-04-14T10:44:37-0400 [AMPProtocol,client] [::]   File "/home/tehom/arx/evennia/evennia/server/portal/telnet.py", line 340, in send_text
2017-04-14T10:44:37-0400 [AMPProtocol,client] [::]     linetosend = ansi.parse_ansi(_RE_N.sub("", text) + ("|n" if text[-1] != "|" else "||n"),
2017-04-14T10:44:37-0400 [AMPProtocol,client] [::] IndexError: string index out of range

# Fixed code
def dataReceived(self, data):
    """
    Handle non-AMP messages, such as HTTP communication.
    """
    if data[0] == NUL:
        # an AMP communication
        if data[-2:] != NULNUL:
            # an incomplete AMP box means more batches are forthcoming.
            self.multibatches += 1
        try:
            super(AMPMultiConnectionProtocol, self).dataReceived(data)
        except KeyError:
            _LOGGER.trace("Discarded incoming partial data: {}".format(to_str(data)))
    elif self.multibatches:
        # invalid AMP, but we have a pending multi-batch that is not yet complete
        if data[-2:] == NULNUL:
            # end of existing multibatch
            self.multibatches = max(0, self.multibatches - 1)
        try:
            super(AMPMultiConnectionProtocol, self).dataReceived(data)
        except KeyError:
            _LOGGER.trace("Discarded incoming multi-batch data:".format(to_str(data)))
    else:
        # not an AMP communication, return warning
        self.transport.write(_HTTP_WARNING)
        self.transport.loseConnection()
        print("HTML received: %s" % data)
# Buggy code
def logerr(string=""):
    "Simple log helper method"
    logger.log_trace()
    self.msg(string)


# Traceback (RuntimeError)
Error: TestSubject.location = TestObject creates a location loop.
Traceback (most recent call last):
File "evennia/evennia/objects/objects.py", line 654, in move_to
self.location = destination
File "/evennia/evennia/objects/models.py", line 249, in __location_set
is_loc_loop(location)
File "/evennia/evennia/objects/models.py", line 247, in is_loc_loop
return is_loc_loop(loc.db_location, depth + 1)
File "/evennia/evennia/objects/models.py", line 244, in is_loc_loop
raise RuntimeError
RuntimeError

# Fixed code
def logerr(string="", err=None):
    "Simple log helper method"
    logger.log_trace()
    self.msg("%s%s" % (string, "" if err is None else " (%s)" % err))
# Buggy code
def __init__(
    self,
    write: typing.Callable[[str], None],
    do_log: bool,
    do_capture: bool,
    max_capture: int,
):
    self.write = write
    self.do_log = do_log
    self.do_capture = do_capture
    self.finished = Lock()
    self.capture = deque(maxlen=max_capture)  # type: typing.MutableSequence[str]
    self.finished.acquire()


# Traceback (json.decoder.JSONDecodeError)
Traceback (most recent call last):
File "/usr/local/bin/telepresence/telepresence/cli.py", line 130, in crash_reporting
yield
File "/usr/local/bin/telepresence/telepresence/main.py", line 77, in main
runner, remote_info, env, socks_port, ssh, mount_dir, pod_info
File "/usr/local/bin/telepresence/telepresence/outbound/setup.py", line 73, in launch
runner_, remote_info, command, args.also_proxy, env, ssh
File "/usr/local/bin/telepresence/telepresence/outbound/local.py", line 121, in launch_vpn
connect_sshuttle(runner, remote_info, also_proxy, ssh)
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 256, in connect_sshuttle
] + get_proxy_cidrs(runner, remote_info, hosts_or_ips),
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 82, in get_proxy_cidrs
result.update(context_cache.lookup("podCIDRs", lambda: podCIDRs(runner)))
File "/usr/local/bin/telepresence/telepresence/runner/cache.py", line 105, in lookup
value = function()
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 82, in <lambda>
result.update(context_cache.lookup("podCIDRs", lambda: podCIDRs(runner)))
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 156, in podCIDRs
runner.get_output(runner.kubectl("get", "nodes", "-o", "json"))
File "/Users/chaitanyaveluvolu/.pyenv/versions/3.7.3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Users/chaitanyaveluvolu/.pyenv/versions/3.7.3/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 21 (char 20)

# Fixed code
def __init__(
    self,
    write: typing.Callable[[str], None],
    do_log: bool,
    do_capture: bool,
    max_capture: int,
):
    self.write = write
    self.do_log = do_log
    self.do_capture = do_capture
    self.finished = Lock()
    if max_capture < 0:
        capture = []  # type: typing.MutableSequence[str]
    else:
        capture = deque(maxlen=max_capture)
    self.capture = capture  # type: typing.MutableSequence[str]
    self.finished.acquire()
# Buggy code
def _make_logger(
    self, track: int, do_log: bool, do_capture: bool, limit_capture=99999
) -> _Logger:
    """Create a logger that optionally captures what is logged"""
    prefix = "{:>3d}".format(track)

    def write(line: str):
        self.output.write(mask_sensitive_data(line), prefix=prefix)

    return _Logger(write, do_log, do_capture, limit_capture)


# Traceback (json.decoder.JSONDecodeError)
Traceback (most recent call last):
File "/usr/local/bin/telepresence/telepresence/cli.py", line 130, in crash_reporting
yield
File "/usr/local/bin/telepresence/telepresence/main.py", line 77, in main
runner, remote_info, env, socks_port, ssh, mount_dir, pod_info
File "/usr/local/bin/telepresence/telepresence/outbound/setup.py", line 73, in launch
runner_, remote_info, command, args.also_proxy, env, ssh
File "/usr/local/bin/telepresence/telepresence/outbound/local.py", line 121, in launch_vpn
connect_sshuttle(runner, remote_info, also_proxy, ssh)
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 256, in connect_sshuttle
] + get_proxy_cidrs(runner, remote_info, hosts_or_ips),
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 82, in get_proxy_cidrs
result.update(context_cache.lookup("podCIDRs", lambda: podCIDRs(runner)))
File "/usr/local/bin/telepresence/telepresence/runner/cache.py", line 105, in lookup
value = function()
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 82, in <lambda>
result.update(context_cache.lookup("podCIDRs", lambda: podCIDRs(runner)))
File "/usr/local/bin/telepresence/telepresence/outbound/vpn.py", line 156, in podCIDRs
runner.get_output(runner.kubectl("get", "nodes", "-o", "json"))
File "/Users/chaitanyaveluvolu/.pyenv/versions/3.7.3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Users/chaitanyaveluvolu/.pyenv/versions/3.7.3/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 21 (char 20)

# Fixed code
def _make_logger(
    self, track: int, do_log: bool, do_capture: bool, limit_capture=-1
) -> _Logger:
    """Create a logger that optionally captures what is logged"""
    prefix = "{:>3d}".format(track)

    def write(line: str):
        self.output.write(mask_sensitive_data(line), prefix=prefix)

    return _Logger(write, do_log, do_capture, limit_capture)
# Buggy code
def __init__(self, logfile_path: str) -> None:
    """
    Create output handle

    :param logfile_path: Path or string file path or "-" for stdout
    """

    if logfile_path == "-":
        self.logfile = sys.stdout
    else:
        # Log file path should be absolute since some processes may run in
        # different directories:
        logfile_path = os.path.abspath(logfile_path)
        self.logfile = _open_logfile(logfile_path)
    self.logfile_path = logfile_path

    self.start_time = curtime()
    self.logtail = deque(maxlen=25)  # type: deque  # keep last 25 lines

    self.write("Telepresence {} launched at {}".format(__version__, ctime()))
    self.write("  {}".format(str_command(sys.argv)))
    if version_override:
        self.write("  TELEPRESENCE_VERSION is {}".format(image_version))
    elif image_version != __version__:
        self.write("  Using images version {} (dev)".format(image_version))


# Traceback (FileNotFoundError)
Traceback (most recent call last):
File "/usr/local/bin/telepresence/telepresence/cli.py", line 131, in crash_reporting
yield
File "/usr/local/bin/telepresence/telepresence/main.py", line 46, in main
runner = Runner(args.logfile, None, args.verbose)
File "/usr/local/bin/telepresence/telepresence/runner/runner.py", line 59, in __init__
self.output = Output(logfile_path)
File "/usr/local/bin/telepresence/telepresence/runner/output.py", line 58, in __init__
logfile_path = os.path.abspath(logfile_path)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/posixpath.py", line 376, in abspath
cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory

# Fixed code
def __init__(self, logfile_path: str) -> None:
    """
    Create output handle

    :param logfile_path: Path or string file path or "-" for stdout
    """

    # Fail if current working directory does not exist so we don't crash in
    # standard library path-handling code.
    try:
        os.getcwd()
    except OSError:
        exit("T: Error: Current working directory does not exist.")

    if logfile_path == "-":
        self.logfile = sys.stdout
    else:
        # Log file path should be absolute since some processes may run in
        # different directories:
        logfile_path = os.path.abspath(logfile_path)
        self.logfile = _open_logfile(logfile_path)
    self.logfile_path = logfile_path

    self.start_time = curtime()
    self.logtail = deque(maxlen=25)  # type: deque  # keep last 25 lines

    self.write("Telepresence {} launched at {}".format(__version__, ctime()))
    self.write("  {}".format(str_command(sys.argv)))
    if version_override:
        self.write("  TELEPRESENCE_VERSION is {}".format(image_version))
    elif image_version != __version__:
        self.write("  Using images version {} (dev)".format(image_version))
# Buggy code
def setup_inject(runner: Runner, args):
    runner.require(["torsocks"], "Please install torsocks (v2.1 or later)")
    if runner.chatty:
        runner.show(
            "Starting proxy with method 'inject-tcp', which has the following "
            "limitations: Go programs, static binaries, suid programs, and "
            "custom DNS implementations are not supported. For a full list of "
            "method limitations see "
            "https://telepresence.io/reference/methods.html"
        )
    command = ["torsocks"] + (args.run or ["bash--norc"])

    def launch(runner_, _remote_info, env, socks_port, _ssh, _mount_dir):
        return launch_inject(runner_, command, socks_port, env)

    return launch


# Traceback (FileNotFoundError)
Traceback (most recent call last):
File "/usr/bin/telepresence/telepresence/cli.py", line 129, in crash_reporting
yield
File "/usr/bin/telepresence/telepresence/main.py", line 78, in main
runner, remote_info, env, socks_port, ssh, mount_dir
File "/usr/bin/telepresence/telepresence/outbound/__init__.py", line 60, in launch
runner_, remote_info, command, args.also_proxy, env, ssh
File "/usr/bin/telepresence/telepresence/outbound/local.py", line 123, in launch_vpn
process = Popen(command, env=env)
File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/usr/lib64/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'bash--norc': 'bash--norc'

# Fixed code
def setup_inject(runner: Runner, args):
    runner.require(["torsocks"], "Please install torsocks (v2.1 or later)")
    if runner.chatty:
        runner.show(
            "Starting proxy with method 'inject-tcp', which has the following "
            "limitations: Go programs, static binaries, suid programs, and "
            "custom DNS implementations are not supported. For a full list of "
            "method limitations see "
            "https://telepresence.io/reference/methods.html"
        )
    command = ["torsocks"] + (args.run or ["bash", "--norc"])

    def launch(runner_, _remote_info, env, socks_port, _ssh, _mount_dir):
        return launch_inject(runner_, command, socks_port, env)

    return launch
# Buggy code
def load(cls, filename):
    """Return a cache loaded from a file"""
    try:
        with open(filename, "r") as f:
            cache = json.load(f)
    except FileNotFoundError:
        cache = {}

    result = Cache(cache)

    def save():
        """Overwrite the original file with current cache contents"""
        with open(filename, "w") as f:
            json.dump(result.values, f)

    result.save = save
    return result


# Traceback (json.decoder.JSONDecodeError)
Traceback (most recent call last):
File "/usr/local/share/telepresence/libexec/lib/python3.6/site-packages/telepresence/cli.py", line 74, in call_f
return f(*args, **kwargs)
File "/usr/local/share/telepresence/libexec/lib/python3.6/site-packages/telepresence/main.py", line 433, in go
runner = Runner.open(args.logfile, kubectl_or_oc(server), args.verbose)
File "/usr/local/share/telepresence/libexec/lib/python3.6/site-packages/telepresence/runner.py", line 138, in open
open(logfile_path, "a", buffering=1), kubectl_cmd, verbose
File "/usr/local/share/telepresence/libexec/lib/python3.6/site-packages/telepresence/runner.py", line 114, in __init__
self.cache = Cache.load(os.path.join(cache_dir, "cache.json"))
File "/usr/local/share/telepresence/libexec/lib/python3.6/site-packages/telepresence/cache.py", line 29, in load
cache = json.load(f)
File "/usr/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

# Fixed code
def load(cls, filename):
    """Return a cache loaded from a file"""
    try:
        with open(filename, "r") as f:
            cache = json.load(f)
    except (FileNotFoundError, ValueError):
        cache = {}

    result = Cache(cache)

    def save():
        """Overwrite the original file with current cache contents"""
        with open(filename, "w") as f:
            json.dump(result.values, f)

    result.save = save
    return result
# Buggy code
def write(self, message: str, prefix="TEL") -> None:
    """Write a message to the log."""
    for sub_message in message.splitlines():
        line = "{:6.1f} {} | {}\n".format(
            time() - self.start_time, prefix, sub_message.rstrip()
        )
        self.logfile.write(line)
    self.logfile.flush()


# Traceback (socket.gaierror)
16.6 TL | [39] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence6')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
16.8 TL | [40] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence7')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

# Fixed code
def write(self, message: str, prefix="TEL") -> None:
    """Write a message to the log."""
    if self.logfile.closed:
        return
    for sub_message in message.splitlines():
        line = "{:6.1f} {} | {}\n".format(
            time() - self.start_time, prefix, sub_message.rstrip()
        )
        self.logfile.write(line)
    self.logfile.flush()
# Buggy code
def launch_command(self, track, args, **kwargs) -> Popen:
    """Call a command, generate stamped, logged output."""
    kwargs = kwargs.copy()
    in_data = kwargs.get("input")
    if "input" in kwargs:
        del kwargs["input"]
        kwargs["stdin"] = PIPE
    kwargs["stdout"] = PIPE
    kwargs["stderr"] = STDOUT
    try:
        process = Popen(args, **kwargs)
    except OSError as exc:
        self.write("[{}] {}".format(track, exc))
        raise
    Popen(
        [
            "stamp-telepresence",
            "--id",
            str(track),
            "--start-time",
            str(self.start_time),
        ],
        stdin=process.stdout,
        stdout=self.logfile,
        stderr=self.logfile,
    )
    if in_data:
        process.communicate(in_data, timeout=kwargs.get("timeout"))
    return process


# Traceback (socket.gaierror)
16.6 TL | [39] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence6')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
16.8 TL | [40] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence7')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

# Fixed code
def launch_command(self, track, out_cb, err_cb, args, **kwargs) -> Popen:
    """Call a command, generate stamped, logged output."""
    try:
        process = launch_command(args, out_cb, err_cb, **kwargs)
    except OSError as exc:
        self.write("[{}] {}".format(track, exc))
        raise
    return process
# Buggy code
def check_call(self, args, **kwargs):
    """Run a subprocess, make sure it exited with 0."""
    self.counter = track = self.counter + 1
    self.write("[{}] Running: {}".format(track, str_command(args)))
    if "input" not in kwargs and "stdin" not in kwargs:
        kwargs["stdin"] = DEVNULL
    span = self.command_span(track, args)
    process = self.launch_command(track, args, **kwargs)
    process.wait()
    spent = span.end()
    retcode = process.poll()
    if retcode:
        self.write("[{}] exit {} in {:0.2f} secs.".format(track, retcode, spent))
        raise CalledProcessError(retcode, args)
    self.write("[{}] ran in {:0.2f} secs.".format(track, spent))


# Traceback (socket.gaierror)
16.6 TL | [39] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence6')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
16.8 TL | [40] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence7')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

# Fixed code
def check_call(self, args, **kwargs):
    """Run a subprocess, make sure it exited with 0."""
    self.counter = track = self.counter + 1
    out_cb = err_cb = self.make_logger(track)
    self.run_command(track, "Running", "ran", out_cb, err_cb, args, **kwargs)
# Buggy code
def get_output(self, args, reveal=False, stderr=None, **kwargs) -> str:
    """Return (stripped) command result as unicode string."""
    if stderr is None:
        stderr = self.logfile
    self.counter = track = self.counter + 1
    self.write("[{}] Capturing: {}".format(track, str_command(args)))
    kwargs["stdin"] = DEVNULL
    kwargs["stderr"] = stderr
    span = self.command_span(track, args)
    try:
        result = str(check_output(args, **kwargs).strip(), "utf-8")
    finally:
        spent = span.end()
        if reveal or self.verbose:
            self.write(result, prefix="{:3d}".format(track))
    if spent > 1:
        self.write("[{}] captured in {:0.2f} secs.".format(track, spent))
    return result


# Traceback (socket.gaierror)
16.6 TL | [39] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence6')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
16.8 TL | [40] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence7')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

# Fixed code
def get_output(self, args, reveal=False, **kwargs) -> str:
    """Return (stripped) command result as unicode string."""
    self.counter = track = self.counter + 1
    capture = []  # type: List[str]
    if reveal or self.verbose:
        out_cb = self.make_logger(track, capture=capture)
    else:
        out_cb = capture.append
    err_cb = self.make_logger(track)
    self.run_command(track, "Capturing", "captured", out_cb, err_cb, args, **kwargs)
    return "".join(capture).strip()
# Buggy code
def popen(self, args, stdin=DEVNULL, **kwargs) -> Popen:
    """Return Popen object."""
    self.counter = track = self.counter + 1
    self.write("[{}] Launching: {}".format(track, str_command(args)))
    kwargs["stdin"] = stdin
    result = self.launch_command(track, args, **kwargs)
    return result


# Traceback (socket.gaierror)
16.6 TL | [39] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence6')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
16.8 TL | [40] Capturing: (['python3', '-c', "import socket; socket.gethostbyname('hellotelepresence7')"],)...
Traceback (most recent call last):
File "<string>", line 1, in <module>
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

# Fixed code
def popen(self, args, **kwargs) -> Popen:
    """Return Popen object."""
    self.counter = track = self.counter + 1
    out_cb = err_cb = self.make_logger(track)

    def done(proc):
        self._popen_done(track, proc)

    self.write("[{}] Launching: {}".format(track, str_command(args)))
    process = self.launch_command(track, out_cb, err_cb, args, done=done, **kwargs)
    return process
# Buggy code
def __init__(self, deployment_name, pod_name, deployment_config):
    self.deployment_name = deployment_name
    self.pod_name = pod_name
    self.deployment_config = deployment_config
    cs = deployment_config["spec"]["template"]["spec"]["containers"]
    self.container_config = [c for c in cs if "telepresence-k8s" in c["image"]][0]
    self.container_name = self.container_config["name"]


# Traceback (NameError)
$ X="-it --rm leastauthority/subscription-converger /app/env/bin/python -c xxx"
$ docker run $X
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'xxx' is not defined
$ ../telepresence --deployment s4-infrastructure --docker-run $X
Starting proxy...
Starting local container...
ImportError: No module named site
Shutting proxy down...
$

# Fixed code
def __init__(self, deployment_name, pod_name, deployment_config):
    self.deployment_name = deployment_name
    self.pod_name = pod_name
    self.deployment_config = deployment_config
    cs = deployment_config["spec"]["template"]["spec"]["containers"]
    self.container_config = [c for c in cs if "telepresence-k8s" in c["image"]][0]
    self.container_name = self.container_config["name"]
    # Wait for pod to be running before we can get environment:
    wait_for_pod(self)
    self.pod_environment = _get_remote_env(pod_name, self.container_name)
    self.service_names = _get_service_names(self.pod_environment)
# Buggy code
def _get_service_names(environment):
    """Return names of Services, as used in env variable names."""
    # XXX duplicated in remote-telepresence
    # XXX also check for TCPness.
    # Order matters for service_keys, need it to be consistent with port
    # forwarding order in remote container.
    result = [
        key[: -len("_SERVICE_HOST")]
        for key in environment
        if key.endswith("_SERVICE_HOST")
    ]
    result.sort()
    return result


# Traceback (NameError)
$ X="-it --rm leastauthority/subscription-converger /app/env/bin/python -c xxx"
$ docker run $X
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'xxx' is not defined
$ ../telepresence --deployment s4-infrastructure --docker-run $X
Starting proxy...
Starting local container...
ImportError: No module named site
Shutting proxy down...
$

# Fixed code
def _get_service_names(environment):
    """Return names of Services, as used in env variable names."""
    # XXX need to check for TCPness.
    # Order matters for service_keys, need it to be consistent with port
    # forwarding order in remote container.
    result = [
        key[: -len("_SERVICE_HOST")]
        for key in environment
        if key.endswith("_SERVICE_HOST")
    ]
    result.sort()
    return result
# Buggy code
def write_env(remote_info, uid):
    for_docker_env, for_local_env = get_env_variables(remote_info)
    with open("/output/unproxied.env.tmp", "w") as f:
        for key, value in for_local_env.items():
            f.write("{}={}\n".format(key, value))
    chown("/output/unproxied.env.tmp", uid, uid)
    rename("/output/unproxied.env.tmp", "/output/unproxied.env")
    with open("/output/docker.env.tmp", "w") as f:
        for key, value in for_docker_env.items():
            f.write("{}={}\n".format(key, value))
    chown("/output/docker.env.tmp", uid, uid)
    rename("/output/docker.env.tmp", "/output/docker.env")


# Traceback (NameError)
$ X="-it --rm leastauthority/subscription-converger /app/env/bin/python -c xxx"
$ docker run $X
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'xxx' is not defined
$ ../telepresence --deployment s4-infrastructure --docker-run $X
Starting proxy...
Starting local container...
ImportError: No module named site
Shutting proxy down...
$

# Fixed code
def write_env(remote_info, uid):
    for_local_env = get_env_variables(remote_info)
    with open("/output/unproxied.env.tmp", "w") as f:
        for key, value in for_local_env.items():
            f.write("{}={}\n".format(key, value))
    chown("/output/unproxied.env.tmp", uid, uid)
    rename("/output/unproxied.env.tmp", "/output/unproxied.env")
# Buggy code
def wait_for_pod(remote_info):
    for i in range(120):
        phase = str(
            check_output(
                [
                    "kubectl",
                    "get",
                    "pod",
                    remote_info.pod_name,
                    "-o",
                    "jsonpath={.status.phase}",
                ]
            ),
            "utf-8",
        ).strip()
        if phase == "Running":
            return
        time.sleep(1)
    raise RuntimeError("Pod isn't starting: {}".format(phase))


# Traceback (NameError)
$ X="-it --rm leastauthority/subscription-converger /app/env/bin/python -c xxx"
$ docker run $X
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'xxx' is not defined
$ ../telepresence --deployment s4-infrastructure --docker-run $X
Starting proxy...
Starting local container...
ImportError: No module named site
Shutting proxy down...
$

# Fixed code
def wait_for_pod(remote_info):
    for i in range(120):
        try:
            pod = loads(
                str(
                    check_output(
                        ["kubectl", "get", "pod", remote_info.pod_name, "-o", "json"]
                    ),
                    "utf-8",
                )
            )
        except CalledProcessError:
            time.sleep(1)
            continue
        if pod["status"]["phase"] == "Running":
            for container in pod["status"]["containerStatuses"]:
                if (
                    container["name"] == remote_info.container_name
                    and (container["ready"])
                ):
                    return
        time.sleep(1)
    raise RuntimeError("Pod isn't starting or can't be found: {}".format(pod["status"]))
# Buggy code
def connect(remote_info, local_exposed_ports, expose_host, custom_proxied_hosts):
    """
    Start all the processes that handle remote proxying.

    Return list of Popen instances.
    """
    processes = []
    # forward remote port to here, by tunneling via remote SSH server:
    processes.append(Popen(["kubectl", "port-forward", remote_info.pod_name, "22"]))
    wait_for_ssh()

    for port_number in local_exposed_ports:
        processes.append(
            ssh(["-R", "*:{}:{}:{}".format(port_number, expose_host, port_number)])
        )

    # start tunnel to remote SOCKS proxy, for telepresence --run:
    processes.append(ssh(["-L", "*:{}:127.0.0.1:{}".format(SOCKS_PORT, SOCKS_PORT)]))

    # start proxies for custom-mapped hosts:
    for host, port in [s.split(":", 1) for s in custom_proxied_hosts]:
        processes.append(ssh(["-L", "{}:{}:{}".format(port, host, port)]))

    # start proxies for Services:
    # XXX maybe just do everything via SSH, now that we have it?
    for port in range(2000, 2020):
        # XXX what if there is more than 20 services
        p = Popen(["kubectl", "port-forward", remote_info.pod_name, str(port)])
        processes.append(p)

    return processes


# Traceback (NameError)
$ X="-it --rm leastauthority/subscription-converger /app/env/bin/python -c xxx"
$ docker run $X
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'xxx' is not defined
$ ../telepresence --deployment s4-infrastructure --docker-run $X
Starting proxy...
Starting local container...
ImportError: No module named site
Shutting proxy down...
$

# Fixed code
def connect(
    remote_info,
    local_exposed_ports,
    expose_host,
):
    """
    Start all the processes that handle remote proxying.

    Return list of Popen instances.
    """
    processes = []
    # forward remote port to here, by tunneling via remote SSH server:
    processes.append(Popen(["kubectl", "port-forward", remote_info.pod_name, "22"]))
    wait_for_ssh()

    for port_number in local_exposed_ports:
        # XXX really only need to bind to external port...
        processes.append(
            ssh(["-R", "*:{}:{}:{}".format(port_number, expose_host, port_number)])
        )

    # start tunnel to remote SOCKS proxy, for telepresence --run.
    # XXX really only need to bind to external port...
    processes.append(ssh(["-L", "*:{}:127.0.0.1:{}".format(SOCKS_PORT, SOCKS_PORT)]))

    return processes
# Buggy code
def main(uid, deployment_name, local_exposed_ports, custom_proxied_hosts, expose_host):
    processes = []
    remote_info = get_remote_info(deployment_name)
    # Wait for pod to be running:
    wait_for_pod(remote_info)
    proxied_ports = set(range(2000, 2020)) | set(map(int, local_exposed_ports))
    proxied_ports.add(22)
    proxied_ports.add(SOCKS_PORT)
    custom_ports = [int(s.split(":", 1)[1]) for s in custom_proxied_hosts]
    for port in custom_ports:
        if port in proxied_ports:
            exit(
                (
                    "OOPS: Can't proxy port {} more than once. "
                    "Currently mapped ports: {}.This error is due "
                    "to a limitation in Telepresence, see "
                    "https://github.com/datawire/telepresence/issues/6"
                ).format(port, proxied_ports)
            )
        else:
            proxied_ports.add(int(port))

    # write /etc/hosts
    write_etc_hosts([s.split(":", 1)[0] for s in custom_proxied_hosts])

    processes = connect(
        remote_info, local_exposed_ports, expose_host, custom_proxied_hosts
    )

    # write docker envfile, which tells CLI we're ready:
    time.sleep(5)
    write_env(remote_info, uid)

    # Now, poll processes; if one dies kill them all and restart them:
    while True:
        for p in processes:
            code = p.poll()
            if code is not None:
                print("A subprocess died, killing all processes...")
                killall(processes)
                # Unfortunatly torsocks doesn't deal well with connections
                # being lost, so best we can do is shut down.
                raise SystemExit(3)


# Traceback (NameError)
$ X="-it --rm leastauthority/subscription-converger /app/env/bin/python -c xxx"
$ docker run $X
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'xxx' is not defined
$ ../telepresence --deployment s4-infrastructure --docker-run $X
Starting proxy...
Starting local container...
ImportError: No module named site
Shutting proxy down...
$

# Fixed code
def main(uid, deployment_name, local_exposed_ports, expose_host):
    remote_info = get_remote_info(deployment_name)

    processes = connect(
        remote_info,
        local_exposed_ports,
        expose_host,
    )

    # write docker envfile, which tells CLI we're ready:
    time.sleep(5)
    write_env(remote_info, uid)

    # Now, poll processes; if one dies kill them all and restart them:
    while True:
        for p in processes:
            code = p.poll()
            if code is not None:
                print("A subprocess died, killing all processes...")
                killall(processes)
                # Unfortunatly torsocks doesn't deal well with connections
                # being lost, so best we can do is shut down.
                raise SystemExit(3)
# Buggy code
def listen():
    reactor.listenTCP(9050, socks.SOCKSv5Factory())
    for i, key in enumerate(_get_service_keys(os.environ)):
        host = os.environ[key]
        port = int(os.environ[key[:-4] + "PORT"])
        service = endpoints.TCP4ServerEndpoint(reactor, 2000 + i)
        service.listen(ProxyFactory(host, port))
        print("Connecting port {} to {}:{} ({})".format(2000 + i, host, port, key))


# Traceback (NameError)
$ X="-it --rm leastauthority/subscription-converger /app/env/bin/python -c xxx"
$ docker run $X
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'xxx' is not defined
$ ../telepresence --deployment s4-infrastructure --docker-run $X
Starting proxy...
Starting local container...
ImportError: No module named site
Shutting proxy down...
$

# Fixed code
def listen():
    reactor.listenTCP(9050, socks.SOCKSv5Factory())
# Buggy code
def run_optimizer_step(
    self,
    optimizer: Optimizer,
    optimizer_idx: int,
    lambda_closure: Callable,
    **kwargs: Any,
) -> None:
    xm.optimizer_step(optimizer, optimizer_args={"closure": lambda_closure, **kwargs})


# Traceback (RuntimeError)
RuntimeError                              Traceback (most recent call last)
<ipython-input-17-587e2a9e3858> in <module>
----> 1 trainer.test(datamodule=dm)

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py in test(self, model, test_dataloaders, ckpt_path, verbose, datamodule)
922             results = self.__test_given_model(model, test_dataloaders)
923         else:
--> 924             results = self.__test_using_best_weights(ckpt_path, test_dataloaders)
925
926         self.teardown('test')

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py in __test_using_best_weights(self, ckpt_path, test_dataloaders)
950                 return {}
951             if not self._device_type == DeviceType.TPU:
--> 952                 self.accelerator.barrier()
953
954             ckpt = pl_load(ckpt_path, map_location=lambda storage, loc: storage)

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/accelerators/accelerator.py in barrier(self, name)
375
376     def barrier(self, name: Optional[str] = None) -> None:
--> 377         self.training_type_plugin.barrier(name=name)
378
379     def broadcast(self, obj: object, src: int = 0) -> object:

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/plugins/training_type/tpu_spawn.py in barrier(self, name)
113
114     def barrier(self, name: Optional[str] = None) -> None:
--> 115         rendezvous(f"pl.Trainer.{name}")
116
117     def transfer_distrib_spawn_state_on_fit_end(self, results):

/opt/conda/lib/python3.7/site-packages/torch_xla/core/xla_model.py in rendezvous(tag, payload, replicas)
859     ordinal `i` at position `i` in the returned tuple.
860   """
--> 861   return torch_xla._XLAC._xla_rendezvous(get_ordinal(), tag, payload, replicas)
862
863

RuntimeError: tensorflow/compiler/xla/xla_client/mesh_service.cc:316 : Check failed: impl_->channel->WaitForConnected( std::chrono::system_clock::now() + std::chrono::seconds(connect_wait_seconds))

# Fixed code
def run_optimizer_step(
    self, optimizer: Optimizer, optimizer_idx: int, lambda_closure: Callable, **kwargs
):
    xm.optimizer_step(
        optimizer, barrier=False, optimizer_args={"closure": lambda_closure, **kwargs}
    )