Skip to content

Commit 564db2b

Browse files
authored
Merge pull request #3419 from ferdnyc/xml-dom-unlink
Free xml.dom.mindom parsers when finished
2 parents ddf3d1e + e7eb348 commit 564db2b

File tree

5 files changed

+50
-27
lines changed

5 files changed

+50
-27
lines changed

src/classes/exporters/final_cut_pro.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def createEffect(xmldoc, name, node, points, scale):
5757
first_value = None
5858
for keyframeTime in sorted(keyframes.keys()):
5959
keyframeValue = keyframes.get(keyframeTime)
60-
if first_value == None:
60+
if first_value is None:
6161
first_value = keyframeValue
6262

6363
# Create keyframe element for each point
@@ -230,4 +230,6 @@ def export_xml():
230230
file.close()
231231
except IOError as inst:
232232
log.error("Error writing XML export: {}".format(str(inst)))
233-
233+
finally:
234+
# Free up DOM memory
235+
xmldoc.unlink()

src/classes/importers/final_cut_pro.py

+3
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,6 @@ def import_xml():
201201
# Update the preview and reselect current frame in properties
202202
app.window.refreshFrameSignal.emit()
203203
app.window.propertyTableView.select_frame(app.window.preview_thread.player.Position())
204+
205+
# Free up DOM memory
206+
xmldoc.unlink()

src/windows/export.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,31 @@
3030
import time
3131
import tempfile
3232

33+
import openshot
34+
3335
# Try to get the security-patched XML functions from defusedxml
3436
try:
3537
from defusedxml import minidom as xml
3638
except ImportError:
3739
from xml.dom import minidom as xml
3840

39-
from PyQt5.QtCore import *
40-
from PyQt5.QtWidgets import *
41-
from PyQt5.QtGui import QIcon
41+
from PyQt5.QtCore import Qt, QCoreApplication, QTimer
42+
from PyQt5.QtWidgets import (
43+
QMessageBox, QDialog, QFileDialog, QDialogButtonBox, QPushButton
44+
)
45+
from PyQt5.QtGui import QSize, QIcon
4246

4347
from classes import info
4448
from classes import ui_util
49+
from classes import settings
50+
from classes.logger import log
4551
from classes.app import get_app
46-
from classes.metrics import *
52+
from classes.metrics import track_metric_screen, track_metric_error
4753
from classes.query import File
4854

4955
import json
5056

57+
5158
class Export(QDialog):
5259
""" Export Dialog """
5360

@@ -233,9 +240,11 @@ def __init__(self):
233240
presets = []
234241
for preset_path in [info.EXPORT_PRESETS_PATH, info.USER_PRESETS_PATH]:
235242
for file in os.listdir(preset_path):
236-
xmldoc = xml.parse(os.path.join(preset_path, file))
237-
type = xmldoc.getElementsByTagName("type")
238-
presets.append(_(type[0].childNodes[0].data))
243+
# Use context manager to automatically unlink xmldoc objects
244+
with xml.parse(os.path.join(preset_path, file)) as xmldoc:
245+
type = xmldoc.getElementsByTagName("type")
246+
presets.append(_(type[0].childNodes[0].data))
247+
239248

240249
# Exclude duplicates
241250
type_index = 0
@@ -399,6 +408,9 @@ def cboSimpleProjectType_index_changed(self, widget, index):
399408
elif openshot.FFmpegWriter.IsValidCodec(codec_text):
400409
acceleration_types[_(title.childNodes[0].data)] = QIcon(":/hw/hw-accel-none.svg")
401410

411+
# Free up DOM memory
412+
xmldoc.unlink()
413+
402414
# Add all targets for selected project type
403415
preset_index = 0
404416
selected_preset = 0
@@ -543,6 +555,9 @@ def cboSimpleTarget_index_changed(self, widget, index):
543555
break
544556
layout_index += 1
545557

558+
# Free up DOM memory
559+
xmldoc.unlink()
560+
546561
# init the profiles combo
547562
for item in sorted(profiles_list):
548563
self.cboSimpleVideoProfile.addItem(self.getProfileName(self.getProfilePath(item)), self.getProfilePath(item))

src/windows/file_properties.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@
3737
from xml.dom import minidom as xml
3838

3939
from PyQt5.QtCore import *
40-
from PyQt5.QtWidgets import *
40+
from PyQt5.QtWidgets import QDialog, QFileDialog, QDialogButtonBox, QPushButton
4141

4242
# Python module for libopenshot (required video editing module installed separately)
4343
import openshot
4444

