Repository: sympy/sympy

Issue:
MpmathPrinter.doprint(expr) fails when expr is string or list
Lambdify-ing string or list expressions using MpmathPrinter fails, since doprint attempts to replace log2 and log1p to log by Expr.replace() in [sympy/printing/pycode.py#L210-L213](https://github.com/sympy/sympy/blob/master/sympy/printing/pycode.py#L210-L213).
As a result, following tests fail:
```
_______________________________________________________________________________________________________________________
________________________________ sympy\utilities\tests\test_lambdify.py:test_str_args _________________________________
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\tests\test_lambdify.py", line 49, in test_str_args
    f = lambdify('x,y,z', 'z,y,x')
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 409, in lambdify
    lstr = lambdastr(args, expr, printer=printer, dummify=dummify)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 583, in lambdastr
    expr = lambdarepr(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 512, in <lambda>
    lambdarepr = lambda expr: printer.doprint(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\printing\pycode.py", line 212, in doprint
    expr = expr.replace(log2, lambda arg: arg.rewrite(log))
TypeError: expected a string or other character buffer object
_______________________________________________________________________________________________________________________
_______________________________ sympy\utilities\tests\test_lambdify.py:test_trig_float ________________________________
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\tests\test_lambdify.py", line 281, in test_trig_float
    f = lambdify([x], [cos(x), sin(x)])
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 409, in lambdify
    lstr = lambdastr(args, expr, printer=printer, dummify=dummify)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 583, in lambdastr
    expr = lambdarepr(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 512, in <lambda>
    lambdarepr = lambda expr: printer.doprint(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\printing\pycode.py", line 212, in doprint
    expr = expr.replace(log2, lambda arg: arg.rewrite(log))
AttributeError: 'list' object has no attribute 'replace'
_______________________________________________________________________________________________________________________
__________________________________ sympy\utilities\tests\test_lambdify.py:test_docs ___________________________________
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\tests\test_lambdify.py", line 290, in test_docs
    f = lambdify([x, y, z], [z, y, x])
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 409, in lambdify
    lstr = lambdastr(args, expr, printer=printer, dummify=dummify)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 583, in lambdastr
    expr = lambdarepr(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 512, in <lambda>
    lambdarepr = lambda expr: printer.doprint(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\printing\pycode.py", line 212, in doprint
    expr = expr.replace(log2, lambda arg: arg.rewrite(log))
AttributeError: 'list' object has no attribute 'replace'
_______________________________________________________________________________________________________________________
______________________________ sympy\utilities\tests\test_lambdify.py:test_lambdify_imps ______________________________
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\tests\test_lambdify.py", line 663, in test_lambdify_imps
    lam = lambdify(x, [f(x), x])
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 409, in lambdify
    lstr = lambdastr(args, expr, printer=printer, dummify=dummify)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 583, in lambdastr
    expr = lambdarepr(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\utilities\lambdify.py", line 512, in <lambda>
    lambdarepr = lambda expr: printer.doprint(expr)
  File "c:\users\wisec\documents\github\sympy\sympy\printing\pycode.py", line 212, in doprint
    expr = expr.replace(log2, lambda arg: arg.rewrite(log))
AttributeError: 'list' object has no attribute 'replace'
```
Is replacing necessary? If so, I think it should check type of expr before replacing.

Discussion:

Patch:
diff --git a/sympy/printing/pycode.py b/sympy/printing/pycode.py
index e68f6595cd..7cf950bf29 100644
--- a/sympy/printing/pycode.py
+++ b/sympy/printing/pycode.py
@@ -202,17 +202,10 @@ class MpmathPrinter(PythonCodePrinter):
     printmethod = "_mpmathcode"
 
     _kf = dict(chain(
-        PythonCodePrinter._kf.items(),
+        _known_functions.items(),
         [(k, 'mpmath.' + v) for k, v in _known_functions_mpmath.items()]
     ))
 
-    def doprint(self, expr, **kwargs):
-        from sympy.functions import log
-        from sympy.codegen.cfunctions import log2, log1p
-        expr = expr.replace(log2, lambda arg: arg.rewrite(log))
-        expr = expr.replace(log1p, lambda arg: arg.rewrite(log))
-        return super(MpmathPrinter, self).doprint(expr, **kwargs)
-
     def _print_Integer(self, e):
         return '%s(%d)' % (self._module_format('mpmath.mpf'), e)
 
@@ -235,6 +228,14 @@ def _print_lowergamma(self,e): #printer for the lowergamma functioin
         return "{0}({1}, 0, {2})".format(
             self._module_format('mpmath.gammainc'), self._print(e.args[0]), self._print(e.args[1]))
 
+    def _print_log2(self, e):
+        return '{0}({1})/{0}(2)'.format(
+            self._module_format('mpmath.log'), self._print(e.args[0]))
+
+    def _print_log1p(self, e):
+        return '{0}({1}+1)'.format(
+            self._module_format('mpmath.log'), self._print(e.args[0]))
+
 for k in MpmathPrinter._kf:
     setattr(MpmathPrinter, '_print_%s' % k, _print_known_func)
 


Test patch:
diff --git a/sympy/utilities/tests/test_lambdify.py b/sympy/utilities/tests/test_lambdify.py
index 2c5d5fee52..1d773f607e 100644
--- a/sympy/utilities/tests/test_lambdify.py
+++ b/sympy/utilities/tests/test_lambdify.py
@@ -302,7 +302,7 @@ def test_math():
 
 def test_sin():
     f = lambdify(x, sin(x)**2)
-    assert isinstance(f(2), float)
+    assert isinstance(f(2), (float, mpmath.ctx_mp_python.mpf))
     f = lambdify(x, sin(x)**2, modules="math")
     assert isinstance(f(2), float)