Skip to content

Commit

Permalink
[config] prevents registering defaults for reserved identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Chovin committed Oct 1, 2024
1 parent d3887b5 commit 9cb65b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions redbot/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class Value:
"""

# to reserve these attributes for __getattr__
__slots__ = ("identifier_data", "default", "_driver", "_config", "__dict__")

def __init__(self, identifier_data: IdentifierData, default_value, driver, config: "Config"):
self.identifier_data = identifier_data
self.default = default_value
Expand Down Expand Up @@ -291,6 +294,9 @@ class Group(Value):
"""

# to reserve these attributes for __getattr__
__slots__ = ("_defaults", "force_registration")

def __init__(
self,
identifier_data: IdentifierData,
Expand Down Expand Up @@ -640,6 +646,18 @@ class Config(metaclass=ConfigMeta):
USER = "USER"
MEMBER = "MEMBER"

# to reserve these attributes for __getattr__
__slots__ = (
"cog_name",
"unique_identifier",
"_driver",
"force_registration",
"_defaults",
"custom_groups",
"_lock_cache",
"__weakref__",
)

def __init__(
self,
cog_name: str,
Expand Down Expand Up @@ -782,6 +800,8 @@ def _get_defaults_dict(key: str, value) -> dict:
for i, k in enumerate(splitted, start=1):
if not k.isidentifier():
raise RuntimeError("'{}' is an invalid config key.".format(k))
if k in [*vars(Config), *vars(Group), *vars(Value)]:
raise RuntimeError("'{}' is a reserved config key.".format(k))
if i == len(splitted):
partial[k] = value
else:
Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def test_config_register_global_badvalues(config):
config.register_global(**{"invalid var name": True})


def test_config_register_reserved_keys(config):
for attr in vars(config):
with pytest.raises((RuntimeError, ValueError)):
config.register_global(**{attr: True})


async def test_config_register_guild(config, empty_guild):
config.register_guild(enabled=False, some_list=[], some_dict={})
assert config.defaults[config.GUILD]["enabled"] is False
Expand Down

0 comments on commit 9cb65b1

Please sign in to comment.