gurudesh/pytracebugs huggingface dataset:
# Buggy code
def input(prompt):
    """
    Version of input (raw_input in Python 2) which forces a flush of sys.stdout
    to avoid problems where the prompt fails to appear due to line buffering
    """
    sys.stdout.write(prompt)
    sys.stdout.flush()
    return sys.stdin.readline().rstrip(b"\n")


# Traceback (TypeError)
Traceback (most recent call last):
File "/opt/venvs/docker-compose/bin/docker-compose", line 9, in <module>
load_entry_point('docker-compose==1.8.0.dev0', 'console_scripts', 'docker-compose')()
File "/opt/venvs/docker-compose/lib64/python3.5/site-packages/compose/cli/main.py", line 58, in main
command()
File "/opt/venvs/docker-compose/lib64/python3.5/site-packages/compose/cli/main.py", line 109, in perform_command
handler(command, command_options)
File "/opt/venvs/docker-compose/lib64/python3.5/site-packages/compose/cli/main.py", line 553, in rm
or yesno("Are you sure? [yN] ", default=False):
File "/opt/venvs/docker-compose/lib64/python3.5/site-packages/compose/cli/utils.py", line 33, in yesno
answer = input(prompt).strip().lower()
File "/opt/venvs/docker-compose/lib64/python3.5/site-packages/compose/cli/utils.py", line 52, in input
return sys.stdin.readline().rstrip(b'\n')
TypeError: rstrip arg must be None or str

# Fixed code
def input(prompt):
    """
    Version of input (raw_input in Python 2) which forces a flush of sys.stdout
    to avoid problems where the prompt fails to appear due to line buffering
    """
    sys.stdout.write(prompt)
    sys.stdout.flush()
    return sys.stdin.readline().rstrip("\n")
# Buggy code
def line_splitter(buffer, separator="\n"):
    index = buffer.find(six.text_type(separator))
    if index == -1:
        return None, None
    return buffer[: index + 1], buffer[index + 1 :]


# Traceback (ValueError)
$ docker-compose -f pep-wilma.yml build pepwilma
Building pepwilma
Step 0 : FROM node:0.10-slim
---> 04e511e59c2e
[...]
Step 16 : ADD https://raw.githubusercontent.com/Bitergia/docker/master/utils/entrypoint-common.sh /
Traceback (most recent call last):
File "/usr/bin/docker-compose", line 9, in <module>
load_entry_point('docker-compose==1.5.1', 'console_scripts', 'docker-compose')()
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 54, in main
command.sys_dispatch()
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 23, in sys_dispatch
self.dispatch(sys.argv[1:], None)
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 26, in dispatch
self.perform_command(*self.parse(argv, global_options))
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 171, in perform_command
handler(project, command_options)
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 192, in build
force_rm=bool(options.get('--force-rm', False)))
File "/usr/lib/python2.7/dist-packages/compose/project.py", line 284, in build
service.build(no_cache, pull, force_rm)
File "/usr/lib/python2.7/dist-packages/compose/service.py", line 727, in build
all_events = stream_output(build_output, sys.stdout)
File "/usr/lib/python2.7/dist-packages/compose/progress_stream.py", line 15, in stream_output
for event in utils.json_stream(output):
File "/usr/lib/python2.7/dist-packages/compose/utils.py", line 131, in split_buffer
yield decoder(buffered)
File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 15 column 1 (char 4 - 544)

# Fixed code
def line_splitter(buffer, separator="\n"):
    index = buffer.find(six.text_type(separator))
    if index == -1:
        return None
    return buffer[: index + 1], buffer[index + 1 :]
# Buggy code
def json_splitter(buffer):
    """Attempt to parse a json object from a buffer. If there is at least one
    object, return it and the rest of the buffer, otherwise return None.
    """
    try:
        obj, index = json_decoder.raw_decode(buffer)
        rest = buffer[json.decoder.WHITESPACE.match(buffer, index).end() :]
        return obj, rest
    except ValueError:
        return None, None


