[tryton-debian-vcs] simpleeval branch debian updated. debian/0.8.2-2-5-gb9502cd

Mathias Behrle tryton-debian-vcs at alioth.debian.org
Tue Jun 30 09:41:52 UTC 2015


The following commit has been merged in the debian branch:
https://alioth.debian.org/plugins/scmgit/cgi-bin/gitweb.cgi/?p=tryton/simpleeval.git;a=commitdiff;h=debian/0.8.2-2-5-gb9502cd

commit b9502cdb7a63d29177f9004f43dbb6073a8d4288
Author: Mathias Behrle <mathiasb at m9s.biz>
Date:   Tue Jun 30 11:33:26 2015 +0200

    Releasing debian version 0.8.5-1.
    
    Signed-off-by: Mathias Behrle <mathiasb at m9s.biz>

diff --git a/debian/changelog b/debian/changelog
index df2a70e..d21cccc 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,12 @@
+simpleeval (0.8.5-1) unstable; urgency=medium
+
+  * Disabling failing test for python3.2.
+  * Adapting section naming in gbp.conf to current git-buildpackage.
+  * Merging upstream version 0.8.5.
+  * Removing all patches, went upstream.
+
+ -- Mathias Behrle <mathiasb at m9s.biz>  Tue, 30 Jun 2015 11:33:01 +0200
+
 simpleeval (0.8.2-2) unstable; urgency=medium
 
   * Disabling temporarily tests for failing test_string_length.
commit 6357a4898d9d5917755a65a143b1ca6e85e14d8d
Author: Mathias Behrle <mathiasb at m9s.biz>
Date:   Tue Jun 30 11:32:21 2015 +0200

    Removing all patches, went upstream.

