gurudesh/pytracebugs huggingface dataset:
# Buggy code
def add_directory(self, path):
    """Add ``*.py`` files under the directory ``path`` to the archive."""
    for root, dirs, files in os.walk(path):
        arc_prefix = os.path.relpath(root, os.path.dirname(path))
        for f in files:
            if not f.endswith(".py"):
                continue
            f_path = os.path.join(root, f)
            dest_path = os.path.join(arc_prefix, f)
            self.add_file(f_path, dest_path)


# Traceback (AttributeError)
$ custodian
Traceback (most recent call last):
File "/Users/kapil/projects/cloud-custodian/bin/custodian", line 11, in <module>
load_entry_point('c7n', 'console_scripts', 'custodian')()
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 335, in main
_setup_logger(options)
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 301, in _setup_logger
level = 3 + (options.verbose or 0) - (options.quiet or 0)
AttributeError: 'Namespace' object has no attribute 'verbose'

# Fixed code
def add_directory(self, path):
    """Add ``*.py`` files under the directory ``path`` to the archive."""
    for root, dirs, files in os.walk(path):
        arc_prefix = os.path.relpath(root, os.path.dirname(path))
        for f in files:
            if f.endswith(".pyc") or f.endswith(".c"):
                continue
            f_path = os.path.join(root, f)
            dest_path = os.path.join(arc_prefix, f)
            self.add_file(f_path, dest_path)
# Buggy code
def __init__(self, ctx):
    super(DirectoryOutput, self).__init__(ctx)
    if self.root_dir.startswith("file://"):
        self.root_dir = self.root_dir[len("file://") :]
    if self.ctx.output_path is not None:
        if not os.path.exists(self.ctx.output_path):
            os.makedirs(self.ctx.output_path)


# Traceback (AttributeError)
$ custodian
Traceback (most recent call last):
File "/Users/kapil/projects/cloud-custodian/bin/custodian", line 11, in <module>
load_entry_point('c7n', 'console_scripts', 'custodian')()
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 335, in main
_setup_logger(options)
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 301, in _setup_logger
level = 3 + (options.verbose or 0) - (options.quiet or 0)
AttributeError: 'Namespace' object has no attribute 'verbose'

# Fixed code
def __init__(self, ctx):
    super(DirectoryOutput, self).__init__(ctx)
    if self.root_dir.startswith("file://"):
        self.root_dir = self.root_dir[len("file://") :]
    if self.ctx.output_path is not None:
        if not os.path.exists(self.root_dir):
            os.makedirs(self.root_dir)
# Buggy code
def get_archive(config):
    archive = PythonPackageArchive(
        "c7n_mailer",
        "ldap3",
        "pyasn1",
        "jinja2",
        "markupsafe",
        "ruamel",
        "redis",
        "datadog",
        "slackclient",
        "requests",
    )

    template_dir = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "..", "msg-templates")
    )

    for t in os.listdir(template_dir):
        with open(os.path.join(template_dir, t)) as fh:
            archive.add_contents("msg-templates/%s" % t, fh.read())

    archive.add_contents("config.json", json.dumps(config))
    archive.add_contents("periodic.py", entry_source)

    archive.close()
    return archive


# Traceback (AttributeError)
$ custodian
Traceback (most recent call last):
File "/Users/kapil/projects/cloud-custodian/bin/custodian", line 11, in <module>
load_entry_point('c7n', 'console_scripts', 'custodian')()
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 335, in main
_setup_logger(options)
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 301, in _setup_logger
level = 3 + (options.verbose or 0) - (options.quiet or 0)
AttributeError: 'Namespace' object has no attribute 'verbose'

# Fixed code
def get_archive(config):
    archive = PythonPackageArchive(
        "c7n_mailer",
        # core deps
        "jinja2",
        "markupsafe",
        "ruamel",
        "ldap3",
        "pyasn1",
        "redis",
        # transport datadog - recursive deps
        "datadog",
        "simplejson",
        "decorator",
        # transport slack - recursive deps
        "slackclient",
        "websocket",
        # requests (recursive deps), needed by datadog and slackclient
        "requests",
        "urllib3",
        "idna",
        "chardet",
        "certifi",
    )

    template_dir = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "..", "msg-templates")
    )

    for t in os.listdir(template_dir):
        with open(os.path.join(template_dir, t)) as fh:
            archive.add_contents("msg-templates/%s" % t, fh.read())

    archive.add_contents("config.json", json.dumps(config))
    archive.add_contents("periodic.py", entry_source)

    archive.close()
    return archive
# Buggy code
def account_tags(account):
    tags = {"AccountName": account["name"], "AccountId": account["account_id"]}
    for t in account.get("tags"):
        if not ":" in t:
            continue
        k, v = t.split(":", 1)
        k = "Account%s" % k.capitalize()
        tags[k] = v
    return tags