# Traceback (ValueError)
$ docker-compose -f pep-wilma.yml build pepwilma
Building pepwilma
Step 0 : FROM node:0.10-slim
---> 04e511e59c2e
[...]
Step 16 : ADD https://raw.githubusercontent.com/Bitergia/docker/master/utils/entrypoint-common.sh /
Traceback (most recent call last):
File "/usr/bin/docker-compose", line 9, in <module>
load_entry_point('docker-compose==1.5.1', 'console_scripts', 'docker-compose')()
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 54, in main
command.sys_dispatch()
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 23, in sys_dispatch
self.dispatch(sys.argv[1:], None)
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 26, in dispatch
self.perform_command(*self.parse(argv, global_options))
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 171, in perform_command
handler(project, command_options)
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 192, in build
force_rm=bool(options.get('--force-rm', False)))
File "/usr/lib/python2.7/dist-packages/compose/project.py", line 284, in build
service.build(no_cache, pull, force_rm)
File "/usr/lib/python2.7/dist-packages/compose/service.py", line 727, in build
all_events = stream_output(build_output, sys.stdout)
File "/usr/lib/python2.7/dist-packages/compose/progress_stream.py", line 15, in stream_output
for event in utils.json_stream(output):
File "/usr/lib/python2.7/dist-packages/compose/utils.py", line 131, in split_buffer
yield decoder(buffered)
File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 15 column 1 (char 4 - 544)

# Fixed code
def json_splitter(buffer):
    """Attempt to parse a json object from a buffer. If there is at least one
    object, return it and the rest of the buffer, otherwise return None.
    """
    try:
        obj, index = json_decoder.raw_decode(buffer)
        rest = buffer[json.decoder.WHITESPACE.match(buffer, index).end() :]
        return obj, rest
    except ValueError:
        return None
# Buggy code
def json_stream(stream):
    """Given a stream of text, return a stream of json objects.
    This handles streams which are inconsistently buffered (some entries may
    be newline delimited, and others are not).
    """
    return split_buffer(stream_as_text(stream), json_splitter, json_decoder.decode)


# Traceback (ValueError)
$ docker-compose -f pep-wilma.yml build pepwilma
Building pepwilma
Step 0 : FROM node:0.10-slim
---> 04e511e59c2e
[...]
Step 16 : ADD https://raw.githubusercontent.com/Bitergia/docker/master/utils/entrypoint-common.sh /
Traceback (most recent call last):
File "/usr/bin/docker-compose", line 9, in <module>
load_entry_point('docker-compose==1.5.1', 'console_scripts', 'docker-compose')()
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 54, in main
command.sys_dispatch()
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 23, in sys_dispatch
self.dispatch(sys.argv[1:], None)
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 26, in dispatch
self.perform_command(*self.parse(argv, global_options))
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 171, in perform_command
handler(project, command_options)
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 192, in build
force_rm=bool(options.get('--force-rm', False)))
File "/usr/lib/python2.7/dist-packages/compose/project.py", line 284, in build
service.build(no_cache, pull, force_rm)
File "/usr/lib/python2.7/dist-packages/compose/service.py", line 727, in build
all_events = stream_output(build_output, sys.stdout)
File "/usr/lib/python2.7/dist-packages/compose/progress_stream.py", line 15, in stream_output
for event in utils.json_stream(output):
File "/usr/lib/python2.7/dist-packages/compose/utils.py", line 131, in split_buffer
yield decoder(buffered)
File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 15 column 1 (char 4 - 544)

# Fixed code
def json_stream(stream):
    """Given a stream of text, return a stream of json objects.
    This handles streams which are inconsistently buffered (some entries may
    be newline delimited, and others are not).
    """
    return split_buffer(stream, json_splitter, json_decoder.decode)
# Buggy code
def load_file(filename, config):
    processed_config = pre_process_config(config)
    validate_against_fields_schema(processed_config)
    return [
        build_service(filename, name, service_config)
        for name, service_config in processed_config.items()
    ]


# Traceback (TypeError)
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 53, in main
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 23, in sys_dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 26, in dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 163, in perform_command
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 54, in project_from_options
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 90, in get_project
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 215, in load
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 205, in merge_services
TypeError: 'NoneType' object is not iterable

# Fixed code
def load_file(filename, config):
    processed_config = interpolate_environment_variables(config)
    validate_against_fields_schema(processed_config)
    return [
        build_service(filename, name, service_config)
        for name, service_config in processed_config.items()
    ]
# Buggy code
def validate_and_construct_extends(self):
    extends = self.service_dict["extends"]
    if not isinstance(extends, dict):
        extends = {"service": extends}

    validate_extends_file_path(self.service_name, extends, self.filename)
    self.extended_config_path = self.get_extended_config_path(extends)
    self.extended_service_name = extends["service"]

    full_extended_config = pre_process_config(load_yaml(self.extended_config_path))

    validate_extended_service_exists(
        self.extended_service_name, full_extended_config, self.extended_config_path
    )
    validate_against_fields_schema(full_extended_config)

    self.extended_config = full_extended_config[self.extended_service_name]


# Traceback (TypeError)
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 53, in main
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 23, in sys_dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 26, in dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 163, in perform_command
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 54, in project_from_options
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 90, in get_project
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 215, in load
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 205, in merge_services
TypeError: 'NoneType' object is not iterable

