Skip to content

Commit

Permalink
Fix memleak by dropping lru cache (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Aug 12, 2022
1 parent 21f51a5 commit ade6619
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
17 changes: 5 additions & 12 deletions tomlkit/items.py
Expand Up @@ -258,13 +258,6 @@ class StringType(Enum):
# Multi Line Literal
MLL = "'''"

def __init__(self, value):
self.is_basic = lru_cache(maxsize=None)(self._is_basic)
self.is_literal = lru_cache(maxsize=None)(self._is_literal)
self.is_singleline = lru_cache(maxsize=None)(self._is_singleline)
self.is_multiline = lru_cache(maxsize=None)(self._is_multiline)
self.toggle = lru_cache(maxsize=None)(self._toggle)

@classmethod
def select(cls, literal=False, multiline=False) -> "StringType":
return {
Expand Down Expand Up @@ -302,19 +295,19 @@ def invalid_sequences(self) -> Collection[str]:
def unit(self) -> str:
return self.value[0]

def _is_basic(self) -> bool:
def is_basic(self) -> bool:
return self in {StringType.SLB, StringType.MLB}

def _is_literal(self) -> bool:
def is_literal(self) -> bool:
return self in {StringType.SLL, StringType.MLL}

def _is_singleline(self) -> bool:
def is_singleline(self) -> bool:
return self in {StringType.SLB, StringType.SLL}

def _is_multiline(self) -> bool:
def is_multiline(self) -> bool:
return self in {StringType.MLB, StringType.MLL}

def _toggle(self) -> "StringType":
def toggle(self) -> "StringType":
return {
StringType.SLB: StringType.MLB,
StringType.MLB: StringType.SLB,
Expand Down
20 changes: 6 additions & 14 deletions tomlkit/toml_char.py
@@ -1,16 +1,8 @@
import string

from functools import lru_cache


class TOMLChar(str):
def __init__(self, c):
self.is_bare_key_char = lru_cache(maxsize=None)(self._is_bare_key_char)
self.is_kv_sep = lru_cache(maxsize=None)(self._is_kv_sep)
self.is_int_float_char = lru_cache(maxsize=None)(self._is_int_float_char)
self.is_ws = lru_cache(maxsize=None)(self._is_ws)
self.is_nl = lru_cache(maxsize=None)(self._is_nl)
self.is_spaces = lru_cache(maxsize=None)(self._is_spaces)
super().__init__()

if len(self) > 1:
Expand All @@ -23,37 +15,37 @@ def __init__(self, c):
NL = "\n\r"
WS = SPACES + NL

def _is_bare_key_char(self) -> bool:
def is_bare_key_char(self) -> bool:
"""
Whether the character is a valid bare key name or not.
"""
return self in self.BARE

def _is_kv_sep(self) -> bool:
def is_kv_sep(self) -> bool:
"""
Whether the character is a valid key/value separator or not.
"""
return self in self.KV

def _is_int_float_char(self) -> bool:
def is_int_float_char(self) -> bool:
"""
Whether the character if a valid integer or float value character or not.
"""
return self in self.NUMBER

def _is_ws(self) -> bool:
def is_ws(self) -> bool:
"""
Whether the character is a whitespace character or not.
"""
return self in self.WS

def _is_nl(self) -> bool:
def is_nl(self) -> bool:
"""
Whether the character is a new line character or not.
"""
return self in self.NL

def _is_spaces(self) -> bool:
def is_spaces(self) -> bool:
"""
Whether the character is a space or not
"""
Expand Down

0 comments on commit ade6619

Please sign in to comment.