python: Fix __dict__ mutation abuse.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-10-07 22:04:09 -07:00 committed by Tim Abbott
parent b45484573e
commit 5e1ebf2861
2 changed files with 3 additions and 4 deletions

View File

@ -173,12 +173,11 @@ def generate_and_save_stripe_fixture(
stripe_object = mocked_function(*args, **kwargs) stripe_object = mocked_function(*args, **kwargs)
except stripe.error.StripeError as e: except stripe.error.StripeError as e:
with open(fixture_path, "w") as f: with open(fixture_path, "w") as f:
error_dict = e.__dict__ error_dict = {**vars(e), "headers": dict(e.headers)}
error_dict["headers"] = dict(error_dict["headers"])
f.write( f.write(
json.dumps(error_dict, indent=2, separators=(",", ": "), sort_keys=True) + "\n" json.dumps(error_dict, indent=2, separators=(",", ": "), sort_keys=True) + "\n"
) )
raise e raise
with open(fixture_path, "w") as f: with open(fixture_path, "w") as f:
if stripe_object is not None: if stripe_object is not None:
f.write(str(stripe_object) + "\n") f.write(str(stripe_object) + "\n")

View File

@ -50,7 +50,7 @@ Usage examples:
for realm in realms: for realm in realms:
# Start with just all the fields on the object, which is # Start with just all the fields on the object, which is
# hacky but doesn't require any work to maintain. # hacky but doesn't require any work to maintain.
realm_dict = realm.__dict__ realm_dict = vars(realm).copy()
# Remove a field that is confusingly useless # Remove a field that is confusingly useless
del realm_dict["_state"] del realm_dict["_state"]
# Fix the one bitfield to display useful data # Fix the one bitfield to display useful data