# Fixed code
def validate_and_construct_extends(self):
    extends = self.service_dict["extends"]
    if not isinstance(extends, dict):
        extends = {"service": extends}

    validate_extends_file_path(self.service_name, extends, self.filename)
    self.extended_config_path = self.get_extended_config_path(extends)
    self.extended_service_name = extends["service"]

    config = load_yaml(self.extended_config_path)
    validate_top_level_object(config)
    full_extended_config = interpolate_environment_variables(config)

    validate_extended_service_exists(
        self.extended_service_name, full_extended_config, self.extended_config_path
    )
    validate_against_fields_schema(full_extended_config)

    self.extended_config = full_extended_config[self.extended_service_name]
# Buggy code
def validate_service_names(func):
    @wraps(func)
    def func_wrapper(config):
        for service_name in config.keys():
            if type(service_name) is int:
                raise ConfigurationError(
                    "Service name: {} needs to be a string, eg '{}'".format(
                        service_name, service_name
                    )
                )
        return func(config)

    return func_wrapper


# Traceback (TypeError)
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 53, in main
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 23, in sys_dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 26, in dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 163, in perform_command
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 54, in project_from_options
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 90, in get_project
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 215, in load
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 205, in merge_services
TypeError: 'NoneType' object is not iterable

# Fixed code
def validate_service_names(config):
    for service_name in config.keys():
        if not isinstance(service_name, six.string_types):
            raise ConfigurationError(
                "Service name: {} needs to be a string, eg '{}'".format(
                    service_name, service_name
                )
            )
# Buggy code
def validate_top_level_object(func):
    @wraps(func)
    def func_wrapper(config):
        if not isinstance(config, dict):
            raise ConfigurationError(
                "Top level object needs to be a dictionary. Check your .yml file that you have defined a service at the top level."
            )
        return func(config)

    return func_wrapper


# Traceback (TypeError)
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 53, in main
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 23, in sys_dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 26, in dispatch
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 163, in perform_command
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 54, in project_from_options
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 90, in get_project
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 215, in load
File "/compose/build/docker-compose/out00-PYZ.pyz/compose.config.config", line 205, in merge_services
TypeError: 'NoneType' object is not iterable

# Fixed code
def validate_top_level_object(config):
    if not isinstance(config, dict):
        raise ConfigurationError(
            "Top level object needs to be a dictionary. Check your .yml file "
            "that you have defined a service at the top level."
        )
    validate_service_names(config)
# Buggy code
def friendly_error_message():
    try:
        yield
    except SSLError as e:
        raise errors.UserError("SSL error: %s" % e)
    except ConnectionError:
        if call_silently(["which", "docker"]) != 0:
            if is_mac():
                raise errors.DockerNotFoundMac()
            elif is_ubuntu():
                raise errors.DockerNotFoundUbuntu()
            else:
                raise errors.DockerNotFoundGeneric()
        elif call_silently(["which", "boot2docker"]) == 0:
            raise errors.ConnectionErrorDockerMachine()
        else:
            raise errors.ConnectionErrorGeneric(self.get_client().base_url)


# Traceback (AttributeError)
Traceback (most recent call last):
File "C:\Python27\Scripts\docker-compose-script.py", line 9, in <module>
load_entry_point('docker-compose==1.5.0.dev0', 'console_scripts', 'docker-compose')()
File "C:\Python27\lib\site-packages\compose\cli\main.py", line 51, in main
command.sys_dispatch()
File "C:\Python27\lib\site-packages\compose\cli\docopt_command.py", line 23, in sys_dispatch
self.dispatch(sys.argv[1:], None)
File "C:\Python27\lib\site-packages\compose\cli\command.py", line 46, in dispatch
raise errors.ConnectionErrorGeneric(self.get_client().base_url)
AttributeError: 'TopLevelCommand' object has no attribute 'get_client'

# Fixed code
def friendly_error_message():
    try:
        yield
    except SSLError as e:
        raise errors.UserError("SSL error: %s" % e)
    except ConnectionError:
        if call_silently(["which", "docker"]) != 0:
            if is_mac():
                raise errors.DockerNotFoundMac()
            elif is_ubuntu():
                raise errors.DockerNotFoundUbuntu()
            else:
                raise errors.DockerNotFoundGeneric()
        elif call_silently(["which", "docker-machine"]) == 0:
            raise errors.ConnectionErrorDockerMachine()
        else:
            raise errors.ConnectionErrorGeneric(get_client().base_url)