diff --git a/debian/patches/01-add_testfile.patch b/debian/patches/01-add_testfile.patch
deleted file mode 100644
index 71349cb..0000000
--- a/debian/patches/01-add_testfile.patch
+++ /dev/null
@@ -1,333 +0,0 @@
-Description: Adding the missing test file.
- The test file test_simpleeval.py is missing from the release tarball.
- We provide it here until upstream includes it.
- .
- simpleeval (0.8.2-1) unstable; urgency=low
-Author: Mathias Behrle <mathiasb at m9s.biz>
-Bug: https://github.com/danthedeckie/simpleeval/issues/4
-Forwarded: not-needed
-
---- /dev/null
-+++ simpleeval-0.8.2/test_simpleeval.py
-@@ -0,0 +1,321 @@
-+'''
-+    Unit tests for simpleeval.
-+    --------------------------
-+
-+    Most of this stuff is pretty basic.
-+
-+'''
-+# pylint: disable=too-many-public-methods, missing-docstring
-+
-+import unittest
-+import simpleeval
-+from simpleeval import SimpleEval, NameNotDefined, InvalidExpression, simple_eval
-+
-+class DRYTest(unittest.TestCase):
-+    ''' Stuff we need to do every test, let's do here instead..
-+        Don't Repeat Yourself. '''
-+
-+    def setUp(self):
-+        ''' initialize a SimpleEval '''
-+        self.s = SimpleEval()
-+
-+    def t(self, expr, shouldbe): #pylint: disable=invalid-name
-+        ''' test an evaluation of an expression against an expected answer '''
-+        return self.assertEqual(self.s.eval(expr), shouldbe)
-+
-+class TestBasic(DRYTest):
-+    ''' Simple expressions. '''
-+
-+    def test_maths_with_ints(self):
-+        ''' simple maths expressions '''
-+
-+        self.t("21 + 21", 42)
-+        self.t("6*7", 42)
-+        self.t("20 + 1 + (10*2) + 1", 42)
-+        self.t("100/10", 10)
-+        self.t("12*12", 144)
-+        self.t("2 ** 10", 1024)
-+        self.t("100 % 9", 1)
-+
-+    def test_bools_and_or(self):
-+        self.t('True and False', False)
-+        self.t('True or False', True)
-+        self.t('1 - 1 or 21', True)
-+        self.t('1 - 1 and 11', False)
-+        self.t('110 == 100 + 10 and True', True)
-+
-+    def test_maths_with_floats(self):
-+        self.t("11.02 - 9.1", 1.92)
-+        self.t("29.1+39", 68.1)
-+
-+    def test_comparisons(self):
-+        # GT & LT:
-+        self.t("1 > 0", True)
-+        self.t("100000 < 28", False)
-+        self.t("-2 < 11", True)
-+        self.t("+2 < 5", True)
-+        self.t("0 == 0", True)
-+
-+        # GtE, LtE
-+        self.t("-2 <= -2", True)
-+        self.t("2 >= 2", True)
-+        self.t("1 >= 12", False)
-+        self.t("1.09 <= 1967392", True)
-+
-+    def test_mixed_comparisons(self):
-+        self.t("1 > 0.999999", True)
-+        self.t("1 == True", True)  # Note ==, not 'is'.
-+        self.t("0 == False", True)  # Note ==, not 'is'.
-+        self.t("False == False", True)
-+        self.t("False < True", True)
-+
-+    def test_if_else(self):
-+        ''' x if y else z '''
-+
-+        # and test if/else expressions:
-+        self.t("'a' if 1 == 1 else 'b'", 'a')
-+        self.t("'a' if 1 > 2 else 'b'", 'b')
-+
-+        # and more complex expressions:
-+        self.t("'a' if 4 < 1 else 'b' if 1 == 2 else 'c'", 'c')
-+
-+    def test_default_conversions(self):
-+        ''' conversion between types '''
-+
-+        self.t('int("20") + int(0.22*100)', 42)
-+        self.t('float("42")', 42.0)
-+        self.t('"Test Stuff!" + str(11)', u"Test Stuff!11")
-+
-+class TestFunctions(DRYTest):
-+    ''' Functions for expressions to play with '''
-+
-+    def test_load_file(self):
-+        ''' add in a function which loads data from an external file. '''
-+
-+        # write to the file:
-+
-+        with open("file.txt", 'w') as f:
-+            f.write("42")
-+
-+        # define the function we'll send to the eval'er
-+
-+        def load_file(filename):
-+            ''' load a file and return its contents '''
-+            with open(filename) as f:
-+                return f.read()
-+
-+        # simple load:
-+
-+        self.s.functions = {u"read": load_file}
-+        self.t("read('file.txt')", "42")
-+
-+        # and we should have *replaced* the default functions. Let's check:
-+
-+        with self.assertRaises(simpleeval.FunctionNotDefined):
-+            self.t("int(read('file.txt'))", 42)
-+
-+        # OK, so we can load in the default functions as well...
-+
-+        self.s.functions.update(simpleeval.DEFAULT_FUNCTIONS)
-+
-+        # now it works:
-+
-+        self.t("int(read('file.txt'))", 42)
-+
-+    def test_randoms(self):
-+        ''' test the rand() and randint() functions '''
-+
-+        self.s.functions['type'] = type
-+
-+        self.t('type(randint(1000))', int)
-+        self.t('type(rand())', float)
-+
-+        self.t("randint(20)<20", True)
-+        self.t("rand()<1.0", True)
-+
-+        # I don't know how to further test these functions.  Ideas?
-+
-+class TestOperators(DRYTest):
-+    ''' Test adding in new operators, removing them, make sure it works. '''
-+    pass
-+
-+class TestTryingToBreakOut(DRYTest):
-+    ''' Test various weird methods to break the security sandbox... '''
-+
-+    def test_import(self):
-+        ''' usual suspect. import '''
-+        # cannot import things:
-+        with self.assertRaises(AttributeError):
-+            self.t("import sys", None)
-+
-+    def test_long_running(self):
-+        ''' exponent operations can take a long time. '''
-+        old_max = simpleeval.MAX_POWER
-+
-+        self.t("9**9**5", 9**9**5)
-+
-+        with self.assertRaises(simpleeval.NumberTooHigh):
-+            self.t("9**9**8", 0)
-+
-+        # and does limiting work?
-+
-+        simpleeval.MAX_POWER = 100
-+
-+        with self.assertRaises(simpleeval.NumberTooHigh):
-+            self.t("101**2", 0)
-+
-+        # good, so set it back:
-+
-+        simpleeval.MAX_POWER = old_max
-+
-+    def test_string_length(self):
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("50000*'text'", 0)
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("'text'*50000", 0)
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("('text'*50000)*1000", 0)
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("(50000*'text')*1000", 0)
-+
-+        self.t("'stuff'*20000", 20000*'stuff')
-+
-+        self.t("20000*'stuff'", 20000*'stuff')
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("('stuff'*20000) + ('stuff'*20000) ", 0)
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("'stuff'*100000", 100000*'stuff')
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("'" + (10000*"stuff") +"'*100", 0)
-+
-+        with self.assertRaises(simpleeval.StringTooLong):
-+            self.t("'" + (50000 * "stuff") + "'", 0)
-+
-+    def test_python_stuff(self):
-+        ''' other various pythony things. '''
-+        # it only evaluates the first statement:
-+        self.t("a = 11; x = 21; x + x", 11)
-+
-+        # list comprehensions don't work:
-+        # this could be changed in a future release, if people want...
-+        with self.assertRaises(simpleeval.FeatureNotAvailable):
-+            self.t("[x for x in (1, 2, 3)]", (1, 2, 3))
-+
-+class TestNames(DRYTest):
-+    ''' 'names', what other languages call variables... '''
-+
-+    def test_none(self):
-+        ''' what to do when names isn't defined, or is 'none' '''
-+        with self.assertRaises(NameNotDefined):
-+            self.t("a == 2", None)
-+
-+        self.s.names["s"] = 21
-+
-+        with self.assertRaises(NameNotDefined):
-+            self.t("s += a", 21)
-+
-+        self.s.names = None
-+
-+        with self.assertRaises(InvalidExpression):
-+            self.t('s', 21)
-+
-+
-+    def test_dict(self):
-+        ''' using a normal dict for names lookup '''
-+
-+        self.s.names = {'a': 42}
-+        self.t("a + a", 84)
-+
-+        self.s.names['also'] = 100
-+
-+        self.t("a + also - a", 100)
-+
-+        # however, you can't assign to those names:
-+
-+        self.t("a = 200", 200)
-+
-+        self.assertEqual(self.s.names['a'], 42)
-+
-+        # or assign to lists
-+
-+        self.s.names['b'] = [0]
-+
-+        self.t("b[0] = 11", 11)
-+
-+        self.assertEqual(self.s.names['b'], [0])
-+
-+        # but you can get items from a list:
-+
-+        self.s.names['b'] = [6, 7]
-+
-+        self.t("b[0] * b[1]", 42)
-+
-+        # or from a dict
-+
-+        self.s.names['c'] = {'i': 11}
-+
-+        self.t("c['i']", 11)
-+
-+        # you still can't assign though:
-+
-+        self.t("c['b'] = 99", 99)
-+
-+        self.assertFalse('b' in self.s.names['c'])
-+
-+        # and going all 'inception' on it doesn't work either:
-+
-+        self.s.names['c']['c'] = {'c': 11}
-+
-+        self.t("c['c']['c'] = 21", 21)
-+
-+        self.assertEqual(self.s.names['c']['c']['c'], 11)
-+
-+    def test_func(self):
-+        ''' using a function for 'names lookup' '''
-+
-+        def resolver(node): # pylint: disable=unused-argument
-+            ''' all names now equal 1024! '''
-+            return 1024
-+
-+        self.s.names = resolver
-+
-+        self.t("a", 1024)
-+        self.t("a + b - c - d", 0)
-+
-+        # the function can do stuff with the value it's sent:
-+
-+        def my_name(node):
-+            ''' all names equal their textual name, twice. '''
-+            return node.id + node.id
-+
-+        self.s.names = my_name
-+
-+        self.t("a", "aa")
-+
-+    def test_from_doc(self):
-+        ''' the 'name first letter as value' example from the docs '''
-+
-+        def name_handler(node):
-+            ''' return the alphabet number of the first letter of
-+                the name's textual name '''
-+            return ord(node.id[0].lower())-96
-+
-+        self.s.names = name_handler
-+        self.t('a', 1)
-+        self.t('a + b', 3)
-+
-+class Test_simple_eval(unittest.TestCase):
-+    ''' test the 'simple_eval' wrapper function '''
-+    def test_basic_run(self):
-+        self.assertEqual(simple_eval('6*7'), 42)
-+
-+    def test_default_functions(self):
-+        self.assertEqual(simple_eval('rand() < 1.0 and rand() > -0.01'), True)
-+        self.assertEqual(simple_eval('randint(200) < 200 and rand() > 0'), True)
diff --git a/debian/patches/02-add-test-fix b/debian/patches/02-add-test-fix
deleted file mode 100644
index 5a83f53..0000000
--- a/debian/patches/02-add-test-fix
+++ /dev/null
@@ -1,34 +0,0 @@
-Description: Adding fix for raise StringTooLong
- Patch from upstream VCS aaee5030d20bd832373485e2c78084f4d92227f7
- to fix test errors.
-Origin: upstream, https://github.com/danthedeckie/simpleeval/commit/aaee5030d20bd832373485e2c78084f4d92227f7
-Bug: https://github.com/danthedeckie/simpleeval/issues/5
-Forwarded: not-needed
-
---- simpleeval.orig/simpleeval.py	2015-04-26 14:23:51.000000000 +0200
-+++ simpleeval/simpleeval.py	2015-04-26 14:23:51.000000000 +0200
-@@ -218,9 +218,9 @@
-             return node.n
-         elif isinstance(node, ast.Str): # <string>
-             if len(node.s) > MAX_STRING_LENGTH:
--                raise StringTooLong("{0} is too long!"
--                                    " ({1}, when {2} is max)".format(
--                                    node.id, len(node.s), MAX_STRING_LENGTH))
-+                raise StringTooLong("String Literal in statement is too long!"
-+                                    " ({0}, when {1} is max)".format(
-+                                    len(node.s), MAX_STRING_LENGTH))
-             return node.s
- 
-         # python 3 compatibility:
-@@ -263,7 +263,10 @@
-                 elif callable(self.names):
-                     return self.names(node)
-                 else:
--                    raise KeyError('undefined name')
-+                    raise InvalidExpression('Trying to use name (variable) "{0}"'
-+                                            ' when no "names" defined for'
-+                                            ' evaluator'.format(node.id))
-+
-             except KeyError:
-                 raise NameNotDefined(node.id, self.expr)
- 
diff --git a/debian/patches/series b/debian/patches/series
deleted file mode 100644
index 9724311..0000000
--- a/debian/patches/series
+++ /dev/null
@@ -1,2 +0,0 @@
-01-add_testfile.patch
-02-add-test-fix
commit c44eefc1843f23300e24926f1c37972187e5c8f8
Author: Mathias Behrle <mathiasb at m9s.biz>
Date:   Tue Jun 30 11:30:44 2015 +0200

    Merging upstream version 0.8.5.

