#35603: "string" in F("...") hangs
-------------------------------------+-------------------------------------
     Reporter:  Tim Graham           |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.1
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * stage:  Unreviewed => Accepted

Comment:

 The issue lies in implementing `__getitem__` without implementing
 `__contains__`, per
 [https://docs.python.org/3/reference/datamodel.html#object.__contains__
 the Python docs]

 > For objects that don’t define `__contains__()`, the membership test
 first tries iteration via `__iter__()`, **then the old sequence iteration
 protocol via `__getitem__()`, see
 [https://docs.python.org/3/reference/expressions.html#membership-test-
 details this section in the language reference]**.

 Basically what `"foo" in F("name")` translates to is the following ''old-
 style iteration protocol''

 > Lastly, the old-style iteration protocol is tried: if a class defines
 `__getitem__()`, `x in y` is `True` if and only if there is a non-negative
 integer index `i` such that `x is y[i] or x == y[i]`, and no lower integer
 index raises the `IndexError` exception.

 {{{#!python
 def __contains__(self, value):
     index = 0
     while True:
         try:
             item = ref.__getitem__(index)
         except IndexError:
             return False
         if item is value or item == value:
             return True
         index += 1
 }}}

 which explains why it hangs indefinitely.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35603#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070190b969a402-3b958796-eee2-4a0b-b7d3-4832c4d5f44d-000000%40eu-central-1.amazonses.com.

Reply via email to