berkerpeksag: Thanks for the PR, @Kodiologist! I will review this later today. Could you also add release notes for #85 and #100 to `docs/changelog.rst`?
Kodiologist: You're welcome. Done.
diff --git a/.travis.yml b/.travis.yml
index e6362f4..c0e59fa 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,18 +1,13 @@
 sudo: false  # run travis jobs in containers
 language: python
 python:
-  - 2.6
   - 2.7
-  - 3.3
   - 3.4
   - 3.5
   - 3.6
   - pypy
   - pypy3.5
   - 3.7-dev
-matrix:
-  allow_failures:
-  - python: 2.6
 cache: pip
 install:
   - pip install tox-travis
diff --git a/AUTHORS b/AUTHORS
index 9949ccc..f00747a 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -13,3 +13,4 @@ And with some modifications based on Armin's code:
 * Ryan Gonzalez <rymg19@gmail.com>
 * Lenny Truong <leonardtruong@protonmail.com>
 * Radomír Bosák <radomir.bosak@gmail.com>
+* Kodi Arfer <git@arfer.net>
diff --git a/astor/code_gen.py b/astor/code_gen.py
index 8e1e6fa..e37b320 100644
--- a/astor/code_gen.py
+++ b/astor/code_gen.py
@@ -18,6 +18,7 @@ this code came from here (in 2012):
 """
 
 import ast
+import math
 import sys
 
 from .op_util import get_op_symbol, get_op_precedence, Precedence
@@ -638,22 +639,38 @@ class SourceGenerator(ExplicitNodeVisitor):
                   # constants
                   new=sys.version_info >= (3, 0)):
         with self.delimit(node) as delimiters:
-            s = repr(node.n)
-
-            # Deal with infinities -- if detected, we can
-            # generate them with 1e1000.
-            signed = s.startswith('-')
-            if s[signed].isalpha():
-                im = s[-1] == 'j' and 'j' or ''
-                assert s[signed:signed + 3] == 'inf', s
-                s = '%s1e1000%s' % ('-' if signed else '', im)
+            x = node.n
+
+            def part(p, imaginary):
+                # Represent infinity as 1e1000 and NaN as 1e1000-1e1000.
+                s = 'j' if imaginary else ''
+                if math.isinf(p):
+                    if p < 0:
+                        return '-1e1000' + s
+                    return '1e1000' + s
+                if math.isnan(p):
+                    return '(1e1000%s-1e1000%s)' % (s, s)
+                return repr(p) + s
+
+            real = part(x.real if isinstance(x, complex) else x, imaginary=False)
+            if isinstance(x, complex):
+                imag = part(x.imag, imaginary=True)
+                if x.real == 0:
+                    s = imag
+                elif x.imag == 0:
+                    s = '(%s+0j)' % real
+                else:
+                    # x has nonzero real and imaginary parts.
+                    s = '(%s%s%s)' % (real, ['+', ''][imag.startswith('-')], imag)
+            else:
+                s = real
             self.write(s)
 
             # The Python 2.x compiler merges a unary minus
             # with a number.  This is a premature optimization
             # that we deal with here...
             if not new and delimiters.discard:
-                if signed:
+                if not isinstance(node.n, complex) and node.n < 0:
                     pow_lhs = Precedence.Pow + 1
                     delimiters.discard = delimiters.pp != pow_lhs
                 else:
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 706d322..9135d8b 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -22,6 +22,9 @@ New features
 
 .. _`Issue 86`: https://github.com/berkerpeksag/astor/issues/86
 
+* Dropped support for Python 2.6 and Python 3.3. Even the latest version of pip
+  dropped its support for both of these versions.
+
 Bug fixes
 ~~~~~~~~~
 
@@ -37,6 +40,12 @@ Bug fixes
 .. _`Issue 89`: https://github.com/berkerpeksag/astor/issues/89
 .. _`Issue 101`: https://github.com/berkerpeksag/astor/issues/101
 
+* Improved code generation to support ``ast.Num`` nodes containing infinities
+  or NaNs.
+  (Reported and fixed by Kodi Arfer in `Issue 85`_ and `Issue 100`_.)
+
+.. _`Issue 85`: https://github.com/berkerpeksag/astor/issues/85
+.. _`Issue 100`: https://github.com/berkerpeksag/astor/issues/100
 
 0.6.2 - 2017-11-11
 ------------------
diff --git a/setup.cfg b/setup.cfg
index 8faaf1f..cf0bb95 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -33,6 +33,7 @@ classifiers =
 zip_safe = True
 include_package_data = True
 packages = find:
+python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
 
 [options.packages.find]
 exclude = tests
diff --git a/tox.ini b/tox.ini
index e364485..5f6ddf0 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist = py26, py27, py33, py34, py35, py36, pypy, pypy3.5
+envlist = py27, py34, py35, py36, pypy, pypy3.5
 skipsdist = True
 
 [testenv]
@@ -7,4 +7,4 @@ usedevelop = True
 commands = nosetests -v --nocapture {posargs}
 deps =
  -rrequirements-tox.txt
- py2{6,7},pypy: unittest2
+ py27,pypy: unittest2

Can't generate code containing ast.Num(NaN)
Somewhat related to #82, the `codegen` of astor 0.5 would produce `nan` if given a `Num` node that contained NaN. In astor 0.6, the following raises an `AssertionError` in code_gen.py:

```py
import ast, astor

nan = 1e1000 - 1e1000

print(astor.code_gen.to_source(
         ast.Module([ast.Expr(ast.BinOp(
             ast.Num(1.0),
             ast.Add(),
             ast.Num(nan)))])))
```

While Python has no NaN literal, you could represent `ast.Num(nan)` as an expression (such as `1e1000 - 1e1000`) if you want to generate real Python. See hylang/hy#1447.

diff --git a/tests/test_code_gen.py b/tests/test_code_gen.py
index a71a939..b8c1c6a 100644
--- a/tests/test_code_gen.py
+++ b/tests/test_code_gen.py
@@ -8,6 +8,7 @@ Copyright (c) 2015 Patrick Maupin
 """
 
 import ast