# Traceback (TypeError)
c7n-org run --debug -c config/accounts.yml  -a tscloud -u policies/tscloud/tag-compliance.yml  -s /tmp/c7n --region us-west-2
Traceback (most recent call last):
File "/home/cbouscal/.local/bin/c7n-org", line 11, in <module>
sys.exit(cli())
File "/usr/lib/python2.7/site-packages/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/usr/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/lib/python2.7/site-packages/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/home/cbouscal/.local/lib/python2.7/site-packages/c7n_org/cli.py", line 387, in run
debug)] = (a, r)
File "/usr/lib/python2.7/site-packages/c7n/executor.py", line 63, in submit
return MainThreadFuture(func(*args, **kw))
File "/home/cbouscal/.local/lib/python2.7/site-packages/c7n_org/cli.py", line 334, in run_account
with environ(**account_tags(account)):
File "/home/cbouscal/.local/lib/python2.7/site-packages/c7n_org/utils.py", line 6, in account_tags
for t in account.get('tags'):
TypeError: 'NoneType' object is not iterable

# Fixed code
def account_tags(account):
    tags = {"AccountName": account["name"], "AccountId": account["account_id"]}
    for t in account.get("tags", ()):
        if ":" not in t:
            continue
        k, v = t.split(":", 1)
        k = "Account%s" % k.capitalize()
        tags[k] = v
    return tags
# Buggy code
def camelResource(obj):
    """Some sources from apis return lowerCased where as describe calls

    always return TitleCase, this function turns the former to the later
    """
    if not isinstance(obj, dict):
        return obj
    for k in list(obj.keys()):
        v = obj.pop(k)
        obj["%s%s" % (k[0].upper(), k[1:])] = v
        if isinstance(v, dict):
            camelResource(v)
        elif isinstance(v, list):
            map(camelResource, v)
    return obj


# Traceback (TypeError)
argument of type 'NoneType' is not iterable: TypeError
Traceback (most recent call last):
File "/var/task/custodian_policy.py", line 4, in run
return handler.dispatch_event(event, context)
File "/var/task/c7n/handler.py", line 86, in dispatch_event
p.push(event, context)
File "/var/task/c7n/policy.py", line 579, in push
return mode.run(event, lambda_ctx)
File "/var/task/c7n/policy.py", line 489, in run
resources = super(ConfigRuleMode, self).run(event, lambda_context)
File "/var/task/c7n/policy.py", line 387, in run
resources, event)
File "/var/task/c7n/manager.py", line 86, in filter_resources
resources = f.process(resources, event)
File "/var/task/c7n/resources/vpc.py", line 692, in process
return super(SGPermission, self).process(resources, event)
File "/var/task/c7n/filters/core.py", line 161, in process
return filter(self, resources)
File "/var/task/c7n/resources/vpc.py", line 775, in __call__
cidr_found = self.process_cidrs(perm)
File "/var/task/c7n/resources/vpc.py", line 718, in process_cidrs
found = vf(ip_range)
File "/var/task/c7n/filters/core.py", line 330, in __call__
matched = self.match(i)
File "/var/task/c7n/filters/core.py", line 386, in match
v, r = self.process_value_type(self.v, r)
File "/var/task/c7n/filters/core.py", line 445, in process_value_type
v = parse_cidr(value)
File "/var/task/c7n/utils.py", line 325, in parse_cidr
if '/' not in value:
TypeError: argument of type 'NoneType' is not iterable

# Fixed code
def camelResource(obj):
    """Some sources from apis return lowerCased where as describe calls

    always return TitleCase, this function turns the former to the later
    """
    if not isinstance(obj, dict):
        return obj
    for k in list(obj.keys()):
        v = obj.pop(k)
        obj["%s%s" % (k[0].upper(), k[1:])] = v
        if isinstance(v, dict):
            camelResource(v)
        elif isinstance(v, list):
            list(map(camelResource, v))
    return obj
# Buggy code
def get(self, resource_type, identities):
    """Get resources by identities"""
    m = self.resolve(resource_type)
    params = {}
    client_filter = False

    # Try to formulate server side query
    if m.filter_name:
        if m.filter_type == "list":
            params[m.filter_name] = identities
        elif m.filter_type == "scalar":
            assert len(identities) == 1, "Scalar server side filter"
            params[m.filter_name] = identities[0]
    else:
        client_filter = True

    resources = self.filter(resource_type, **params)
    if client_filter:
        resources = [r for r in resources if r[m.id] in identities]

    return resources


