From c5741c7225860afb015aaff8eeff4b79d016c117 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 13 Oct 2018 20:17:16 +0200 Subject: [PATCH] Use xml.etree.cElementTree whenever possible to avoid memory leaks --- resources/lib/downloadutils.py | 3 +-- resources/lib/initialsetup.py | 3 +-- resources/lib/music.py | 3 +-- resources/lib/service_entry.py | 1 + resources/lib/utils.py | 10 ++++++++-- resources/lib/videonodes.py | 2 +- resources/lib/websocket_client.py | 3 +-- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/resources/lib/downloadutils.py b/resources/lib/downloadutils.py index 3ae64e7c..d68dfb81 100644 --- a/resources/lib/downloadutils.py +++ b/resources/lib/downloadutils.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals from logging import getLogger -import defusedxml.ElementTree as etree # etree parse unsafe import requests from . import utils @@ -276,7 +275,7 @@ class DownloadUtils(): return r try: # xml response - r = etree.fromstring(r.content) + r = utils.defused_etree.fromstring(r.content) return r except: r.encoding = 'utf-8' diff --git a/resources/lib/initialsetup.py b/resources/lib/initialsetup.py index 863ad414..b4dcf221 100644 --- a/resources/lib/initialsetup.py +++ b/resources/lib/initialsetup.py @@ -3,7 +3,6 @@ from __future__ import absolute_import, division, unicode_literals from logging import getLogger from Queue import Queue -import xml.etree.ElementTree as etree from xbmc import executebuiltin, translatePath @@ -494,7 +493,7 @@ class InitialSetup(object): xml.set_setting(['video', 'ignoresecondsatstart'], value='60') reboot = xml.write_xml - except etree.ParseError: + except utils.etree.ParseError: cache = None reboot = False # Kodi default cache if no setting is set diff --git a/resources/lib/music.py b/resources/lib/music.py index 93770a32..94da2c37 100644 --- a/resources/lib/music.py +++ b/resources/lib/music.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals from logging import getLogger -from xml.etree.ElementTree import ParseError from . import utils from .plex_api import API @@ -66,7 +65,7 @@ def excludefromscan_music_folders(xml): element.text) parent.remove(element) xml_file.write_xml = True - except (ParseError, IOError): + except (utils.etree.ParseError, IOError): LOG.error('Could not adjust advancedsettings.xml') if reboot is True: # 'New Plex music library detected. Sorry, but we need to diff --git a/resources/lib/service_entry.py b/resources/lib/service_entry.py index 333fac73..1cc6c074 100644 --- a/resources/lib/service_entry.py +++ b/resources/lib/service_entry.py @@ -67,6 +67,7 @@ class Service(): utils.settings('syncSpecificPlexPlaylistsPrefix') == 'true') LOG.info('Play playlist prefix: %s', utils.settings('syncSpecificPlexPlaylistsPrefix')) + LOG.info('XML decoding being used: %s', utils.ETREE) self.monitor = xbmc.Monitor() # Load/Reset PKC entirely - important for user/Kodi profile switch initialsetup.reload_pkc() diff --git a/resources/lib/utils.py b/resources/lib/utils.py index fe2fe69a..e8fd8f0e 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -9,8 +9,14 @@ from sqlite3 import connect, OperationalError from datetime import datetime, timedelta from time import localtime, strftime from unicodedata import normalize -import xml.etree.ElementTree as etree -import defusedxml.ElementTree as defused_etree # etree parse unsafe +try: + import xml.etree.cElementTree as etree + import defusedxml.cElementTree as defused_etree # etree parse unsafe + ETREE = 'cElementTree' +except ImportError: + import xml.etree.ElementTree as etree + import defusedxml.ElementTree as defused_etree # etree parse unsafe + ETREE = 'ElementTree' from functools import wraps, partial from urllib import quote_plus import hashlib diff --git a/resources/lib/videonodes.py b/resources/lib/videonodes.py index 07180cf9..15b372b4 100644 --- a/resources/lib/videonodes.py +++ b/resources/lib/videonodes.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals from logging import getLogger -import xml.etree.ElementTree as etree from . import utils +from .utils import etree from . import path_ops from . import variables as v from . import state diff --git a/resources/lib/websocket_client.py b/resources/lib/websocket_client.py index ff81c30f..c796e457 100644 --- a/resources/lib/websocket_client.py +++ b/resources/lib/websocket_client.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- from logging import getLogger from json import loads -import defusedxml.ElementTree as etree # etree parse unsafe from threading import Thread from ssl import CERT_NONE from xbmc import sleep @@ -222,7 +221,7 @@ class Alexa_Websocket(WebSocket): self.__class__.__name__) LOG.debug('%s: %s', self.__class__.__name__, message) try: - message = etree.fromstring(message) + message = utils.defused_etree.fromstring(message) except Exception as ex: LOG.error('%s: Error decoding message from Alexa: %s', self.__class__.__name__, ex)