Skip to content

Commit

Permalink
avoid clashes with flags of same fieldname
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Oct 23, 2023
1 parent 288483d commit cd6e6a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/test_util_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_str_flag_to_int():
assert flag == flags[0]

for v in versions:
assert v in _flag_cache
assert f"BufferUsage.{v}" in _flag_cache


if __name__ == "__main__":
Expand Down
11 changes: 8 additions & 3 deletions wgpu/_coreutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ def error_message_hash(message):


def str_flag_to_int(flag, s):
"""Allow using strings for flags, i.e. 'READ' instead of wgpu.MapMode.READ."""
value = _flag_cache.get(s, None)
"""Allow using strings for flags, i.e. 'READ' instead of wgpu.MapMode.READ.
No worries about repeated overhead, because the resuls are cached.
"""
cache_key = (
f"{flag._name}.{s}" # using private attribute, lets call this a friend func
)
value = _flag_cache.get(cache_key, None)

if value is None:
parts = [p.strip() for p in s.split("|")]
Expand All @@ -61,7 +66,7 @@ def str_flag_to_int(flag, s):
value += v
except KeyError:
raise ValueError(f"Invalid flag value for {flag}: '{p}'")
_flag_cache[s] = value
_flag_cache[cache_key] = value

return value

Expand Down

0 comments on commit cd6e6a6

Please sign in to comment.