# Traceback (TypeError)
string indices must be integers: TypeError
Traceback (most recent call last):
File "/var/task/custodian_policy.py", line 4, in run
return handler.dispatch_event(event, context)
File "/var/task/c7n/handler.py", line 109, in dispatch_event
p.push(event, context)
File "/var/task/c7n/policy.py", line 650, in push
return mode.run(event, lambda_ctx)
File "/var/task/c7n/policy.py", line 428, in run
resources = self.resolve_resources(event)
File "/var/task/c7n/policy.py", line 412, in resolve_resources
resources = self.policy.resource_manager.get_resources(resource_ids)
File "/var/task/c7n/query.py", line 308, in get_resources
resources = self.augment(self.source.get_resources(ids))
File "/var/task/c7n/query.py", line 147, in get_resources
return self.query.get(self.manager.resource_type, ids)
File "/var/task/c7n/query.py", line 94, in get
resources = [r for r in resources if r[m.id] in identities]
TypeError: string indices must be integers

# Fixed code
def get(self, resource_type, identities):
    """Get resources by identities"""
    m = self.resolve(resource_type)
    params = {}
    client_filter = False

    # Try to formulate server side query
    if m.filter_name:
        if m.filter_type == "list":
            params[m.filter_name] = identities
        elif m.filter_type == "scalar":
            assert len(identities) == 1, "Scalar server side filter"
            params[m.filter_name] = identities[0]
    else:
        client_filter = True

    resources = self.filter(resource_type, **params)
    if client_filter:
        # This logic was added to prevent the issue from:
        # https://github.com/capitalone/cloud-custodian/issues/1398
        if all(map(lambda r: isinstance(r, six.string_types), resources)):
            resources = [r for r in resources if r in identities]
        else:
            resources = [r for r in resources if r[m.id] in identities]

    return resources
# Buggy code
def get_appelb_target_groups(self):
    manager = self.manager.get_resource_manager("app-elb")
    return set([a["TargetGroupArn"] for a in manager.resources()])


# Traceback (KeyError)
2017-01-06 20:10:44,873: custodian.resources.keypair:INFO Filtered from 110 to 110 keypair
2017-01-06 20:10:46,097: custodian.resources.elb:INFO Filtered from 2 to 2 elb
2017-01-06 20:10:47,047: custodian.resources.appelb:INFO Filtered from 1 to 1 appelb
2017-01-06 20:10:47,048: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/src/c7n/policy.py", line 174, in run
resources = self.policy.resource_manager.resources()
File "/src/c7n/query.py", line 171, in resources
return self.filter_resources(resources)
File "/src/c7n/manager.py", line 73, in filter_resources
resources = f.process(resources, event)
File "/src/c7n/resources/asg.py", line 236, in process
self.initialize(asgs)
File "/src/c7n/resources/asg.py", line 188, in initialize
self.appelb_target_groups = self.get_appelb_target_groups()
File "/src/c7n/resources/asg.py", line 210, in get_appelb_target_groups
return set([a['TargetGroupArn'] for a in manager.resources()])
KeyError: 'TargetGroupArn'

# Fixed code
def get_appelb_target_groups(self):
    manager = self.manager.get_resource_manager("app-elb-target-group")
    return set([a["TargetGroupArn"] for a in manager.resources()])
# Buggy code
def process(self, resources, event=None):
    days = self.data.get("days", 14)
    duration = timedelta(days)

    self.metric = self.data["name"]
    self.end = datetime.utcnow()
    self.start = self.end - duration
    self.period = int(self.data.get("period", duration.total_seconds()))
    self.statistics = self.data.get("statistics", "Average")
    self.model = self.manager.query.resolve(self.manager.resource_type)
    self.op = OPERATORS[self.data.get("op", "less-than")]
    self.value = self.data["value"]

    ns = self.data.get("namespace")
    if not ns:
        ns = getattr(self.model, "default_namespace", None)
        if not ns:
            ns = self.DEFAULT_NAMESPACE[self.model.service]
    self.namespace = ns

    self.log.debug("Querying metrics for %d", len(resources))
    matched = []
    with self.executor_factory(max_workers=3) as w:
        futures = []
        for resource_set in chunks(resources, 50):
            futures.append(w.submit(self.process_resource_set, resource_set))

        for f in as_completed(futures):
            if f.exception():
                self.log.warning("CW Retrieval error: %s" % f.exception())
                continue
            matched.extend(f.result())
    return matched


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process(self, resources, event=None):
    days = self.data.get("days", 14)
    duration = timedelta(days)

    self.metric = self.data["name"]
    self.end = datetime.utcnow()
    self.start = self.end - duration
    self.period = int(self.data.get("period", duration.total_seconds()))
    self.statistics = self.data.get("statistics", "Average")
    self.model = self.manager.get_model()
    self.op = OPERATORS[self.data.get("op", "less-than")]
    self.value = self.data["value"]

    ns = self.data.get("namespace")
    if not ns:
        ns = getattr(self.model, "default_namespace", None)
        if not ns:
            ns = self.DEFAULT_NAMESPACE[self.model.service]
    self.namespace = ns

    self.log.debug("Querying metrics for %d", len(resources))
    matched = []
    with self.executor_factory(max_workers=3) as w:
        futures = []
        for resource_set in chunks(resources, 50):
            futures.append(w.submit(self.process_resource_set, resource_set))

        for f in as_completed(futures):
            if f.exception():
                self.log.warning("CW Retrieval error: %s" % f.exception())
                continue
            matched.extend(f.result())
    return matched
