From 283304f9a2032b200e654371874b011fff45445c Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Wed, 25 Jan 2012 14:24:07 +0100 Subject: [PATCH] [IMP] warnings: turn warnings.warn into logging.warning: Warnings are handled with the other logs (and not always sent to stderr), they also appear under a module __name__ channel instead of py.warn. The disadvantage is that there is no longer specific warnings, such as pending deprecation warning or deprecation warning. bzr revid: vmt@openerp.com-20120125132407-u33idc0qh7ecs1i5 --- openerp/loglevels.py | 17 +++++++++-------- openerp/osv/fields.py | 35 +++++++++++++++++++++++------------ openerp/osv/orm.py | 5 ++--- openerp/report/pyPdf/pdf.py | 1 - openerp/sql_db.py | 4 +--- openerp/tools/misc.py | 1 - openerp/wizard/__init__.py | 3 +-- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/openerp/loglevels.py b/openerp/loglevels.py index f678afe62d6..7d3b708c428 100644 --- a/openerp/loglevels.py +++ b/openerp/loglevels.py @@ -21,7 +21,6 @@ import sys import logging -import warnings LOG_NOTSET = 'notset' LOG_DEBUG_SQL = 'debug_sql' @@ -44,20 +43,22 @@ logging.addLevelName(logging.DEBUG_SQL, 'DEBUG_SQL') logging.TEST = logging.INFO - 5 logging.addLevelName(logging.TEST, 'TEST') +_logger = logging.getLogger(__name__) + class Logger(object): def __init__(self): - warnings.warn("The netsvc.Logger API shouldn't be used anymore, please " - "use the standard `logging.getLogger` API instead", - PendingDeprecationWarning, stacklevel=2) + _logger.warning( + "The netsvc.Logger API shouldn't be used anymore, please " + "use the standard `logging.getLogger` API instead.") super(Logger, self).__init__() def notifyChannel(self, name, level, msg): - warnings.warn("notifyChannel API shouldn't be used anymore, please use " - "the standard `logging` module instead", - PendingDeprecationWarning, stacklevel=2) + _logger.warning( + "notifyChannel API shouldn't be used anymore, please use " + "the standard `logging` module instead.") from service.web_services import common - log = logging.getLogger(ustr(name)) + log = logging.getLogger(__name__ + '(deprecated channel: ' + ustr(name) + ')') if level in [LOG_DEBUG_RPC, LOG_TEST] and not hasattr(log, level): fct = lambda msg, *args, **kwargs: log.log(getattr(logging, level.upper()), msg, *args, **kwargs) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index 871fc32f884..224c4be17c5 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -34,10 +34,10 @@ import base64 import datetime as DT +import logging import re import string import sys -import warnings import xmlrpclib from psycopg2 import Binary @@ -48,6 +48,8 @@ from openerp.tools.translate import _ from openerp.tools import float_round, float_repr import simplejson +_logger = logging.getLogger(__name__) + def _symbol_set(symb): if symb == None or symb == False: return None @@ -139,8 +141,10 @@ class boolean(_column): def __init__(self, string='unknown', required=False, **args): super(boolean, self).__init__(string=string, required=required, **args) if required: - warnings.warn("Making a boolean field `required` has no effect, as NULL values are " - "automatically turned into False", PendingDeprecationWarning, stacklevel=2) + _logger.warning( + "required=True is deprecated: making a boolean field" + " `required` has no effect, as NULL values are " + "automatically turned into False.") class integer(_column): _type = 'integer' @@ -152,8 +156,10 @@ class integer(_column): def __init__(self, string='unknown', required=False, **args): super(integer, self).__init__(string=string, required=required, **args) if required: - warnings.warn("Making an integer field `required` has no effect, as NULL values are " - "automatically turned into 0", PendingDeprecationWarning, stacklevel=2) + _logger.warning( + "required=True is deprecated: making an integer field" + " `required` has no effect, as NULL values are " + "automatically turned into 0.") class integer_big(_column): """Experimental 64 bit integer column type, currently unused. @@ -176,8 +182,10 @@ class integer_big(_column): def __init__(self, string='unknown', required=False, **args): super(integer_big, self).__init__(string=string, required=required, **args) if required: - warnings.warn("Making an integer_big field `required` has no effect, as NULL values are " - "automatically turned into 0", PendingDeprecationWarning, stacklevel=2) + _logger.warning( + "required=True is deprecated: making an integer_big field" + " `required` has no effect, as NULL values are " + "automatically turned into 0.") class reference(_column): _type = 'reference' @@ -238,8 +246,10 @@ class float(_column): # synopsis: digits_compute(cr) -> (precision, scale) self.digits_compute = digits_compute if required: - warnings.warn("Making a float field `required` has no effect, as NULL values are " - "automatically turned into 0.0", PendingDeprecationWarning, stacklevel=2) + _logger.warning( + "required=True is deprecated: making a float field" + " `required` has no effect, as NULL values are " + "automatically turned into 0.0.") def digits_change(self, cr): if self.digits_compute: @@ -355,7 +365,7 @@ class one2one(_column): _deprecated = True def __init__(self, obj, string='unknown', **args): - warnings.warn("The one2one field doesn't work anymore", DeprecationWarning) + _logger.warning("The one2one field is deprecated and doesn't work anymore.") _column.__init__(self, string=string, **args) self._obj = obj @@ -620,8 +630,9 @@ class many2many(_column): for id in ids: res[id] = [] if offset: - warnings.warn("Specifying offset at a many2many.get() may produce unpredictable results.", - DeprecationWarning, stacklevel=2) + _logger.warning( + "Specifying offset at a many2many.get() is deprecated and may" + " produce unpredictable results.") obj = model.pool.get(self._obj) rel, id1, id2 = self._sql_names(model) diff --git a/openerp/osv/orm.py b/openerp/osv/orm.py index 380193dbce6..a0764ebb09a 100644 --- a/openerp/osv/orm.py +++ b/openerp/osv/orm.py @@ -52,7 +52,6 @@ import re import simplejson import time import types -import warnings from lxml import etree import fields @@ -4745,8 +4744,8 @@ class BaseModel(object): return [x[0] for x in cr.fetchall()] def check_recursion(self, cr, uid, ids, context=None, parent=None): - warnings.warn("You are using deprecated %s.check_recursion(). Please use the '_check_recursion()' instead!" % \ - self._name, DeprecationWarning, stacklevel=3) + _logger.warning("You are using deprecated %s.check_recursion(). Please use the '_check_recursion()' instead!" % \ + self._name) assert parent is None or parent in self._columns or parent in self._inherit_fields,\ "The 'parent' parameter passed to check_recursion() must be None or a valid field name" return self._check_recursion(cr, uid, ids, context, parent) diff --git a/openerp/report/pyPdf/pdf.py b/openerp/report/pyPdf/pdf.py index bf60d0157dc..53c0b428c14 100644 --- a/openerp/report/pyPdf/pdf.py +++ b/openerp/report/pyPdf/pdf.py @@ -50,7 +50,6 @@ except ImportError: import filters import utils -import warnings from generic import * from utils import readNonWhitespace, readUntilWhitespace, ConvertFunctionsToVirtualList diff --git a/openerp/sql_db.py b/openerp/sql_db.py index 7abc15e8e06..e5acf1fa401 100644 --- a/openerp/sql_db.py +++ b/openerp/sql_db.py @@ -42,7 +42,6 @@ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ from psycopg2.pool import PoolError from psycopg2.psycopg1 import cursor as psycopg1cursor from threading import currentThread -import warnings psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) @@ -471,8 +470,7 @@ class Connection(object): def __nonzero__(self): """Check if connection is possible""" try: - warnings.warn("You use an expensive function to test a connection.", - DeprecationWarning, stacklevel=1) + _logger.warning("__nonzero__() is deprecated. (It is too expensive to test a connection.)") cr = self.cursor() cr.close() return True diff --git a/openerp/tools/misc.py b/openerp/tools/misc.py index 1285f64e65f..a5c72900fc7 100644 --- a/openerp/tools/misc.py +++ b/openerp/tools/misc.py @@ -37,7 +37,6 @@ import socket import sys import threading import time -import warnings import zipfile from collections import defaultdict from datetime import datetime diff --git a/openerp/wizard/__init__.py b/openerp/wizard/__init__.py index 2f5f085ae08..3c22f802279 100644 --- a/openerp/wizard/__init__.py +++ b/openerp/wizard/__init__.py @@ -32,7 +32,6 @@ import openerp.pooler as pooler from openerp.osv.osv import except_osv from openerp.osv.orm import except_orm import sys -import warnings _logger = logging.getLogger(__name__) @@ -51,7 +50,7 @@ class interface(netsvc.Service): def __init__(self, name): assert not self.exists('wizard.'+name), 'The wizard "%s" already exists!' % (name,) - warnings.warn( + _logger.warning( "The wizard %s uses the deprecated openerp.wizard.interface class.\n" "It must use the openerp.osv.TransientModel class instead." % \ name, DeprecationWarning, stacklevel=3)