# Buggy code
def loop(self):
    self._init_readers()

    while self._num_running > 0:
        try:
            item, exception = self.queue.get(timeout=0.1)

            if exception:
                raise exception

            if item is STOP:
                self._num_running -= 1
            else:
                yield item
        except Empty:
            pass


# Traceback (thread.error)
^CGracefully stopping... (press Ctrl+C again to force)
Stopping dailylead_db_1...
Stopping dailylead_queue_1...
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.main", line 39, in main
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.docopt_command", line 21, in sys_dispatch
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.command", line 30, in dispatch
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.docopt_command", line 24, in dispatch
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.command", line 47, in perform_command
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.docopt_command", line 27, in perform_command
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.main", line 316, in up
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.log_printer", line 20, in run
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/fig.cli.multiplexer", line 20, in loop
File "/Users/ben/fig/build/fig/out00-PYZ.pyz/Queue", line 182, in get
thread.error: release unlocked lock

# Fixed code
def loop(self):
    self._init_readers()

    while self._num_running > 0:
        try:
            item, exception = self.queue.get(timeout=0.1)

            if exception:
                raise exception

            if item is STOP:
                self._num_running -= 1
            else:
                yield item
        except Empty:
            pass
        # See https://github.com/docker/compose/issues/189
        except thread.error:
            raise KeyboardInterrupt()
# Buggy code
def pull(self, insecure_registry=False):
    if "image" in self.options:
        image_name = self._get_image_name(self.options["image"])
        log.info("Pulling %s (%s)..." % (self.name, image_name))
        self.client.pull(image_name, insecure_registry=insecure_registry)


# Traceback (compose.progress_stream.StreamOutputError)
Pulling image 50ff1e85c429:latest...
Pulling repository 50ff1e85c429
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 31, in main
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 21, in sys_dispatch
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 27, in dispatch
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 24, in dispatch
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 59, in perform_command
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 445, in up
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.project", line 183, in up
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.service", line 258, in recreate_containers
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.service", line 243, in create_container
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.progress_stream", line 37, in stream_output
File "/Users/ben/fig/build/docker-compose/out00-PYZ.pyz/compose.progress_stream", line 50, in print_output_event
compose.progress_stream.StreamOutputError: Error: image library/50ff1e85c429:latest not found

# Fixed code
def pull(self, insecure_registry=False):
    if "image" not in self.options:
        return

    repo, tag = parse_repository_tag(self.options["image"])
    tag = tag or "latest"
    log.info("Pulling %s (%s:%s)..." % (self.name, repo, tag))
    output = self.client.pull(
        repo, tag=tag, stream=True, insecure_registry=insecure_registry
    )
    stream_output(output, sys.stdout)
# Buggy code
def title(self):
    return URIRef(self + "title")


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def title(self):
    # overrides unicode.title to allow DCTERMS.title for example
    return URIRef(self + "title")
# Buggy code
def term(self, name):
    uri = self.__uris.get(name)
    if uri is None:
        raise Exception("term '%s' not in namespace '%s'" % (name, self.uri))
    else:
        return uri


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def term(self, name):
    uri = self.__uris.get(name)
    if uri is None:
        raise Exception("term '%s' not in namespace '%s'" % (name, self))
    else:
        return uri
# Buggy code
def __repr__(self):
    return """rdf.namespace.ClosedNamespace('%s')""" % str(self.uri)


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def __repr__(self):
    return """rdf.namespace.ClosedNamespace('%s')""" % self
# Buggy code
def term(self, name):
    try:
        i = int(name)
        return URIRef("%s_%s" % (self.uri, i))
    except ValueError:
        return super(_RDFNamespace, self).term(name)


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def term(self, name):
    try:
        i = int(name)
        return URIRef("%s_%s" % (self, i))
    except ValueError:
        return super(_RDFNamespace, self).term(name)
# Buggy code
def __new__(cls, value):
    try:
        rt = unicode.__new__(cls, value)
    except UnicodeDecodeError:
        rt = unicode.__new__(cls, value, "utf-8")
    return rt


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def __new__(cls):
    return ClosedNamespace.__new__(
        cls,
        "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        terms=[
            # Syntax Names
            "RDF",
            "Description",
            "ID",
            "about",
            "parseType",
            "resource",
            "li",
            "nodeID",
            "datatype",
            # RDF Classes
            "Seq",
            "Bag",
            "Alt",
            "Statement",
            "Property",
            "List",
            "PlainLiteral",
            # RDF Properties
            "subject",
            "predicate",
            "object",
            "type",
            "value",
            "first",
            "rest",
            # and _n where n is a non-negative integer
            # RDF Resources
            "nil",
            # Added in RDF 1.1
            "XMLLiteral",
            "HTML",
            "langString",
        ],
    )
