django-pwa
django-pwa copied to clipboard
Django 4.2 : ResourceWarning: unclosed file serviceworker.js
When running with Django 4.2, there is a log :
/Users/pmind/.virtualenvs/myproject/lib/python3.10/site-packages/pwa/views.py:8: ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/pmind/Projects/myproject/myproject/templates/serviceworker.js' mode='r' encoding='UTF-8'>
response = HttpResponse(open(app_settings.PWA_SERVICE_WORKER_PATH).read(), content_type='application/javascript')
The source code is :
def service_worker(request):
response = HttpResponse(open(app_settings.PWA_SERVICE_WORKER_PATH).read(), content_type='application/javascript')
return response
This is a suggestion to change this :
def service_worker(request):
data = ''
try:
f = open(app_settings.PWA_SERVICE_WORKER_PATH)
data = f.read()
finally:
f.close()
response = HttpResponse(data, content_type='application/javascript')
return response
better:
def service_worker(request):
return FileResponse(open(app_settings.PWA_SERVICE_WORKER_PATH, 'rb'), content_type='application/javascript')
anyway this is also not clean. Better would be to use the file API of django as it could also handle aws and co
you're right, but it one step forward ;)
A fix will ship in the next release.