Opened 4 months ago

Closed 3 months ago

Last modified 3 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 Changed 4 months ago by Mariusz Felisiak

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 Changed 4 months ago by Piyush

Owner: changed from nobody to Piyush
Status: newassigned

comment:3 Changed 3 months ago by Adam Johnson

The default argument of max() can be used.

comment:4 Changed 3 months ago by Mariusz Felisiak

Easy pickings: set

comment:5 Changed 3 months ago by Daniel Ivanov

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

comment:6 Changed 3 months ago by Daniel Ivanov

Has patch: set

comment:7 Changed 3 months ago by Mariusz Felisiak

Owner: changed from Piyush to Daniel Ivanov

comment:8 Changed 3 months ago by Mariusz Felisiak

Severity: NormalRelease blocker

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

comment:9 Changed 3 months ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:10 Changed 3 months ago by Mariusz Felisiak <felisiak.mariusz@…>

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 Changed 3 months ago by Mariusz Felisiak <felisiak.mariusz@…>

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