Opened 6 years ago

Closed 6 years ago

#29698 closed Bug (fixed)

Model Field._check_choices() crashes with TypeError on some invalid values.

Reported by: Franck Michea Owned by: Franck Michea
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Model field checks ensure that the choices field contains only pairs of (real value, human readable value). To do this it unpacks the values, protecting against ValueError (example: not a pair but a three-value tuple), however it does not protect against TypeError (example: integer), which will look something like this trying to runserver:

Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10ee2f8c8>
Traceback (most recent call last):
  File ".../lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File ".../lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File ".../lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File ".../lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File ".../lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File ".../lib/python3.7/site-packages/django/core/checks/model_checks.py", line 27, in check_all_models
    errors.extend(model.check(**kwargs))
  File ".../lib/python3.7/site-packages/django/db/models/base.py", line 1183, in check
    *cls._check_fields(**kwargs),
  File ".../lib/python3.7/site-packages/django/db/models/base.py", line 1261, in _check_fields
    errors.extend(field.check(**kwargs))
  File ".../lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1770, in check
    *super().check(**kwargs),
  File ".../lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 203, in check
    *self._check_choices(),
  File ".../lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 262, in _check_choices
    group_name, group_choices = choices_group
TypeError: cannot unpack non-iterable int object

Pull request coming right up.

Change History (3)

comment:2 by Tim Graham, 6 years ago

Has patch: set
Summary: Model Field _check_choices does not handle some invalid values.Model Field._check_choices() crashes with TypeError on some invalid values.
Triage Stage: UnreviewedReady for checkin

comment:3 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 7def8bed:

Fixed #29698 -- Fixed Field._check_choices() crash on invalid choices.

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