From a27f12f3884de8c51b7f8bf6fd669d38f449360c Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Tue, 26 Sep 2006 06:33:32 +0000 Subject: [PATCH] Fixed #2265 -- Fixed problem with using iterators for "choices" attribute. Thanks, Alex Dedul. git-svn-id: http://code.djangoproject.com/svn/django/trunk@3851 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 11 +++++++++- django/utils/itercompat.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 django/utils/itercompat.py diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 3314f8ded0..d82f38527d 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -5,6 +5,7 @@ from django.core import validators from django import forms from django.core.exceptions import ObjectDoesNotExist from django.utils.functional import curry +from django.utils.itercompat import tee from django.utils.text import capfirst from django.utils.translation import gettext, gettext_lazy import datetime, os, time @@ -80,7 +81,7 @@ class Field(object): self.prepopulate_from = prepopulate_from self.unique_for_date, self.unique_for_month = unique_for_date, unique_for_month self.unique_for_year = unique_for_year - self.choices = choices or [] + self._choices = choices or [] self.radio_admin = radio_admin self.help_text = help_text self.db_column = db_column @@ -324,6 +325,14 @@ class Field(object): def bind(self, fieldmapping, original, bound_field_class): return bound_field_class(self, fieldmapping, original) + def _get_choices(self): + if hasattr(self._choices, 'next'): + choices, self._choices = tee(self._choices) + return choices + else: + return self._choices + choices = property(_get_choices) + class AutoField(Field): empty_strings_allowed = False def __init__(self, *args, **kwargs): diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py new file mode 100644 index 0000000000..f259b26139 --- /dev/null +++ b/django/utils/itercompat.py @@ -0,0 +1,31 @@ +""" +Providing iterator functions that are not in all version of Python we support. +Where possible, we try to use the system-native version and only fall back to +these implementations if necessary. +""" + +import itertools + +def compat_tee(iterable): + """Return two independent iterators from a single iterable. + + Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html + """ + # Note: Using a dictionary and a list as the default arguments here is + # deliberate and safe in this instance. + def gen(next, data={}, cnt=[0]): + dpop = data.pop + for i in count(): + if i == cnt[0]: + item = data[i] = next() + cnt[0] += 1 + else: + item = dpop(i) + yield item + next = iter(iterable).next + return gen(next), gen(next) + +if hasattr(itertools, 'tee'): + tee = itertools.tee +else: + tee = compat_tee