diff --git a/MANIFEST.in b/MANIFEST.in
index 9561fb1..4f3b2b8 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1 +1,2 @@
 include README.rst
+include test_simpleeval.py
diff --git a/PKG-INFO b/PKG-INFO
index a321e93..b3a0e24 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,12 +1,12 @@
 Metadata-Version: 1.1
 Name: simpleeval
-Version: 0.8.2
+Version: 0.8.5
 Summary: A simple, safe single expression evaluator library.
 Home-page: https://github.com/danthedeckie/simpleeval
 Author: Daniel Fairhead
 Author-email: danthedeckie at gmail.com
 License: UNKNOWN
-Download-URL: https://github.com/danthedeckie/simpleeval/tarball/0.8.2
+Download-URL: https://github.com/danthedeckie/simpleeval/tarball/0.8.5
 Description: simpleeval (Simple Eval)
         ========================
         
@@ -224,7 +224,7 @@ Description: simpleeval (Simple Eval)
         Other...
         --------
         
-        This is written using python 2.7, but should be trivial to convert to python3 with the 2to3 converter.  It totals around 100 lines of code, so it isn't a complex beast.
+        The library supports both python 2 and 3 using the 2to3 converter.
         
         Please read the ``test_simpleeval.py`` file for other potential gotchas or details.  I'm very happy to accept pull requests, suggestions, or other issues.  Enjoy!
         
