Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve symengine import time #417

Merged
merged 3 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions symengine/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import sys

try:
import py
from py.test import skip, raises
USE_PYTEST = getattr(sys, '_running_pytest', False)
except ImportError:
USE_PYTEST = False

if not USE_PYTEST:
def raises(expectedException, code=None):
"""
Tests that ``code`` raises the exception ``expectedException``.

``code`` may be a callable, such as a lambda expression or function
name.

If ``code`` is not given or None, ``raises`` will return a context
manager for use in ``with`` statements; the code to execute then
comes from the scope of the ``with``.

``raises()`` does nothing if the callable raises the expected
exception, otherwise it raises an AssertionError.

Examples
========

>>> from symengine.pytest import raises

>>> raises(ZeroDivisionError, lambda: 1/0)
>>> raises(ZeroDivisionError, lambda: 1/2)
Traceback (most recent call last):
...
AssertionError: DID NOT RAISE

>>> with raises(ZeroDivisionError):
... n = 1/0
>>> with raises(ZeroDivisionError):
... n = 1/2
Traceback (most recent call last):
...
AssertionError: DID NOT RAISE

Note that you cannot test multiple statements via
``with raises``:

>>> with raises(ZeroDivisionError):
... n = 1/0 # will execute and raise, aborting the ``with``
... n = 9999/0 # never executed

This is just what ``with`` is supposed to do: abort the
contained statement sequence at the first exception and let
the context manager deal with the exception.

To test multiple statements, you'll need a separate ``with``
for each:

>>> with raises(ZeroDivisionError):
... n = 1/0 # will execute and raise
>>> with raises(ZeroDivisionError):
... n = 9999/0 # will also execute and raise

"""
if code is None:
return RaisesContext(expectedException)
elif callable(code):
try:
code()
except expectedException:
return
raise AssertionError("DID NOT RAISE")
elif isinstance(code, str):
raise TypeError(
'\'raises(xxx, "code")\' has been phased out; '
'change \'raises(xxx, "expression")\' '
'to \'raises(xxx, lambda: expression)\', '
'\'raises(xxx, "statement")\' '
'to \'with raises(xxx): statement\'')
else:
raise TypeError(
'raises() expects a callable for the 2nd argument.')

class RaisesContext(object):
def __init__(self, expectedException):
self.expectedException = expectedException

def __enter__(self):
return None

def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
raise AssertionError("DID NOT RAISE")
return issubclass(exc_type, self.expectedException)


2 changes: 1 addition & 1 deletion symengine/tests/test_arit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises

from symengine import (Symbol, Integer, Add, Mul, Pow, Rational, sqrt,
symbols, S, I, count_ops)
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_dict_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises

from symengine import symbols, DictBasic, sin, Integer

Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_eval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine import (Symbol, sin, cos, Integer, Add, I, RealDouble, ComplexDouble, sqrt)

from unittest.case import SkipTest
Expand Down
4 changes: 2 additions & 2 deletions symengine/tests/test_expr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from symengine import Add, Mul, Symbol, Integer
from symengine.utilities import raises
from symengine import Symbol, Integer
from symengine.test_utilities import raises


def test_as_coefficients_dict():
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
loggamma, beta, polygamma, digamma, trigamma, sign, floor, ceiling, conjugate,
nan, Float, UnevaluatedExpr
)
from symengine.utilities import raises
from symengine.test_utilities import raises

import unittest

Expand Down
3 changes: 1 addition & 2 deletions symengine/tests/test_lambdify.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import itertools
from operator import mul
import math
import sys

import symengine as se
from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine import have_numpy
import unittest
from unittest.case import SkipTest
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_logic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine.lib.symengine_wrapper import (true, false, Eq, Ne, Ge, Gt, Le, Lt, Symbol,
I, And, Or, Not, Nand, Nor, Xor, Xnor, Piecewise,
Contains, Interval, FiniteSet, oo, log)
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from symengine.lib.symengine_wrapper import (DenseMatrix, Symbol, Integer,
Rational, function_symbol, I, NonSquareMatrixError, ShapeError, zeros,
ones, eye, ImmutableMatrix)
from symengine.utilities import raises
from symengine.test_utilities import raises


try:
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_ntheory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises

