mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 13:19:24 +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 re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import weakref
|
||||||
import threading
|
import threading
|
||||||
conf = None
|
conf = None
|
||||||
# Don't import conf here ; because conf needs this module
|
# 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')
|
directory = os.path.join(base, 'locales')
|
||||||
return '%s/%s.%s' % (directory, localeName, extension)
|
return '%s/%s.%s' % (directory, localeName, extension)
|
||||||
|
|
||||||
i18nClasses = {}
|
i18nClasses = weakref.WeakValueDictionary()
|
||||||
internationalizedCommands = {}
|
internationalizedCommands = weakref.WeakValueDictionary()
|
||||||
internationalizedFunctions = [] # No need to know there name
|
internationalizedFunctions = [] # No need to know their name
|
||||||
|
|
||||||
def reloadLocalesIfRequired():
|
def reloadLocalesIfRequired():
|
||||||
global currentLocale
|
global currentLocale
|
||||||
@ -120,11 +121,15 @@ def reloadLocalesIfRequired():
|
|||||||
reloadLocales()
|
reloadLocales()
|
||||||
|
|
||||||
def reloadLocales():
|
def reloadLocales():
|
||||||
for pluginName in i18nClasses:
|
for pluginClass in i18nClasses.values():
|
||||||
i18nClasses[pluginName].loadLocale()
|
pluginClass.loadLocale()
|
||||||
for command in internationalizedCommands.values():
|
for command in internationalizedCommands.values():
|
||||||
internationalizeDocstring(command)
|
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()
|
function.loadLocale()
|
||||||
|
|
||||||
def parse(translationFile):
|
def parse(translationFile):
|
||||||
@ -334,7 +339,7 @@ class internationalizedFunction:
|
|||||||
self._internationalizer = internationalizer
|
self._internationalizer = internationalizer
|
||||||
self._name = name
|
self._name = name
|
||||||
self._origin = function
|
self._origin = function
|
||||||
internationalizedFunctions.append(self)
|
internationalizedFunctions.append(weakref.proxy(self))
|
||||||
def loadLocale(self):
|
def loadLocale(self):
|
||||||
self.__call__ = self._internationalizer.localizeFunction(self._name)
|
self.__call__ = self._internationalizer.localizeFunction(self._name)
|
||||||
if self.__call__ == None:
|
if self.__call__ == None:
|
||||||
|
Loading…
Reference in New Issue
Block a user