[pypy-svn] r43820 - in pypy/branch/kill-ctypes/pypy/rpython: . module module/test

fijal at codespeak.net fijal at codespeak.net
Mon May 28 20:07:45 CEST 2007


Author: fijal
Date: Mon May 28 20:07:45 2007
New Revision: 43820

Added:
   pypy/branch/kill-ctypes/pypy/rpython/module/ll_termios.py
   pypy/branch/kill-ctypes/pypy/rpython/module/test/test_ll_termios.py
Modified:
   pypy/branch/kill-ctypes/pypy/rpython/extfuncregistry.py
Log:
First function for termios, only on rpython level.


Modified: pypy/branch/kill-ctypes/pypy/rpython/extfuncregistry.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/extfuncregistry.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/extfuncregistry.py	Mon May 28 20:07:45 2007
@@ -10,6 +10,12 @@
 from pypy.rpython.lltypesystem.module import ll_math
 from pypy.rpython.ootypesystem.module import ll_math as oo_math
 from pypy.rpython.module import ll_os
+try:
+    import termios
+except ImportError:
+    pass
+else:
+    from pypy.rpython.module import ll_termios
 
 # the following functions all take one float, return one float
 # and are part of math.h

Added: pypy/branch/kill-ctypes/pypy/rpython/module/ll_termios.py
==============================================================================
--- (empty file)
+++ pypy/branch/kill-ctypes/pypy/rpython/module/ll_termios.py	Mon May 28 20:07:45 2007
@@ -0,0 +1,51 @@
+
+"""
+The low-level implementation of termios module
+note that this module should only be imported when
+termios module is there
+"""
+
+import termios
+from pypy.rpython.lltypesystem import rffi
+from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.extfunc import register_external
+from pypy.rlib.rarithmetic import intmask
+
+# XXX is this portable? well.. not at all, ideally
+# I would like to have NCCS = CLaterConstant(NCCS)
+TCFLAG_T = rffi.UINT
+CC_T = rffi.UCHAR
+NCCS = 32
+SPEED_T = rffi.UINT
+
+includes = ['termios.h', 'unistd.h']
+
+TERMIOSP = rffi.CStruct('termios', ('c_iflag', TCFLAG_T), ('c_oflag', TCFLAG_T),
+                        ('c_cflag', TCFLAG_T), ('c_lflag', TCFLAG_T),
+                        ('c_cc', lltype.FixedSizeArray(CC_T, NCCS)))
+
+c_tcgetattr = rffi.llexternal('tcgetattr', [lltype.Signed, TERMIOSP],
+                              lltype.Signed, includes=includes)
+c_cfgetispeed = rffi.llexternal('cfgetispeed', [TERMIOSP], SPEED_T,
+                                includes=includes)
+c_cfgetospeed = rffi.llexternal('cfgetospeed', [TERMIOSP], SPEED_T,
+                                includes=includes)
+
+def tcgetattr_llimpl(fd):
+    c_struct = lltype.malloc(TERMIOSP.TO, flavor='raw')
+    error = c_tcgetattr(fd, c_struct)
+    if error == -1:
+        lltype.free(c_struct, flavor='raw')
+        raise OSError(error, 'tcgetattr failed')
+    cc = [chr(c_struct.c_c_cc[i]) for i in range(NCCS)]
+    ispeed = c_cfgetispeed(c_struct)
+    ospeed = c_cfgetospeed(c_struct)
+    result = (intmask(c_struct.c_c_iflag), intmask(c_struct.c_c_oflag),
+              intmask(c_struct.c_c_cflag), intmask(c_struct.c_c_lflag),
+              intmask(ispeed), intmask(ospeed), cc)
+    lltype.free(c_struct, flavor='raw')
+    return result
+
+register_external(termios.tcgetattr, [int], (int, int, int, int, int, int, [str]),
+                  llimpl=tcgetattr_llimpl, export_name='termios.tcgetattr')
+

Added: pypy/branch/kill-ctypes/pypy/rpython/module/test/test_ll_termios.py
==============================================================================
--- (empty file)
+++ pypy/branch/kill-ctypes/pypy/rpython/module/test/test_ll_termios.py	Mon May 28 20:07:45 2007
@@ -0,0 +1,71 @@
+
+import py
+import sys
+import os
+
+from pypy.tool.autopath import pypydir
+from pypy.translator.c.test.test_genc import compile
+from pypy.tool.udir import udir
+
+def setup_module(mod):
+    try:
+        import pexpect
+        mod.pexpect = pexpect
+    except ImportError:
+        py.test.skip("Pexpect not found")
+    try:
+        import termios
+        mod.termios = termios
+    except ImportError:
+        py.test.skip("termios not found")
+    py_py = py.path.local(pypydir).join('bin', 'py.py')
+    assert py_py.check()
+    mod.py_py = py_py
+
+class TestTermios(object):
+    def _spawn(self, *args, **kwds):
+        print 'SPAWN:', args, kwds
+        child = pexpect.spawn(*args, **kwds)
+        child.logfile = sys.stdout
+        return child
+
+    def spawn(self, argv):
+        return self._spawn(sys.executable, argv)
+
+    def test_getattr(self):
+        source = py.code.Source("""
+        import sys
+        sys.path.insert(0, '%s')
+        from pypy.translator.c.test.test_genc import compile
+        import termios
+        def runs_tcgetattr():
+            tpl = list(termios.tcgetattr(2)[:-1])
+            print tpl
+
+        fn = compile(runs_tcgetattr, [], backendopt=False,
+)
+        print 'XXX'
+        fn(expected_extra_mallocs=1)
+        print str(termios.tcgetattr(2)[:-1])
+        """ % os.path.dirname(pypydir))
+        f = udir.join("test_tcgetattr.py")
+        f.write(source)
+        child = self.spawn([str(f)])
+        child.expect("XXX")
+        child.expect('\[[^\]]*\]')
+        first = child.match.group(0)
+        child.expect('\[[^\]]*\]')
+        second = child.match.group(0)
+        assert first == second
+
+    #def test_one(self):
+    #    child = self.spawn()
+    #    child.expect("Python ")
+    #    child.expect('>>> ')
+    #    child.sendline('import termios')
+    #    child.expect('>>> ')
+    #    child.sendline('termios.tcgetattr(0)')
+    #    child.expect('\[.*?\[.*?\]\]')
+    #    lst = eval(child.match.group(0))
+    #    assert len(lst) == 7
+    #    assert len(lst[-1]) == 32 # XXX is this portable???



More information about the Pypy-commit mailing list