Skip to content

Commit

Permalink
Fixed a memory leak (#131)
Browse files Browse the repository at this point in the history
Co-authored-by: Lennart Regebro <lennart.regebro@fidelity.com>
  • Loading branch information
regebro and Lennart Regebro authored Sep 18, 2024
1 parent dc4c016 commit 2bf8f11
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
2.2.2 (unreleased)
------------------

- Nothing changed yet.
- Fixed a memory leak in unoserver.


2.2.1 (2024-07-24)
Expand Down
24 changes: 20 additions & 4 deletions src/unoserver/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def __init__(self, interface="127.0.0.1", port="2002"):
self.type_service = self.service.createInstanceWithContext(
"com.sun.star.document.TypeDetection", self.context
)
self._export_filters = None
self._import_filters = None

def find_filter(self, import_type, export_type):
for export_filter in self.get_available_export_filters():
Expand All @@ -103,26 +105,40 @@ def find_filter(self, import_type, export_type):
return None

def get_available_import_filters(self):
# Doing this call for some reason uses up memory each time, so we do it
# only once, and cache it here:
if self._import_filters is not None:
return self._import_filters

# List import filters. You can only search on module, iflags and eflags,
# so the import and export types we have to test in a loop
import_filters = self.filter_service.createSubSetEnumerationByQuery(
"getSortedFilterList():iflags=1"
)

self._import_filters = []
while import_filters.hasMoreElements():
# Filter DocumentService here
yield prop2dict(import_filters.nextElement())
self._import_filters.append(prop2dict(import_filters.nextElement()))

return self._import_filters

def get_available_export_filters(self):
# Doing this call for some reason uses up memory each time, so we do it
# only once, and cache it here:
if self._export_filters is not None:
return self._export_filters

# List export filters. You can only search on module, iflags and eflags,
# so the import and export types we have to test in a loop
export_filters = self.filter_service.createSubSetEnumerationByQuery(
"getSortedFilterList():iflags=2"
)

self._export_filters = []
while export_filters.hasMoreElements():
# Filter DocumentService here
yield prop2dict(export_filters.nextElement())
self._export_filters.append(prop2dict(export_filters.nextElement()))

return self._export_filters

def get_filter_names(self, filters):
names = {}
Expand Down
32 changes: 16 additions & 16 deletions src/unoserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,29 @@ def signal_handler(signum, frame):
time.sleep(10)

self.xmlrcp_thread.start()

return self.libreoffice_process

def serve(self):
# Create server
with XMLRPCServer((self.interface, int(self.port)), allow_none=True) as server:
self.xmlrcp_server = server
self.conv = converter.UnoConverter(
interface=self.uno_interface, port=self.uno_port
)
self.comp = comparer.UnoComparer(
interface=self.uno_interface, port=self.uno_port
)

self.xmlrcp_server = server
server.register_introspection_functions()

@server.register_function
def info():
conv = converter.UnoConverter(
interface=self.uno_interface, port=self.uno_port
import_filters = self.conv.get_filter_names(
self.conv.get_available_import_filters()
)
import_filters = conv.get_filter_names(
conv.get_available_import_filters()
)
export_filters = conv.get_filter_names(
conv.get_available_export_filters()
export_filters = self.conv.get_filter_names(
self.conv.get_available_export_filters()
)
return {
"unoserver": __version__,
Expand All @@ -146,10 +150,8 @@ def convert(
):
if indata is not None:
indata = indata.data
conv = converter.UnoConverter(
interface=self.uno_interface, port=self.uno_port
)
result = conv.convert(

result = self.conv.convert(
inpath,
indata,
outpath,
Expand All @@ -174,10 +176,8 @@ def compare(
olddata = olddata.data
if newdata is not None:
newdata = newdata.data
comp = comparer.UnoComparer(
interface=self.uno_interface, port=self.uno_port
)
result = comp.compare(

result = self.comp.compare(
oldpath, olddata, newpath, newdata, outpath, filetype
)
return result
Expand Down

0 comments on commit 2bf8f11

Please sign in to comment.