Skip to content

Commit

Permalink
[FIX] imports of moved or removed stdlib modules
Browse files Browse the repository at this point in the history
issue #8530
  • Loading branch information
xmo-odoo committed Apr 27, 2017
1 parent 1249443 commit ca37244
Show file tree
Hide file tree
Showing 36 changed files with 157 additions and 267 deletions.
2 changes: 1 addition & 1 deletion addons/base_automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import models
from . import tests
from .tests import test_models
1 change: 0 additions & 1 deletion addons/base_sparse_field/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-

from . import models
from . import tests
6 changes: 3 additions & 3 deletions addons/hw_escpos/escpos/escpos.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import copy
import io
import math
import md5
import re
import traceback
import xml.etree.ElementTree as ET
from hashlib import md5

from PIL import Image
import xml.etree.ElementTree as ET

try:
import jcconv
Expand Down Expand Up @@ -431,7 +431,7 @@ def print_base64_image(self,img):

print('print_b64_img')

id = md5.new(img).digest()
id = md5(img).digest()

if id not in self.img_cache:
print('not in cache')
Expand Down
2 changes: 1 addition & 1 deletion addons/mail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from . import models
from . import wizard
from . import controllers
from . import tests.test_mail_model
from .tests import test_mail_model
2 changes: 1 addition & 1 deletion addons/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import models
from . import tests.test_resource_model
from .tests import test_resource_model
2 changes: 1 addition & 1 deletion addons/resource/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from . import resource
from . import resource_mixin
from . import res_company
from . import res_users
from . import res_users
1 change: 0 additions & 1 deletion addons/survey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
from . import controllers
from . import models
from . import wizard
from . import tests
1 change: 0 additions & 1 deletion addons/web_editor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@

from . import controllers
from . import models
from . import tests
1 change: 0 additions & 1 deletion addons/website/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
from . import controllers
from . import models
from . import wizard
from . import tests
1 change: 0 additions & 1 deletion addons/website_crm_partner_assign/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
from . import controllers
from . import models
from . import report
from . import tests
from . import wizard
1 change: 0 additions & 1 deletion addons/website_forum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

from . import controllers
from . import models
from . import tests
17 changes: 16 additions & 1 deletion doc/python3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ features whereas:
Moved and removed
=================

Standard Library Modules
------------------------

Python 3 reorganised, moved or removed a number of modules in the standard
library:

* ``StringIO`` and ``cStringIO`` were removed, you can use ``io.BytesIO`` and
``io.StringIO`` to replace them in a cross-version manner (``io.BytesIO``
for binary data, ``io.StringIO`` for text/unicode data).
* ``urllib``, ``urllib2`` and ``urlparse`` were redistributed across
``urllib.parse`` and ``urllib.request``, you may want to use conditional
imports e.g. try to import the Python 3 version and fallback on the Python
2 version.

Absolute Imports (:pep:`328`)
-----------------------------

Expand Down Expand Up @@ -92,7 +106,8 @@ multiple leading ``.``.
be used everywhere.

You can ensure you are not using any implicitly relative import by adding
``from __future__ import absolute_import`` at the top of your files.
``from __future__ import absolute_import`` at the top of your files, or by
running the ``relative-import`` PyLint.

Exception Handlers
------------------
Expand Down
1 change: 0 additions & 1 deletion odoo/addons/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from . import module
from . import res
from . import report
from . import tests

def post_init(cr, registry):
"""Rewrite ICP's to force groups"""
Expand Down
1 change: 0 additions & 1 deletion odoo/addons/base/ir/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import os
import re
import sys
import urllib2

import werkzeug
import werkzeug.exceptions
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/ir/ir_qweb/fields.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
import re
from collections import OrderedDict
from io import BytesIO
from odoo import api, fields, models, _
from PIL import Image
from cStringIO import StringIO
import babel
from odoo.tools import html_escape as escape, posix_to_ldml, safe_eval, float_utils, format_date
from .qweb import unicodifier
Expand Down Expand Up @@ -269,7 +269,7 @@ class ImageConverter(models.AbstractModel):
@api.model
def value_to_html(self, value, options):
try:
image = Image.open(StringIO(value.decode('base64')))
image = Image.open(BytesIO(value.decode('base64')))
image.verify()
except IOError:
raise ValueError("Non-image binary fields can not be converted to HTML")
Expand Down
22 changes: 14 additions & 8 deletions odoo/addons/base/ir/ir_qweb/ir_qweb.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import ast
from urlparse import urlparse
import json
import logging
from collections import OrderedDict
from time import time
try:
from urllib.parse import urlparse
except ImportError:
# pylint: disable=bad-python3-import
from urlparse import urlparse

from lxml import html
from lxml import etree

from odoo.tools import pycompat
from .qweb import QWeb, Contextifier
from .assetsbundle import AssetsBundle
from lxml import etree
from collections import OrderedDict

from odoo import api, models, tools
from odoo.tools.safe_eval import assert_valid_codeobj, _BUILTINS, _SAFE_OPCODES
from odoo.http import request
from odoo.modules.module import get_resource_path
import json
from time import time

import logging
from .qweb import QWeb, Contextifier
from .assetsbundle import AssetsBundle