# Buggy code
def augment(self, resources):
    session = local_session(self.session_factory)
    if self.account_id is None:
        self.account_id = get_account_id(session)
    _rds_tags(
        self.query.resolve(self.resource_type),
        resources,
        self.session_factory,
        self.executor_factory,
        self.account_id,
        region=self.config.region,
    )
    return resources


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def augment(self, resources):
    _rds_tags(
        self.get_model(),
        resources,
        self.session_factory,
        self.executor_factory,
        self.arn_generator,
    )
    return resources
# Buggy code
def _rds_tags(model, dbs, session_factory, executor_factory, account_id, region):
    """Augment rds instances with their respective tags."""

    def process_tags(db):
        client = local_session(session_factory).client("rds")
        arn = "arn:aws:rds:%s:%s:db:%s" % (region, account_id, db[model.id])
        tag_list = client.list_tags_for_resource(ResourceName=arn)["TagList"]
        db["Tags"] = tag_list or []
        return db

    # Rds maintains a low api call limit, so this can take some time :-(
    with executor_factory(max_workers=1) as w:
        list(w.map(process_tags, dbs))


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def _rds_tags(model, dbs, session_factory, executor_factory, arn_generator):
    """Augment rds instances with their respective tags."""

    def process_tags(db):
        client = local_session(session_factory).client("rds")
        arn = arn_generator.generate(db[model.id])
        tag_list = client.list_tags_for_resource(ResourceName=arn)["TagList"]
        db["Tags"] = tag_list or []
        return db

    # Rds maintains a low api call limit, so this can take some time :-(
    with executor_factory(max_workers=1) as w:
        list(w.map(process_tags, dbs))
# Buggy code
def process_tags(db):
    client = local_session(session_factory).client("rds")
    arn = "arn:aws:rds:%s:%s:db:%s" % (region, account_id, db[model.id])
    tag_list = client.list_tags_for_resource(ResourceName=arn)["TagList"]
    db["Tags"] = tag_list or []
    return db


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process_tags(db):
    client = local_session(session_factory).client("rds")
    arn = arn_generator.generate(db[model.id])
    tag_list = client.list_tags_for_resource(ResourceName=arn)["TagList"]
    db["Tags"] = tag_list or []
    return db
# Buggy code
def process_resource_set(self, resources, tags):
    client = local_session(self.manager.session_factory).client("rds")
    for r in resources:
        arn = "arn:aws:rds:%s:%s:db:%s" % (
            self.manager.config.region,
            self.manager.account_id,
            r["DBInstanceIdentifier"],
        )
        client.add_tags_to_resource(ResourceName=arn, Tags=tags)


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process_resource_set(self, resources, tags):
    client = local_session(self.manager.session_factory).client("rds")
    for r in resources:
        arn = self.manager.arn_generator.generate(r["DBInstanceIdentifier"])
        client.add_tags_to_resource(ResourceName=arn, Tags=tags)
# Buggy code
def process_resource_set(self, resources, tag_keys):
    client = local_session(self.manager.session_factory).client("rds")
    for r in resources:
        arn = "arn:aws:rds:%s:%s:db:%s" % (
            self.manager.config.region,
            self.manager.account_id,
            r["DBInstanceIdentifier"],
        )
        client.remove_tags_from_resource(ResourceName=arn, TagKeys=tag_keys)


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process_resource_set(self, resources, tag_keys):
    client = local_session(self.manager.session_factory).client("rds")
    for r in resources:
        arn = self.manager.arn_generator.generate(r["DBInstanceIdentifier"])
        client.remove_tags_from_resource(ResourceName=arn, TagKeys=tag_keys)
