mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-23 19:19:32 +01:00
Fix memory leak in i18n by using weak references to plugin classes and methods.
This commit is contained in:
parent
fe57cdb4dc
commit
f3e292d518
19
src/i18n.py
19
src/i18n.py
@ -37,6 +37,7 @@ import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import weakref
|
||||
import threading
|
||||
conf = None
|
||||
# Don't import conf here ; because conf needs this module
|
||||
@ -107,9 +108,9 @@ def getLocalePath(name, localeName, extension):
|
||||
directory = os.path.join(base, 'locales')
|
||||
return '%s/%s.%s' % (directory, localeName, extension)
|
||||
|
||||
i18nClasses = {}
|
||||
internationalizedCommands = {}
|
||||
internationalizedFunctions = [] # No need to know there name
|
||||
i18nClasses = weakref.WeakValueDictionary()
|
||||
internationalizedCommands = weakref.WeakValueDictionary()
|
||||
internationalizedFunctions = [] # No need to know their name
|
||||
|
||||
def reloadLocalesIfRequired():
|
||||
global currentLocale
|
||||
@ -120,11 +121,15 @@ def reloadLocalesIfRequired():
|
||||
reloadLocales()
|
||||
|
||||
def reloadLocales():
|
||||
for pluginName in i18nClasses:
|
||||
i18nClasses[pluginName].loadLocale()
|
||||
for pluginClass in i18nClasses.values():
|
||||
pluginClass.loadLocale()
|
||||
for command in internationalizedCommands.values():
|
||||
internationalizeDocstring(command)
|
||||
for function in internationalizedFunctions:
|
||||
for function_ref in list(internationalizedFunctions):
|
||||
function = function_ref
|
||||
if not function:
|
||||
internationalizedFunctions.remove(function_ref)
|
||||
else:
|
||||
function.loadLocale()
|
||||
|
||||
def parse(translationFile):
|
||||
@ -334,7 +339,7 @@ class internationalizedFunction:
|
||||
self._internationalizer = internationalizer
|
||||
self._name = name
|
||||
self._origin = function
|
||||
internationalizedFunctions.append(self)
|
||||
internationalizedFunctions.append(weakref.proxy(self))
|
||||
def loadLocale(self):
|
||||
self.__call__ = self._internationalizer.localizeFunction(self._name)
|
||||
if self.__call__ == None:
|
||||
|
Loading…
Reference in New Issue
Block a user