Repository: sympy/sympy

Issue:
limit(): AttributeError: 'NoneType' object has no attribute 'rewrite'
```python
import sympy as sp

def stirling(n):
    return sp.sqrt(2 * sp.pi * n) * (n / sp.E)**n

def binom_stirling(n, k):
    return stirling(n) / (stirling(k) * stirling(n - k))

n, k = sp.symbols('n k')

r0 = sp.simplify(binom_stirling(2*(3*k), k))
r1 = sp.simplify(binom_stirling(2*(3*k+1), k))
r = sp.simplify(r0 / r1)

print(sp.limit(r, k, sp.oo))
```

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-135-c7cb354b6b00> in <module>()
     13 r = sp.simplify(r0 / r1)
     14 
---> 15 print(sp.limit(r, k, sp.oo))

~/.local/lib/python3.5/site-packages/sympy/series/limits.py in limit(e, z, z0, dir)
     47     """
     48 
---> 49     return Limit(e, z, z0, dir).doit(deep=False)
     50 
     51 

~/.local/lib/python3.5/site-packages/sympy/series/limits.py in doit(self, **hints)
    178 
    179         try:
--> 180             r = gruntz(e, z, z0, dir)
    181             if r is S.NaN:
    182                 raise PoleError()

~/.local/lib/python3.5/site-packages/sympy/series/gruntz.py in gruntz(e, z, z0, dir)
    662     # It might be nicer to rewrite the exactly to what they were initially,
    663     # but that would take some work to implement.
--> 664     return r.rewrite('intractable', deep=True)

AttributeError: 'NoneType' object has no attribute 'rewrite'
```

Sympy version is 1.1.1.

Discussion:

Patch:
diff --git a/sympy/series/gruntz.py b/sympy/series/gruntz.py
index 325638f51b..cdaf4af822 100644
--- a/sympy/series/gruntz.py
+++ b/sympy/series/gruntz.py
@@ -124,6 +124,10 @@
 from sympy.functions import log, exp
 from sympy.series.order import Order
 from sympy.simplify.powsimp import powsimp, powdenest
+from sympy import cacheit
+
+from sympy.core.compatibility import reduce
+
 from sympy.utilities.misc import debug_decorator as debug
 from sympy.utilities.timeutils import timethis
 timeit = timethis('gruntz')
@@ -407,8 +411,13 @@ def sign(e, x):
 @debug
 @timeit
 @cacheit
-def limitinf(e, x):
-    """Limit e(x) for x-> oo"""
+def limitinf(e, x, leadsimp=False):
+    """Limit e(x) for x-> oo.
+
+    If ``leadsimp`` is True, an attempt is made to simplify the leading
+    term of the series expansion of ``e``. That may succeed even if
+    ``e`` cannot be simplified.
+    """
     # rewrite e in terms of tractable functions only
     e = e.rewrite('tractable', deep=True)
 
@@ -436,7 +445,11 @@ def limitinf(e, x):
             raise ValueError("Leading term should not be 0")
         return s*oo
     elif sig == 0:
-        return limitinf(c0, x)  # e0=0: lim f = lim c0
+        if leadsimp:
+            c0 = c0.simplify()
+        return limitinf(c0, x, leadsimp)  # e0=0: lim f = lim c0
+    else:
+        raise ValueError("{} could not be evaluated".format(sig))
 
 
 def moveup2(s, x):
@@ -642,10 +655,10 @@ def gruntz(e, z, z0, dir="+"):
 
     # convert all limits to the limit z->oo; sign of z is handled in limitinf
     r = None
-    if z0 is oo:
-        r = limitinf(e, z)
-    elif z0 is -oo:
-        r = limitinf(e.subs(z, -z), z)
+    if z0 == oo:
+        e0 = e
+    elif z0 == -oo:
+        e0 = e.subs(z, -z)
     else:
         if str(dir) == "-":
             e0 = e.subs(z, z0 - 1/z)
@@ -653,7 +666,11 @@ def gruntz(e, z, z0, dir="+"):
             e0 = e.subs(z, z0 + 1/z)
         else:
             raise NotImplementedError("dir must be '+' or '-'")
+
+    try:
         r = limitinf(e0, z)
+    except ValueError:
+        r = limitinf(e0, z, leadsimp=True)
 
     # This is a bit of a heuristic for nice results... we always rewrite
     # tractable functions in terms of familiar intractable ones.


Test patch:
diff --git a/sympy/series/tests/test_limits.py b/sympy/series/tests/test_limits.py
index aef027e9d9..e2e2fe98f7 100644
--- a/sympy/series/tests/test_limits.py
+++ b/sympy/series/tests/test_limits.py
@@ -206,6 +206,7 @@ def test_exponential():
     assert limit((1 + x/(2*n + 1))**n, n, oo) == exp(x/2)
     assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2)
     assert limit(1 + (1 + 1/x)**x, x, oo) == 1 + S.Exp1
+    assert limit((2 + 6*x)**x/(6*x)**x, x, oo) == exp(S('1/3'))
 
 
 @XFAIL
@@ -502,6 +503,11 @@ def test_issue_12555():
     assert limit((3**x + 2* x**10) / (x**10 + exp(x)), x, oo) is oo
 
 
+def test_issue_13332():
+    assert limit(sqrt(30)*5**(-5*x - 1)*(46656*x)**x*(5*x + 2)**(5*x + 5*S.Half) *
+                (6*x + 2)**(-6*x - 5*S.Half), x, oo) == Rational(25, 36)
+
+
 def test_issue_12564():
     assert limit(x**2 + x*sin(x) + cos(x), x, -oo) is oo
     assert limit(x**2 + x*sin(x) + cos(x), x, oo) is oo