Skip to content

Commit

Permalink
Fixed a memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Regebro committed Sep 18, 2024
1 parent dc4c016 commit a57a898
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
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
22 changes: 11 additions & 11 deletions src/unoserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def signal_handler(signum, frame):
time.sleep(10)

self.xmlrcp_thread.start()

self.conv = converter.UnoConverter(
interface=self.uno_interface, port=self.uno_port
)

return self.libreoffice_process

def serve(self):
Expand All @@ -118,14 +123,11 @@ def serve(self):

@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 +148,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 Down

0 comments on commit a57a898

Please sign in to comment.