_logger = logging.getLogger(__name__)


Expand Down
23 changes: 14 additions & 9 deletions odoo/addons/base/ir/ir_qweb/qweb.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# -*- coding: utf-8 -*-
import ast
from collections import OrderedDict, Sized, Mapping, defaultdict
from lxml import etree, html
import logging
import re
import traceback
from itertools import count

from collections import OrderedDict, Sized, Mapping, defaultdict
from functools import reduce
from itertools import chain, izip, tee, count
from textwrap import dedent

from lxml import etree, html
import werkzeug
from werkzeug.utils import escape as _escape
from itertools import chain, izip, tee
import __builtin__
from functools import reduce

from odoo.tools import pycompat

builtin_defaults = {name: getattr(__builtin__, name) for name in dir(__builtin__)}
try:
import builtins
builtin_defaults = {name: getattr(builtins, name) for name in dir(builtins)}
except ImportError:
# pylint: disable=bad-python3-import
import __builtin__
builtin_defaults = {name: getattr(__builtin__, name) for name in dir(__builtin__)}

try:
import astor
except ImportError:
astor = None

import logging

unsafe_eval = eval

_logger = logging.getLogger(__name__)
Expand Down
20 changes: 11 additions & 9 deletions odoo/addons/base/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
from collections import defaultdict
from operator import attrgetter
import importlib
import io
import logging
import os
import shutil
import tempfile
import urllib2
import urlparse
import zipfile

try:
from urllib import parse as urlparse
from urllib.request import urlopen
except ImportError:
# pylint: disable=bad-python3-import
import urlparse
from urllib2 import urlopen

from docutils import nodes
from docutils.core import publish_string
from docutils.transforms import Transform, writer_aux
from docutils.writers.html4css1 import Writer
import lxml.html

try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO # NOQA

import odoo
from odoo import api, fields, models, modules, tools, _
from odoo.exceptions import AccessDenied, UserError
Expand Down Expand Up @@ -676,12 +678,12 @@ def install_from_urls(self, urls):

try:
_logger.info('Downloading module `%s` from OpenERP Apps', module_name)
content = urllib2.urlopen(url).read()
content = urlopen(url).read()
except Exception:
_logger.exception('Failed to fetch module %s', module_name)
raise UserError(_('The `%s` module appears to be unavailable at the moment, please try again later.') % module_name)
else:
zipfile.ZipFile(StringIO(content)).extractall(tmp)
zipfile.ZipFile(io.BytesIO(content)).extractall(tmp)
assert os.path.isdir(os.path.join(tmp, module_name))

# 2a. Copy/Replace module source in addons path
Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/module/wizard/base_export_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import base64
import contextlib
import cStringIO
import io

from odoo import api, fields, models, tools, _

Expand Down Expand Up @@ -34,7 +34,7 @@ def act_getfile(self):
lang = this.lang if this.lang != NEW_LANG_KEY else False
mods = sorted(this.mapped('modules.name')) or ['all']

with contextlib.closing(cStringIO.StringIO()) as buf:
with contextlib.closing(io.BytesIO()) as buf:
tools.trans_export(lang, mods, buf, this.format, self._cr)
out = base64.encodestring(buf.getvalue())

Expand Down
4 changes: 2 additions & 2 deletions odoo/addons/base/module/wizard/base_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import contextlib
import cStringIO
import io

from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError
Expand Down Expand Up @@ -37,7 +37,7 @@ def _get_lang_name(self, lang_code):
def act_update(self):
this = self[0]
lang_name = self._get_lang_name(this.lang)
with contextlib.closing(cStringIO.StringIO()) as buf:
with contextlib.closing(io.BytesIO()) as buf:
tools.trans_export(this.lang, ['all'], buf, 'csv', self._cr)
tools.trans_load_data(self._cr, buf, 'csv', this.lang, lang_name=lang_name)
return {'type': 'ir.actions.act_window_close'}
13 changes: 9 additions & 4 deletions odoo/addons/base/res/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
import hashlib
import pytz
import threading
import urllib2
import urlparse

try:
from urllib import parse as urlparse
from urllib.request import urlopen
except ImportError:
# pylint: disable=bad-python3-import
import urlparse
from urllib2 import urlopen

from email.utils import formataddr
from lxml import etree
Expand All @@ -16,7 +22,6 @@
from odoo.modules import get_module_resource
from odoo.osv.expression import get_unaccent_wrapper
from odoo.exceptions import UserError, ValidationError
from odoo.osv.orm import browse_record

# Global variables used for the warning fields declared on the res.partner
# in the following modules : sale, purchase, account, stock
Expand Down Expand Up @@ -702,7 +707,7 @@ def _get_gravatar_image(self, email):
email_hash = hashlib.md5(email.lower()).hexdigest()
url = "https://www.gravatar.com/avatar/" + email_hash
try:
image_content = urllib2.urlopen(url + "?d=404&s=128", timeout=5).read()
image_content = urlopen(url + "?d=404&s=128", timeout=5).read()
gravatar_image = base64.b64encode(image_content)
except Exception:
pass
Expand Down
Loading

0 comments on commit ca37244

Please sign in to comment.