# Buggy code
def __init__(self, system_id=None):
    xmlreader.InputSource.__init__(self, system_id=system_id)
    self.content_type = None


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def __init__(self, system_id=None):
    xmlreader.InputSource.__init__(self, system_id=system_id)
    self.content_type = None
    self.auto_close = False  # see Graph.parse(), true if opened by us
# Buggy code
def __init__(self, name):
    """
    @param name: URL to be opened
    @keyword additional_headers: additional HTTP request headers to be added to the call
    """
    try:
        # Note the removal of the fragment ID. This is necessary, per the HTTP spec
        req = Request(url=name.split("#")[0])

        req.add_header("Accept", "text/html, application/xhtml+xml")

        self.data = urlopen(req)
        self.headers = self.data.info()

        if URIOpener.CONTENT_LOCATION in self.headers:
            self.location = urlparse.urljoin(
                self.data.geturl(), self.headers[URIOpener.CONTENT_LOCATION]
            )
        else:
            self.location = name

    except urllib_HTTPError:
        e = sys.exc_info()[1]
        from pyMicrodata import HTTPError

        msg = BaseHTTPRequestHandler.responses[e.code]
        raise HTTPError("%s" % msg[1], e.code)
    except Exception:
        e = sys.exc_info()[1]
        from pyMicrodata import MicrodataError

        raise MicrodataError("%s" % e)


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def __init__(self, name):
    """
    @param name: URL to be opened
    @keyword additional_headers: additional HTTP request headers to be added to the call
    """
    try:
        # Note the removal of the fragment ID. This is necessary, per the HTTP spec
        req = Request(url=name.split("#")[0])

        req.add_header("Accept", "text/html, application/xhtml+xml")

        self.data = urlopen(req)
        self.headers = self.data.info()

        if URIOpener.CONTENT_LOCATION in self.headers:
            self.location = urljoin(
                self.data.geturl(), self.headers[URIOpener.CONTENT_LOCATION]
            )
        else:
            self.location = name

    except urllib_HTTPError:
        e = sys.exc_info()[1]
        from pyMicrodata import HTTPError

        msg = BaseHTTPRequestHandler.responses[e.code]
        raise HTTPError("%s" % msg[1], e.code)
    except Exception:
        e = sys.exc_info()[1]
        from pyMicrodata import MicrodataError

        raise MicrodataError("%s" % e)
# Buggy code
def _get_literal(Pnode):
    """
    Get (recursively) the full text from a DOM Node.

    @param Pnode: DOM Node
    @return: string
    """
    rc = ""
    for node in Pnode.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
        elif node.nodeType == node.ELEMENT_NODE:
            rc = rc + self._get_literal(node)
    if state.options.space_preserve:
        return rc
    else:
        return re.sub(r"(\r| |\n|\t)+", " ", rc).strip()


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def _get_literal(Pnode):
    """
    Get (recursively) the full text from a DOM Node.

    @param Pnode: DOM Node
    @return: string
    """
    rc = ""
    for node in Pnode.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
        elif node.nodeType == node.ELEMENT_NODE:
            rc = rc + _get_literal(node)
    if state.options.space_preserve:
        return rc
    else:
        return re.sub(r"(\r| |\n|\t)+", " ", rc).strip()
# Buggy code
def absolutize(self, iri):
    """
    Apply BASE / PREFIXes to URIs
    (and to datatypes in Literals)

    TODO: Move resolving URIs to pre-processing
    """

    if isinstance(iri, CompValue):
        if iri.name == "pname":
            return self.resolvePName(iri.prefix, iri.localname)
        if iri.name == "literal":
            return Literal(
                iri.string, lang=iri.lang, datatype=self.absolutize(iri.datatype)
            )
    elif isinstance(iri, URIRef) and not ":" in iri:  # TODO: Check for relative URI?
        return URIRef(self.base + iri)
    return iri


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def absolutize(self, iri):
    """
    Apply BASE / PREFIXes to URIs
    (and to datatypes in Literals)

    TODO: Move resolving URIs to pre-processing
    """

    if isinstance(iri, CompValue):
        if iri.name == "pname":
            return self.resolvePName(iri.prefix, iri.localname)
        if iri.name == "literal":
            return Literal(
                iri.string, lang=iri.lang, datatype=self.absolutize(iri.datatype)
            )
    elif isinstance(iri, URIRef) and not ":" in iri:
        return URIRef(iri, base=self.base)

    return iri
