pypy-arp11 / pypy / interpreter / test /

from cStringIO import StringIO

import py
from pypy.tool.udir import udir
from pypy.interpreter.baseobjspace import OperationError
from pypy.interpreter import main

testcode = """\
def main():
    aStr = 'hello world'
    print len(aStr)


# On module test we want to ensure that the called module __name__ is
# '__main__' and argv is set as expected.
testmodulecode = """
import sys
if __name__ == '__main__':
    aStr = sys.argv[1]
    print len(aStr)

testresultoutput = '11\n'

def checkoutput(space, expected_output,f,*args):
    w_oldout = space.sys.get('stdout')
    capturefn = udir.join('capturefile')
    capturefile ='w')
    w_sys = space.sys.getmodule('sys')
    space.setattr(w_sys, space.wrap("stdout"), space.wrap(capturefile))
        f(*(args + (space,)))
        space.setattr(w_sys, space.wrap("stdout"), w_oldout)
    assert'rU') == expected_output

testfn = udir.join('')
testmodule = 'tmp_hello_module'
testpackage = 'tmp_package'

class TestMain:
    def setup_class(cls):
        testfn.write(testcode, 'w')
        udir.join(testmodule + '.py').write(testmodulecode, 'w')
        udir.ensure(testpackage, '')
        udir.join(testpackage, testmodule + '.py').write(testmodulecode, 'w')
        space =
        cls.w_oldsyspath = space.appexec([space.wrap(str(udir))], """(udir):
            import sys
            old = sys.path[:]
            sys.path.insert(0, udir)
            return old

    def teardown_class(cls):[cls.w_oldsyspath], """(old):
            import sys
            sys.path[:] = old

    def test_run_file(self):
        checkoutput(, testresultoutput, main.run_file, str(testfn))

    def test_run_string(self):
        checkoutput(, testresultoutput,
                                main.run_string, testcode, str(testfn))

    def test_eval_string(self):
        w_x = main.eval_string('2+2',

    def test_run_module(self):
        checkoutput(, testresultoutput, main.run_module,
                    testmodule, ['hello world'])
        checkoutput(, testresultoutput, main.run_module,
                    testpackage + '.' + testmodule, ['hello world'])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.