Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 31950:cc70c6dbac30
util: add a way to issue deprecation warning without a UI object
Our current deprecation warning mechanism relies on ui object. They are case
where we cannot have access to the UI object. On a general basis we avoid using
the python mechanism for deprecation warning because up to Python 2.6 it is
exposing warning to unsuspecting user who cannot do anything to deal with them.
So we build a "safe" strategy to hide this warnings behind a flag in an
environment variable. The test runner set this flag so that tests show these
warning. This will help us marker API as deprecated for extensions to update
their code.
author | Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
---|---|
date | Tue, 04 Apr 2017 11:03:29 +0200 |
parents | f3b80537a70d |
children | a34b5e7c6683 |
comparison
equal
deleted
inserted
replaced
31949:eaf3819631c2 | 31950:cc70c6dbac30 |
---|---|
36 import sys | 36 import sys |
37 import tempfile | 37 import tempfile |
38 import textwrap | 38 import textwrap |
39 import time | 39 import time |
40 import traceback | 40 import traceback |
41 import warnings | |
41 import zlib | 42 import zlib |
42 | 43 |
43 from . import ( | 44 from . import ( |
44 encoding, | 45 encoding, |
45 error, | 46 error, |
153 def bitsfrom(container): | 154 def bitsfrom(container): |
154 bits = 0 | 155 bits = 0 |
155 for bit in container: | 156 for bit in container: |
156 bits |= bit | 157 bits |= bit |
157 return bits | 158 return bits |
159 | |
160 # python 2.6 still have deprecation warning enabled by default. We do not want | |
161 # to display anything to standard user so detect if we are running test and | |
162 # only use python deprecation warning in this case. | |
163 _dowarn = bool(encoding.environ.get('HGEMITWARNINGS')) | |
164 if _dowarn: | |
165 # explicitly unfilter our warning for python 2.7 | |
166 # | |
167 # The option of setting PYTHONWARNINGS in the test runner was investigated. | |
168 # However, module name set through PYTHONWARNINGS was exactly matched, so | |
169 # we cannot set 'mercurial' and have it match eg: 'mercurial.scmutil'. This | |
170 # makes the whole PYTHONWARNINGS thing useless for our usecase. | |
171 warnings.filterwarnings('default', '', DeprecationWarning, 'mercurial') | |
172 warnings.filterwarnings('default', '', DeprecationWarning, 'hgext') | |
173 warnings.filterwarnings('default', '', DeprecationWarning, 'hgext3rd') | |
174 | |
175 def nouideprecwarn(msg, version, stacklevel=1): | |
176 """Issue an python native deprecation warning | |
177 | |
178 This is a noop outside of tests, use 'ui.deprecwarn' when possible. | |
179 """ | |
180 if _dowarn: | |
181 msg += ("\n(compatibility will be dropped after Mercurial-%s," | |
182 " update your code.)") % version | |
183 warnings.warn(msg, DeprecationWarning, stacklevel + 1) | |
158 | 184 |
159 DIGESTS = { | 185 DIGESTS = { |
160 'md5': hashlib.md5, | 186 'md5': hashlib.md5, |
161 'sha1': hashlib.sha1, | 187 'sha1': hashlib.sha1, |
162 'sha512': hashlib.sha512, | 188 'sha512': hashlib.sha512, |