+import math
 import sys
 import textwrap
 
@@ -22,6 +23,8 @@ import astor
 def canonical(srctxt):
     return textwrap.dedent(srctxt).strip()
 
+def astornum(x):
+    return eval(astor.to_source(ast.Expression(body=ast.Num(n=x))))
 
 class Comparisons(object):
 
@@ -245,6 +248,14 @@ class CodegenTestCase(unittest.TestCase, Comparisons):
         """
         self.assertAstRoundtripsGtVer(source, (2, 7))
 
+    def test_complex(self):
+        source = """
+            (3) + (4j) + (1+2j) + (1+0j)
+        """
+        self.assertAstRoundtrips(source)
+
+        self.assertIsInstance(astornum(1+0j), complex)
+
     def test_inf(self):
         source = """
             (1e1000) + (-1e1000) + (1e1000j) + (-1e1000j)
@@ -259,6 +270,19 @@ class CodegenTestCase(unittest.TestCase, Comparisons):
         # Returns 'a = 1e1000'.
         self.assertSrcDoesNotRoundtrip(source)
 
+        self.assertIsInstance(astornum((1e1000+1e1000)+0j), complex)
+
+    def test_nan(self):
+        self.assertTrue(math.isnan(astornum(float('nan'))))
+
+        v = astornum(complex(-1e1000, float('nan')))
+        self.assertEqual(v.real, -1e1000)
+        self.assertTrue(math.isnan(v.imag))
+
+        v = astornum(complex(float('nan'), -1e1000))
+        self.assertTrue(math.isnan(v.real))
+        self.assertEqual(v.imag, -1e1000)
+
     def test_unary(self):
         source = """
             -(1) + ~(2) + +(3)

pip install -e .
parse_log_pytest
pytest
pytest
3.6
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
-e git+https://github.com/berkerpeksag/astor.git@f87ae3b55f9ea403b4c04fb6cd9b0ab675aaa496#egg=astor
attrs @ file:///opt/conda/conda-bld/attrs_1642510447205/work
certifi==2021.5.30
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1631916693255/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
more-itertools @ file:///tmp/build/80754af9/more-itertools_1637733554872/work
packaging @ file:///tmp/build/80754af9/packaging_1637314298585/work
pluggy @ file:///tmp/build/80754af9/pluggy_1615976315926/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work
pytest==6.2.4
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
typing_extensions @ file:///opt/conda/conda-bld/typing_extensions_1647553014482/work
zipp @ file:///tmp/build/80754af9/zipp_1633618647012/work

name: astor
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=21.4.0=pyhd3eb1b0_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - importlib-metadata=4.8.1=py36h06a4308_0
  - importlib_metadata=4.8.1=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - more-itertools=8.12.0=pyhd3eb1b0_0
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=21.3=pyhd3eb1b0_0
  - pip=21.2.2=py36h06a4308_0
  - pluggy=0.13.1=py36h06a4308_0
  - py=1.11.0=pyhd3eb1b0_0
  - pyparsing=3.0.4=pyhd3eb1b0_0
  - pytest=6.2.4=py36h06a4308_2
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - toml=0.10.2=pyhd3eb1b0_0
  - typing_extensions=4.1.1=pyh06a4308_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.6.0=pyhd3eb1b0_0
  - zlib=1.2.13=h5eee18b_1
prefix: /opt/conda/envs/astor

tests/test_code_gen.py::CodegenTestCase::test_inf tests/test_code_gen.py::CodegenTestCase::test_nan
tests/test_code_gen.py::CodegenTestCase::test_annassign tests/test_code_gen.py::CodegenTestCase::test_arguments tests/test_code_gen.py::CodegenTestCase::test_async_comprehension tests/test_code_gen.py::CodegenTestCase::test_async_def_with_for tests/test_code_gen.py::CodegenTestCase::test_class_definition_with_starbases_and_kwargs tests/test_code_gen.py::CodegenTestCase::test_compile_types tests/test_code_gen.py::CodegenTestCase::test_complex tests/test_code_gen.py::CodegenTestCase::test_comprehension tests/test_code_gen.py::CodegenTestCase::test_del_statement tests/test_code_gen.py::CodegenTestCase::test_dictionary_literals tests/test_code_gen.py::CodegenTestCase::test_docstring_class tests/test_code_gen.py::CodegenTestCase::test_docstring_function tests/test_code_gen.py::CodegenTestCase::test_docstring_method tests/test_code_gen.py::CodegenTestCase::test_docstring_module tests/test_code_gen.py::CodegenTestCase::test_double_await tests/test_code_gen.py::CodegenTestCase::test_elif tests/test_code_gen.py::CodegenTestCase::test_fstring_trailing_newline tests/test_code_gen.py::CodegenTestCase::test_fstrings tests/test_code_gen.py::CodegenTestCase::test_imports tests/test_code_gen.py::CodegenTestCase::test_matrix_multiplication tests/test_code_gen.py::CodegenTestCase::test_multiple_call_unpackings tests/test_code_gen.py::CodegenTestCase::test_non_string_leakage tests/test_code_gen.py::CodegenTestCase::test_output_formatting tests/test_code_gen.py::CodegenTestCase::test_pass_arguments_node tests/test_code_gen.py::CodegenTestCase::test_pow tests/test_code_gen.py::CodegenTestCase::test_right_hand_side_dictionary_unpacking tests/test_code_gen.py::CodegenTestCase::test_slicing tests/test_code_gen.py::CodegenTestCase::test_try_expect tests/test_code_gen.py::CodegenTestCase::test_tuple_corner_cases tests/test_code_gen.py::CodegenTestCase::test_unary tests/test_code_gen.py::CodegenTestCase::test_unicode_literals tests/test_code_gen.py::CodegenTestCase::test_with tests/test_code_gen.py::CodegenTestCase::test_yield
docs/changelog.rst .travis.yml setup.cfg tox.ini AUTHORS astor/code_gen.py
docs/changelog.rst .travis.yml setup.cfg tox.ini AUTHORS astor/code_gen.py