Repository: berkerpeksag/astor

Issue:
Suspected API breakage from 0.6 to 0.6.1
Hi, our builds started failing with `AttributeError: module 'astor' has no attribute 'codegen'`:

https://travis-ci.org/google/tangent/jobs/300437272

I wonder if it's related to the new 0.6.1 release?

Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 419cb0e..64bedd8 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -9,6 +9,9 @@ python:
   - 3.6
   - pypy
   - pypy3.3-5.2-alpha1
+matrix:
+  allow_failures:
+  - python: 2.6
 cache: pip
 install:
   - pip install tox-travis
diff --git a/astor/__init__.py b/astor/__init__.py
index de48a5c..bb96de4 100644
--- a/astor/__init__.py
+++ b/astor/__init__.py
@@ -19,7 +19,7 @@ from .op_util import get_op_symbol, get_op_precedence  # NOQA
 from .op_util import symbol_data  # NOQA
 from .tree_walk import TreeWalk  # NOQA
 
-__version__ = '0.6.1'
+__version__ = '0.6.2'
 
 parse_file = code_to_ast.parse_file
 
@@ -34,6 +34,7 @@ codetoast = code_to_ast
 dump = dump_tree
 all_symbols = symbol_data
 treewalk = tree_walk
+codegen = code_gen
 """
 
 exec(deprecated)
diff --git a/docs/changelog.rst b/docs/changelog.rst
index fb259ff..54bc9b7 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -2,6 +2,25 @@
 Release Notes
 =============
 
+0.6.2 - 2017-11-11
+------------------
+
+Bug fixes
+---------
+
+* Restore backwards compatibility that was broken after 0.6.1.
+  You can now continue to use the following pattern::
+
+     import astor
+
+     class SpamCodeGenerator(astor.codegen.SourceGenerator):
+         ...
+
+  (Reported by Dan Moldovan and fixed by Berker Peksag in `Issue 87`_.)
+
+.. _`Issue 87`: https://github.com/berkerpeksag/astor/issues/87
+
+
 0.6.1 - 2017-11-11
 ------------------
 
diff --git a/tox.ini b/tox.ini
index 5f8e1ae..5149f5c 100644
--- a/tox.ini
+++ b/tox.ini
@@ -4,7 +4,7 @@ skipsdist = True
 
 [testenv]
 usedevelop = True
-commands = nosetests -v
+commands = nosetests -v --nocapture {posargs}
 deps =
  -rrequirements-tox.txt
  py2{6,7},pypy: unittest2


Test patch:
diff --git a/tests/support.py b/tests/support.py
new file mode 100644
index 0000000..db8c4ea
--- /dev/null
+++ b/tests/support.py
@@ -0,0 +1,44 @@
+import importlib
+import sys
+
+
+def _save_and_remove_module(name, orig_modules):
+    """Helper function to save and remove a module from sys.modules
+    Raise ImportError if the module can't be imported.
+    """
+    # try to import the module and raise an error if it can't be imported
+    if name not in sys.modules:
+        __import__(name)
+        del sys.modules[name]
+    for modname in list(sys.modules):
+        if modname == name or modname.startswith(name + '.'):
+            orig_modules[modname] = sys.modules[modname]
+            del sys.modules[modname]
+
+
+def import_fresh_module(name, fresh=(), blocked=()):
+    """Import and return a module, deliberately bypassing sys.modules.
+
+    This function imports and returns a fresh copy of the named Python module
+    by removing the named module from sys.modules before doing the import.
+    Note that unlike reload, the original module is not affected by
+    this operation.
+    """
+    orig_modules = {}
+    names_to_remove = []
+    _save_and_remove_module(name, orig_modules)
+    try:
+        for fresh_name in fresh:
+            _save_and_remove_module(fresh_name, orig_modules)
+        for blocked_name in blocked:
+            if not _save_and_block_module(blocked_name, orig_modules):
+                names_to_remove.append(blocked_name)
+        fresh_module = importlib.import_module(name)
+    except ImportError:
+        fresh_module = None
+    finally:
+        for orig_name, module in orig_modules.items():
+            sys.modules[orig_name] = module
+        for name_to_remove in names_to_remove:
+            del sys.modules[name_to_remove]
+    return fresh_module
diff --git a/tests/test_misc.py b/tests/test_misc.py
index 983de40..7a7f5a4 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -8,6 +8,8 @@ except ImportError:
 
 import astor
 
+from .support import import_fresh_module
+
 
 class GetSymbolTestCase(unittest.TestCase):
 
@@ -22,16 +24,27 @@ class PublicAPITestCase(unittest.TestCase):
     def test_aliases(self):
         self.assertIs(astor.parse_file, astor.code_to_ast.parse_file)
 
-
-class DeprecationTestCase(unittest.TestCase):
-
-    def test_deprecate_parsefile(self):
-        with self.assertWarns(DeprecationWarning):
-            astor.parsefile(__file__)
-
-    def test_deprecate_astor_codegen(self):
-        with self.assertWarns(DeprecationWarning):
+    def test_codegen_from_root(self):
+        with self.assertWarns(DeprecationWarning) as cm:
+            astor = import_fresh_module('astor')
+            astor.codegen.SourceGenerator
+        self.assertEqual(len(cm.warnings), 1)
+        # This message comes from 'astor/__init__.py'.
+        self.assertEqual(
+            str(cm.warnings[0].message),
+            'astor.codegen is deprecated.  Please use astor.code_gen.'
+        )
+
+    def test_codegen_as_submodule(self):
+        with self.assertWarns(DeprecationWarning) as cm:
             import astor.codegen
+        self.assertEqual(len(cm.warnings), 1)
+        # This message comes from 'astor/codegen.py'.
+        self.assertEqual(
+            str(cm.warnings[0].message),
+            'astor.codegen module is deprecated. Please import '
+            'astor.code_gen module instead.'
+        )
 
 
 class FastCompareTestCase(unittest.TestCase):