From 8cc789edbb63928682517d102107a354296d07d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Fri, 22 May 2015 20:34:57 +0200 Subject: [PATCH 1/3] spring cleanup of symbolic/ : py3 raise syntax and pyflakes found errors --- src/sage/symbolic/assumptions.py | 5 ++-- src/sage/symbolic/callable.py | 2 +- src/sage/symbolic/expression.pyx | 2 +- src/sage/symbolic/expression_conversions.py | 2 ++ src/sage/symbolic/function.pyx | 28 ++++++++++----------- src/sage/symbolic/function_factory.py | 2 +- src/sage/symbolic/getitem.pyx | 6 ++--- src/sage/symbolic/pynac.pyx | 8 +++--- src/sage/symbolic/random_tests.py | 4 ++- src/sage/symbolic/relation.py | 16 ++++++------ src/sage/symbolic/ring.pyx | 10 +++----- src/sage/symbolic/tests.py | 1 - 12 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/sage/symbolic/assumptions.py b/src/sage/symbolic/assumptions.py index 34c09bbcc6a..73a27c06ae5 100644 --- a/src/sage/symbolic/assumptions.py +++ b/src/sage/symbolic/assumptions.py @@ -155,9 +155,9 @@ def assume(self): self._context = maxima.newcontext('context' + maxima._next_var_name()) try: maxima.eval("declare(%s, %s)" % (self._var._maxima_init_(), self._assumption)) -# except TypeError, mess: +# except TypeError: # if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant -# raise ValueError, "Assumption is inconsistent" +# raise ValueError("Assumption is inconsistent") except RuntimeError as mess: if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant raise ValueError("Assumption is inconsistent") @@ -605,7 +605,6 @@ def _forget_all(): sage: sin(m*pi).simplify() sin(pi*m) """ - from sage.calculus.calculus import maxima global _assumptions if len(_assumptions) == 0: return diff --git a/src/sage/symbolic/callable.py b/src/sage/symbolic/callable.py index 9aad84ffa77..71553f77727 100644 --- a/src/sage/symbolic/callable.py +++ b/src/sage/symbolic/callable.py @@ -239,7 +239,7 @@ def unify_arguments(self, x): b = x.arguments() # Rule #1 - if [str(x) for x in a] == [str(x) for x in b]: + if [str(y) for y in a] == [str(z) for z in b]: return a # Rule #2 diff --git a/src/sage/symbolic/expression.pyx b/src/sage/symbolic/expression.pyx index ac9cb2776a3..f6bd4900842 100644 --- a/src/sage/symbolic/expression.pyx +++ b/src/sage/symbolic/expression.pyx @@ -10947,7 +10947,7 @@ cdef class Expression(CommutativeRingElement): from sage.symbolic.function_factory import SymbolicFunction if not self.has(Y): - raise ValueError, "Expression {} contains no {} terms".format(self, Y) + raise ValueError("Expression {} contains no {} terms".format(self, Y)) x = SR.symbol() yy = SR.symbol() y = SymbolicFunction('y', 1)(x) diff --git a/src/sage/symbolic/expression_conversions.py b/src/sage/symbolic/expression_conversions.py index c37fee08124..498bc34000b 100644 --- a/src/sage/symbolic/expression_conversions.py +++ b/src/sage/symbolic/expression_conversions.py @@ -16,6 +16,7 @@ ############################################################################### import operator as _operator +from sage.rings.rational_field import QQ from sage.symbolic.ring import SR from sage.symbolic.pynac import I from sage.functions.all import exp @@ -24,6 +25,7 @@ from functools import reduce GaussianField = I.pyobject().parent() + class FakeExpression(object): r""" Pynac represents `x/y` as `xy^{-1}`. Often, tree-walkers would prefer diff --git a/src/sage/symbolic/function.pyx b/src/sage/symbolic/function.pyx index bf8a913d6df..ba2a3b64a26 100644 --- a/src/sage/symbolic/function.pyx +++ b/src/sage/symbolic/function.pyx @@ -63,7 +63,7 @@ cdef class Function(SageObject): TESTS:: # eval_func raises exception - sage: def ef(self, x): raise RuntimeError, "foo" + sage: def ef(self, x): raise RuntimeError("foo") sage: bar = function("bar", nargs=1, eval_func=ef) sage: bar(x) Traceback (most recent call last): @@ -96,17 +96,17 @@ cdef class Function(SageObject): # latex printing can be customised either by setting a string latex_name # or giving a custom function argument print_latex_func if latex_name and hasattr(self, '_print_latex_'): - raise ValueError, "only one of latex_name or _print_latex_ should be specified." + raise ValueError("only one of latex_name or _print_latex_ should be specified.") # only one of derivative and tderivative should be defined if hasattr(self, '_derivative_') and hasattr(self, '_tderivative_'): - raise ValueError, "only one of _derivative_ or _tderivative_ should be defined." + raise ValueError("only one of _derivative_ or _tderivative_ should be defined.") for fname in sfunctions_funcs: real_fname = '_%s_'%fname if hasattr(self, real_fname) and not \ callable(getattr(self, real_fname)): - raise ValueError, real_fname + " parameter must be callable" + raise ValueError(real_fname + " parameter must be callable") if not self._is_registered(): self._register_function() @@ -124,7 +124,7 @@ cdef class Function(SageObject): Check if this function is already registered. If it is, set `self._serial` to the right value. """ - raise NotImplementedError, "this is an abstract base class, it shouldn't be initialized directly" + raise NotImplementedError("this is an abstract base class, it shouldn't be initialized directly") cdef _register_function(self): """ @@ -408,15 +408,15 @@ cdef class Function(SageObject): """ if self._nargs > 0 and len(args) != self._nargs: - raise TypeError, "Symbolic function %s takes exactly %s arguments (%s given)"%(self._name, self._nargs, len(args)) + raise TypeError("Symbolic function %s takes exactly %s arguments (%s given)" % (self._name, self._nargs, len(args))) # support fast_float if self._nargs == 1: if isinstance(args[0], FastDoubleFunc): try: method = getattr(args[0], self._name) - except AttributeError, err: - raise TypeError, "cannot handle fast float arguments" + except AttributeError: + raise TypeError("cannot handle fast float arguments") else: return method() @@ -487,12 +487,12 @@ cdef class Function(SageObject): try: nargs[i] = SR.coerce(carg) except Exception: - raise TypeError, "cannot coerce arguments: %s"%(err) + raise TypeError("cannot coerce arguments: %s" % (err)) args = nargs else: # coerce == False for a in args: if not isinstance(a, Expression): - raise TypeError, "arguments must be symbolic expressions" + raise TypeError("arguments must be symbolic expressions") cdef GEx res cdef GExVector vec @@ -861,7 +861,7 @@ cdef class GinacFunction(BuiltinFunction): try: self._serial = find_function(fname, self._nargs) except ValueError as err: - raise ValueError, "cannot find GiNaC function with name %s and %s arguments"%(fname, self._nargs) + raise ValueError("cannot find GiNaC function with name %s and %s arguments" % (fname, self._nargs)) global sfunction_serial_dict return self._serial in sfunction_serial_dict @@ -1097,7 +1097,7 @@ cdef class BuiltinFunction(Function): self.__init__() else: # we should never end up here - raise ValueError, "cannot read pickle" + raise ValueError("cannot read pickle") cdef class SymbolicFunction(Function): @@ -1307,7 +1307,7 @@ cdef class SymbolicFunction(Function): # check input if not ((state[0] == 1 and len(state) == 6) or \ (state[0] == 2 and len(state) == 7)): - raise ValueError, "unknown state information" + raise ValueError("unknown state information") name = state[1] nargs = state[2] @@ -1366,7 +1366,7 @@ cdef class DeprecatedSFunction(SymbolicFunction): try: return self.__dict__[attr] except KeyError: - raise AttributeError, attr + raise AttributeError(attr) def __setattr__(self, attr, value): """ diff --git a/src/sage/symbolic/function_factory.py b/src/sage/symbolic/function_factory.py index aac429816dc..6a2d27afc84 100644 --- a/src/sage/symbolic/function_factory.py +++ b/src/sage/symbolic/function_factory.py @@ -323,7 +323,7 @@ def function(s, *args, **kwds): names = s.split(' ') else: names = [s] - names = [s.strip() for s in names if s.strip()] + names = [sn.strip() for sn in names if sn.strip()] funcs = [function_factory(name, **kwds) for name in names] diff --git a/src/sage/symbolic/getitem.pyx b/src/sage/symbolic/getitem.pyx index 5bf4c1d322b..35a54e99941 100644 --- a/src/sage/symbolic/getitem.pyx +++ b/src/sage/symbolic/getitem.pyx @@ -128,7 +128,7 @@ cdef class OperandsWrapper(SageObject): if arg.step: step = arg.step if step != arg.step: - raise ValueError, "step value must be an integer" + raise ValueError("step value must be an integer") else: step = 1 return [new_Expression_from_GEx(self._expr._parent, @@ -139,12 +139,12 @@ cdef class OperandsWrapper(SageObject): if isinstance(arg, (list, tuple)): # handle nested index if len(arg) == 0: # or not all(lambda x: x in ZZ for t in args): - raise TypeError, ind_err_msg + raise TypeError(ind_err_msg) GEx_construct_ex(&cur_ex, self._expr._gobj) for x in arg: nops = cur_ex.nops() if nops == 0: - raise TypeError, "expressions containing only a numeric coefficient, constant or symbol have no operands" + raise TypeError("expressions containing only a numeric coefficient, constant or symbol have no operands") ind = normalize_index(x, nops, ind_err_msg) cur_ex = cur_ex.op(ind) return new_Expression_from_GEx(self._expr._parent, cur_ex) diff --git a/src/sage/symbolic/pynac.pyx b/src/sage/symbolic/pynac.pyx index fab79e3412d..22ad05e14aa 100644 --- a/src/sage/symbolic/pynac.pyx +++ b/src/sage/symbolic/pynac.pyx @@ -179,11 +179,11 @@ cdef public GEx pyExpression_to_ex(object res) except *: functions back to C++ level. """ if res is None: - raise TypeError, "function returned None, expected return value of type sage.symbolic.expression.Expression" + raise TypeError("function returned None, expected return value of type sage.symbolic.expression.Expression") try: t = ring.SR.coerce(res) except TypeError as err: - raise TypeError, "function did not return a symbolic expression or an element that can be coerced into a symbolic expression" + raise TypeError("function did not return a symbolic expression or an element that can be coerced into a symbolic expression") return (t)._gobj cdef public object paramset_to_PyTuple(const_paramset_ref s): @@ -1336,7 +1336,7 @@ def py_factorial_py(x): cdef public object py_doublefactorial(object x) except +: n = Integer(x) if n < -1: - raise ValueError, "argument must be >= -1" + raise ValueError("argument must be >= -1") from sage.misc.misc_c import prod # fast balanced product return prod([n - 2*i for i in range(n//2)]) @@ -1656,7 +1656,7 @@ cdef public object py_atan2(object x, object y) except +: if sgn_x > 0: return 0 elif x == 0: - raise ValueError, "arctan2(0,0) undefined" + raise ValueError("arctan2(0,0) undefined") else: return pi_n diff --git a/src/sage/symbolic/random_tests.py b/src/sage/symbolic/random_tests.py index 389c8c5f9ae..928e75997d6 100644 --- a/src/sage/symbolic/random_tests.py +++ b/src/sage/symbolic/random_tests.py @@ -17,7 +17,9 @@ from sage.rings.all import QQ from sage.symbolic.ring import SR import sage.symbolic.pynac -from sage.symbolic.constants import * +from sage.symbolic.constants import (pi, e, golden_ratio, log2, euler_gamma, + catalan, khinchin, twinprime, mertens, + brun) from sage.functions.hypergeometric import hypergeometric diff --git a/src/sage/symbolic/relation.py b/src/sage/symbolic/relation.py index 007ce114f38..6fad4b39ab5 100644 --- a/src/sage/symbolic/relation.py +++ b/src/sage/symbolic/relation.py @@ -329,7 +329,7 @@ """ import operator -from sage.calculus.calculus import maxima + def test_relation_maxima(relation): """ @@ -459,20 +459,20 @@ def test_relation_maxima(relation): if relation.operator() == operator.eq: # operator is equality try: s = m.parent()._eval_line('is (equal(%s,%s))'%(repr(m.lhs()),repr(m.rhs()))) - except TypeError as msg: - raise ValueError("unable to evaluate the predicate '%s'"%repr(relation)) + except TypeError: + raise ValueError("unable to evaluate the predicate '%s'" % repr(relation)) elif relation.operator() == operator.ne: # operator is not equal try: s = m.parent()._eval_line('is (notequal(%s,%s))'%(repr(m.lhs()),repr(m.rhs()))) - except TypeError as msg: - raise ValueError("unable to evaluate the predicate '%s'"%repr(relation)) + except TypeError: + raise ValueError("unable to evaluate the predicate '%s'" % repr(relation)) else: # operator is < or > or <= or >=, which Maxima handles fine try: s = m.parent()._eval_line('is (%s)'%repr(m)) - except TypeError as msg: - raise ValueError("unable to evaluate the predicate '%s'"%repr(relation)) + except TypeError: + raise ValueError("unable to evaluate the predicate '%s'" % repr(relation)) if s == 'true': return True @@ -1153,7 +1153,7 @@ def solve_ineq_univar(ineq): """ ineqvar = ineq.variables() if len(ineqvar) != 1: - raise NotImplementedError, "The command solve_ineq_univar accepts univariate inequalities only. Your variables are ", ineqvar + raise NotImplementedError("The command solve_ineq_univar accepts univariate inequalities only. Your variables are " + ineqvar) ineq0 = ineq._maxima_() ineq0.parent().eval("if solve_rat_ineq_loaded#true then (solve_rat_ineq_loaded:true,load(\"solve_rat_ineq.mac\")) ") sol = ineq0.solve_rat_ineq().sage() diff --git a/src/sage/symbolic/ring.pyx b/src/sage/symbolic/ring.pyx index 388ad9fd802..84476dfaaa2 100644 --- a/src/sage/symbolic/ring.pyx +++ b/src/sage/symbolic/ring.pyx @@ -268,7 +268,7 @@ cdef class SymbolicRing(CommutativeRing): return self(symbolic_expression_from_string(x)) except SyntaxError as err: msg, s, pos = err.args - raise TypeError, "%s: %s !!! %s" % (msg, s[:pos], s[pos:]) + raise TypeError("%s: %s !!! %s" % (msg, s[:pos], s[pos:])) from sage.rings.infinity import (infinity, minus_infinity, unsigned_infinity) @@ -674,7 +674,7 @@ cdef class SymbolicRing(CommutativeRing): return self.symbol(name, latex_name=formatted_latex_name, domain=domain) if len(names_list) > 1: if latex_name: - raise ValueError, "cannot specify latex_name for multiple symbol names" + raise ValueError("cannot specify latex_name for multiple symbol names") return tuple([self.symbol(s, domain=domain) for s in names_list]) def _repr_element_(self, Expression x): @@ -783,7 +783,7 @@ cdef class SymbolicRing(CommutativeRing): try: d[ vars[i] ] = arg except IndexError: - raise ValueError, "the number of arguments must be less than or equal to %s"%len(vars) + raise ValueError("the number of arguments must be less than or equal to %s"%len(vars)) return _the_element.subs(d, **kwds) @@ -1046,6 +1046,4 @@ def isidentifier(x): code = parser.expr(x).compile() except (MemoryError, OverflowError, SyntaxError, SystemError, parser.ParserError), msg: return False - return len(code.co_names)==1 and code.co_names[0]==x - - + return len(code.co_names) == 1 and code.co_names[0] == x diff --git a/src/sage/symbolic/tests.py b/src/sage/symbolic/tests.py index 772dd582d88..8cd32c4eba7 100644 --- a/src/sage/symbolic/tests.py +++ b/src/sage/symbolic/tests.py @@ -39,4 +39,3 @@ def rational_powers_memleak(): c1 = sum(1 for obj in gc.get_objects()) # Test that we do not leak an object at each iteration return (c1 - c0) >= 1000 - From 128d3ebbad4ff53f9217327868f18d5a4d306c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sat, 23 May 2015 17:16:29 +0200 Subject: [PATCH 2/3] trac #18476 remove 3 commented lines --- src/sage/symbolic/assumptions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/sage/symbolic/assumptions.py b/src/sage/symbolic/assumptions.py index 73a27c06ae5..7129ef3725e 100644 --- a/src/sage/symbolic/assumptions.py +++ b/src/sage/symbolic/assumptions.py @@ -155,9 +155,6 @@ def assume(self): self._context = maxima.newcontext('context' + maxima._next_var_name()) try: maxima.eval("declare(%s, %s)" % (self._var._maxima_init_(), self._assumption)) -# except TypeError: -# if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant -# raise ValueError("Assumption is inconsistent") except RuntimeError as mess: if 'inconsistent' in str(mess): # note Maxima doesn't tell you if declarations are redundant raise ValueError("Assumption is inconsistent") From 330a9a88a1c202d12f30111ac1cb49ff8ff43846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sun, 24 May 2015 08:38:45 +0200 Subject: [PATCH 3/3] trac #18476 remove brun function --- src/sage/symbolic/random_tests.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sage/symbolic/random_tests.py b/src/sage/symbolic/random_tests.py index 8fcbedddfe6..a2164f69bda 100644 --- a/src/sage/symbolic/random_tests.py +++ b/src/sage/symbolic/random_tests.py @@ -18,8 +18,7 @@ from sage.symbolic.ring import SR import sage.symbolic.pynac from sage.symbolic.constants import (pi, e, golden_ratio, log2, euler_gamma, - catalan, khinchin, twinprime, mertens, - brun) + catalan, khinchin, twinprime, mertens) from sage.functions.hypergeometric import hypergeometric