# Buggy code
def _report_options(p):
    """Add options specific to the report subcommand."""
    _default_options(p, blacklist=["cache", "log-group", "quiet"])
    p.add_argument(
        "--days", type=float, default=1, help="Number of days of history to consider"
    )
    p.add_argument(
        "--raw",
        type=argparse.FileType("wb"),
        help="Store raw json of collected records to given file path",
    )
    p.add_argument(
        "--field",
        action="append",
        default=[],
        type=_key_val_pair,
        metavar="HEADER=FIELD",
        help="Repeatable. JMESPath of field to include in the output OR "
        "for a tag use prefix `tag:`. Special case fields `region` and"
        "`policy` are available",
    )
    p.add_argument(
        "--no-default-fields",
        action="store_true",
        help="Exclude default fields for report.",
    )
    p.add_argument(
        "--format",
        default="csv",
        choices=["csv", "grid", "simple", "json"],
        help="Format to output data in (default: %(default)s). "
        "Options include simple, grid, csv, json",
    )


# Traceback (TypeError)
$ custodian report -s output --raw outfile.json list-ec2-instances.yaml
CustodianDate,InstanceId,tag:Name,InstanceType,LaunchTime,VpcId,PrivateIpAddress
Traceback (most recent call last):
File "/home/ec2-user/custodian/bin/custodian", line 8, in <module>
sys.exit(main())
File "/home/ec2-user/custodian/lib/python3.7/site-packages/c7n/cli.py", line 374, in main
command(config)
File "/home/ec2-user/custodian/lib/python3.7/site-packages/c7n/commands.py", line 141, in _load_policies
return f(options, list(policies))
File "/home/ec2-user/custodian/lib/python3.7/site-packages/c7n/commands.py", line 311, in report
policies, begin_date, options, sys.stdout, raw_output_fh=options.raw)
File "/home/ec2-user/custodian/lib/python3.7/site-packages/c7n/reports/csvout.py", line 112, in report
dumps(records, raw_output_fh, indent=2)
File "/home/ec2-user/custodian/lib/python3.7/site-packages/c7n/utils.py", line 122, in dumps
return json.dump(data, fh, cls=DateTimeEncoder, indent=indent)
File "/usr/lib64/python3.7/json/__init__.py", line 180, in dump
fp.write(chunk)
TypeError: a bytes-like object is required, not 'str'

# Fixed code
def _report_options(p):
    """Add options specific to the report subcommand."""
    _default_options(p, blacklist=["cache", "log-group", "quiet"])
    p.add_argument(
        "--days", type=float, default=1, help="Number of days of history to consider"
    )
    p.add_argument(
        "--raw",
        type=argparse.FileType("w"),
        help="Store raw json of collected records to given file path",
    )
    p.add_argument(
        "--field",
        action="append",
        default=[],
        type=_key_val_pair,
        metavar="HEADER=FIELD",
        help="Repeatable. JMESPath of field to include in the output OR "
        "for a tag use prefix `tag:`. Special case fields `region` and"
        "`policy` are available",
    )
    p.add_argument(
        "--no-default-fields",
        action="store_true",
        help="Exclude default fields for report.",
    )
    p.add_argument(
        "--format",
        default="csv",
        choices=["csv", "grid", "simple", "json"],
        help="Format to output data in (default: %(default)s). "
        "Options include simple, grid, csv, json",
    )