Kaydet (Commit) 7189366e authored tarafından Patrick Maupin's avatar Patrick Maupin Kaydeden (comit) GitHub

Fix new deprecation warnings (#72)

* Fix new deprecation warnings
  - Change to DeprecationWarning to reduce noise in other testers
  - Set stack level so warning appears with proper source line
  - Check for warning in test

* Explicitly verify warning content

  - Bonus:  works with all testrunners.
üst bc147ece
...@@ -54,12 +54,12 @@ def deprecate(): ...@@ -54,12 +54,12 @@ def deprecate():
msg = "astor.%s is deprecated. Please use astor.%s." % (deprecated_name, target_name) msg = "astor.%s is deprecated. Please use astor.%s." % (deprecated_name, target_name)
if callable(target_func): if callable(target_func):
def newfunc(*args, **kwarg): def newfunc(*args, **kwarg):
warnings.warn(msg, FutureWarning) warnings.warn(msg, DeprecationWarning, stacklevel=2)
return target_func(*args, **kwarg) return target_func(*args, **kwarg)
else: else:
class ModProxy: class ModProxy:
def __getattr__(self, name): def __getattr__(self, name):
warnings.warn(msg, FutureWarning) warnings.warn(msg, DeprecationWarning, stacklevel=2)
return getattr(target_func, name) return getattr(target_func, name)
newfunc = ModProxy() newfunc = ModProxy()
......
...@@ -10,6 +10,7 @@ Copyright (c) 2015 Patrick Maupin ...@@ -10,6 +10,7 @@ Copyright (c) 2015 Patrick Maupin
import ast import ast
import sys import sys
import textwrap import textwrap
import warnings
try: try:
import unittest2 as unittest import unittest2 as unittest
...@@ -413,9 +414,26 @@ class CodegenTestCase(unittest.TestCase): ...@@ -413,9 +414,26 @@ class CodegenTestCase(unittest.TestCase):
self.assertEqual('(%s)' % code.strip(), dsttxt.strip()) self.assertEqual('(%s)' % code.strip(), dsttxt.strip())
def test_deprecation(self): def test_deprecation(self):
ast1 = astor.code_to_ast.parse_file(__file__) with warnings.catch_warnings(record=True) as w:
ast2 = astor.parsefile(__file__) # Cause all warnings to always be triggered.
self.assertEqual(astor.to_source(ast1), astor.codegen.to_source(ast2)) warnings.simplefilter("always")
ast1 = astor.code_to_ast.parse_file(__file__)
src1 = astor.to_source(ast1)
ast2 = astor.parsefile(__file__)
src2 = astor.codegen.to_source(ast2)
self.assertEqual(len(w), 2)
w = [warnings.formatwarning(x.message, x.category,
x.filename, x.lineno) for x in w]
w = [x.rsplit(':', 1)[-1].strip() for x in w]
self.assertEqual(w[0], 'astor.parsefile is deprecated. '
'Please use astor.code_to_ast.parse_file.\n'
' ast2 = astor.parsefile(__file__)')
self.assertEqual(w[1], 'astor.codegen is deprecated. '
'Please use astor.code_gen.\n'
' src2 = astor.codegen.to_source(ast2)')
self.assertEqual(src1, src2)
def test_unicode_literals(self): def test_unicode_literals(self):
source = """ source = """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment