Exemplo n.º 1
0
 def test_long_lines(self):
     """ Test for spurious syntax error created by the interpreter."""
     test_strings = [u'( 1 +\n 1\n )\n\n',
                     u'(1 \n + 1\n )\n\n',
                    ]
     i = Interpreter()
     for s in test_strings:
         i.execute(s)
Exemplo n.º 2
0
 def test_split_commands(self):
     """ Test that commands are indeed individually split."""
     i = Interpreter()
     test_atoms = [('(1\n + 1)', ),
                   ('1', '1', ),
                   ]
     for atoms in test_atoms:
         atoms = [atom.rstrip() + '\n' for atom in atoms]
         self.assertEquals(i.split_commands(''.join(atoms)),atoms)
Exemplo n.º 3
0
    def runTestExecute(self, cmd):
        self.shell = Interpreter()
        actual = self.shell.execute(cmd)

        def compare(computed):
            actual['id'] = computed['id']
            self.assertEquals(actual, computed)

        d = self.engine.execute(cmd)
        d.addCallback(compare)
        return d
Exemplo n.º 4
0
    def runTestGetResult(self, cmd):
        self.shell = Interpreter()
        actual = self.shell.execute(cmd)

        def compare(computed):
            actual['id'] = computed['id']
            self.assertEquals(actual, computed)

        d = self.engine.execute(cmd)
        d.addCallback(lambda r: self.engine.get_result(r['number']))
        d.addCallback(compare)
        return d
Exemplo n.º 5
0
    def __init__(self, shell=None, history=None, banner=None, *args, **kwargs):
        if shell is None:
            shell = Interpreter()
        FrontEndBase.__init__(self, shell=shell, history=history)

        if banner is not None:
            self.banner = banner
Exemplo n.º 6
0
 def testGetResultAll(self):
     self.addEngine(4)
     d= defer.Deferred()
     shell = Interpreter()
     for cmd in validCommands:
             self.runGetResultAll(d, cmd, shell)
     d.callback(None)
     return d
Exemplo n.º 7
0
 def runTestExecute(self, cmd):
     self.shell = Interpreter()
     actual = self.shell.execute(cmd)
     def compare(computed):
         actual['id'] = computed['id']
         self.assertEquals(actual, computed)
     d = self.engine.execute(cmd)
     d.addCallback(compare)
     return d
Exemplo n.º 8
0
 def testGetResult(self):
     shell = Interpreter()
     result1 = shell.execute('a=10')
     result1['id'] = 0
     result2 = shell.execute('b=20')
     result2['id'] = 0
     execute= self.multiengine.execute
     get_result = self.multiengine.get_result
     self.addEngine(1)
     d= execute('a=10')
     d.addCallback(lambda _: get_result())
     d.addCallback(lambda r: self.assertEquals(r[0], result1))
     d.addCallback(lambda _: execute('b=20'))
     d.addCallback(lambda _: get_result(1))
     d.addCallback(lambda r: self.assertEquals(r[0], result1))
     d.addCallback(lambda _: get_result(2, block=False))
     d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
     d.addCallback(lambda r: self.assertEquals(r[0], result2))
     return d
Exemplo n.º 9
0
 def testGetResult(self):
     shell = Interpreter()
     result1 = shell.execute('a=10')
     result1['id'] = 0
     result2 = shell.execute('b=20')
     result2['id'] = 0
     execute= self.multiengine.execute
     get_result = self.multiengine.get_result
     self.addEngine(1)
     d= execute('a=10')
     d.addCallback(lambda _: get_result())
     d.addCallback(lambda r: self.assertEquals(r[0], result1))
     d.addCallback(lambda _: execute('b=20'))
     d.addCallback(lambda _: get_result(1))
     d.addCallback(lambda r: self.assertEquals(r[0], result1))
     d.addCallback(lambda _: get_result(2, block=False))
     d.addCallback(lambda did: self.multiengine.get_pending_deferred(did, True))
     d.addCallback(lambda r: self.assertEquals(r[0], result2))
     return d
Exemplo n.º 10
0
 def runTestGetResult(self, cmd):
     self.shell = Interpreter()
     actual = self.shell.execute(cmd)
     def compare(computed):
         actual['id'] = computed['id']
         self.assertEquals(actual, computed)
     d = self.engine.execute(cmd)
     d.addCallback(lambda r: self.engine.get_result(r['number']))
     d.addCallback(compare)
     return d
