Opened 19 months ago

Closed 18 months ago

Last modified 18 months ago

#34088 closed Bug (fixed)

Sitemaps without items raise ValueError on callable lastmod.

Reported by: Michal Čihař Owned by: Daniel Ivanov
Component: contrib.sitemaps Version: 4.1
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When sitemap contains not items, but supports returning lastmod for an item, it fails with a ValueError:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/sitemaps/views.py", line 34, in inner
    response = func(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/sitemaps/views.py", line 76, in index
    site_lastmod = site.get_latest_lastmod()
  File "/usr/local/lib/python3.10/site-packages/django/contrib/sitemaps/__init__.py", line 170, in get_latest_lastmod
    return max([self.lastmod(item) for item in self.items()])

Exception Type: ValueError at /sitemap.xml
Exception Value: max() arg is an empty sequence

Something like this might be a solution:

     def get_latest_lastmod(self):
         if not hasattr(self, "lastmod"):
             return None
         if callable(self.lastmod):
             try:
                 return max([self.lastmod(item) for item in self.items()])
-            except TypeError:
+            except (TypeError, ValueError):
                 return None
         else:
             return self.lastmod

Change History (11)

comment:1 by Mariusz Felisiak, 19 months ago

Component: Uncategorizedcontrib.sitemaps
Summary: ValueError on sitemap without itemsSitemaps without items raise ValueError on callable lastmod.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Thanks for the report.

comment:2 by Piyush, 19 months ago

Owner: changed from nobody to Piyush
Status: newassigned

comment:3 by Adam Johnson, 18 months ago

The default argument of max() can be used.

comment:4 by Mariusz Felisiak, 18 months ago

Easy pickings: set

comment:5 by Daniel Ivanov, 18 months ago

Prepared a patch in this PR. PLMK if I should reassign the ticket to myself.

comment:6 by Daniel Ivanov, 18 months ago

Has patch: set

comment:7 by Mariusz Felisiak, 18 months ago

Owner: changed from Piyush to Daniel Ivanov

comment:8 by Mariusz Felisiak, 18 months ago

Severity: NormalRelease blocker

It's a bug in the new feature introduced in Django 4.1, see #25916 and 480191244d12fefbf95854b2b117c71ffe44749a.

comment:9 by Mariusz Felisiak, 18 months ago

Triage Stage: AcceptedReady for checkin

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 18 months ago

Resolution: fixed
Status: assignedclosed

In eca526ea:

[4.1.x] Fixed #34088 -- Fixed Sitemap.get_latest_lastmod() crash with empty items.

Bug in 480191244d12fefbf95854b2b117c71ffe44749a.

Thanks Michal Čihař for the report.

Backport of 5eab4d1924613a5506e517f157054b4852ae7dc2 from main

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 18 months ago

In 5eab4d19:

Fixed #34088 -- Fixed Sitemap.get_latest_lastmod() crash with empty items.

Bug in 480191244d12fefbf95854b2b117c71ffe44749a.

Thanks Michal Čihař for the report.

Note: See TracTickets for help on using tickets.
Back to Top