[3.4] Avoid potential memory leak in Filter cog (#5578) (#5735)

(cherry picked from commit eeffbf8231)

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Red-GitHubBot
2022-06-04 03:05:44 +02:00
committed by GitHub
parent d8bbf1c4c7
commit 9ff2085b94

View File

@@ -2,7 +2,7 @@ import asyncio
import discord
import re
from datetime import timezone
from typing import Union, Set, Literal
from typing import Union, Set, Literal, Optional
from redbot.core import checks, Config, modlog, commands
from redbot.core.bot import Red
@@ -346,12 +346,14 @@ class Filter(commands.Cog):
else:
await ctx.send(_("Names and nicknames will now be filtered."))
def invalidate_cache(self, guild: discord.Guild, channel: discord.TextChannel = None):
def invalidate_cache(
self, guild: discord.Guild, channel: Optional[discord.TextChannel] = None
) -> None:
""" Invalidate a cached pattern"""
self.pattern_cache.pop((guild, channel), None)
self.pattern_cache.pop((guild.id, channel and channel.id), None)
if channel is None:
for keyset in list(self.pattern_cache.keys()): # cast needed, no remove
if guild in keyset:
if guild.id == keyset[0]:
self.pattern_cache.pop(keyset, None)
async def add_to_filter(
@@ -408,7 +410,7 @@ class Filter(commands.Cog):
hits: Set[str] = set()
try:
pattern = self.pattern_cache[(guild, channel)]
pattern = self.pattern_cache[(guild.id, channel and channel.id)]
except KeyError:
word_list = set(await self.config.guild(guild).filter())
if channel:
@@ -421,7 +423,7 @@ class Filter(commands.Cog):
else:
pattern = None
self.pattern_cache[(guild, channel)] = pattern
self.pattern_cache[(guild.id, channel and channel.id)] = pattern
if pattern:
hits |= set(pattern.findall(text))