summaryrefslogtreecommitdiff
path: root/numpy/matrixlib/defmatrix.py
diff options
context:
space:
mode:
authorLeon Weber <leon@leonweber.de>2013-05-23 19:24:28 +0200
committerLeon Weber <leon@leonweber.de>2013-05-29 15:35:45 +0200
commitfc4ade26312bd66474834aa0bee42265e7e15353 (patch)
treef359efccf95e83fa8f281f3c14bf39d621149b56 /numpy/matrixlib/defmatrix.py
parent8ff5e37bff03925da4c1b121b38188f9fd779b4d (diff)
downloadnumpy-fc4ade26312bd66474834aa0bee42265e7e15353.tar.gz
ENH: Fix SyntaxError when matrix() is called with invalid string
The numpy.matrix constructor uses eval(str.translate(table)) to convert input strings to numeric matrix contents. str.translate(table) will return empty string if str consists only of invalid characters, causing SyntaxError in eval(). This is confusing, as one would expect an exception like TypeError when trying to construct a matrix from invalid input. This fix makes sure eval() is only called if str is not empty and TypeError is raised otherwise.
Diffstat (limited to 'numpy/matrixlib/defmatrix.py')
-rw-r--r--numpy/matrixlib/defmatrix.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py
index 080e83286..1ca835af2 100644
--- a/numpy/matrixlib/defmatrix.py
+++ b/numpy/matrixlib/defmatrix.py
@@ -19,7 +19,12 @@ if sys.version_info[0] >= 3:
return None
_table = _NumCharTable()
def _eval(astr):
- return eval(astr.translate(_table))
+ str_ = astr.translate(_table)
+ if not str_:
+ raise TypeError("Invalid data string supplied: " + astr)
+ else:
+ return eval(str_)
+
else:
_table = [None]*256
for k in range(256):
@@ -34,7 +39,11 @@ else:
del k
def _eval(astr):
- return eval(astr.translate(_table,_todelete))
+ str_ = astr.translate(_table,_todelete)
+ if not str_:
+ raise TypeError("Invalid data string supplied: " + astr)
+ else:
+ return eval(str_)
def _convert_from_string(data):
rows = data.split(';')