Exemplo n.º 11
0
 def testControllerExecutesCode(self):
     code ="""5+5"""
     expected = Interpreter().execute(code)
     del expected['number']
     def removeNumberAndID(result):
         del result['number']
         del result['id']
         return result
     d = self.controller.execute(code)
     d.addCallback(removeNumberAndID)
     d.addCallback(lambda r: self.assertEquals(r, expected))
Exemplo n.º 12
0
def interpreter(pipe):
    """Set up an :class:`IPython.kernel.core.interpreter.Interpreter`.
    
    A kernel will listen for connections on a random local port.
    
    Each kernel's global namespace contains an instance of the 
    class :class:`visualize.dec.Viz`.  This variable `Viz` is callable::
        
        >>> a = range(5)
        >>> print Viz(a)
        ...
    
    Otherwise, this is a normal IPython interpreter.
    
    :param pipe: A :class:`multiprocessing.Connection` that enables \
    communication between this process and the main :class:`ws_tornado` server.
    """
    
    listener = Listener(_SETUP_IP)
    pipe.send(listener.address)
    
    shell = Interpreter()
    load_ipython_extension(shell.user_ns)
    
    conn = listener.accept()
    
    while True:
        message, caller = conn.recv()
        try:
            res = shell.execute(message)
        except Exception, error:
            res = {'stdout': '%s: %s' % (error.__class__.__name__, error)}
        conn.send({
            'content': res.get('stdout', ''), 
            'target': caller,
            'type': 'output',
        })
 def _interpreter_default(self):
     # Create an interpreter that has a reference to our namespace.
     return Interpreter(user_ns=self.namespace)
Exemplo n.º 14
0
class IEngineCoreTestCase(object):
    """Test an IEngineCore implementer."""
    def createShell(self):
        return Interpreter()

    def catchQueueCleared(self, f):
        try:
            f.raiseException()
        except error.QueueCleared:
            pass

    def testIEngineCoreInterface(self):
        """Does self.engine claim to implement IEngineCore?"""
        self.assert_(es.IEngineCore.providedBy(self.engine))

    def testIEngineCoreInterfaceMethods(self):
        """Does self.engine have the methods and attributes in IEngineCore."""
        for m in list(es.IEngineCore):
            self.assert_(hasattr(self.engine, m))

    def testIEngineCoreDeferreds(self):
        d = self.engine.execute('a=5')
        d.addCallback(lambda _: self.engine.pull('a'))
        d.addCallback(lambda _: self.engine.get_result())
        d.addCallback(lambda _: self.engine.keys())
        d.addCallback(lambda _: self.engine.push(dict(a=10)))
        return d

    def runTestExecute(self, cmd):
        self.shell = Interpreter()
        actual = self.shell.execute(cmd)

        def compare(computed):
            actual['id'] = computed['id']
            self.assertEquals(actual, computed)

        d = self.engine.execute(cmd)
        d.addCallback(compare)
        return d

    @parametric
    def testExecute(cls):
        return [(cls.runTestExecute, cmd) for cmd in validCommands]

    def runTestExecuteFailures(self, cmd, exc):
        def compare(f):
            self.assertRaises(exc, f.raiseException)

        d = self.engine.execute(cmd)
        d.addErrback(compare)
        return d

    @parametric
    def testExecuteFailuresEngineService(cls):
        return [(cls.runTestExecuteFailures, cmd, exc)
                for cmd, exc in invalidCommands]

    def runTestPushPull(self, o):
        d = self.engine.push(dict(a=o))
        d.addCallback(lambda r: self.engine.pull('a'))
        d.addCallback(lambda r: self.assertEquals(o, r))
        return d

    @parametric
    def testPushPull(cls):
        objs = [10, "hi there", 1.2342354, {"p": (1, 2)}, None]
        return [(cls.runTestPushPull, o) for o in objs]

    def testPullNameError(self):
        d = self.engine.push(dict(a=5))
        d.addCallback(lambda _: self.engine.reset())
        d.addCallback(lambda _: self.engine.pull("a"))
        d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
        return d

    def testPushPullFailures(self):
        d = self.engine.pull('a')
        d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
        d.addCallback(lambda _: self.engine.execute('l = lambda x: x'))
        d.addCallback(lambda _: self.engine.pull('l'))
        d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.
                                                 raiseException))
        d.addCallback(lambda _: self.engine.push(dict(l=lambda x: x)))
        d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.
                                                 raiseException))
        return d

    def testPushPullArray(self):
        try:
            import numpy
        except:
            return
        a = numpy.random.random(1000)
        d = self.engine.push(dict(a=a))
        d.addCallback(lambda _: self.engine.pull('a'))
        d.addCallback(lambda b: b == a)
        d.addCallback(lambda c: c.all())
        return self.assertDeferredEquals(d, True)

    def testPushFunction(self):

        d = self.engine.push_function(dict(f=testf))
        d.addCallback(lambda _: self.engine.execute('result = f(10)'))
        d.addCallback(lambda _: self.engine.pull('result'))
        d.addCallback(lambda r: self.assertEquals(r, testf(10)))
        return d

    def testPullFunction(self):
        d = self.engine.push_function(dict(f=testf, g=testg))
        d.addCallback(lambda _: self.engine.pull_function(('f', 'g')))
        d.addCallback(lambda r: self.assertEquals(r[0](10), testf(10)))
        return d

    def testPushFunctionGlobal(self):
        """Make sure that pushed functions pick up the user's namespace for globals."""
        d = self.engine.push(dict(globala=globala))
        d.addCallback(lambda _: self.engine.push_function(dict(g=testg)))
        d.addCallback(lambda _: self.engine.execute('result = g(10)'))
        d.addCallback(lambda _: self.engine.pull('result'))
        d.addCallback(lambda r: self.assertEquals(r, testg(10)))
        return d

    def testGetResultFailure(self):
        d = self.engine.get_result(None)
        d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
        d.addCallback(lambda _: self.engine.get_result(10))
        d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
        return d

    def runTestGetResult(self, cmd):
        self.shell = Interpreter()
        actual = self.shell.execute(cmd)

        def compare(computed):
            actual['id'] = computed['id']
            self.assertEquals(actual, computed)

        d = self.engine.execute(cmd)
        d.addCallback(lambda r: self.engine.get_result(r['number']))
        d.addCallback(compare)
        return d

    @parametric
    def testGetResult(cls):
        return [(cls.runTestGetResult, cmd) for cmd in validCommands]

    def testGetResultDefault(self):
        cmd = 'a=5'
        shell = self.createShell()
        shellResult = shell.execute(cmd)

        def popit(dikt, key):
            dikt.pop(key)
            return dikt

        d = self.engine.execute(cmd)
        d.addCallback(lambda _: self.engine.get_result())
        d.addCallback(lambda r: self.assertEquals(shellResult, popit(r, 'id')))
        return d

    def testKeys(self):
        d = self.engine.keys()
        d.addCallback(lambda s: isinstance(s, list))
        d.addCallback(lambda r: self.assertEquals(r, True))
        return d