# Buggy code
def __getitem__(self, item):
    if isinstance(item, slice):
        if item.step:
            raise TypeError(
                "Resources fix the subject for slicing, and can only be sliced by predicate/object. "
            )
        p, o = item.start, item.stop
        if p is None and o is None:
            return self.predicate_objects()
        elif p is None:
            return self.predicates(o)
        elif o is None:
            return self.objects(p)
        else:
            return (self.identifier, p, o) in self._graph
    elif isinstance(item, (Node, Path)):
        return self.objects(item)
    else:
        raise TypeError(
            "You can only index a resource by a single rdflib term, a slice of rdflib terms, not %s (%s)"
            % (item, type(item))
        )


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def __getitem__(self, item):
    if isinstance(item, slice):
        if item.step:
            raise TypeError(
                "Resources fix the subject for slicing, and can only be sliced by predicate/object. "
            )
        p, o = item.start, item.stop
        if isinstance(p, Resource):
            p = p._identifier
        if isinstance(o, Resource):
            o = o._identifier
        if p is None and o is None:
            return self.predicate_objects()
        elif p is None:
            return self.predicates(o)
        elif o is None:
            return self.objects(p)
        else:
            return (self.identifier, p, o) in self._graph
    elif isinstance(item, (Node, Path)):
        return self.objects(item)
    else:
        raise TypeError(
            "You can only index a resource by a single rdflib term, a slice of rdflib terms, not %s (%s)"
            % (item, type(item))
        )
# Buggy code
def setup_python3():
    # Taken from "distribute" setup.py
    from distutils.filelist import FileList
    from distutils import dir_util, file_util, util, log
    from os.path import join, exists

    tmp_src = join("build", "src")
    # Not covered by "setup.py clean --all", so explicit deletion required.
    if exists(tmp_src):
        dir_util.remove_tree(tmp_src)
    log.set_verbosity(1)
    fl = FileList()
    for line in open("MANIFEST.in"):
        if not line.strip():
            continue
        fl.process_template_line(line)
    dir_util.create_tree(tmp_src, fl.files)
    outfiles_2to3 = []
    for f in fl.files:
        outf, copied = file_util.copy_file(f, join(tmp_src, f), update=1)
        if copied and outf.endswith(".py"):
            outfiles_2to3.append(outf)

    util.run_2to3(outfiles_2to3)

    # arrange setup to use the copy
    sys.path.insert(0, tmp_src)

    return tmp_src


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def setup_python3():
    # Taken from "distribute" setup.py
    from distutils.filelist import FileList
    from distutils import dir_util, file_util, util, log
    from os.path import join, exists

    tmp_src = join("build", "src")
    # Not covered by "setup.py clean --all", so explicit deletion required.
    if exists(tmp_src):
        dir_util.remove_tree(tmp_src)
    # log.set_verbosity(1)
    fl = FileList()
    for line in open("MANIFEST.in"):
        if not line.strip():
            continue
        fl.process_template_line(line)
    dir_util.create_tree(tmp_src, fl.files)
    outfiles_2to3 = []
    for f in fl.files:
        outf, copied = file_util.copy_file(f, join(tmp_src, f), update=1)
        if copied and outf.endswith(".py"):
            outfiles_2to3.append(outf)

    util.run_2to3(outfiles_2to3)

    # arrange setup to use the copy
    sys.path.insert(0, tmp_src)

    return tmp_src
# Buggy code
def compute_qname(self, uri, generate=True):
    if not uri in self.__cache:
        namespace, name = split_uri(uri)
        namespace = URIRef(namespace)
        prefix = self.store.prefix(namespace)
        if prefix is None:
            if not generate:
                raise Exception("No known prefix for %s and generate=False")
            num = 1
            while 1:
                prefix = "ns%s" % num
                if not self.store.namespace(prefix):
                    break
                num += 1
            self.bind(prefix, namespace)
        self.__cache[uri] = (prefix, namespace, name)
    return self.__cache[uri]


# Traceback (Exception)
Traceback (most recent call last):
File "<string>", line 4, in <module>
...
File ".../rdflib/term.py", line 208, in __new__
raise Exception('%s does not look like a valid URI, perhaps you want to urlencode it?'%value)
Exception: file:///private/tmp/<stdin> does not look like a valid URI, perhaps you want to urlencode it?