4545
from classes import info, ui_util, settings
4646
from classes.app import get_app
4747
from classes.logger import log
48-
from classes.metrics import *
48+
from classes.metrics import track_metric_screen
49+
4950

5051

5152
class FileProperties(QDialog):
@@ -190,7 +191,7 @@ def accept(self):
190191
self.file.data["name"] = self.txtFileName.text()
191192
self.file.data["tags"] = self.txtTags.text()
192193

193-
#experimental: update file path
194+
# experimental: update file path
194195
self.file.data["path"] = self.txtFilePath.text()
195196

196197
# Update Framerate

src/windows/title_editor.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2727
"""
2828

29-
import sys
3029
import os
3130
import re
3231
import shutil
@@ -35,17 +34,20 @@
3534
import tempfile
3635
from xml.dom import minidom
3736

38-
from PyQt5.QtCore import *
39-
from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem, QFont
40-
from PyQt5.QtWidgets import *
41-
from PyQt5 import uic, QtSvg, QtGui
37+
from PyQt5.QtCore import Qt, QTimer
38+
from PyQt5 import QtGui
39+
from PyQt5.QtWidgets import (
40+
QGraphicsScene, QMessageBox, QDialog, QColorDialog, QFontDialog,
41+
QPushButton, QTextEdit, QLabel
42+
)
43+
4244
import openshot
4345

44-
from classes import info, ui_util, settings, qt_types, updates
46+
from classes import info, ui_util, settings
4547
from classes.logger import log
4648
from classes.app import get_app
4749
from classes.query import File
48-
from classes.metrics import *
50+
from classes.metrics import track_metric_screen
4951
from windows.views.titles_listview import TitlesListView
5052

5153
import json
@@ -102,7 +104,7 @@ def __init__(self, edit_file_path=None, duplicate=False):
102104
self.font_family = "Bitstream Vera Sans"
103105
self.tspan_node = None
104106

105-
self.qfont = QFont(self.font_family)
107+
self.qfont = QtGui.QFont(self.font_family)
106108

107109
# Add titles list view
108110
self.titlesTreeView = TitlesListView(self)
@@ -202,7 +204,7 @@ def load_svg_template(self):
202204
self.font_family = "Bitstream Vera Sans"
203205
if self.qfont:
204206
del self.qfont
205-
self.qfont = QFont(self.font_family)
207+
self.qfont = QtGui.QFont(self.font_family)
206208

207209
# Loop through child widgets (and remove them)
208210
for child in self.settingsContainer.children():
@@ -276,7 +278,6 @@ def load_svg_template(self):
276278
widget.textChanged.connect(functools.partial(self.txtLine_changed, widget))
277279
self.settingsContainer.layout().addRow(label, widget)
278280

279-
280281
# Add Font button
281282
label = QLabel()
282283
label.setText(_("Font:"))
@@ -340,7 +341,7 @@ def writeToFile(self, xmldoc):
340341
file.write(bytes(xmldoc.toxml(), 'UTF-8'))
341342
file.close()
342343
except IOError as inst:
343-
log.error("Error writing SVG title")
344+
log.error("Error writing SVG title: {}".format(inst))
344345

345346
def btnFontColor_clicked(self):
346347
app = get_app()
@@ -624,7 +625,6 @@ def set_font_color_elements(self, color, alpha):
624625
t = ";"
625626
text_child.setAttribute("style", t.join(ar))
626627

627-
628628
# Loop through each TSPAN
629629
for tspan_child in self.tspan_node:
630630

@@ -660,8 +660,11 @@ def accept(self):
660660
if self.txtFileName.toPlainText().strip():
661661
# Do we have unsaved changes?
662662
if os.path.exists(file_path) and not self.edit_file_path:
663-
ret = QMessageBox.question(self, _("Title Editor"), _("%s already exists.\nDo you want to replace it?") % file_name,
664-
QMessageBox.No | QMessageBox.Yes)
663+
ret = QMessageBox.question(
664+
self, _("Title Editor"),
665+
_("%s already exists.\nDo you want to replace it?") % file_name,
666+
QMessageBox.No | QMessageBox.Yes
667+
)
665668
if ret == QMessageBox.No:
666669
# Do nothing
667670
return
@@ -682,7 +685,6 @@ def add_file(self, filepath):
682685
filename = os.path.basename(filepath)
683686

684687
# Add file into project
685-
app = get_app()
686688
_ = get_app()._tr
687689

688690
# Check for this path in our existing project data

0 commit comments

Comments
 (0)