silviolleite / django-pwa

Looks like an app, feels like an app, but IS NOT an app.
MIT License
541 stars 112 forks source link

Django 4.2 : ResourceWarning: unclosed file serviceworker.js #101

Closed pulse-mind closed 7 months ago

pulse-mind commented 1 year ago

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
devkral commented 1 year ago

better:


def service_worker(request):
    return FileResponse(open(app_settings.PWA_SERVICE_WORKER_PATH, 'rb'), content_type='application/javascript')
devkral commented 1 year ago

anyway this is also not clean. Better would be to use the file API of django as it could also handle aws and co

pulse-mind commented 1 year ago

you're right, but it one step forward ;)

hartungstenio commented 7 months ago

A fix will ship in the next release.