# Fixed code
def compute_qname(self, uri, generate=True):
    if not _is_valid_uri(uri):
        raise Exception(
            '"%s" does not look like a valid URI, I cannot serialize this. Perhaps you wanted to urlencode it?'
            % uri
        )

    if not uri in self.__cache:
        namespace, name = split_uri(uri)
        namespace = URIRef(namespace)
        prefix = self.store.prefix(namespace)
        if prefix is None:
            if not generate:
                raise Exception("No known prefix for %s and generate=False")
            num = 1
            while 1:
                prefix = "ns%s" % num
                if not self.store.namespace(prefix):
                    break
                num += 1
            self.bind(prefix, namespace)
        self.__cache[uri] = (prefix, namespace, name)
    return self.__cache[uri]
# Buggy code
async def checkData(tweet, location, config, conn):
    usernames = []
    user_ids = set()
    global _duplicate_dict
    copyright = tweet.find("div", "StreamItemContent--withheld")
    if copyright is None and is_tweet(tweet):
        tweet = Tweet(tweet, location, config)

    if datecheck(tweet.datestamp, config):
        output = format.Tweet(config, tweet)

        if config.Database:
            db.tweets(conn, tweet, config)

        if config.Pandas:
            panda.update(tweet, config)

        if config.Store_object:
            tweets_object.append(tweet)

        if config.Elasticsearch:
            elasticsearch.Tweet(tweet, config)

        _output(tweet, output, config)


# Traceback (TypeError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/dist-packages/twint-1.2.1-py3.6.egg/twint/run.py", line 215, in Search
File "/usr/local/lib/python3.6/dist-packages/twint-1.2.1-py3.6.egg/twint/run.py", line 167, in run
File "/usr/lib/python3.6/asyncio/base_events.py", line 473, in run_until_complete
return future.result()
File "/usr/local/lib/python3.6/dist-packages/twint-1.2.1-py3.6.egg/twint/run.py", line 154, in main
File "/usr/local/lib/python3.6/dist-packages/twint-1.2.1-py3.6.egg/twint/run.py", line 117, in tweets
File "/usr/local/lib/python3.6/dist-packages/twint-1.2.1-py3.6.egg/twint/output.py", line 118, in Tweets
File "/usr/local/lib/python3.6/dist-packages/twint-1.2.1-py3.6.egg/twint/output.py", line 96, in checkData
File "/usr/local/lib/python3.6/dist-packages/twint-1.2.1-py3.6.egg/twint/storage/db.py", line 241, in tweets
TypeError: can only join an iterable

# Fixed code
async def checkData(tweet, location, config, conn):
    usernames = []
    copyright = tweet.find("div", "StreamItemContent--withheld")
    if copyright is None and is_tweet(tweet):
        tweet = Tweet(tweet, location, config)

        if not tweet.datestamp:
            print("[x] Hidden tweet found, account suspended due to violation of TOS")
            return

        if datecheck(tweet.datestamp, config):
            output = format.Tweet(config, tweet)

            if config.Database:
                db.tweets(conn, tweet, config)

            if config.Pandas:
                panda.update(tweet, config)

            if config.Store_object:
                tweets_object.append(tweet)

            if config.Elasticsearch:
                elasticsearch.Tweet(tweet, config)

            _output(tweet, output, config)
# Buggy code
def update(Tweet, session):
    day = weekday(strftime("%A", localtime(Tweet.datetime)))
    dt = "{} {}".format(Tweet.datestamp, Tweet.timestamp)

    _data = {
        "id": Tweet.id,
        "date": dt,
        "timezone": Tweet.timezone,
        "location": Tweet.location,
        "tweet": Tweet.tweet,
        "hashtags": Tweet.hashtags,
        "user_id": Tweet.user_id,
        "username": Tweet.username,
        "day": day,
        "hour": hour(Tweet.datetime),
        "link": Tweet.link,
        "retweet": Tweet.retweet,
        "user_rt": Tweet.user_rt,
        "essid": str(session),
    }
    _blocks.append(_data)


# Traceback (UnicodeEncodeError)
Traceback (most recent call last):
File "/path/to/my/script.py", line 21, in <module>
twint.run.Search(c)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/run.py", line 25, in Search
run(search.Search(config).main())
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/run.py", line 5, in run
get_event_loop().run_until_complete(x)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
return future.result()
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/search.py", line 49, in main
await self.tweets()
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/search.py", line 37, in tweets
await output.Tweets(tweet, "", self.config, self.conn)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/output.py", line 59, in Tweets
_output(tweet, output, config)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/output.py", line 45, in _output
print(output)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 389-389: Non-BMP character not supported in Tk

