Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[py3] Fixed access to dict keys/values/items.

  • Loading branch information...
commit ee191715eae73362768184aa95206cf61bac5d38 1 parent fa3f0aa
Aymeric Augustin aaugustin authored
Showing with 187 additions and 155 deletions.
  1. +1 −1  django/conf/__init__.py
  2. +2 −2 django/contrib/admin/helpers.py
  3. +3 −3 django/contrib/admin/options.py
  4. +4 −3 django/contrib/admin/sites.py
  5. +2 −1  django/contrib/admin/templatetags/admin_list.py
  6. +3 −2 django/contrib/admindocs/views.py
  7. +2 −1  django/contrib/auth/admin.py
  8. +2 −1  django/contrib/auth/tests/forms.py
  9. +3 −2 django/contrib/contenttypes/management.py
  10. +3 −5 django/contrib/databrowse/datastructures.py
  11. +1 −1  django/contrib/databrowse/plugins/calendars.py
  12. +1 −1  django/contrib/databrowse/plugins/fieldchoices.py
  13. +4 −3 django/contrib/formtools/wizard/storage/base.py
  14. +4 −4 django/contrib/formtools/wizard/views.py
  15. +3 −1 django/contrib/gis/db/backends/mysql/operations.py
  16. +1 −1  django/contrib/gis/db/backends/oracle/operations.py
  17. +2 −2 django/contrib/gis/db/backends/postgis/operations.py
  18. +1 −1  django/contrib/gis/db/backends/spatialite/operations.py
  19. +3 −2 django/contrib/gis/db/models/query.py
  20. +3 −2 django/contrib/gis/db/models/sql/compiler.py
  21. +2 −1  django/contrib/gis/geometry/test_data.py
  22. +1 −1  django/contrib/gis/measure.py
  23. +2 −1  django/contrib/gis/sitemaps/views.py
  24. +2 −1  django/contrib/messages/storage/cookie.py
  25. +10 −9 django/contrib/sessions/tests.py
  26. +2 −1  django/contrib/sitemaps/views.py
  27. +2 −1  django/contrib/staticfiles/finders.py
  28. +3 −2 django/core/management/__init__.py
  29. +1 −3 django/core/management/commands/diffsettings.py
  30. +3 −2 django/core/serializers/__init__.py
  31. +2 −1  django/core/serializers/python.py
  32. +1 −1  django/core/urlresolvers.py
  33. +1 −1  django/core/validators.py
  34. +2 −1  django/db/backends/mysql/introspection.py
  35. +2 −2 django/db/models/base.py
  36. +13 −12 django/db/models/deletion.py
  37. +4 −4 django/db/models/fields/related.py
  38. +3 −2 django/db/models/loading.py
  39. +8 −9 django/db/models/options.py
  40. +10 −10 django/db/models/query.py
  41. +3 −2 django/db/models/query_utils.py
  42. +4 −3 django/db/models/sql/compiler.py
  43. +13 −12 django/db/models/sql/query.py
  44. +3 −2 django/db/models/sql/subqueries.py
  45. +1 −1  django/forms/extras/widgets.py
  46. +3 −3 django/forms/forms.py
  47. +2 −1  django/forms/models.py
  48. +1 −2  django/forms/widgets.py
  49. +1 −1  django/template/base.py
  50. +3 −2 django/template/defaulttags.py
  51. +3 −2 django/template/loader_tags.py
  52. +1 −1  django/templatetags/i18n.py
  53. +4 −3 django/utils/dateparse.py
  54. +1 −1  django/utils/dictconfig.py
  55. +1 −1  django/utils/functional.py
  56. +1 −1  django/utils/html.py
  57. +3 −1 django/utils/termcolors.py
  58. +1 −1  django/views/debug.py
  59. +2 −1  django/views/generic/base.py
  60. +5 −4 tests/modeltests/timezones/tests.py
  61. +2 −1  tests/regressiontests/aggregation_regress/tests.py
  62. +2 −1  tests/regressiontests/db_typecasts/tests.py
  63. +3 −3 tests/regressiontests/templates/templatetags/custom.py
  64. +2 −1  tests/regressiontests/templates/tests.py