@@ -236,5 +236,4 @@ Classifier: Development Status :: 4 - Beta
 Classifier: Intended Audience :: Developers
 Classifier: License :: OSI Approved :: MIT License
 Classifier: Topic :: Software Development :: Libraries :: Python Modules
-Classifier: Programming Language :: Python :: 2
-Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python
diff --git a/README.rst b/README.rst
index 035b0f7..0bfb650 100644
--- a/README.rst
+++ b/README.rst
@@ -215,7 +215,7 @@ Say.  This would allow a certain level of 'scriptyness' if you had these evaluat
 Other...
 --------
 
-This is written using python 2.7, but should be trivial to convert to python3 with the 2to3 converter.  It totals around 100 lines of code, so it isn't a complex beast.
+The library supports both python 2 and 3 using the 2to3 converter.
 
 Please read the ``test_simpleeval.py`` file for other potential gotchas or details.  I'm very happy to accept pull requests, suggestions, or other issues.  Enjoy!
 
diff --git a/setup.py b/setup.py
index f319b8b..2d49312 100644
--- a/setup.py
+++ b/setup.py
@@ -1,5 +1,5 @@
 from setuptools import setup
-__version__ = '0.8.2'
+__version__ = '0.8.5'
 
 setup(
     name = 'simpleeval',
@@ -18,7 +18,6 @@ setup(
                    'Intended Audience :: Developers',
                    'License :: OSI Approved :: MIT License',
                    'Topic :: Software Development :: Libraries :: Python Modules',
-                   'Programming Language :: Python :: 2',
-                   'Programming Language :: Python :: 3',
+                   'Programming Language :: Python',
                   ],
     )