# Buggy code
def register_tags(filters, actions, id_key):
    filters.register("marked-for-op", TagActionFilter)
    filters.register("tag-count", TagCountFilter)
    actions.register("mark-for-op", TagDelayedAction.set_id(id_key))
    actions.register("tag-trim", TagTrim.set_id(id_key))

    tag = Tag.set_id(id_key)
    actions.register("mark", tag)
    actions.register("tag", tag)

    remove_tag = RemoveTag.set_id(id_key)
    actions.register("unmark", remove_tag)
    actions.register("untag", remove_tag)
    actions.register("remove-tag", remove_tag)


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def register_tags(filters, actions):
    filters.register("marked-for-op", TagActionFilter)
    filters.register("tag-count", TagCountFilter)

    actions.register("mark-for-op", TagDelayedAction)
    actions.register("tag-trim", TagTrim)

    actions.register("mark", Tag)
    actions.register("tag", Tag)

    actions.register("unmark", RemoveTag)
    actions.register("untag", RemoveTag)
    actions.register("remove-tag", RemoveTag)
# Buggy code
def process(self, resources):
    self.preserve = set(self.data.get("preserve"))
    self.space = self.data.get("space", 3)

    with self.executor_factory(max_workers=3) as w:
        list(w.map(self.process_resource, resources))


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process(self, resources):
    self.id_key = self.manager.get_model().id

    self.preserve = set(self.data.get("preserve"))
    self.space = self.data.get("space", 3)

    with self.executor_factory(max_workers=3) as w:
        list(w.map(self.process_resource, resources))
# Buggy code
def process(self, resources):
    # Legacy
    msg = self.data.get("msg")
    msg = self.data.get("value") or msg

    tag = self.data.get("tag", DEFAULT_TAG)
    tag = self.data.get("key") or tag

    # Support setting multiple tags in a single go with a mapping
    tags = self.data.get("tags")

    if tags is None:
        tags = []
    else:
        tags = [{"Key": k, "Value": v} for k, v in tags.items()]

    if msg:
        tags.append({"Key": tag, "Value": msg})

    batch_size = self.data.get("batch_size", self.batch_size)

    with self.executor_factory(max_workers=self.concurrency) as w:
        futures = []
        for resource_set in utils.chunks(resources, size=batch_size):
            futures.append(w.submit(self.process_resource_set, resource_set, tags))

        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Exception removing tags: %s on resources:%s \n %s"
                    % (tags, self.id_key, f.exception())
                )


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process(self, resources):
    self.id_key = self.manager.get_model().id

    # Legacy
    msg = self.data.get("msg")
    msg = self.data.get("value") or msg

    tag = self.data.get("tag", DEFAULT_TAG)
    tag = self.data.get("key") or tag

    # Support setting multiple tags in a single go with a mapping
    tags = self.data.get("tags")

    if tags is None:
        tags = []
    else:
        tags = [{"Key": k, "Value": v} for k, v in tags.items()]

    if msg:
        tags.append({"Key": tag, "Value": msg})

    batch_size = self.data.get("batch_size", self.batch_size)

    with self.executor_factory(max_workers=self.concurrency) as w:
        futures = []
        for resource_set in utils.chunks(resources, size=batch_size):
            futures.append(w.submit(self.process_resource_set, resource_set, tags))

        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Exception removing tags: %s on resources:%s \n %s"
                    % (
                        tags,
                        ", ".join([r[self.id_key] for r in resource_set]),
                        f.exception(),
                    )
                )
# Buggy code
def process(self, resources):
    tags = self.data.get("tags", [DEFAULT_TAG])
    batch_size = self.data.get("batch_size", self.batch_size)

    with self.executor_factory(max_workers=self.concurrency) as w:
        futures = []
        for resource_set in utils.chunks(resources, size=batch_size):
            futures.append(w.submit(self.process_resource_set, resource_set, tags))

        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Exception removing tags: %s on resources:%s \n %s"
                    % (tags, self.id_key, f.exception())
                )


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process(self, resources):
    self.id_key = self.manager.get_model().id

    tags = self.data.get("tags", [DEFAULT_TAG])
    batch_size = self.data.get("batch_size", self.batch_size)

    with self.executor_factory(max_workers=self.concurrency) as w:
        futures = []
        for resource_set in utils.chunks(resources, size=batch_size):
            futures.append(w.submit(self.process_resource_set, resource_set, tags))

        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Exception removing tags: %s on resources:%s \n %s"
                    % (
                        tags,
                        ", ".join([r[self.id_key] for r in resource_set]),
                        f.exception(),
                    )
                )
