[FIX] base: always preload dateutil
The dateutil package uses lazy import to selectively expose its features: `parser`, `relativedelta`, `rrule`, `tz`. Depending on installed modules and order of initialization, there was no guarantee that a given feature was already loaded during the preparation of the action context. This patch ensures that we always preload the feature set we need, and only that feature set. This way we have a consistent `dateutil` lib in the action context at all times.
This commit is contained in:
@@ -5,13 +5,12 @@ import odoo
|
||||
from odoo import api, fields, models, tools, SUPERUSER_ID, _
|
||||
from odoo.exceptions import MissingError, UserError, ValidationError, AccessError
|
||||
from odoo.tools.safe_eval import safe_eval, test_python_expr
|
||||
from odoo.tools import pycompat
|
||||
from odoo.tools import pycompat, wrap_module
|
||||
from odoo.http import request
|
||||
|
||||
import base64
|
||||
from collections import defaultdict
|
||||
import datetime
|
||||
import dateutil
|
||||
import logging
|
||||
import time
|
||||
|
||||
@@ -19,6 +18,16 @@ from pytz import timezone
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
# build dateutil helper, starting with the relevant *lazy* imports
|
||||
import dateutil
|
||||
import dateutil.parser
|
||||
import dateutil.relativedelta
|
||||
import dateutil.rrule
|
||||
import dateutil.tz
|
||||
mods = {'parser', 'relativedelta', 'rrule', 'tz'}
|
||||
attribs = {atr for m in mods for atr in getattr(dateutil, m).__all__}
|
||||
dateutil = wrap_module(dateutil, mods | attribs)
|
||||
|
||||
|
||||
class IrActions(models.Model):
|
||||
_name = 'ir.actions.actions'
|
||||
|
||||
@@ -1151,3 +1151,23 @@ pickle.load = _pickle_load
|
||||
pickle.loads = lambda text, encoding='ASCII': _pickle_load(io.BytesIO(text), encoding=encoding)
|
||||
pickle.dump = pickle_.dump
|
||||
pickle.dumps = pickle_.dumps
|
||||
|
||||
def wrap_module(module, attr_list):
|
||||
"""Helper for wrapping a package/module to expose selected attributes
|
||||
|
||||
:param Module module: the actual package/module to wrap, as returned by ``import <module>``
|
||||
:param iterable attr_list: a global list of attributes to expose, usually the top-level
|
||||
attributes and their own main attributes. No support for hiding attributes in case
|
||||
of name collision at different levels.
|
||||
"""
|
||||
attr_list = set(attr_list)
|
||||
class WrappedModule(object):
|
||||
def __getattr__(self, attrib):
|
||||
if attrib in attr_list:
|
||||
target = getattr(module, attrib)
|
||||
if isinstance(target, types.ModuleType):
|
||||
return wrap_module(target, attr_list)
|
||||
return target
|
||||
raise AttributeError(attrib)
|
||||
# module and attr_list are in the closure
|
||||
return WrappedModule()
|
||||
|
||||
Reference in New Issue
Block a user