diff --git a/simpleeval.egg-info/PKG-INFO b/simpleeval.egg-info/PKG-INFO
index a321e93..b3a0e24 100644
--- a/simpleeval.egg-info/PKG-INFO
+++ b/simpleeval.egg-info/PKG-INFO
@@ -1,12 +1,12 @@
 Metadata-Version: 1.1
 Name: simpleeval
-Version: 0.8.2
+Version: 0.8.5
 Summary: A simple, safe single expression evaluator library.
 Home-page: https://github.com/danthedeckie/simpleeval
 Author: Daniel Fairhead
 Author-email: danthedeckie at gmail.com
 License: UNKNOWN
-Download-URL: https://github.com/danthedeckie/simpleeval/tarball/0.8.2
+Download-URL: https://github.com/danthedeckie/simpleeval/tarball/0.8.5
 Description: simpleeval (Simple Eval)
         ========================
         
@@ -224,7 +224,7 @@ Description: simpleeval (Simple Eval)
         Other...
         --------
         
-        This is written using python 2.7, but should be trivial to convert to python3 with the 2to3 converter.  It totals around 100 lines of code, so it isn't a complex beast.
+        The library supports both python 2 and 3 using the 2to3 converter.
         
         Please read the ``test_simpleeval.py`` file for other potential gotchas or details.  I'm very happy to accept pull requests, suggestions, or other issues.  Enjoy!
         
@@ -236,5 +236,4 @@ Classifier: Development Status :: 4 - Beta
 Classifier: Intended Audience :: Developers
 Classifier: License :: OSI Approved :: MIT License
 Classifier: Topic :: Software Development :: Libraries :: Python Modules
