Fixes issues 1035 and 1178:
-- Issue 1035: Have sympify("x^2") return x**2
-- Issue 1178: Don't use mutable keyword args.
---
sympy/core/sympify.py | 6 +++++-
sympy/core/tests/test_sympify.py | 1 +
2 files changed, 6 insertions(+), 1 deletions(-)
diff --git a/sympy/core/sympify.py b/sympy/core/sympify.py
index 77c035d..e1cba95 100644
--- a/sympy/core/sympify.py
+++ b/sympy/core/sympify.py
@@ -14,7 +14,7 @@ class SympifyError(ValueError):
return "Sympify of expression '%s' failed, because of exception being raised:\n%s: %s" % (self.expr, self.base_exc.__class__.__name__, str(self.base_exc))
-def sympify(a, locals= {}):
+def sympify(a, locals=None, convert_xor=True):
"""Converts an arbitrary expression to a type that can be used
inside sympy. For example, it will convert python int's into
instance of sympy.Rational, floats into intances of sympy.Real,
@@ -51,6 +51,8 @@ def sympify(a, locals= {}):
#
# When everything settles, let's refactor this.
# -- kirr
+ if locals is None:
+ locals = {}
if isinstance(a, Basic):
return a
if isinstance(a, BasicType):
@@ -114,6 +116,8 @@ def sympify(a, locals= {}):
a = str(a)
try:
+ if convert_xor:
+ a = a.replace('^','**')
import ast_parser
return ast_parser.SymPyParser(local_dict=locals).parse_expr(a)
except Exception, exc:
diff --git a/sympy/core/tests/test_sympify.py b/sympy/core/tests/test_sympify.py
index e0b26a5..2dfc667 100644
--- a/sympy/core/tests/test_sympify.py
+++ b/sympy/core/tests/test_sympify.py
@@ -29,6 +29,7 @@ def test_sympify2():
def test_sympify3():
assert sympify("x**3") == x**3
+ assert sympify("x^3") == x**3
assert sympify("1/2") == Integer(1)/2
raises(SympifyError, "_sympify('x**3')")
--
1.6.0.3
Let's push this in before a release.
Ondrej