~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/rpython/module/test/test_ll_os.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from pypy.tool.udir import udir
 
3
from pypy.tool.pytest.modcheck import skipimporterror
 
4
skipimporterror("ctypes")
 
5
 
 
6
from pypy.rpython.lltypesystem.module.ll_os import Implementation as impl
 
7
import sys
 
8
 
 
9
def test_access():
 
10
    filename = str(udir.join('test_access.txt'))
 
11
    rsfilename = impl.to_rstr(filename)
 
12
 
 
13
    fd = file(filename, 'w')
 
14
    fd.close()
 
15
 
 
16
    for mode in os.R_OK, os.W_OK, os.X_OK, os.R_OK | os.W_OK | os.X_OK:
 
17
        assert os.access(filename, mode) == impl.ll_os_access(rsfilename, mode)
 
18
 
 
19
 
 
20
def test_open_read_write_close():
 
21
    filename = str(udir.join('test_open_read_write_close.txt'))
 
22
    rsfilename = impl.to_rstr(filename)
 
23
 
 
24
    fd = impl.ll_os_open(rsfilename, os.O_WRONLY | os.O_CREAT, 0777)
 
25
    count = impl.ll_os_write(fd, impl.to_rstr("hello world\n"))
 
26
    assert count == len("hello world\n")
 
27
    impl.ll_os_close(fd)
 
28
 
 
29
    fd = impl.ll_os_open(rsfilename, os.O_RDONLY, 0777)
 
30
    data = impl.ll_os_read(fd, 500)
 
31
    assert impl.from_rstr(data) == "hello world\n"
 
32
    impl.ll_os_close(fd)
 
33
 
 
34
    os.unlink(filename)
 
35
 
 
36
def test_getcwd():
 
37
    data = impl.ll_os_getcwd()
 
38
    assert impl.from_rstr(data) == os.getcwd()
 
39
 
 
40
def test_strerror():
 
41
    data = impl.ll_os_strerror(2)
 
42
    assert impl.from_rstr(data) == os.strerror(2)
 
43
 
 
44
def test_system():
 
45
    filename = str(udir.join('test_system.txt'))
 
46
    arg = impl.to_rstr('python -c "print 1+1" > %s' % filename)
 
47
    data = impl.ll_os_system(arg)
 
48
    assert data == 0
 
49
    assert file(filename).read().strip() == '2'
 
50
    os.unlink(filename)
 
51
 
 
52
def test_putenv_unsetenv():
 
53
    filename = str(udir.join('test_putenv.txt'))
 
54
    arg = impl.to_rstr('abcdefgh=12345678')
 
55
    impl.ll_os_putenv(arg)
 
56
    cmd = '''python -c "import os; print os.environ['abcdefgh']" > %s''' % filename
 
57
    os.system(cmd)
 
58
    f = file(filename)
 
59
    result = f.read().strip()
 
60
    assert result == '12345678'
 
61
    f.close()
 
62
    os.unlink(filename)
 
63
    posix = __import__(os.name)
 
64
    if hasattr(posix, "unsetenv"):
 
65
        impl.ll_os_unsetenv(impl.to_rstr("abcdefgh"))
 
66
        cmd = '''python -c "import os; print repr(os.getenv('abcdefgh'))" > %s''' % filename
 
67
        os.system(cmd)
 
68
        f = file(filename)
 
69
        result = f.read().strip()
 
70
        assert result == 'None'
 
71
        f.close()
 
72
        os.unlink(filename)
 
73
 
 
74
test_src = """
 
75
import os
 
76
from pypy.tool.udir import udir
 
77
from pypy.rpython.lltypesystem.module.ll_os import Implementation as impl
 
78
 
 
79
def test_environ():
 
80
    count = 0
 
81
    while 1:
 
82
        if not impl.ll_os_environ(count):
 
83
            break
 
84
        count += 1
 
85
    channel.send(count == len(os.environ.keys()))
 
86
test_environ()
 
87
"""
 
88
 
 
89
def test_environ():
 
90
    import py
 
91
    gw = py.execnet.PopenGateway()
 
92
    chan = gw.remote_exec(py.code.Source(test_src))
 
93
    res = chan.receive()
 
94
    assert res
 
95
    chan.close()
 
96
 
 
97
def test_opendir_readdir():
 
98
    dirname = str(udir)
 
99
    rsdirname = impl.to_rstr(dirname)
 
100
    result = []
 
101
    DIR = impl.ll_os_opendir(rsdirname)
 
102
    try:
 
103
        while True:
 
104
            nextentry = impl.ll_os_readdir(DIR)
 
105
            if not nextentry:   # null pointer check
 
106
                break
 
107
            result.append(impl.from_rstr(nextentry))
 
108
    finally:
 
109
        impl.ll_os_closedir(DIR)
 
110
    assert '.' in result
 
111
    assert '..' in result
 
112
    result.remove('.')
 
113
    result.remove('..')
 
114
    result.sort()
 
115
    compared_with = os.listdir(dirname)
 
116
    compared_with.sort()
 
117
    assert result == compared_with
 
118
 
 
119
if hasattr(os, 'execv'):
 
120
    from pypy.rpython.extregistry import lookup
 
121
    os_execv = lookup(os.execv).lltypeimpl
 
122
    
 
123
    def test_execv():
 
124
        filename = str(udir.join('test_execv_ctypes.txt'))
 
125
 
 
126
        progname = str(sys.executable)
 
127
        l = ['', '']
 
128
        l[0] = progname
 
129
        l[1] = "-c"
 
130
        l.append('open("%s","w").write("1")' % filename)
 
131
        pid = os.fork()
 
132
        if pid == 0:
 
133
            os_execv(progname, l)
 
134
        else:
 
135
            os.waitpid(pid, 0)
 
136
        assert open(filename).read() == "1"
 
137
 
 
138
def test_dup():
 
139
    from pypy.rpython.extregistry import lookup
 
140
    os_dup = lookup(os.dup).lltypeimpl
 
141
    testf = udir.join('test.txt')
 
142
    testf.write("foo")
 
143
    path = testf.strpath
 
144
 
 
145
    def ff(fi):
 
146
        g = os_dup(fi)
 
147
        return g
 
148
    fi = os.open(path,os.O_RDONLY,0755)
 
149
    g = ff(fi)
 
150
    assert os.fstat(g) == os.fstat(fi)
 
151
 
 
152