# Buggy code
def process(self, resources):
    # Move this to policy? / no resources bypasses actions?
    if not len(resources):
        return

    msg_tmpl = self.data.get("msg", "Resource does not meet policy: {op}@{action_date}")

    op = self.data.get("op", "stop")
    tag = self.data.get("tag", DEFAULT_TAG)
    date = self.data.get("days", 4)

    n = datetime.now(tz=tzutc())
    action_date = n + timedelta(days=date)
    msg = msg_tmpl.format(op=op, action_date=action_date.strftime("%Y/%m/%d"))

    self.log.info(
        "Tagging %d resources for %s on %s"
        % (len(resources), op, action_date.strftime("%Y/%m/%d"))
    )

    tags = [{"Key": tag, "Value": msg}]

    with self.executor_factory(max_workers=2) as w:
        futures = []
        for resource_set in utils.chunks(resources, size=self.batch_size):
            futures.append(w.submit(self.process_resource_set, resource_set, tags))

        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Exception tagging resource set: %s  \n %s" % (tags, f.exception())
                )


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def process(self, resources):
    self.id_key = self.manager.get_model().id

    # Move this to policy? / no resources bypasses actions?
    if not len(resources):
        return

    msg_tmpl = self.data.get("msg", "Resource does not meet policy: {op}@{action_date}")

    op = self.data.get("op", "stop")
    tag = self.data.get("tag", DEFAULT_TAG)
    date = self.data.get("days", 4)

    n = datetime.now(tz=tzutc())
    action_date = n + timedelta(days=date)
    msg = msg_tmpl.format(op=op, action_date=action_date.strftime("%Y/%m/%d"))

    self.log.info(
        "Tagging %d resources for %s on %s"
        % (len(resources), op, action_date.strftime("%Y/%m/%d"))
    )

    tags = [{"Key": tag, "Value": msg}]

    with self.executor_factory(max_workers=2) as w:
        futures = []
        for resource_set in utils.chunks(resources, size=self.batch_size):
            futures.append(w.submit(self.process_resource_set, resource_set, tags))

        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Exception tagging resource set: %s  \n %s" % (tags, f.exception())
                )
# Buggy code
def _getEmbedding(self, obj):
    # optimisation: Passing an Offsets position checks nCharacters, which is an extra call we don't need.
    info = self._makeRawTextInfo(obj.parent, textInfos.POSITION_FIRST)
    if isinstance(info, FakeEmbeddingTextInfo):
        info._startOffset = obj.indexInParent
        info._endOffset = info._startOffset + 1
        return info
    try:
        hl = obj.IAccessibleObject.QueryInterface(
            IAccessibleHandler.IAccessibleHyperlink
        )
        hlOffset = hl.startIndex
        info._startOffset = hlOffset
        info._endOffset = hlOffset + 1
        return info
    except COMError:
        pass
    return None


# Traceback (AttributeError)
ERROR - scriptHandler.executeScript (20:38:17):
error executing script: <bound method Dynamic_EditableTextWithAutoSelectDetectionEditorMozillaIAccessible.script_caret_newLine of <NVDAObjects.Dynamic_EditableTextWithAutoSelectDetectionEditorMozillaIAccessible object at 0x02ABF190>> with gesture u'enter'
Traceback (most recent call last):
File "scriptHandler.pyc", line 186, in executeScript
File "editableText.pyc", line 128, in script_caret_newLine
File "speech.pyc", line 673, in speakTextInfo
File "NVDAObjects\IAccessible\ia2TextMozilla.pyc", line 291, in getTextWithFields
File "NVDAObjects\IAccessible\ia2TextMozilla.pyc", line 240, in _getText
AttributeError: 'NoneType' object has no attribute 'obj'
DEBUGWARNING - NVDAObjects.IAccessible.Dynamic_EditableTextWithAutoSelectDetectionEditorMozillaIAccessible._get_IA2Attributes (20:38:17):
IAccessibleObject.attributes COMError (-2147220995, 'Object is not connected to server', (None, None, None, 0, None))
DEBUGWARNING - NVDAObjects.IAccessible.IA2TextTextInfo._getCaretOffset (20:38:17):
IAccessibleText::caretOffset failed
Traceback (most recent call last):
File "NVDAObjects\IAccessible\__init__.pyc", line 151, in _getCaretOffset
COMError: (-2147220995, 'Object is not connected to server', (None, None, None, 0, None))

# Fixed code
def _getEmbedding(self, obj):
    parent = obj.parent
    if not parent:
        # obj is probably dead.
        return None
    # optimisation: Passing an Offsets position checks nCharacters, which is an extra call we don't need.
    info = self._makeRawTextInfo(parent, textInfos.POSITION_FIRST)
    if isinstance(info, FakeEmbeddingTextInfo):
        info._startOffset = obj.indexInParent
        info._endOffset = info._startOffset + 1
        return info
    try:
        hl = obj.IAccessibleObject.QueryInterface(
            IAccessibleHandler.IAccessibleHyperlink
        )
        hlOffset = hl.startIndex
        info._startOffset = hlOffset
        info._endOffset = hlOffset + 1
        return info
    except COMError:
        pass
    return None
