diff options
| author | xmo-odoo <xmo@odoo.com> | 2017-05-10 09:39:55 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-10 09:39:55 +0200 |
| commit | fffaf735f58f1ac57781c095eb552de6d1fcdc47 (patch) | |
| tree | 88ab0ab65430d871862a836524e0ecc890d1674d /addons/website | |
| parent | 10eee18a047d35305dc46282e610e88936d81ffc (diff) | |
[FIX] P3: list -> iterable builtins (#16811)
In Python 3:
* various builtins and dict methods were changed to return
view/iterable objects rather than lists
* and the separate Python 2 view/iterable builtins and methods were
removed altogether
This is problematic when using these items as list (which the happens
repeatedly in Odoo), but more viciously when iterating *multiple times*
over them (which also happens, which I've messed up multiple times while
writing this, and which is a pain to debug even when you've just created
the issue).
Convert all code using these to semantics-matching cross-version
helper functions to get the LCD behaviour between P2 and P3, and
forbid the builtins via lint.
issue #8530
Diffstat (limited to 'addons/website')
| -rw-r--r-- | addons/website/controllers/main.py | 8 | ||||
| -rw-r--r-- | addons/website/models/ir_http.py | 4 | ||||
| -rw-r--r-- | addons/website/models/ir_qweb.py | 5 | ||||
| -rw-r--r-- | addons/website/models/website.py | 13 | ||||
| -rw-r--r-- | addons/website/tests/test_converter.py | 4 | ||||
| -rw-r--r-- | addons/website/tests/test_crawl.py | 2 | ||||
| -rw-r--r-- | addons/website/tests/test_views.py | 3 |
7 files changed, 23 insertions, 16 deletions
diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index ccdd44e1c4d..1d0e0a85dd8 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -21,6 +21,8 @@ from odoo.exceptions import AccessError from odoo.addons.website.models.website import slug from odoo.addons.web.controllers.main import WebClient, Binary, Home +from odoo.tools import pycompat + logger = logging.getLogger(__name__) # Completely arbitrary limits @@ -37,11 +39,11 @@ class QueryURL(object): def __call__(self, path=None, path_args=None, **kw): path = path or self.path - for key, value in self.args.items(): + for key, value in pycompat.items(self.args): kw.setdefault(key, value) path_args = set(path_args or []).union(self.path_args) paths, fragments = [], [] - for key, value in kw.items(): + for key, value in pycompat.items(kw): if value and key in path_args: if isinstance(value, browse_record): paths.append((key, slug(value))) @@ -193,7 +195,7 @@ class Website(Home): }) else: # TODO: in master/saas-15, move current_website_id in template directly - pages_with_website = map(lambda p: "%d-%d" % (current_website.id, p), range(1, pages + 1)) + pages_with_website = ["%d-%d" % (current_website.id, p) for p in range(1, pages + 1)] # Sitemaps must be split in several smaller files with a sitemap index content = View.render_template('website.sitemap_index_xml', { diff --git a/addons/website/models/ir_http.py b/addons/website/models/ir_http.py index b6902b17c41..a4648e7d99d 100644 --- a/addons/website/models/ir_http.py +++ b/addons/website/models/ir_http.py @@ -14,7 +14,7 @@ import odoo from odoo import api, models from odoo import SUPERUSER_ID from odoo.http import request -from odoo.tools import config +from odoo.tools import config, pycompat from odoo.exceptions import QWebException from odoo.tools.safe_eval import safe_eval @@ -241,7 +241,7 @@ class Http(models.AbstractModel): def _postprocess_args(cls, arguments, rule): super(Http, cls)._postprocess_args(arguments, rule) - for key, val in arguments.items(): + for key, val in pycompat.items(arguments): # Replace uid placeholder by the current request.uid if isinstance(val, models.BaseModel) and isinstance(val._uid, RequestUID): arguments[key] = val.sudo(request.uid) diff --git a/addons/website/models/ir_qweb.py b/addons/website/models/ir_qweb.py index 4ecee90fd5e..e6cedfe07d2 100644 --- a/addons/website/models/ir_qweb.py +++ b/addons/website/models/ir_qweb.py @@ -5,6 +5,7 @@ import ast from odoo import models from odoo.http import request +from odoo.tools import pycompat class QWeb(models.AbstractModel): @@ -65,7 +66,7 @@ class QWeb(models.AbstractModel): else: return item - return map(process, items) + return [process(it) for it in items] def _compile_static_attributes(self, el, options): items = super(QWeb, self)._compile_static_attributes(el, options) @@ -81,7 +82,7 @@ class QWeb(models.AbstractModel): atts = super(QWeb, self)._get_dynamic_att(tagName, atts, options, values) if options.get('rendering_bundle'): return atts - for name, value in atts.iteritems(): + for name, value in pycompat.items(atts): atts[name] = self._website_build_attribute(tagName, name, value, options, values) return atts diff --git a/addons/website/models/website.py b/addons/website/models/website.py index bd7621c767d..9968ad8e636 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -367,7 +367,7 @@ class Website(models.Model): def get_url_localized(router, lang): arguments = dict(request.endpoint_arguments) - for key, val in arguments.items(): + for key, val in list(pycompat.items(arguments)): if isinstance(val, models.BaseModel): arguments[key] = val.with_context(lang=lang) return router.build(request.endpoint, arguments) @@ -486,7 +486,7 @@ class Website(models.Model): 'num': pmax }, "pages": [ - {'url': get_url(page), 'num': page} for page in pycompat.range(pmin, pmax+1) + {'url': get_url(page), 'num': page} for page in range(pmin, pmax+1) ] } @@ -499,7 +499,7 @@ class Website(models.Model): endpoint = rule.endpoint methods = endpoint.routing.get('methods') or ['GET'] - converters = rule._converters.values() + converters = list(pycompat.values(rule._converters)) if not ('GET' in methods and endpoint.routing['type'] == 'http' and endpoint.routing['auth'] in ('none', 'public') @@ -542,9 +542,10 @@ class Website(models.Model): if query_string and not converters and (query_string not in rule.build([{}], append_unknown=False)[1]): continue values = [{}] - convitems = converters.items() # converters with a domain are processed after the other ones - convitems.sort(key=lambda x: hasattr(x[1], 'domain') and (x[1].domain != '[]')) + convitems = sorted( + pycompat.items(converters), + key=lambda x: hasattr(x[1], 'domain') and (x[1].domain != '[]')) for (i, (name, converter)) in enumerate(convitems): newval = [] for val in values: @@ -559,7 +560,7 @@ class Website(models.Model): for value in values: domain_part, url = rule.build(value, append_unknown=False) page = {'loc': url} - for key, val in value.items(): + for key, val in pycompat.items(value): if key.startswith('__'): page[key[2:]] = val if url in ('/sitemap.xml',): diff --git a/addons/website/tests/test_converter.py b/addons/website/tests/test_converter.py index 0c3dfe0434a..9267826c689 100644 --- a/addons/website/tests/test_converter.py +++ b/addons/website/tests/test_converter.py @@ -4,6 +4,8 @@ import unittest from odoo.addons.website.models.website import slugify, unslug +from odoo.tools import pycompat + class TestUnslug(unittest.TestCase): @@ -23,7 +25,7 @@ class TestUnslug(unittest.TestCase): 'foo1': (None, None), } - for slug, expected in tests.iteritems(): + for slug, expected in pycompat.items(tests): self.assertEqual(unslug(slug), expected) diff --git a/addons/website/tests/test_crawl.py b/addons/website/tests/test_crawl.py index 704cd62a41b..a9f1e9b1f84 100644 --- a/addons/website/tests/test_crawl.py +++ b/addons/website/tests/test_crawl.py @@ -40,7 +40,7 @@ class Crawler(odoo.tests.HttpCase): _logger.info("%s %s", msg, url) r = self.url_open(url) code = r.getcode() - self.assertIn(code, pycompat.range(200, 300), "%s Fetching %s returned error response (%d)" % (msg, url, code)) + self.assertIn(code, range(200, 300), "%s Fetching %s returned error response (%d)" % (msg, url, code)) if r.info().gettype() == 'text/html': doc = lxml.html.fromstring(r.read()) diff --git a/addons/website/tests/test_views.py b/addons/website/tests/test_views.py index a90c14f3193..415e1668ce1 100644 --- a/addons/website/tests/test_views.py +++ b/addons/website/tests/test_views.py @@ -8,10 +8,11 @@ from lxml import etree as ET, html from lxml.html import builder as h from odoo.tests import common +from odoo.tools import pycompat def attrs(**kwargs): - return dict(('data-oe-%s' % key, str(value)) for key, value in kwargs.iteritems()) + return {'data-oe-%s' % key: str(value) for key, value in pycompat.items(kwargs)} class TestViewSaving(common.TransactionCase): |
