Skip to content

Commit

Permalink
DEP: Deprecate class SafeEval (numpy#14335)
Browse files Browse the repository at this point in the history
* Deprecate class SafeEval
  • Loading branch information
maxwell-aladago authored and mattip committed Aug 24, 2019
1 parent f7f8759 commit 989efb8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 87 deletions.
2 changes: 2 additions & 0 deletions doc/release/upcoming_changes/14335.expired.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Class ``SafeEval`` in ``numpy/lib/utils.py`` has been removed. This was deprecated in NumPy 1.10.
Use ``np.safe_eval`` instead.
87 changes: 0 additions & 87 deletions numpy/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,93 +1003,6 @@ def _getmembers(item):
if hasattr(item, x)]
return members

#-----------------------------------------------------------------------------

# The following SafeEval class and company are adapted from Michael Spencer's
# ASPN Python Cookbook recipe: https://code.activestate.com/recipes/364469/
#
# Accordingly it is mostly Copyright 2006 by Michael Spencer.
# The recipe, like most of the other ASPN Python Cookbook recipes was made
# available under the Python license.
# https://en.wikipedia.org/wiki/Python_License

# It has been modified to:
# * handle unary -/+
# * support True/False/None
# * raise SyntaxError instead of a custom exception.

class SafeEval(object):
"""
Object to evaluate constant string expressions.
This includes strings with lists, dicts and tuples using the abstract
syntax tree created by ``compiler.parse``.
.. deprecated:: 1.10.0
See Also
--------
safe_eval
"""
def __init__(self):
# 2014-10-15, 1.10
warnings.warn("SafeEval is deprecated in 1.10 and will be removed.",
DeprecationWarning, stacklevel=2)

def visit(self, node):
cls = node.__class__
meth = getattr(self, 'visit' + cls.__name__, self.default)
return meth(node)

def default(self, node):
raise SyntaxError("Unsupported source construct: %s"
% node.__class__)

def visitExpression(self, node):
return self.visit(node.body)

def visitNum(self, node):
return node.n

def visitStr(self, node):
return node.s

def visitBytes(self, node):
return node.s

def visitDict(self, node,**kw):
return dict([(self.visit(k), self.visit(v))
for k, v in zip(node.keys, node.values)])

def visitTuple(self, node):
return tuple([self.visit(i) for i in node.elts])

def visitList(self, node):
return [self.visit(i) for i in node.elts]

def visitUnaryOp(self, node):
import ast
if isinstance(node.op, ast.UAdd):
return +self.visit(node.operand)
elif isinstance(node.op, ast.USub):
return -self.visit(node.operand)
else:
raise SyntaxError("Unknown unary op: %r" % node.op)

def visitName(self, node):
if node.id == 'False':
return False
elif node.id == 'True':
return True
elif node.id == 'None':
return None
else:
raise SyntaxError("Unknown name: %s" % node.id)

def visitNameConstant(self, node):
return node.value


def safe_eval(source):
"""
Expand Down

0 comments on commit 989efb8

Please sign in to comment.