2  django/conf/__init__.py
View
@@ -158,7 +158,7 @@ def __getattr__(self, name):
return getattr(self.default_settings, name)
def __dir__(self):
- return self.__dict__.keys() + dir(self.default_settings)
+ return list(six.iterkeys(self.__dict__)) + dir(self.default_settings)
# For Python < 2.6:
__members__ = property(lambda self: self.__dir__())
4 django/contrib/admin/helpers.py
View
@@ -325,11 +325,11 @@ class AdminErrorList(forms.util.ErrorList):
"""
def __init__(self, form, inline_formsets):
if form.is_bound:
- self.extend(form.errors.values())
+ self.extend(list(six.itervalues(form.errors)))
for inline_formset in inline_formsets:
self.extend(inline_formset.non_form_errors())
for errors_in_inline_form in inline_formset.errors:
- self.extend(errors_in_inline_form.values())
+ self.extend(list(six.itervalues(errors_in_inline_form)))
def normalize_fieldsets(fieldsets):
"""
6 django/contrib/admin/options.py
View
@@ -425,7 +425,7 @@ def get_fieldsets(self, request, obj=None):
if self.declared_fieldsets:
return self.declared_fieldsets
form = self.get_form(request, obj)
- fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj))
+ fields = list(six.iterkeys(form.base_fields)) + list(self.get_readonly_fields(request, obj))
return [(None, {'fields': fields})]
def get_form(self, request, obj=None, **kwargs):
@@ -608,7 +608,7 @@ def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
tuple (name, description).
"""
choices = [] + default_choices
- for func, name, description in self.get_actions(request).itervalues():
+ for func, name, description in six.itervalues(self.get_actions(request)):
choice = (name, description % model_format_dict(self.opts))
choices.append(choice)
return choices
@@ -1415,7 +1415,7 @@ def get_fieldsets(self, request, obj=None):
if self.declared_fieldsets:
return self.declared_fieldsets
form = self.get_formset(request, obj).form
- fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj))
+ fields = list(six.iterkeys(form.base_fields)) + list(self.get_readonly_fields(request, obj))
return [(None, {'fields': fields})]
def queryset(self, request):
7 django/contrib/admin/sites.py
View
@@ -10,6 +10,7 @@
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template.response import TemplateResponse
from django.utils.safestring import mark_safe
+from django.utils import six
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache
@@ -133,7 +134,7 @@ def actions(self):
"""
Get all the enabled actions as an iterable of (name, func).
"""
- return self._actions.iteritems()
+ return six.iteritems(self._actions)
def has_permission(self, request):
"""
@@ -239,7 +240,7 @@ def wrapper(*args, **kwargs):
)
# Add in each model's views.
- for model, model_admin in self._registry.iteritems():
+ for model, model_admin in six.iteritems(self._registry):
urlpatterns += patterns('',
url(r'^%s/%s/' % (model._meta.app_label, model._meta.module_name),
include(model_admin.urls))
@@ -370,7 +371,7 @@ def index(self, request, extra_context=None):
}
# Sort the apps alphabetically.
- app_list = app_dict.values()
+ app_list = list(six.itervalues(app_dict))
app_list.sort(key=lambda x: x['name'])
# Sort the models alphabetically within each app.
3  django/contrib/admin/templatetags/admin_list.py
View
@@ -12,6 +12,7 @@
from django.utils import formats
from django.utils.html import format_html
from django.utils.safestring import mark_safe
+from django.utils import six
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
from django.utils.encoding import smart_unicode, force_unicode
@@ -125,7 +126,7 @@ def result_headers(cl):
if i in ordering_field_columns:
sorted = True
order_type = ordering_field_columns.get(i).lower()
- sort_priority = ordering_field_columns.keys().index(i) + 1
+ sort_priority = list(six.iterkeys(ordering_field_columns)).index(i) + 1
th_classes.append('sorted %sending' % order_type)
new_order_type = {'asc': 'desc', 'desc': 'asc'}[order_type]
5 django/contrib/admindocs/views.py
View
@@ -14,6 +14,7 @@
from django.contrib.admindocs import utils
from django.contrib.sites.models import Site
from django.utils.importlib import import_module
+from django.utils import six
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
@@ -48,7 +49,7 @@ def template_tag_index(request):
load_all_installed_template_libraries()
tags = []
- app_libs = template.libraries.items()
+ app_libs = list(six.iteritems(template.libraries))
builtin_libs = [(None, lib) for lib in template.builtins]
for module_name, library in builtin_libs + app_libs:
for tag_name, tag_func in library.tags.items():
@@ -83,7 +84,7 @@ def template_filter_index(request):
load_all_installed_template_libraries()
filters = []
- app_libs = template.libraries.items()
+ app_libs = list(six.iteritems(template.libraries))
builtin_libs = [(None, lib) for lib in template.builtins]
for module_name, library in builtin_libs + app_libs:
for filter_name, filter_func in library.filters.items():
3  django/contrib/auth/admin.py
View
@@ -12,6 +12,7 @@
from django.utils.html import escape
from django.utils.decorators import method_decorator
from django.utils.safestring import mark_safe
+from django.utils import six
from django.utils.translation import ugettext, ugettext_lazy as _
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.debug import sensitive_post_parameters
@@ -128,7 +129,7 @@ def user_change_password(self, request, id, form_url=''):
else:
form = self.change_password_form(user)
- fieldsets = [(None, {'fields': form.base_fields.keys()})]
+ fieldsets = [(None, {'fields': list(six.iterkeys(form.base_fields))})]
adminForm = admin.helpers.AdminForm(form, fieldsets, {})
context = {
3  django/contrib/auth/tests/forms.py
View
@@ -9,6 +9,7 @@
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.encoding import force_unicode
+from django.utils import six
from django.utils import translation
from django.utils.translation import ugettext as _
@@ -203,7 +204,7 @@ def test_success(self):
def test_field_order(self):
# Regression test - check the order of fields:
user = User.objects.get(username='testclient')
- self.assertEqual(PasswordChangeForm(user, {}).fields.keys(),
+ self.assertEqual(list(six.iterkeys(PasswordChangeForm(user, {}).fields)),
['old_password', 'new_password1', 'new_password2'])
5 django/contrib/contenttypes/management.py
View
@@ -1,6 +1,7 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import get_apps, get_models, signals
from django.utils.encoding import smart_unicode
+from django.utils import six
def update_contenttypes(app, created_models, verbosity=2, **kwargs):
"""
@@ -24,7 +25,7 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs):
)
to_remove = [
ct
- for (model_name, ct) in content_types.iteritems()
+ for (model_name, ct) in six.iteritems(content_types)
if model_name not in app_models
]
@@ -34,7 +35,7 @@ def update_contenttypes(app, created_models, verbosity=2, **kwargs):
app_label=app_label,
model=model_name,
)
- for (model_name, model) in app_models.iteritems()
+ for (model_name, model) in six.iteritems(app_models)
if model_name not in content_types
])
if verbosity >= 2:
8 django/contrib/databrowse/datastructures.py
View
@@ -17,7 +17,7 @@ class EasyModel(object):
def __init__(self, site, model):
self.site = site
self.model = model
- self.model_list = site.registry.keys()
+ self.model_list = list(site.registry.keys())
self.verbose_name = model._meta.verbose_name
self.verbose_name_plural = model._meta.verbose_name_plural
@@ -176,8 +176,6 @@ def urls(self):
for plugin_name, plugin in self.model.model_databrowse().plugins.items():
urls = plugin.urls(plugin_name, self)
if urls is not None:
- #plugin_urls.append(urls)
- values = self.values()
return zip(self.values(), urls)
if self.field.rel:
m = EasyModel(self.model.site, self.field.rel.to)
@@ -196,10 +194,10 @@ def urls(self):
url = '%s%s/%s/fields/%s/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.field.name, iri_to_uri(self.raw_value))
lst.append((value, url))
elif isinstance(self.field, models.URLField):
- val = self.values()[0]
+ val = list(self.values())[0]
lst = [(val, iri_to_uri(val))]
else:
- lst = [(self.values()[0], None)]
+ lst = [(list(self.values())[0], None)]
return lst
class EasyQuerySet(QuerySet):
2  django/contrib/databrowse/plugins/calendars.py
View
@@ -96,7 +96,7 @@ def model_view(self, request, model_databrowse, url):
def homepage_view(self, request):
easy_model = EasyModel(self.site, self.model)
- field_list = self.fields.values()
+ field_list = list(self.fields.values())
field_list.sort(key=lambda k:k.verbose_name)
return render_to_response('databrowse/calendar_homepage.html', {
'root_url': self.site.root_url,
2  django/contrib/databrowse/plugins/fieldchoices.py
View
@@ -63,7 +63,7 @@ def model_view(self, request, model_databrowse, url):
def homepage_view(self, request):
easy_model = EasyModel(self.site, self.model)
- field_list = self.fields.values()
+ field_list = list(self.fields.values())
field_list.sort(key=lambda k: k.verbose_name)
return render_to_response('databrowse/fieldchoice_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list})
7 django/contrib/formtools/wizard/storage/base.py
View
@@ -2,6 +2,7 @@
from django.utils.datastructures import MultiValueDict
from django.utils.encoding import smart_str
from django.utils.functional import lazy_property
+from django.utils import six
from django.contrib.formtools.wizard.storage.exceptions import NoFileStorageConfigured
@@ -72,9 +73,9 @@ def get_step_files(self, step):
raise NoFileStorageConfigured
files = {}
- for field, field_dict in wizard_files.iteritems():
+ for field, field_dict in six.iteritems(wizard_files):
field_dict = dict((smart_str(k), v)
- for k, v in field_dict.iteritems())
+ for k, v in six.iteritems(field_dict))
tmp_name = field_dict.pop('tmp_name')
files[field] = UploadedFile(
file=self.file_storage.open(tmp_name), **field_dict)
@@ -87,7 +88,7 @@ def set_step_files(self, step, files):
if step not in self.data[self.step_files_key]:
self.data[self.step_files_key][step] = {}
- for field, field_file in (files or {}).iteritems():
+ for field, field_file in six.iteritems(files or {}):
tmp_filename = self.file_storage.save(field_file.name, field_file)
file_dict = {
'tmp_name': tmp_filename,
8 django/contrib/formtools/wizard/views.py
View
@@ -44,7 +44,7 @@ def __repr__(self):
@property
def all(self):
"Returns the names of all steps/forms."
- return self._wizard.get_form_list().keys()
+ return list(six.iterkeys(self._wizard.get_form_list()))
@property
def count(self):
@@ -164,14 +164,14 @@ def get_initkwargs(cls, form_list, initial_dict=None,
init_form_list[six.text_type(i)] = form
# walk through the new created list of forms
- for form in init_form_list.itervalues():
+ for form in six.itervalues(init_form_list):
if issubclass(form, formsets.BaseFormSet):
# if the element is based on BaseFormSet (FormSet/ModelFormSet)
# we need to override the form variable.
form = form.form
# check if any form contains a FileField, if yes, we need a
# file_storage added to the wizardview (by subclassing).
- for field in form.base_fields.itervalues():
+ for field in six.itervalues(form.base_fields):
if (isinstance(field, forms.FileField) and
not hasattr(cls, 'file_storage')):
raise NoFileStorageConfigured
@@ -196,7 +196,7 @@ def get_form_list(self):
could use data from other (maybe previous forms).
"""
form_list = SortedDict()
- for form_key, form_class in self.form_list.iteritems():
+ for form_key, form_class in six.iteritems(self.form_list):
# try to fetch the value from condition list, by default, the form
# gets passed to the new list.
condition = self.condition_dict.get(form_key, True)
4 django/contrib/gis/db/backends/mysql/operations.py
View
@@ -3,6 +3,8 @@
from django.contrib.gis.db.backends.adapter import WKTAdapter
from django.contrib.gis.db.backends.base import BaseSpatialOperations
+from django.utils import six
+
class MySQLOperations(DatabaseOperations, BaseSpatialOperations):
compiler_module = 'django.contrib.gis.db.backends.mysql.compiler'
@@ -30,7 +32,7 @@ class MySQLOperations(DatabaseOperations, BaseSpatialOperations):
'within' : 'MBRWithin',
}
- gis_terms = dict([(term, None) for term in geometry_functions.keys() + ['isnull']])
+ gis_terms = dict([(term, None) for term in list(six.iterkeys(geometry_functions)) + ['isnull']])
def geo_db_type(self, f):
return f.geom_type
2  django/contrib/gis/db/backends/oracle/operations.py
View
@@ -128,7 +128,7 @@ class OracleOperations(DatabaseOperations, BaseSpatialOperations):
geometry_functions.update(distance_functions)
gis_terms = ['isnull']
- gis_terms += geometry_functions.keys()
+ gis_terms += list(six.iterkeys(geometry_functions))
gis_terms = dict([(term, None) for term in gis_terms])
truncate_params = {'relate' : None}
4 django/contrib/gis/db/backends/postgis/operations.py
View
@@ -217,8 +217,8 @@ def get_dist_ops(operator):
# Creating a dictionary lookup of all GIS terms for PostGIS.
gis_terms = ['isnull']
- gis_terms += self.geometry_operators.keys()
- gis_terms += self.geometry_functions.keys()
+ gis_terms += list(six.iterkeys(self.geometry_operators))
+ gis_terms += list(six.iterkeys(self.geometry_functions))
self.gis_terms = dict([(term, None) for term in gis_terms])
self.area = prefix + 'Area'
2  django/contrib/gis/db/backends/spatialite/operations.py
View
@@ -131,7 +131,7 @@ def __init__(self, connection):
# Creating the GIS terms dictionary.
gis_terms = ['isnull']
- gis_terms += self.geometry_functions.keys()
+ gis_terms += list(six.iterkeys(self.geometry_functions))
self.gis_terms = dict([(term, None) for term in gis_terms])
if version >= (2, 4, 0):
5 django/contrib/gis/db/models/query.py
View
@@ -1,5 +1,6 @@
from django.db import connections
from django.db.models.query import QuerySet, ValuesQuerySet, ValuesListQuerySet
+from django.utils import six
from django.contrib.gis.db.models import aggregates
from django.contrib.gis.db.models.fields import get_srid_info, PointField, LineStringField
@@ -25,7 +26,7 @@ def values_list(self, *fields, **kwargs):
flat = kwargs.pop('flat', False)
if kwargs:
raise TypeError('Unexpected keyword arguments to values_list: %s'
- % (kwargs.keys(),))
+ % (list(six.iterkeys(kwargs)),))
if flat and len(fields) > 1:
raise TypeError("'flat' is not valid when values_list is called with more than one field.")
return self._clone(klass=GeoValuesListQuerySet, setup=True, flat=flat,
@@ -531,7 +532,7 @@ def _spatial_attribute(self, att, settings, field_name=None, model_att=None):
if settings.get('setup', True):
default_args, geo_field = self._spatial_setup(att, desc=settings['desc'], field_name=field_name,
geo_field_type=settings.get('geo_field_type', None))
- for k, v in default_args.iteritems(): settings['procedure_args'].setdefault(k, v)
+ for k, v in six.iteritems(default_args): settings['procedure_args'].setdefault(k, v)
else:
geo_field = settings['geo_field']
5 django/contrib/gis/db/models/sql/compiler.py
View
@@ -3,6 +3,7 @@
from django.db.backends.util import truncate_name, typecast_timestamp
from django.db.models.sql import compiler
from django.db.models.sql.constants import MULTI
+from django.utils import six
SQLCompiler = compiler.SQLCompiler
@@ -24,7 +25,7 @@ def get_columns(self, with_aliases=False):
qn = self.quote_name_unless_alias
qn2 = self.connection.ops.quote_name
result = ['(%s) AS %s' % (self.get_extra_select_format(alias) % col[0], qn2(alias))
- for alias, col in self.query.extra_select.iteritems()]
+ for alias, col in six.iteritems(self.query.extra_select)]
aliases = set(self.query.extra_select.keys())
if with_aliases:
col_aliases = aliases.copy()
@@ -170,7 +171,7 @@ def resolve_columns(self, row, fields=()):
objects.
"""
values = []
- aliases = self.query.extra_select.keys()
+ aliases = list(six.iterkeys(self.query.extra_select))
# Have to set a starting row number offset that is used for
# determining the correct starting row index -- needed for
3  django/contrib/gis/geometry/test_data.py
View
@@ -7,6 +7,7 @@
import os
from django.contrib import gis
+from django.utils import six
# This global used to store reference geometry data.
@@ -25,7 +26,7 @@ def tuplize(seq):
def strconvert(d):
"Converts all keys in dictionary to str type."
- return dict([(str(k), v) for k, v in d.iteritems()])
+ return dict([(str(k), v) for k, v in six.iteritems(d)])
def get_ds_file(name, ext):
2  django/contrib/gis/measure.py
View
@@ -169,7 +169,7 @@ def default_units(self, kwargs):
"""
val = 0.0
default_unit = self.STANDARD_UNIT
- for unit, value in kwargs.iteritems():
+ for unit, value in six.iteritems(kwargs):
if not isinstance(value, float): value = float(value)
if unit in self.UNITS:
val += self.UNITS[unit] * value
3  django/contrib/gis/sitemaps/views.py
View
@@ -9,6 +9,7 @@
from django.db import connections, DEFAULT_DB_ALIAS
from django.db.models import get_model
from django.utils.encoding import smart_str
+from django.utils import six
from django.utils.translation import ugettext as _
from django.contrib.gis.shortcuts import render_to_kml, render_to_kmz
@@ -46,7 +47,7 @@ def sitemap(request, sitemaps, section=None):
raise Http404(_("No sitemap available for section: %r") % section)
maps.append(sitemaps[section])
else:
- maps = sitemaps.values()
+ maps = list(six.itervalues(sitemaps))
page = request.GET.get("p", 1)
current_site = get_current_site(request)
3  django/contrib/messages/storage/cookie.py
View
@@ -4,6 +4,7 @@
from django.contrib.messages.storage.base import BaseStorage, Message
from django.http import SimpleCookie
from django.utils.crypto import salted_hmac, constant_time_compare
+from django.utils import six
class MessageEncoder(json.JSONEncoder):
@@ -33,7 +34,7 @@ def process_messages(self, obj):
return [self.process_messages(item) for item in obj]
if isinstance(obj, dict):
return dict([(key, self.process_messages(value))
- for key, value in obj.iteritems()])
+ for key, value in six.iteritems(obj)])
return obj
def decode(self, s, **kwargs):
19 django/contrib/sessions/tests.py
View
@@ -16,6 +16,7 @@
from django.http import HttpResponse
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
+from django.utils import six
from django.utils import timezone
from django.utils import unittest
@@ -86,16 +87,16 @@ def test_has_key(self):
self.assertFalse(self.session.modified)
def test_values(self):
- self.assertEqual(self.session.values(), [])
+ self.assertEqual(list(self.session.values()), [])
self.assertTrue(self.session.accessed)
self.session['some key'] = 1
- self.assertEqual(self.session.values(), [1])
+ self.assertEqual(list(self.session.values()), [1])
def test_iterkeys(self):
self.session['x'] = 1
self.session.modified = False
self.session.accessed = False
- i = self.session.iterkeys()
+ i = six.iterkeys(self.session)
self.assertTrue(hasattr(i, '__iter__'))
self.assertTrue(self.session.accessed)
self.assertFalse(self.session.modified)
@@ -105,7 +106,7 @@ def test_itervalues(self):
self.session['x'] = 1
self.session.modified = False
self.session.accessed = False
- i = self.session.itervalues()
+ i = six.itervalues(self.session)
self.assertTrue(hasattr(i, '__iter__'))
self.assertTrue(self.session.accessed)
self.assertFalse(self.session.modified)
@@ -115,7 +116,7 @@ def test_iteritems(self):
self.session['x'] = 1
self.session.modified = False
self.session.accessed = False
- i = self.session.iteritems()
+ i = six.iteritems(self.session)
self.assertTrue(hasattr(i, '__iter__'))
self.assertTrue(self.session.accessed)
self.assertFalse(self.session.modified)
@@ -125,9 +126,9 @@ def test_clear(self):
self.session['x'] = 1
self.session.modified = False
self.session.accessed = False
- self.assertEqual(self.session.items(), [('x', 1)])
+ self.assertEqual(list(self.session.items()), [('x', 1)])
self.session.clear()
- self.assertEqual(self.session.items(), [])
+ self.assertEqual(list(self.session.items()), [])
self.assertTrue(self.session.accessed)
self.assertTrue(self.session.modified)
@@ -154,10 +155,10 @@ def test_cycle(self):
self.session['a'], self.session['b'] = 'c', 'd'
self.session.save()
prev_key = self.session.session_key
- prev_data = self.session.items()
+ prev_data = list(self.session.items())
self.session.cycle_key()
self.assertNotEqual(self.session.session_key, prev_key)
- self.assertEqual(self.session.items(), prev_data)
+ self.assertEqual(list(self.session.items()), prev_data)
def test_invalid_key(self):
# Submitting an invalid session key (either by guessing, or if the db has
3  django/contrib/sitemaps/views.py
View
@@ -3,6 +3,7 @@
from django.core.paginator import EmptyPage, PageNotAnInteger
from django.http import Http404
from django.template.response import TemplateResponse
+from django.utils import six
def index(request, sitemaps,
template_name='sitemap_index.xml', mimetype='application/xml',
@@ -35,7 +36,7 @@ def sitemap(request, sitemaps, section=None,
raise Http404("No sitemap available for section: %r" % section)
maps = [sitemaps[section]]
else:
- maps = sitemaps.values()
+ maps = list(six.itervalues(sitemaps))
page = request.GET.get("p", 1)
urls = []
3  django/contrib/staticfiles/finders.py
View
@@ -6,6 +6,7 @@
from django.utils.functional import empty, memoize, LazyObject
from django.utils.importlib import import_module
from django.utils._os import safe_join
+from django.utils import six
from django.contrib.staticfiles import utils
from django.contrib.staticfiles.storage import AppStaticStorage
@@ -132,7 +133,7 @@ def list(self, ignore_patterns):
"""
List all files in all app storages.
"""
- for storage in self.storages.itervalues():
+ for storage in six.itervalues(self.storages):
if storage.exists(''): # check if storage location exists
for path in utils.get_files(storage, ignore_patterns):
yield path, storage
5 django/core/management/__init__.py
View
@@ -8,6 +8,7 @@
from django.core.management.base import BaseCommand, CommandError, handle_default_options
from django.core.management.color import color_style
from django.utils.importlib import import_module
+from django.utils import six
# For backwards compatibility: get_version() used to be in this module.
from django import get_version
@@ -228,7 +229,7 @@ def main_help_text(self, commands_only=False):
"Available subcommands:",
]
commands_dict = collections.defaultdict(lambda: [])
- for name, app in get_commands().iteritems():
+ for name, app in six.iteritems(get_commands()):
if app == 'django.core':
app = 'django'
else:
@@ -294,7 +295,7 @@ def autocomplete(self):
except IndexError:
curr = ''
- subcommands = get_commands().keys() + ['help']
+ subcommands = list(six.iterkeys(get_commands())) + ['help']
options = [('--help', None)]
# subcommand
4 django/core/management/commands/diffsettings.py
View
@@ -22,9 +22,7 @@ def handle_noargs(self, **options):
default_settings = module_to_dict(global_settings)
output = []
- keys = user_settings.keys()
- keys.sort()
- for key in keys:
+ for key in sorted(user_settings.keys()):
if key not in default_settings:
output.append("%s = %s ###" % (key, user_settings[key]))
elif user_settings[key] != default_settings[key]:
5 django/core/serializers/__init__.py
View
@@ -18,6 +18,7 @@
from django.conf import settings
from django.utils import importlib
+from django.utils import six
from django.core.serializers.base import SerializerDoesNotExist
# Built-in serializers
@@ -75,12 +76,12 @@ def get_serializer(format):
def get_serializer_formats():
if not _serializers:
_load_serializers()
- return _serializers.keys()
+ return list(six.iterkeys(_serializers))
def get_public_serializer_formats():
if not _serializers:
_load_serializers()
- return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only]
+ return [k for k, v in six.iteritems(_serializers) if not v.Serializer.internal_use_only]
def get_deserializer(format):
if not _serializers:
3  django/core/serializers/python.py
View
@@ -9,6 +9,7 @@
from django.core.serializers import base
from django.db import models, DEFAULT_DB_ALIAS
from django.utils.encoding import smart_unicode, is_protected_type
+from django.utils import six
class Serializer(base.Serializer):
"""
@@ -87,7 +88,7 @@ def Deserializer(object_list, **options):
m2m_data = {}
# Handle each field
- for (field_name, field_value) in d["fields"].iteritems():
+ for (field_name, field_value) in six.iteritems(d["fields"]):
if isinstance(field_value, str):
field_value = smart_unicode(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True)
2  django/core/urlresolvers.py
View
@@ -376,7 +376,7 @@ def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs):
unicode_args = [force_unicode(val) for val in args]
candidate = (prefix_norm + result) % dict(zip(prefix_args + params, unicode_args))
else:
- if set(kwargs.keys() + defaults.keys()) != set(params + defaults.keys() + prefix_args):
+ if set(kwargs.keys()) | set(defaults.keys()) != set(params) | set(defaults.keys()) | set(prefix_args):
continue
matches = True
for k, v in defaults.items():
2  django/core/validators.py
View
@@ -138,7 +138,7 @@ def ip_address_validators(protocol, unpack_ipv4):
return ip_address_validator_map[protocol.lower()]
except KeyError:
raise ValueError("The protocol '%s' is unknown. Supported: %s"
- % (protocol, ip_address_validator_map.keys()))
+ % (protocol, list(six.iterkeys(ip_address_validator_map))))
comma_separated_int_list_re = re.compile('^[\d,]+$')
validate_comma_separated_integer_list = RegexValidator(comma_separated_int_list_re, _('Enter only digits separated by commas.'), 'invalid')
3  django/db/backends/mysql/introspection.py
View
@@ -1,4 +1,5 @@
from django.db.backends import BaseDatabaseIntrospection
+from django.utils import six
from MySQLdb import ProgrammingError, OperationalError
from MySQLdb.constants import FIELD_TYPE
import re
@@ -79,7 +80,7 @@ def get_primary_key_column(self, cursor, table_name):
"""
Returns the name of the primary key column for the given table
"""
- for column in self.get_indexes(cursor, table_name).iteritems():
+ for column in six.iteritems(self.get_indexes(cursor, table_name)):
if column[1]['primary_key']:
return column[0]
return None
4 django/db/models/base.py
View
@@ -364,14 +364,14 @@ def __init__(self, *args, **kwargs):
setattr(self, field.attname, val)
if kwargs:
- for prop in kwargs.keys():
+ for prop in list(six.iterkeys(kwargs)):
try:
if isinstance(getattr(self.__class__, prop), property):
setattr(self, prop, kwargs.pop(prop))
except AttributeError:
pass
if kwargs:
- raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
+ raise TypeError("'%s' is an invalid keyword argument for this function" % list(six.iterkeys(kwargs))[0])
super(Model, self).__init__()
signals.post_init.send(sender=self.__class__, instance=self)
25 django/db/models/deletion.py
View
@@ -4,6 +4,7 @@
from django.db import connections, transaction, IntegrityError
from django.db.models import signals, sql
from django.utils.datastructures import SortedDict
+from django.utils import six
class ProtectedError(IntegrityError):
@@ -157,7 +158,7 @@ def collect(self, objs, source=None, nullable=False, collect_related=True,
# Recursively collect concrete model's parent models, but not their
# related objects. These will be found by meta.get_all_related_objects()
concrete_model = model._meta.concrete_model
- for ptr in concrete_model._meta.parents.itervalues():
+ for ptr in six.itervalues(concrete_model._meta.parents):
if ptr:
parent_objs = [getattr(obj, ptr.name) for obj in new_objs]
self.collect(parent_objs, source=model,
@@ -199,14 +200,14 @@ def related_objects(self, related, objs):
)
def instances_with_model(self):
- for model, instances in self.data.iteritems():
+ for model, instances in six.iteritems(self.data):
for obj in instances:
yield model, obj
def sort(self):
sorted_models = []
concrete_models = set()
- models = self.data.keys()
+ models = list(six.iterkeys(self.data))
while len(sorted_models) < len(models):
found = False
for model in models:
@@ -241,24 +242,24 @@ def delete(self):
)
# update fields
- for model, instances_for_fieldvalues in self.field_updates.iteritems():
+ for model, instances_for_fieldvalues in six.iteritems(self.field_updates):
query = sql.UpdateQuery(model)
- for (field, value), instances in instances_for_fieldvalues.iteritems():
+ for (field, value), instances in six.iteritems(instances_for_fieldvalues):
query.update_batch([obj.pk for obj in instances],
{field.name: value}, self.using)
# reverse instance collections
- for instances in self.data.itervalues():
+ for instances in six.itervalues(self.data):
instances.reverse()
# delete batches
- for model, batches in self.batches.iteritems():
+ for model, batches in six.iteritems(self.batches):
query = sql.DeleteQuery(model)
- for field, instances in batches.iteritems():
+ for field, instances in six.iteritems(batches):
query.delete_batch([obj.pk for obj in instances], self.using, field)
# delete instances
- for model, instances in self.data.iteritems():
+ for model, instances in six.iteritems(self.data):
query = sql.DeleteQuery(model)
pk_list = [obj.pk for obj in instances]
query.delete_batch(pk_list, self.using)
@@ -271,10 +272,10 @@ def delete(self):
)
# update collected instances
- for model, instances_for_fieldvalues in self.field_updates.iteritems():
- for (field, value), instances in instances_for_fieldvalues.iteritems():
+ for model, instances_for_fieldvalues in six.iteritems(self.field_updates):
+ for (field, value), instances in six.iteritems(instances_for_fieldvalues):
for obj in instances:
setattr(obj, field.attname, value)
- for model, instances in self.data.iteritems():
+ for model, instances in six.iteritems(self.data):
for instance in instances:
setattr(instance, model._meta.pk.attname, None)
8 django/db/models/fields/related.py
View
@@ -241,7 +241,7 @@ def get_prefetch_query_set(self, instances):
rel_obj_attr = attrgetter(self.related.field.attname)
instance_attr = lambda obj: obj._get_pk_val()
instances_dict = dict((instance_attr(inst), inst) for inst in instances)
- params = {'%s__pk__in' % self.related.field.name: instances_dict.keys()}
+ params = {'%s__pk__in' % self.related.field.name: list(six.iterkeys(instances_dict))}
qs = self.get_query_set(instance=instances[0]).filter(**params)
# Since we're going to assign directly in the cache,
# we must manage the reverse relation cache manually.
@@ -335,9 +335,9 @@ def get_prefetch_query_set(self, instances):
instance_attr = attrgetter(self.field.attname)
instances_dict = dict((instance_attr(inst), inst) for inst in instances)
if other_field.rel:
- params = {'%s__pk__in' % self.field.rel.field_name: instances_dict.keys()}
+ params = {'%s__pk__in' % self.field.rel.field_name: list(six.iterkeys(instances_dict))}
else:
- params = {'%s__in' % self.field.rel.field_name: instances_dict.keys()}
+ params = {'%s__in' % self.field.rel.field_name: list(six.iterkeys(instances_dict))}
qs = self.get_query_set(instance=instances[0]).filter(**params)
# Since we're going to assign directly in the cache,
# we must manage the reverse relation cache manually.
@@ -488,7 +488,7 @@ def get_prefetch_query_set(self, instances):
instance_attr = attrgetter(attname)
instances_dict = dict((instance_attr(inst), inst) for inst in instances)
db = self._db or router.db_for_read(self.model, instance=instances[0])
- query = {'%s__%s__in' % (rel_field.name, attname): instances_dict.keys()}
+ query = {'%s__%s__in' % (rel_field.name, attname): list(six.iterkeys(instances_dict))}
qs = super(RelatedManager, self).get_query_set().using(db).filter(**query)
# Since we just bypassed this class' get_query_set(), we must manage
# the reverse relation manually.
5 django/db/models/loading.py
View
@@ -5,6 +5,7 @@
from django.utils.datastructures import SortedDict
from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule
+from django.utils import six
import imp
import sys
@@ -193,9 +194,9 @@ def get_models(self, app_mod=None,
else:
if only_installed:
app_list = [self.app_models.get(app_label, SortedDict())
- for app_label in self.app_labels.iterkeys()]
+ for app_label in six.iterkeys(self.app_labels)]
else:
- app_list = self.app_models.itervalues()
+ app_list = six.itervalues(self.app_models)
model_list = []
for app in app_list:
model_list.extend(
17 django/db/models/options.py
View
@@ -127,7 +127,7 @@ def _prepare(self, model):
if self.parents:
# Promote the first parent link in lieu of adding yet another
# field.
- field = next(self.parents.itervalues())
+ field = next(six.itervalues(self.parents))
# Look for a local field with the same name as the
# first parent link. If a local field has already been
# created, use it instead of promoting the parent
@@ -147,13 +147,13 @@ def _prepare(self, model):
# self.duplicate_targets will map each duplicate field column to the
# columns it duplicates.
collections = {}
- for column, target in self.duplicate_targets.iteritems():
+ for column, target in six.iteritems(self.duplicate_targets):
try:
collections[target].add(column)
except KeyError:
collections[target] = set([column])
self.duplicate_targets = {}
- for elt in collections.itervalues():
+ for elt in six.itervalues(collections):
if len(elt) == 1:
continue
for column in elt:
@@ -258,7 +258,7 @@ def _many_to_many(self):
self._m2m_cache
except AttributeError:
self._fill_m2m_cache()
- return self._m2m_cache.keys()
+ return list(six.iterkeys(self._m2m_cache))
many_to_many = property(_many_to_many)
def get_m2m_with_model(self):
@@ -269,7 +269,7 @@ def get_m2m_with_model(self):
self._m2m_cache
except AttributeError:
self._fill_m2m_cache()
- return self._m2m_cache.items()
+ return list(six.iteritems(self._m2m_cache))
Simon Charette Collaborator

This changed the return type of get_m2m_with_model from tuple to list. I know it's a private API but it's a bit inconsistent with the get_fields_with_model which still returns a tuple. I stumbled on this regression when trying to concatenate both doing get_fields_with_models() + get_m2m_with_models(). This used to work but is broken as of this commit.

Aymeric Augustin Owner

Would you mind creating a ticket in Trac to discuss this backwards incompatibility?

Is the fix just s/list/tuple/?

Thanks!

Simon Charette Collaborator

Nevermind, get_m2m_with_models() always returned a list, was just surprised it returns a list when get_fields_with_models returns a tuple.

I guess that's what you get when playing with internal API.

Aymeric Augustin Owner

We tend to be skeptical of tickets about internal APIs, but you can file one anyway if you have some arguments against this inconsistency.

Simon Charette Collaborator

I don't think my consistency arguments is worth a possible break of backward compatibility, there might be a couple of projects relying on this in the wild.

Thanks for suggesting filing a ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
def _fill_m2m_cache(self):
cache = SortedDict()
@@ -326,8 +326,7 @@ def get_all_field_names(self):
cache = self._name_map
except AttributeError:
cache = self.init_name_map()
- names = cache.keys()
- names.sort()
+ names = sorted(cache.keys())
# Internal-only names end with "+" (symmetrical m2m related names being
# the main example). Trim them.
return [val for val in names if not val.endswith('+')]
@@ -417,7 +416,7 @@ def get_all_related_many_to_many_objects(self, local_only=False):
cache = self._fill_related_many_to_many_cache()
if local_only:
return [k for k, v in cache.items() if not v]
- return cache.keys()
+ return list(six.iterkeys(cache))
def get_all_related_m2m_objects_with_model(self):
"""
@@ -428,7 +427,7 @@ def get_all_related_m2m_objects_with_model(self):
cache = self._related_many_to_many_cache
except AttributeError:
cache = self._fill_related_many_to_many_cache()
- return cache.items()
+ return list(six.iteritems(cache))
def _fill_related_many_to_many_cache(self):
cache = SortedDict()
20 django/db/models/query.py
View
@@ -245,8 +245,8 @@ def iterator(self):
requested = None
max_depth = self.query.max_depth
- extra_select = self.query.extra_select.keys()
- aggregate_select = self.query.aggregate_select.keys()
+ extra_select = list(six.iterkeys(self.query.extra_select))
+ aggregate_select = list(six.iterkeys(self.query.aggregate_select))
only_load = self.query.get_loaded_field_names()
if not fill_cache:
@@ -593,7 +593,7 @@ def values_list(self, *fields, **kwargs):
flat = kwargs.pop('flat', False)
if kwargs:
raise TypeError('Unexpected keyword arguments to values_list: %s'
- % (kwargs.keys(),))
+ % (list(six.iterkeys(kwargs)),))
if flat and len(fields) > 1:
raise TypeError("'flat' is not valid when values_list is called with more than one field.")
return self._clone(klass=ValuesListQuerySet, setup=True, flat=flat,
@@ -693,7 +693,7 @@ def select_related(self, *fields, **kwargs):
depth = kwargs.pop('depth', 0)
if kwargs:
raise TypeError('Unexpected keyword arguments to select_related: %s'
- % (kwargs.keys(),))
+ % (list(six.iterkeys(kwargs)),))
obj = self._clone()
if fields:
if depth:
@@ -751,7 +751,7 @@ def annotate(self, *args, **kwargs):
obj = self._clone()
- obj._setup_aggregate_query(kwargs.keys())
+ obj._setup_aggregate_query(list(six.iterkeys(kwargs)))
# Add the aggregates to the query
for (alias, aggregate_expr) in kwargs.items():
@@ -966,9 +966,9 @@ def __init__(self, *args, **kwargs):
def iterator(self):
# Purge any extra columns that haven't been explicitly asked for
- extra_names = self.query.extra_select.keys()
+ extra_names = list(six.iterkeys(self.query.extra_select))
field_names = self.field_names
- aggregate_names = self.query.aggregate_select.keys()
+ aggregate_names = list(six.iterkeys(self.query.aggregate_select))
names = extra_names + field_names + aggregate_names
@@ -1097,9 +1097,9 @@ def iterator(self):
# When extra(select=...) or an annotation is involved, the extra
# cols are always at the start of the row, and we need to reorder
# the fields to match the order in self._fields.
- extra_names = self.query.extra_select.keys()
+ extra_names = list(six.iterkeys(self.query.extra_select))
field_names = self.field_names
- aggregate_names = self.query.aggregate_select.keys()
+ aggregate_names = list(six.iterkeys(self.query.aggregate_select))
names = extra_names + field_names + aggregate_names
@@ -1527,7 +1527,7 @@ def __iter__(self):
# Associate fields to values
if skip:
model_init_kwargs = {}
- for attname, pos in model_init_field_names.iteritems():
+ for attname, pos in six.iteritems(model_init_field_names):
model_init_kwargs[attname] = values[pos]
instance = model_cls(**model_init_kwargs)
else:
5 django/db/models/query_utils.py
View
@@ -8,6 +8,7 @@
from __future__ import unicode_literals
from django.db.backends import util
+from django.utils import six
from django.utils import tree
@@ -40,7 +41,7 @@ class Q(tree.Node):
default = AND
def __init__(self, *args, **kwargs):
- super(Q, self).__init__(children=list(args) + kwargs.items())
+ super(Q, self).__init__(children=list(args) + list(six.iteritems(kwargs)))
def _combine(self, other, conn):
if not isinstance(other, Q):
@@ -114,7 +115,7 @@ def __set__(self, instance, value):
def _check_parent_chain(self, instance, name):
"""
- Check if the field value can be fetched from a parent field already
+ Check if the field value can be fetched from a parent field already
loaded in the instance. This can be done if the to-be fetched
field is a primary key field.
"""
7 django/db/models/sql/compiler.py
View
@@ -9,6 +9,7 @@
from django.db.models.sql.expressions import SQLEvaluator
from django.db.models.sql.query import get_order_dir, Query
from django.db.utils import DatabaseError
+from django.utils import six
class SQLCompiler(object):
@@ -82,7 +83,7 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
where, w_params = self.query.where.as_sql(qn=qn, connection=self.connection)
having, h_params = self.query.having.as_sql(qn=qn, connection=self.connection)
params = []
- for val in self.query.extra_select.itervalues():
+ for val in six.itervalues(self.query.extra_select):
params.extend(val[1])
result = ['SELECT']
@@ -177,7 +178,7 @@ def get_columns(self, with_aliases=False):
"""
qn = self.quote_name_unless_alias
qn2 = self.connection.ops.quote_name
- result = ['(%s) AS %s' % (col[0], qn2(alias)) for alias, col in self.query.extra_select.iteritems()]
+ result = ['(%s) AS %s' % (col[0], qn2(alias)) for alias, col in six.iteritems(self.query.extra_select)]
aliases = set(self.query.extra_select.keys())
if with_aliases:
col_aliases = aliases.copy()
@@ -553,7 +554,7 @@ def get_grouping(self):
group_by = self.query.group_by or []
extra_selects = []
- for extra_select, extra_params in self.query.extra_select.itervalues():
+ for extra_select, extra_params in six.itervalues(self.query.extra_select):
extra_selects.append(extra_select)
params.extend(extra_params)
cols = (group_by + self.query.select +
25 django/db/models/sql/query.py
View
@@ -12,6 +12,7 @@
from django.utils.datastructures import SortedDict
from django.utils.encoding import force_unicode
from django.utils.tree import Node
+from django.utils import six
from django.db import connections, DEFAULT_DB_ALIAS
from django.db.models import signals
from django.db.models.expressions import ExpressionNode
@@ -602,22 +603,22 @@ def deferred_to_data(self, target, callback):
# slight complexity here is handling fields that exist on parent
# models.
workset = {}
- for model, values in seen.iteritems():
+ for model, values in six.iteritems(seen):
for field, m in model._meta.get_fields_with_model():
if field in values:
continue
add_to_dict(workset, m or model, field)
- for model, values in must_include.iteritems():
+ for model, values in six.iteritems(must_include):
# If we haven't included a model in workset, we don't add the
# corresponding must_include fields for that model, since an
# empty set means "include all fields". That's why there's no
# "else" branch here.
if model in workset:
workset[model].update(values)
- for model, values in workset.iteritems():
+ for model, values in six.iteritems(workset):
callback(target, model, values)
else:
- for model, values in must_include.iteritems():
+ for model, values in six.iteritems(must_include):
if model in seen:
seen[model].update(values)
else:
@@ -631,7 +632,7 @@ def deferred_to_data(self, target, callback):
for model in orig_opts.get_parent_list():
if model not in seen:
seen[model] = set()
- for model, values in seen.iteritems():
+ for model, values in six.iteritems(seen):
callback(target, model, values)
@@ -770,7 +771,7 @@ def change_aliases(self, change_map):
for k, aliases in self.join_map.items():
aliases = tuple([change_map.get(a, a) for a in aliases])
self.join_map[k] = aliases
- for old_alias, new_alias in change_map.iteritems():
+ for old_alias, new_alias in six.iteritems(change_map):
alias_data = self.alias_map[old_alias]
alias_data = alias_data._replace(rhs_alias=new_alias)
self.alias_refcount[new_alias] = self.alias_refcount[old_alias]
@@ -792,7 +793,7 @@ def change_aliases(self, change_map):
self.included_inherited_models[key] = change_map[alias]
# 3. Update any joins that refer to the old alias.
- for alias, data in self.alias_map.iteritems():
+ for alias, data in six.iteritems(self.alias_map):
lhs = data.lhs_alias
if lhs in change_map:
data = data._replace(lhs_alias=change_map[lhs])
@@ -842,7 +843,7 @@ def count_active_tables(self):
count. Note that after execution, the reference counts are zeroed, so
tables added in compiler will not be seen by this method.
"""
- return len([1 for count in self.alias_refcount.itervalues() if count])
+ return len([1 for count in six.itervalues(self.alias_refcount) if count])
def join(self, connection, always_create=False, exclusions=(),
promote=False, outer_if_first=False, nullable=False, reuse=None):
@@ -1302,7 +1303,7 @@ def setup_joins(self, names, opts, alias, dupe_multis, allow_many=True,
field, model, direct, m2m = opts.get_field_by_name(f.name)
break
else:
- names = opts.get_all_field_names() + self.aggregate_select.keys()
+ names = opts.get_all_field_names() + list(six.iterkeys(self.aggregate_select))
raise FieldError("Cannot resolve keyword %r into field. "
"Choices are: %s" % (name, ", ".join(names)))
@@ -1571,7 +1572,7 @@ def split_exclude(self, filter_expr, prefix, can_reuse):
# Tag.objects.exclude(parent__parent__name='t1'), a tag with no parent
# would otherwise be overlooked).
active_positions = [pos for (pos, count) in
- enumerate(query.alias_refcount.itervalues()) if count]
+ enumerate(six.itervalues(query.alias_refcount)) if count]
if active_positions[-1] > 1:
self.add_filter(('%s__isnull' % prefix, False), negate=True,
trim=True, can_reuse=can_reuse)
@@ -1660,8 +1661,8 @@ def add_fields(self, field_names, allow_m2m=True):
# from the model on which the lookup failed.
raise
else:
- names = sorted(opts.get_all_field_names() + self.extra.keys()
- + self.aggregate_select.keys())
+ names = sorted(opts.get_all_field_names() + list(six.iterkeys(self.extra))
+ + list(six.iterkeys(self.aggregate_select)))
raise FieldError("Cannot resolve keyword %r into field. "
"Choices are: %s" % (name, ", ".join(names)))
self.remove_inherited_models()
5 django/db/models/sql/subqueries.py
View
@@ -11,6 +11,7 @@
from django.utils.datastructures import SortedDict
from django.utils.functional import Promise
from django.utils.encoding import force_unicode
+from django.utils import six
__all__ = ['DeleteQuery', 'UpdateQuery', 'InsertQuery', 'DateQuery',
@@ -87,7 +88,7 @@ def add_update_values(self, values):
querysets.
"""
values_seq = []
- for name, val in values.iteritems():
+ for name, val in six.iteritems(values):
field, model, direct, m2m = self.model._meta.get_field_by_name(name)
if not direct or m2m:
raise FieldError('Cannot update model field %r (only non-relations and foreign keys permitted).' % field)
@@ -129,7 +130,7 @@ def get_related_updates(self):
if not self.related_updates:
return []
result = []
- for model, values in self.related_updates.iteritems():
+ for model, values in six.iteritems(self.related_updates):
query = UpdateQuery(model)
query.values = values
if self.related_ids is not None:
2  django/forms/extras/widgets.py
View
@@ -79,7 +79,7 @@ def render(self, name, value, attrs=None):
year_val, month_val, day_val = [int(v) for v in match.groups()]
choices = [(i, i) for i in self.years]
year_html = self.create_select(name, self.year_field, value, year_val, choices)
- choices = MONTHS.items()
+ choices = list(six.iteritems(MONTHS))
month_html = self.create_select(name, self.month_field, value, month_val, choices)
choices = [(i, i) for i in range(1, 32)]
day_html = self.create_select(name, self.day_field, value, day_val, choices)
6 django/forms/forms.py
View
@@ -38,7 +38,7 @@ def get_declared_fields(bases, attrs, with_base_fields=True):
used. The distinction is useful in ModelForm subclassing.
Also integrates any additional media definitions
"""
- fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
+ fields = [(field_name, attrs.pop(field_name)) for field_name, obj in list(six.iteritems(attrs)) if isinstance(obj, Field)]
fields.sort(key=lambda x: x[1].creation_counter)
# If this class is subclassing another Form, add that Form's fields.
@@ -47,11 +47,11 @@ def get_declared_fields(bases, attrs, with_base_fields=True):
if with_base_fields:
for base in bases[::-1]:
if hasattr(base, 'base_fields'):
- fields = base.base_fields.items() + fields
+ fields = list(six.iteritems(base.base_fields)) + fields
else:
for base in bases[::-1]:
if hasattr(base, 'declared_fields'):
- fields = base.declared_fields.items() + fields
+ fields = list(six.iteritems(base.declared_fields)) + fields
return SortedDict(fields)
3  django/forms/models.py
View
@@ -18,6 +18,7 @@
from django.utils import six
from django.utils.text import get_text_list, capfirst
from django.utils.translation import ugettext_lazy as _, ugettext
+from django.utils import six
__all__ = (
@@ -206,7 +207,7 @@ def __new__(cls, name, bases, attrs):
fields = fields_for_model(opts.model, opts.fields,
opts.exclude, opts.widgets, formfield_callback)
# make sure opts.fields doesn't specify an invalid field
- none_model_fields = [k for k, v in fields.iteritems() if not v]
+ none_model_fields = [k for k, v in six.iteritems(fields) if not v]
missing_fields = set(none_model_fields) - \
set(declared_fields.keys())
if missing_fields:
3  django/forms/widgets.py
View
@@ -63,8 +63,7 @@ def render_js(self):
def render_css(self):
# To keep rendering order consistent, we can't just iterate over items().
# We need to sort the keys, and iterate over the sorted list.
- media = self._css.keys()
- media.sort()
+ media = sorted(self._css.keys())
return chain(*[
[format_html('<link href="{0}" type="text/css" media="{1}" rel="stylesheet" />', self.absolute_path(path), medium)
for path in self._css[medium]]
2  django/template/base.py
View
@@ -961,7 +961,7 @@ def parse_bits(parser, bits, params, varargs, varkw, defaults,
kwarg = token_kwargs([bit], parser)
if kwarg:
# The kwarg was successfully extracted
- param, value = kwarg.items()[0]
+ param, value = list(six.iteritems(kwarg))[0]
if param not in params and varkw is None:
# An unexpected keyword argument was supplied
raise TemplateSyntaxError(
5 django/template/defaulttags.py
View
@@ -17,6 +17,7 @@
from django.utils.encoding import smart_unicode
from django.utils.safestring import mark_safe
from django.utils.html import format_html
+from django.utils import six
from django.utils import timezone
register = Library()
@@ -473,7 +474,7 @@ def __repr__(self):
def render(self, context):
values = dict([(key, val.resolve(context)) for key, val in
- self.extra_context.iteritems()])
+ six.iteritems(self.extra_context)])
context.update(values)
output = self.nodelist.render(context)
context.pop()
@@ -1188,7 +1189,7 @@ def templatetag(parser, token):
if tag not in TemplateTagNode.mapping:
raise TemplateSyntaxError("Invalid templatetag argument: '%s'."
" Must be one of: %s" %
- (tag, TemplateTagNode.mapping.keys()))
+ (tag, list(six.iterkeys(TemplateTagNode.mapping))))
return TemplateTagNode(tag)
@register.tag
5 django/template/loader_tags.py
View
@@ -3,6 +3,7 @@
token_kwargs, Variable
from django.template.loader import get_template
from django.utils.safestring import mark_safe
+from django.utils import six
register = Library()
@@ -17,7 +18,7 @@ def __init__(self):
self.blocks = {}
def add_blocks(self, blocks):
- for name, block in blocks.iteritems():
+ for name, block in six.iteritems(blocks):
if name in self.blocks:
self.blocks[name].insert(0, block)
else:
@@ -130,7 +131,7 @@ def __init__(self, *args, **kwargs):
def render_template(self, template, context):
values = dict([(name, var.resolve(context)) for name, var
- in self.extra_context.iteritems()])
+ in six.iteritems(self.extra_context)])
if self.isolated_context:
return template.render(context.new(values))
context.update(values)
2  django/templatetags/i18n.py
View
@@ -425,7 +425,7 @@ def do_block_translate(parser, token):
options[option] = value
if 'count' in options:
- countervar, counter = options['count'].items()[0]
+ countervar, counter = list(six.iteritems(options['count']))[0]
else:
countervar, counter = None, None
if 'context' in options:
7 django/utils/dateparse.py
View
@@ -7,6 +7,7 @@
import datetime
import re
+from django.utils import six
from django.utils.timezone import utc
from django.utils.tzinfo import FixedOffset
@@ -34,7 +35,7 @@ def parse_date(value):
"""
match = date_re.match(value)
if match:
- kw = dict((k, int(v)) for k, v in match.groupdict().iteritems())
+ kw = dict((k, int(v)) for k, v in six.iteritems(match.groupdict()))
return datetime.date(**kw)
def parse_time(value):
@@ -53,7 +54,7 @@ def parse_time(value):
kw = match.groupdict()
if kw['microsecond']:
kw['microsecond'] = kw['microsecond'].ljust(6, '0')
- kw = dict((k, int(v)) for k, v in kw.iteritems() if v is not None)
+ kw = dict((k, int(v)) for k, v in six.iteritems(kw) if v is not None)
return datetime.time(**kw)
def parse_datetime(value):
@@ -80,6 +81,6 @@ def parse_datetime(value):
if tzinfo[0] == '-':
offset = -offset
tzinfo = FixedOffset(offset)
- kw = dict((k, int(v)) for k, v in kw.iteritems() if v is not None)
+ kw = dict((k, int(v)) for k, v in six.iteritems(kw) if v is not None)
kw['tzinfo'] = tzinfo
return datetime.datetime(**kw)
2  django/utils/dictconfig.py
View
@@ -363,7 +363,7 @@ def configure(self):
#which were in the previous configuration but
#which are not in the new configuration.
root = logging.root
- existing = root.manager.loggerDict.keys()
+ existing = list(six.iterkeys(root.manager.loggerDict))
#The list needs to be sorted so that we can
#avoid disabling child loggers of explicitly
#named loggers. With a sorted list it is easier
2  django/utils/functional.py
View
@@ -178,7 +178,7 @@ def allow_lazy(func, *resultclasses):
"""
@wraps(func)
def wrapper(*args, **kwargs):
- for arg in list(args) + kwargs.values():
+ for arg in list(args) + list(six.itervalues(kwargs)):
if isinstance(arg, Promise):
break
else:
2  django/utils/html.py
View
@@ -84,7 +84,7 @@ def format_html(format_string, *args, **kwargs):
"""
args_safe = map(conditional_escape, args)
kwargs_safe = dict([(k, conditional_escape(v)) for (k, v) in
- kwargs.iteritems()])
+ six.iteritems(kwargs)])
return mark_safe(format_string.format(*args_safe, **kwargs_safe))
def format_html_join(sep, format_string, args_generator):
4 django/utils/termcolors.py
View
@@ -2,6 +2,8 @@
termcolors.py
"""
+from django.utils import six
+
color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
background = dict([(color_names[x], '4%s' % x) for x in range(8)])
@@ -41,7 +43,7 @@ def colorize(text='', opts=(), **kwargs):
code_list = []
if text == '' and len(opts) == 1 and opts[0] == 'reset':
return '\x1b[%sm' % RESET
- for k, v in kwargs.iteritems():
+ for k, v in six.iteritems(kwargs):
if k == 'fg':
code_list.append(foreground[v])
elif k == 'bg':
2  django/views/debug.py
View
@@ -111,7 +111,7 @@ def get_post_parameters(self, request):
return request.POST
def get_traceback_frame_variables(self, request, tb_frame):
- return tb_frame.f_locals.items()
+ return list(six.iteritems(tb_frame.f_locals))
class SafeExceptionReporterFilter(ExceptionReporterFilter):
"""
3  django/views/generic/base.py
View
@@ -6,6 +6,7 @@
from django.template.response import TemplateResponse
from django.utils.log import getLogger
from django.utils.decorators import classonlymethod
+from django.utils import six
logger = getLogger('django.request')
@@ -35,7 +36,7 @@ def __init__(self, **kwargs):
"""
# Go through keyword arguments, and either save their values to our
# instance, or raise an error.
- for key, value in kwargs.iteritems():
+ for key, value in six.iteritems(kwargs):
setattr(self, key, value)
@classonlymethod
9 tests/modeltests/timezones/tests.py
View
@@ -20,6 +20,7 @@
from django.template import Context, RequestContext, Template, TemplateSyntaxError
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.test.utils import override_settings
+from django.utils import six
from django.utils import timezone
from django.utils.tzinfo import FixedOffset
from django.utils.unittest import skipIf, skipUnless
@@ -690,8 +691,8 @@ def t(*result):
}
}
- for k1, dt in datetimes.iteritems():
- for k2, tpl in templates.iteritems():
+ for k1, dt in six.iteritems(datetimes):
+ for k2, tpl in six.iteritems(templates):
ctx = Context({'dt': dt, 'ICT': ICT})
actual = tpl.render(ctx)
expected = results[k1][k2]
@@ -703,8 +704,8 @@ def t(*result):
results['ict']['notag'] = t('ict', 'eat', 'utc', 'ict')
with self.settings(USE_TZ=False):
- for k1, dt in datetimes.iteritems():
- for k2, tpl in templates.iteritems():
+ for k1, dt in six.iteritems(datetimes):
+ for k2, tpl in six.iteritems(templates):
ctx = Context({'dt': dt, 'ICT': ICT})
actual = tpl.render(ctx)
expected = results[k1][k2]
3  tests/regressiontests/aggregation_regress/tests.py
View
@@ -8,6 +8,7 @@
from django.core.exceptions import FieldError
from django.db.models import Count, Max, Avg, Sum, StdDev, Variance, F, Q
from django.test import TestCase, Approximate, skipUnlessDBFeature
+from django.utils import six
from .models import Author, Book, Publisher, Clues, Entries, HardbackBook
@@ -16,7 +17,7 @@ class AggregationTests(TestCase):
fixtures = ["aggregation_regress.json"]
def assertObjectAttrs(self, obj, **kwargs):
- for attr, value in kwargs.iteritems():
+ for attr, value in six.iteritems(kwargs):
self.assertEqual(getattr(obj, attr), value)
def test_aggregates_in_where_clause(self):
3  tests/regressiontests/db_typecasts/tests.py
View
@@ -3,6 +3,7 @@
import datetime
from django.db.backends import util as typecasts
+from django.utils import six
from django.utils import unittest
@@ -49,7 +50,7 @@
class DBTypeCasts(unittest.TestCase):
def test_typeCasts(self):
- for k, v in TEST_CASES.iteritems():
+ for k, v in six.iteritems(TEST_CASES):
for inpt, expected in v:
got = getattr(typecasts, k)(inpt)
self.assertEqual(got, expected, "In %s: %r doesn't match %r. Got %r instead." % (k, inpt, expected, got))
6 tests/regressiontests/templates/templatetags/custom.py
View
@@ -70,7 +70,7 @@ def simple_only_unlimited_args(*args):
def simple_unlimited_args_kwargs(one, two='hi', *args, **kwargs):
"""Expected simple_unlimited_args_kwargs __doc__"""
# Sort the dictionary by key to guarantee the order for testing.
- sorted_kwarg = sorted(kwargs.iteritems(), key=operator.itemgetter(0))
+ sorted_kwarg = sorted(six.iteritems(kwargs), key=operator.itemgetter(0))
return "simple_unlimited_args_kwargs - Expected result: %s / %s" % (
', '.join([six.text_type(arg) for arg in [one, two] + list(args)]),
', '.join(['%s=%s' % (k, v) for (k, v) in sorted_kwarg])
@@ -221,7 +221,7 @@ def inclusion_tag_use_l10n(context):
def inclusion_unlimited_args_kwargs(one, two='hi', *args, **kwargs):
"""Expected inclusion_unlimited_args_kwargs __doc__"""
# Sort the dictionary by key to guarantee the order for testing.
- sorted_kwarg = sorted(kwargs.iteritems(), key=operator.itemgetter(0))
+ sorted_kwarg = sorted(six.iteritems(kwargs), key=operator.itemgetter(0))
return {"result": "inclusion_unlimited_args_kwargs - Expected result: %s / %s" % (
', '.join([six.text_type(arg) for arg in [one, two] + list(args)]),
', '.join(['%s=%s' % (k, v) for (k, v) in sorted_kwarg])
@@ -292,7 +292,7 @@ def assignment_only_unlimited_args(*args):
def assignment_unlimited_args_kwargs(one, two='hi', *args, **kwargs):
"""Expected assignment_unlimited_args_kwargs __doc__"""
# Sort the dictionary by key to guarantee the order for testing.
- sorted_kwarg = sorted(kwargs.iteritems(), key=operator.itemgetter(0))
+ sorted_kwarg = sorted(six.iteritems(kwargs), key=operator.itemgetter(0))
return "assignment_unlimited_args_kwargs - Expected result: %s / %s" % (
', '.join([six.text_type(arg) for arg in [one, two] + list(args)]),
', '.join(['%s=%s' % (k, v) for (k, v) in sorted_kwarg])
3  tests/regressiontests/templates/tests.py
View
@@ -30,6 +30,7 @@
from django.utils.formats import date_format
from django.utils.translation import activate, deactivate, ugettext as _
from django.utils.safestring import mark_safe
+from django.utils import six
from django.utils.tzinfo import LocalTimezone
from .callables import CallableVariablesTests
@@ -402,7 +403,7 @@ def test_templates(self):
template_tests.update(filter_tests)
cache_loader = setup_test_template_loader(
- dict([(name, t[0]) for name, t in template_tests.iteritems()]),
+ dict([(name, t[0]) for name, t in six.iteritems(template_tests)]),
use_cached_loader=True,
)
Simon Charette

This changed the return type of get_m2m_with_model from tuple to list. I know it's a private API but it's a bit inconsistent with the get_fields_with_model which still returns a tuple. I stumbled on this regression when trying to concatenate both doing get_fields_with_models() + get_m2m_with_models(). This used to work but is broken as of this commit.

Aymeric Augustin

Would you mind creating a ticket in Trac to discuss this backwards incompatibility?

Is the fix just s/list/tuple/?

Thanks!

Simon Charette

Nevermind, get_m2m_with_models() always returned a list, was just surprised it returns a list when get_fields_with_models returns a tuple.

I guess that's what you get when playing with internal API.

Aymeric Augustin

We tend to be skeptical of tickets about internal APIs, but you can file one anyway if you have some arguments against this inconsistency.

Simon Charette

I don't think my consistency arguments is worth a possible break of backward compatibility, there might be a couple of projects relying on this in the wild.

Thanks for suggesting filing a ticket.

Please sign in to comment.
Something went wrong with that request. Please try again.