# Fixed code
def update(Tweet, session):
    day = weekday(strftime("%A", localtime(Tweet.datetime)))
    dt = "{} {}".format(Tweet.datestamp, Tweet.timestamp)

    _data = {
        "id": Tweet.id,
        "date": dt,
        "timezone": Tweet.timezone,
        "location": Tweet.location,
        "tweet": Tweet.tweet,
        "hashtags": Tweet.hashtags,
        "user_id": Tweet.user_id,
        "username": Tweet.username,
        "day": day,
        "hour": hour(Tweet.datetime),
        "link": Tweet.link,
        "retweet": Tweet.retweet,
        "user_rt": Tweet.user_rt,
        "essid": str(session),
        "mentions": Tweet.mentions,
    }
    _blocks.append(_data)
# Buggy code
def _output(obj, output, config):
    if config.Output != None:
        if config.Store_csv:
            write.Csv(obj, config)
        elif config.Store_json:
            write.Json(obj, config)
        else:
            write.Text(output, config.Output)

    if config.Pandas:
        Pandas.update(obj, config.Essid)

    if config.Elasticsearch:
        if config.Store_object:
            tweets_object.append(obj)
        else:
            print(output, end=".", flush=True)
    else:
        if config.Store_object:
            tweets_object.append(obj)
        else:
            print(output)


# Traceback (UnicodeEncodeError)
Traceback (most recent call last):
File "/path/to/my/script.py", line 21, in <module>
twint.run.Search(c)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/run.py", line 25, in Search
run(search.Search(config).main())
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/run.py", line 5, in run
get_event_loop().run_until_complete(x)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
return future.result()
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/search.py", line 49, in main
await self.tweets()
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/search.py", line 37, in tweets
await output.Tweets(tweet, "", self.config, self.conn)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/output.py", line 59, in Tweets
_output(tweet, output, config)
File "/anaconda3/envs/twitter_scrape/lib/python3.6/site-packages/twint/output.py", line 45, in _output
print(output)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 389-389: Non-BMP character not supported in Tk

# Fixed code
def _output(obj, output, config):
    if config.Output != None:
        if config.Store_csv:
            write.Csv(obj, config)
        elif config.Store_json:
            write.Json(obj, config)
        else:
            write.Text(output, config.Output)

    if config.Pandas:
        Pandas.update(obj, config.Essid)

    if config.Elasticsearch:
        if config.Store_object:
            tweets_object.append(obj)
        else:
            print(output, end=".", flush=True)
    else:
        if config.Store_object:
            tweets_object.append(obj)
        else:
            try:
                print(output)
            except UnicodeEncodeError:
                pass
# Buggy code
def __str__(self):
    cls = self.obj.__class__
    schema_path = ["{}.{}".format(cls.__module__, cls.__name__)]
    schema_path.extend(self.schema_path)
    schema_path = "->".join(
        val
        for val in schema_path[:-1]
        if val not in ("properties", "additionalProperties", "patternProperties")
    )
    return """Invalid specification

        {}, validating {!r}

        {}
        """.format(schema_path, self.validator, self.message)


# Traceback (altair.utils.schemapi.SchemaValidationError)
$ cat test.py
import pandas, altair

d = [{'WijkenEnBuurten': 'Nederland', 'Codering_3': 'NL00', 'Mannen_6': 8475102, 'Vrouwen_7': 8606405, 'SoortRegio_2': 'Land      ', 'GeboorteRelatief_25': 9}, {'WijkenEnBuurten': 'Aa en Hunze', 'Codering_3': 'GM1680', 'Mannen_6': 12603, 'Vrouwen_7': 12683, 'SoortRegio_2': 'Gemeente  ', 'GeboorteRelatief_25': 6}]

df = pandas.DataFrame(d)
a = altair.Data(df)

$ python ./test.py
Traceback (most recent call last):
File "./t_altair_schema.py", line 5, in <module>
a = altair.Data(df)
File "/home/paulm/.local/lib/python3.8/site-packages/altair/vegalite/v3/schema/core.py", line 3741, in __init__
super(Data, self).__init__(*args, **kwds)
File "/home/paulm/.local/lib/python3.8/site-packages/altair/utils/schemapi.py", line 154, in __init__
self.to_dict(validate=True)
File "/home/paulm/.local/lib/python3.8/site-packages/altair/utils/schemapi.py", line 302, in to_dict
raise SchemaValidationError(self, err)
altair.utils.schemapi.SchemaValidationError: <exception str() failed>

# Fixed code
def __str__(self):
    cls = self.obj.__class__
    schema_path = ["{}.{}".format(cls.__module__, cls.__name__)]
    schema_path.extend(self.schema_path)
    schema_path = "->".join(
        str(val)
        for val in schema_path[:-1]
        if val not in ("properties", "additionalProperties", "patternProperties")
    )
    return """Invalid specification

        {}, validating {!r}

        {}
        """.format(schema_path, self.validator, self.message)