from symengine.lib.symengine_wrapper import (isprime, nextprime, gcd,
lcm, gcd_ext, mod, quotient, quotient_mod, mod_inverse, crt, fibonacci,
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_number.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises

from symengine import Integer, I, S, Symbol, pi, Rational
from symengine.lib.symengine_wrapper import (perfect_power, is_square, integer_nthroot)
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_pickling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from symengine import symbols, sin, sinh, have_numpy, have_llvm, cos, Symbol
from symengine.utilities import raises
from symengine.test_utilities import raises
import pickle
import unittest

Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_printing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from symengine import (ccode, unicode, Symbol, sqrt, Pow, Max, sin, Integer, MutableDenseMatrix)
from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine.printing import CCodePrinter, init_printing

def test_ccode():
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_series_expansion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine.lib.symengine_wrapper import (series, have_piranha, have_flint,
Symbol, Integer, sin, cos, exp, sqrt, E)

Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_sets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine.lib.symengine_wrapper import (Interval, EmptySet, UniversalSet,
FiniteSet, Union, Complement, ImageSet, ConditionSet, Reals, Rationals,
Integers, And, Or, oo, Symbol, true, Ge, Eq, Gt)
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_solve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine import (Interval, EmptySet, FiniteSet, I, oo, Eq, Symbol,
linsolve)
from symengine.lib.symengine_wrapper import solve
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_subs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from symengine.utilities import raises
from symengine.test_utilities import raises
from symengine import Symbol, sin, cos, sqrt, Add, function_symbol, have_numpy


Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_symbol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from symengine import Symbol, symbols, symarray, has_symbol, Dummy
from symengine.utilities import raises
from symengine.test_utilities import raises
import unittest
import platform

Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_sympify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine.utilities import raises
from symengine.test_utilities import raises

from symengine import (Symbol, Integer, sympify, SympifyError, true, false, pi, nan, oo,
zoo, E, I, GoldenRatio, Catalan, Rational, sqrt, Eq)
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_sympy_compat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from symengine.sympy_compat import (Integer, Rational, S, Basic, Add, Mul,
Pow, symbols, Symbol, log, sin, cos, sech, csch, zeros, atan2, nan, Number, Float,
Min, Max, RealDouble, have_mpfr, Abs)
from symengine.utilities import raises
from symengine.test_utilities import raises


def test_Integer():
Expand Down
2 changes: 1 addition & 1 deletion symengine/tests/test_var.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Tests for var are in their own file, because var pollutes global namespace.

from symengine import Symbol, var
from symengine.utilities import raises
from symengine.test_utilities import raises
# make z1 with call-depth = 1


Expand Down
93 changes: 0 additions & 93 deletions symengine/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,99 +241,6 @@ def traverse(symbols, frame):
return syms


try:
import py
from py.test import skip, raises
USE_PYTEST = getattr(sys, '_running_pytest', False)
except ImportError:
USE_PYTEST = False

if not USE_PYTEST:
def raises(expectedException, code=None):
"""
Tests that ``code`` raises the exception ``expectedException``.

``code`` may be a callable, such as a lambda expression or function
name.

If ``code`` is not given or None, ``raises`` will return a context
manager for use in ``with`` statements; the code to execute then
comes from the scope of the ``with``.

``raises()`` does nothing if the callable raises the expected
exception, otherwise it raises an AssertionError.

Examples
========

>>> from symengine.pytest import raises

>>> raises(ZeroDivisionError, lambda: 1/0)
>>> raises(ZeroDivisionError, lambda: 1/2)
Traceback (most recent call last):
...
AssertionError: DID NOT RAISE

>>> with raises(ZeroDivisionError):
... n = 1/0
>>> with raises(ZeroDivisionError):
... n = 1/2
Traceback (most recent call last):
...
AssertionError: DID NOT RAISE

Note that you cannot test multiple statements via
``with raises``:

>>> with raises(ZeroDivisionError):
... n = 1/0 # will execute and raise, aborting the ``with``
... n = 9999/0 # never executed

This is just what ``with`` is supposed to do: abort the
contained statement sequence at the first exception and let
the context manager deal with the exception.

To test multiple statements, you'll need a separate ``with``
for each:

>>> with raises(ZeroDivisionError):
... n = 1/0 # will execute and raise
>>> with raises(ZeroDivisionError):
... n = 9999/0 # will also execute and raise

"""
if code is None:
return RaisesContext(expectedException)
elif callable(code):
try:
code()
except expectedException:
return
raise AssertionError("DID NOT RAISE")
elif isinstance(code, str):
raise TypeError(
'\'raises(xxx, "code")\' has been phased out; '
'change \'raises(xxx, "expression")\' '
'to \'raises(xxx, lambda: expression)\', '
'\'raises(xxx, "statement")\' '
'to \'with raises(xxx): statement\'')
else:
raise TypeError(
'raises() expects a callable for the 2nd argument.')

class RaisesContext(object):
def __init__(self, expectedException):
self.expectedException = expectedException

def __enter__(self):
return None

def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
raise AssertionError("DID NOT RAISE")
return issubclass(exc_type, self.expectedException)


class NotIterable:
"""
Use this as mixin when creating a class which is not supposed to return
Expand Down