-Classifier: Programming Language :: Python :: 2
-Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python
diff --git a/simpleeval.egg-info/SOURCES.txt b/simpleeval.egg-info/SOURCES.txt
index 0547ed6..e0716d8 100644
--- a/simpleeval.egg-info/SOURCES.txt
+++ b/simpleeval.egg-info/SOURCES.txt
@@ -2,6 +2,7 @@ MANIFEST.in
 README.rst
 setup.py
 simpleeval.py
+test_simpleeval.py
 simpleeval.egg-info/PKG-INFO
 simpleeval.egg-info/SOURCES.txt
 simpleeval.egg-info/dependency_links.txt
diff --git a/simpleeval.py b/simpleeval.py
index 67af226..7f29d2e 100644
--- a/simpleeval.py
+++ b/simpleeval.py
@@ -34,6 +34,10 @@ Initial idea copied from J.F. Sebastian on Stack Overflow
 modifications and many improvments.
 
 -------------------------------------
+Contributors:
+corro (Robin Baumgartner) (py3k)
+
+-------------------------------------
 Usage:
 
 >>> s = SimpleEval()
@@ -218,9 +222,9 @@ class SimpleEval(object): # pylint: disable=too-few-public-methods
             return node.n
         elif isinstance(node, ast.Str): # <string>
             if len(node.s) > MAX_STRING_LENGTH:
-                raise StringTooLong("{0} is too long!"
-                                    " ({1}, when {2} is max)".format(
-                                    node.id, len(node.s), MAX_STRING_LENGTH))
+                raise StringTooLong("String Literal in statement is too long!"
+                                    " ({0}, when {1} is max)".format(
+                                    len(node.s), MAX_STRING_LENGTH))
             return node.s
 
         # python 3 compatibility:
@@ -263,7 +267,10 @@ class SimpleEval(object): # pylint: disable=too-few-public-methods
                 elif callable(self.names):
                     return self.names(node)
                 else:
-                    raise KeyError('undefined name')
+                    raise InvalidExpression('Trying to use name (variable) "{0}"'
+                                            ' when no "names" defined for'
+                                            ' evaluator'.format(node.id))
+
             except KeyError:
                 raise NameNotDefined(node.id, self.expr)
 