# Buggy code
def __init__(self, dispatcher):
    """
    Args:
        dispatcher (:class:`telegram.ext.Dispatcher`):
    """
    if not dispatcher.use_context:
        raise ValueError(
            "CallbackContext should not be used with a non context aware dispatcher!"
        )
    self._dispatcher = dispatcher
    self._bot_data = dispatcher.bot_data
    self._chat_data = None
    self._user_data = None
    self.args = None
    self.matches = None
    self.error = None
    self.job = None


# Traceback (telegram.error.BadRequest)
<project_path>/venv/bin/python <project_path>/issue.py
2017-06-20 23:24:11,743 - __main__ - INFO - /start
2017-06-20 23:24:11,906 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update.
2017-06-20 23:24:11,907 - __main__ - ERROR - Boom!
2017-06-20 23:24:11,907 - __main__ - INFO - /async_start
2017-06-20 23:24:12,188 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
Traceback (most recent call last):
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/promise.py", line 42, in run
self._result = self.pooled_function(*self.args, **self.kwargs)
File "<project_path>/issue.py", line 25, in action_async_start
update.message.reply_text(msg, parse_mode=ParseMode.HTML)
File "<project_path>/venv/src/python-telegram-bot/telegram/message.py", line 340, in reply_text
return self.bot.send_message(self.chat_id, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 125, in decorator
result = func(self, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 158, in decorator
return Bot._message_wrapper(self, url, data, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 146, in _message_wrapper
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 252, in post
**urlopen_kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 194, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: unsupported start tag "invalid_tag" at byte offset 0

Process finished with exit code 0

# Fixed code
def __init__(self, dispatcher):
    """
    Args:
        dispatcher (:class:`telegram.ext.Dispatcher`):
    """
    if not dispatcher.use_context:
        raise ValueError(
            "CallbackContext should not be used with a non context aware dispatcher!"
        )
    self._dispatcher = dispatcher
    self._bot_data = dispatcher.bot_data
    self._chat_data = None
    self._user_data = None
    self.args = None
    self.matches = None
    self.error = None
    self.job = None
    self.async_args = None
    self.async_kwargs = None
# Buggy code
def from_error(cls, update, error, dispatcher):
    self = cls.from_update(update, dispatcher)
    self.error = error
    return self


# Traceback (telegram.error.BadRequest)
<project_path>/venv/bin/python <project_path>/issue.py
2017-06-20 23:24:11,743 - __main__ - INFO - /start
2017-06-20 23:24:11,906 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update.
2017-06-20 23:24:11,907 - __main__ - ERROR - Boom!
2017-06-20 23:24:11,907 - __main__ - INFO - /async_start
2017-06-20 23:24:12,188 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
Traceback (most recent call last):
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/promise.py", line 42, in run
self._result = self.pooled_function(*self.args, **self.kwargs)
File "<project_path>/issue.py", line 25, in action_async_start
update.message.reply_text(msg, parse_mode=ParseMode.HTML)
File "<project_path>/venv/src/python-telegram-bot/telegram/message.py", line 340, in reply_text
return self.bot.send_message(self.chat_id, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 125, in decorator
result = func(self, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 158, in decorator
return Bot._message_wrapper(self, url, data, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 146, in _message_wrapper
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 252, in post
**urlopen_kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 194, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: unsupported start tag "invalid_tag" at byte offset 0

Process finished with exit code 0

# Fixed code
def from_error(cls, update, error, dispatcher, async_args=None, async_kwargs=None):
    self = cls.from_update(update, dispatcher)
    self.error = error
    self.async_args = async_args
    self.async_kwargs = async_kwargs
    return self
# Buggy code
def __init__(
    self,
    callback,
    pass_update_queue=False,
    pass_job_queue=False,
    pattern=None,
    pass_groups=False,
    pass_groupdict=False,
    pass_user_data=False,
    pass_chat_data=False,
):
    super().__init__(
        callback,
        pass_update_queue=pass_update_queue,
        pass_job_queue=pass_job_queue,
        pass_user_data=pass_user_data,
        pass_chat_data=pass_chat_data,
    )

    if isinstance(pattern, str):
        pattern = re.compile(pattern)

    self.pattern = pattern
    self.pass_groups = pass_groups
    self.pass_groupdict = pass_groupdict


# Traceback (telegram.error.BadRequest)
<project_path>/venv/bin/python <project_path>/issue.py
2017-06-20 23:24:11,743 - __main__ - INFO - /start
2017-06-20 23:24:11,906 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update.
2017-06-20 23:24:11,907 - __main__ - ERROR - Boom!
2017-06-20 23:24:11,907 - __main__ - INFO - /async_start
2017-06-20 23:24:12,188 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
Traceback (most recent call last):
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/promise.py", line 42, in run
self._result = self.pooled_function(*self.args, **self.kwargs)
File "<project_path>/issue.py", line 25, in action_async_start
update.message.reply_text(msg, parse_mode=ParseMode.HTML)
File "<project_path>/venv/src/python-telegram-bot/telegram/message.py", line 340, in reply_text
return self.bot.send_message(self.chat_id, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 125, in decorator
result = func(self, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 158, in decorator
return Bot._message_wrapper(self, url, data, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 146, in _message_wrapper
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 252, in post
**urlopen_kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 194, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: unsupported start tag "invalid_tag" at byte offset 0

Process finished with exit code 0

# Fixed code
def __init__(
    self,
    callback,
    pass_update_queue=False,
    pass_job_queue=False,
    pattern=None,
    pass_groups=False,
    pass_groupdict=False,
    pass_user_data=False,
    pass_chat_data=False,
    run_async=False,
):
    super().__init__(
        callback,
        pass_update_queue=pass_update_queue,
        pass_job_queue=pass_job_queue,
        pass_user_data=pass_user_data,
        pass_chat_data=pass_chat_data,
        run_async=run_async,
    )

    if isinstance(pattern, str):
        pattern = re.compile(pattern)

    self.pattern = pattern
    self.pass_groups = pass_groups
    self.pass_groupdict = pass_groupdict
# Buggy code
def async_func(*args, **kwargs):
    return Dispatcher.get_instance().run_async(func, *args, **kwargs)


# Traceback (telegram.error.BadRequest)
<project_path>/venv/bin/python <project_path>/issue.py
2017-06-20 23:24:11,743 - __main__ - INFO - /start
2017-06-20 23:24:11,906 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update.
2017-06-20 23:24:11,907 - __main__ - ERROR - Boom!
2017-06-20 23:24:11,907 - __main__ - INFO - /async_start
2017-06-20 23:24:12,188 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
Traceback (most recent call last):
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/promise.py", line 42, in run
self._result = self.pooled_function(*self.args, **self.kwargs)
File "<project_path>/issue.py", line 25, in action_async_start
update.message.reply_text(msg, parse_mode=ParseMode.HTML)
File "<project_path>/venv/src/python-telegram-bot/telegram/message.py", line 340, in reply_text
return self.bot.send_message(self.chat_id, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 125, in decorator
result = func(self, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 158, in decorator
return Bot._message_wrapper(self, url, data, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 146, in _message_wrapper
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 252, in post
**urlopen_kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 194, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: unsupported start tag "invalid_tag" at byte offset 0

Process finished with exit code 0

# Fixed code
def async_func(*args, **kwargs):
    warnings.warn(
        "The @run_async decorator is deprecated. Use the `run_async` parameter of"
        "`Dispatcher.add_handler` or `Dispatcher.run_async` instead.",
        TelegramDeprecationWarning,
        stacklevel=2,
    )
    return Dispatcher.get_instance()._run_async(
        func, *args, update=None, error_handling=False, **kwargs
    )
# Buggy code
def remove_error_handler(self, callback):
    """Removes an error handler.

    Args:
        callback (:obj:`callable`): The error handler to remove.

    """
    if callback in self.error_handlers:
        self.error_handlers.remove(callback)


# Traceback (telegram.error.BadRequest)
<project_path>/venv/bin/python <project_path>/issue.py
2017-06-20 23:24:11,743 - __main__ - INFO - /start
2017-06-20 23:24:11,906 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update.
2017-06-20 23:24:11,907 - __main__ - ERROR - Boom!
2017-06-20 23:24:11,907 - __main__ - INFO - /async_start
2017-06-20 23:24:12,188 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
Traceback (most recent call last):
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/promise.py", line 42, in run
self._result = self.pooled_function(*self.args, **self.kwargs)
File "<project_path>/issue.py", line 25, in action_async_start
update.message.reply_text(msg, parse_mode=ParseMode.HTML)
File "<project_path>/venv/src/python-telegram-bot/telegram/message.py", line 340, in reply_text
return self.bot.send_message(self.chat_id, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 125, in decorator
result = func(self, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 158, in decorator
return Bot._message_wrapper(self, url, data, *args, **kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 146, in _message_wrapper
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 252, in post
**urlopen_kwargs)
File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 194, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: unsupported start tag "invalid_tag" at byte offset 0

Process finished with exit code 0

# Fixed code
def remove_error_handler(self, callback):
    """Removes an error handler.

    Args:
        callback (:obj:`callable`): The error handler to remove.

    """
    self.error_handlers.pop(callback, None)