Exemplo n.º 15
0
class IEngineCoreTestCase(object):
    """Test an IEngineCore implementer."""

    def createShell(self):
        return Interpreter()

    def catchQueueCleared(self, f):
        try:
            f.raiseException()
        except error.QueueCleared:
            pass

    def testIEngineCoreInterface(self):
        """Does self.engine claim to implement IEngineCore?"""
        self.assert_(es.IEngineCore.providedBy(self.engine))

    def testIEngineCoreInterfaceMethods(self):
        """Does self.engine have the methods and attributes in IEngineCore."""
        for m in list(es.IEngineCore):
            self.assert_(hasattr(self.engine, m))

    def testIEngineCoreDeferreds(self):
        d = self.engine.execute("a=5")
        d.addCallback(lambda _: self.engine.pull("a"))
        d.addCallback(lambda _: self.engine.get_result())
        d.addCallback(lambda _: self.engine.keys())
        d.addCallback(lambda _: self.engine.push(dict(a=10)))
        return d

    def runTestExecute(self, cmd):
        self.shell = Interpreter()
        actual = self.shell.execute(cmd)

        def compare(computed):
            actual["id"] = computed["id"]
            self.assertEquals(actual, computed)

        d = self.engine.execute(cmd)
        d.addCallback(compare)
        return d

    @parametric
    def testExecute(cls):
        return [(cls.runTestExecute, cmd) for cmd in validCommands]

    def runTestExecuteFailures(self, cmd, exc):
        def compare(f):
            self.assertRaises(exc, f.raiseException)

        d = self.engine.execute(cmd)
        d.addErrback(compare)
        return d

    @parametric
    def testExecuteFailuresEngineService(cls):
        return [(cls.runTestExecuteFailures, cmd, exc) for cmd, exc in invalidCommands]

    def runTestPushPull(self, o):
        d = self.engine.push(dict(a=o))
        d.addCallback(lambda r: self.engine.pull("a"))
        d.addCallback(lambda r: self.assertEquals(o, r))
        return d

    @parametric
    def testPushPull(cls):
        objs = [10, "hi there", 1.2342354, {"p": (1, 2)}, None]
        return [(cls.runTestPushPull, o) for o in objs]

    def testPullNameError(self):
        d = self.engine.push(dict(a=5))
        d.addCallback(lambda _: self.engine.reset())
        d.addCallback(lambda _: self.engine.pull("a"))
        d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
        return d

    def testPushPullFailures(self):
        d = self.engine.pull("a")
        d.addErrback(lambda f: self.assertRaises(NameError, f.raiseException))
        d.addCallback(lambda _: self.engine.execute("l = lambda x: x"))
        d.addCallback(lambda _: self.engine.pull("l"))
        d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
        d.addCallback(lambda _: self.engine.push(dict(l=lambda x: x)))
        d.addErrback(lambda f: self.assertRaises(pickle.PicklingError, f.raiseException))
        return d

    def testPushPullArray(self):
        try:
            import numpy
        except:
            return
        a = numpy.random.random(1000)
        d = self.engine.push(dict(a=a))
        d.addCallback(lambda _: self.engine.pull("a"))
        d.addCallback(lambda b: b == a)
        d.addCallback(lambda c: c.all())
        return self.assertDeferredEquals(d, True)

    def testPushFunction(self):

        d = self.engine.push_function(dict(f=testf))
        d.addCallback(lambda _: self.engine.execute("result = f(10)"))
        d.addCallback(lambda _: self.engine.pull("result"))
        d.addCallback(lambda r: self.assertEquals(r, testf(10)))
        return d

    def testPullFunction(self):
        d = self.engine.push_function(dict(f=testf, g=testg))
        d.addCallback(lambda _: self.engine.pull_function(("f", "g")))
        d.addCallback(lambda r: self.assertEquals(r[0](10), testf(10)))
        return d

    def testPushFunctionGlobal(self):
        """Make sure that pushed functions pick up the user's namespace for globals."""
        d = self.engine.push(dict(globala=globala))
        d.addCallback(lambda _: self.engine.push_function(dict(g=testg)))
        d.addCallback(lambda _: self.engine.execute("result = g(10)"))
        d.addCallback(lambda _: self.engine.pull("result"))
        d.addCallback(lambda r: self.assertEquals(r, testg(10)))
        return d

    def testGetResultFailure(self):
        d = self.engine.get_result(None)
        d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
        d.addCallback(lambda _: self.engine.get_result(10))
        d.addErrback(lambda f: self.assertRaises(IndexError, f.raiseException))
        return d

    def runTestGetResult(self, cmd):
        self.shell = Interpreter()
        actual = self.shell.execute(cmd)

        def compare(computed):
            actual["id"] = computed["id"]
            self.assertEquals(actual, computed)

        d = self.engine.execute(cmd)
        d.addCallback(lambda r: self.engine.get_result(r["number"]))
        d.addCallback(compare)
        return d

    @parametric
    def testGetResult(cls):
        return [(cls.runTestGetResult, cmd) for cmd in validCommands]

    def testGetResultDefault(self):
        cmd = "a=5"
        shell = self.createShell()
        shellResult = shell.execute(cmd)

        def popit(dikt, key):
            dikt.pop(key)
            return dikt

        d = self.engine.execute(cmd)
        d.addCallback(lambda _: self.engine.get_result())
        d.addCallback(lambda r: self.assertEquals(shellResult, popit(r, "id")))
        return d

    def testKeys(self):
        d = self.engine.keys()
        d.addCallback(lambda s: isinstance(s, list))
        d.addCallback(lambda r: self.assertEquals(r, True))
        return d
Exemplo n.º 16
0
 def test_unicode(self):
     """ Test unicode handling with the interpreter."""
     i = Interpreter()
     i.execute_python(u'print "ù"')
     i.execute_python('print "ù"')
Exemplo n.º 17
0
 def createShell(self):
     return Interpreter()
Exemplo n.º 18
0
 def test_ticket266993(self):
     """ Test for ticket 266993."""
     i = Interpreter()
     i.execute('str("""a\nb""")')
Exemplo n.º 19
0
 def test_ticket364347(self):
     """Test for ticket 364347."""
     i = Interpreter()
     i.split_commands('str("a\\nb")')