diff --git a/test_simpleeval.py b/test_simpleeval.py
new file mode 100644
index 0000000..6fdf849
--- /dev/null
+++ b/test_simpleeval.py
@@ -0,0 +1,321 @@
+'''
+    Unit tests for simpleeval.
+    --------------------------
+
+    Most of this stuff is pretty basic.
+
+'''
+# pylint: disable=too-many-public-methods, missing-docstring
+
+import unittest
+import simpleeval
+from simpleeval import SimpleEval, NameNotDefined, InvalidExpression, simple_eval
+
+class DRYTest(unittest.TestCase):
+    ''' Stuff we need to do every test, let's do here instead..
+        Don't Repeat Yourself. '''
+
+    def setUp(self):
+        ''' initialize a SimpleEval '''
+        self.s = SimpleEval()
+
+    def t(self, expr, shouldbe): #pylint: disable=invalid-name
+        ''' test an evaluation of an expression against an expected answer '''
+        return self.assertEqual(self.s.eval(expr), shouldbe)
+
+class TestBasic(DRYTest):
+    ''' Simple expressions. '''
+
+    def test_maths_with_ints(self):
+        ''' simple maths expressions '''
+
+        self.t("21 + 21", 42)
+        self.t("6*7", 42)
+        self.t("20 + 1 + (10*2) + 1", 42)
+        self.t("100/10", 10)
+        self.t("12*12", 144)
+        self.t("2 ** 10", 1024)
+        self.t("100 % 9", 1)
+
+    def test_bools_and_or(self):
+        self.t('True and False', False)
+        self.t('True or False', True)
+        self.t('1 - 1 or 21', True)
+        self.t('1 - 1 and 11', False)
+        self.t('110 == 100 + 10 and True', True)
+
+    def test_maths_with_floats(self):
+        self.t("11.02 - 9.1", 1.92)
+        self.t("29.1+39", 68.1)
+
+    def test_comparisons(self):
+        # GT & LT:
+        self.t("1 > 0", True)
+        self.t("100000 < 28", False)
+        self.t("-2 < 11", True)
+        self.t("+2 < 5", True)
+        self.t("0 == 0", True)
+
+        # GtE, LtE
+        self.t("-2 <= -2", True)
+        self.t("2 >= 2", True)
+        self.t("1 >= 12", False)
+        self.t("1.09 <= 1967392", True)
+
+    def test_mixed_comparisons(self):
+        self.t("1 > 0.999999", True)
+        self.t("1 == True", True)  # Note ==, not 'is'.
+        self.t("0 == False", True)  # Note ==, not 'is'.
+        self.t("False == False", True)
+        self.t("False < True", True)
+
+    def test_if_else(self):
+        ''' x if y else z '''
+
+        # and test if/else expressions:
+        self.t("'a' if 1 == 1 else 'b'", 'a')
+        self.t("'a' if 1 > 2 else 'b'", 'b')
+
+        # and more complex expressions:
+        self.t("'a' if 4 < 1 else 'b' if 1 == 2 else 'c'", 'c')
+
+    def test_default_conversions(self):
+        ''' conversion between types '''
+
+        self.t('int("20") + int(0.22*100)', 42)
+        self.t('float("42")', 42.0)
+        self.t('"Test Stuff!" + str(11)', u"Test Stuff!11")
+
+class TestFunctions(DRYTest):
+    ''' Functions for expressions to play with '''
+
+    def test_load_file(self):
+        ''' add in a function which loads data from an external file. '''
+
+        # write to the file:
+
+        with open("file.txt", 'w') as f:
+            f.write("42")
+
+        # define the function we'll send to the eval'er
+
+        def load_file(filename):
+            ''' load a file and return its contents '''
+            with open(filename) as f:
+                return f.read()
+
+        # simple load:
+
+        self.s.functions = {u"read": load_file}
+        self.t("read('file.txt')", "42")
+
+        # and we should have *replaced* the default functions. Let's check:
+
+        with self.assertRaises(simpleeval.FunctionNotDefined):
+            self.t("int(read('file.txt'))", 42)
+
+        # OK, so we can load in the default functions as well...
+
+        self.s.functions.update(simpleeval.DEFAULT_FUNCTIONS)
+
+        # now it works:
+
+        self.t("int(read('file.txt'))", 42)
+
+    def test_randoms(self):
+        ''' test the rand() and randint() functions '''
+
+        self.s.functions['type'] = type
+
+        self.t('type(randint(1000))', int)
+        self.t('type(rand())', float)
+
+        self.t("randint(20)<20", True)
+        self.t("rand()<1.0", True)
+
+        # I don't know how to further test these functions.  Ideas?
+
+class TestOperators(DRYTest):
+    ''' Test adding in new operators, removing them, make sure it works. '''
+    pass
+
+class TestTryingToBreakOut(DRYTest):
+    ''' Test various weird methods to break the security sandbox... '''
+
+    def test_import(self):
+        ''' usual suspect. import '''
+        # cannot import things:
+        with self.assertRaises(AttributeError):
+            self.t("import sys", None)
+
+    def test_long_running(self):
+        ''' exponent operations can take a long time. '''
+        old_max = simpleeval.MAX_POWER
+
+        self.t("9**9**5", 9**9**5)
+
+        with self.assertRaises(simpleeval.NumberTooHigh):
+            self.t("9**9**8", 0)
+
+        # and does limiting work?
+
+        simpleeval.MAX_POWER = 100
+
+        with self.assertRaises(simpleeval.NumberTooHigh):
+            self.t("101**2", 0)
+
+        # good, so set it back:
+
+        simpleeval.MAX_POWER = old_max
+
+    def test_string_length(self):
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("50000*'text'", 0)
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("'text'*50000", 0)
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("('text'*50000)*1000", 0)
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("(50000*'text')*1000", 0)
+
+        self.t("'stuff'*20000", 20000*'stuff')
+
+        self.t("20000*'stuff'", 20000*'stuff')
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("('stuff'*20000) + ('stuff'*20000) ", 0)
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("'stuff'*100000", 100000*'stuff')
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("'" + (10000*"stuff") +"'*100", 0)
+
+        with self.assertRaises(simpleeval.StringTooLong):
+            self.t("'" + (50000 * "stuff") + "'", 0)
+
+    def test_python_stuff(self):
+        ''' other various pythony things. '''
+        # it only evaluates the first statement:
+        self.t("a = 11; x = 21; x + x", 11)
+
+        # list comprehensions don't work:
+        # this could be changed in a future release, if people want...
+        with self.assertRaises(simpleeval.FeatureNotAvailable):
+            self.t("[x for x in (1, 2, 3)]", (1, 2, 3))
+
+class TestNames(DRYTest):
+    ''' 'names', what other languages call variables... '''
+
+    def test_none(self):
+        ''' what to do when names isn't defined, or is 'none' '''
+        with self.assertRaises(NameNotDefined):
+            self.t("a == 2", None)
+
+        self.s.names["s"] = 21
+
+        with self.assertRaises(NameNotDefined):
+            self.t("s += a", 21)
+
+        self.s.names = None
+
+        with self.assertRaises(InvalidExpression):
+            self.t('s', 21)
+
+
+    def test_dict(self):
+        ''' using a normal dict for names lookup '''
+
+        self.s.names = {'a': 42}
+        self.t("a + a", 84)
+
+        self.s.names['also'] = 100
+
+        self.t("a + also - a", 100)
+
+        # however, you can't assign to those names:
+
+        self.t("a = 200", 200)
+
+        self.assertEqual(self.s.names['a'], 42)
+
+        # or assign to lists
+
+        self.s.names['b'] = [0]
+
+        self.t("b[0] = 11", 11)
+
+        self.assertEqual(self.s.names['b'], [0])
+
+        # but you can get items from a list:
+
+        self.s.names['b'] = [6, 7]
+
+        self.t("b[0] * b[1]", 42)
+
+        # or from a dict
+
+        self.s.names['c'] = {'i': 11}
+
+        self.t("c['i']", 11)
+
+        # you still can't assign though:
+
+        self.t("c['b'] = 99", 99)
+
+        self.assertFalse('b' in self.s.names['c'])
+
+        # and going all 'inception' on it doesn't work either:
+
+        self.s.names['c']['c'] = {'c': 11}
+
+        self.t("c['c']['c'] = 21", 21)
+
+        self.assertEqual(self.s.names['c']['c']['c'], 11)
+
+    def test_func(self):
+        ''' using a function for 'names lookup' '''
+
+        def resolver(node): # pylint: disable=unused-argument
+            ''' all names now equal 1024! '''
+            return 1024
+
+        self.s.names = resolver
+
+        self.t("a", 1024)
+        self.t("a + b - c - d", 0)
+
+        # the function can do stuff with the value it's sent:
+
+        def my_name(node):
+            ''' all names equal their textual name, twice. '''
+            return node.id + node.id
+
+        self.s.names = my_name
+
+        self.t("a", "aa")
+
+    def test_from_doc(self):
+        ''' the 'name first letter as value' example from the docs '''
+
+        def name_handler(node):
+            ''' return the alphabet number of the first letter of
+                the name's textual name '''
+            return ord(node.id[0].lower())-96
+
+        self.s.names = name_handler
+        self.t('a', 1)
+        self.t('a + b', 3)
+
+class Test_simple_eval(unittest.TestCase):
+    ''' test the 'simple_eval' wrapper function '''
+    def test_basic_run(self):
+        self.assertEqual(simple_eval('6*7'), 42)
+
+    def test_default_functions(self):
+        self.assertEqual(simple_eval('rand() < 1.0 and rand() > -0.01'), True)
+        self.assertEqual(simple_eval('randint(200) < 200 and rand() > 0'), True)
-- 
simpleeval



More information about the tryton-debian-vcs mailing list