[pypy-svn] r43886 - in pypy/branch/kill-ctypes/pypy: module/termios module/termios/test rpython/module rpython/module/test

fijal at codespeak.net fijal at codespeak.net
Tue May 29 22:46:06 CEST 2007


Author: fijal
Date: Tue May 29 22:46:05 2007
New Revision: 43886

Modified:
   pypy/branch/kill-ctypes/pypy/module/termios/__init__.py
   pypy/branch/kill-ctypes/pypy/module/termios/interp_termios.py
   pypy/branch/kill-ctypes/pypy/module/termios/test/test_termios.py
   pypy/branch/kill-ctypes/pypy/rpython/module/ll_termios.py
   pypy/branch/kill-ctypes/pypy/rpython/module/test/test_ll_termios.py
Log:
Add some obscure annotator hack and make it translatable and testable


Modified: pypy/branch/kill-ctypes/pypy/module/termios/__init__.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/module/termios/__init__.py	(original)
+++ pypy/branch/kill-ctypes/pypy/module/termios/__init__.py	Tue May 29 22:46:05 2007
@@ -1,5 +1,8 @@
 
 from pypy.interpreter.mixedmodule import MixedModule
+import termios
+from pypy.rpython.module.ll_termios import termios_error
+from pypy.rlib.nonconst import NonConstant
 
 class Module(MixedModule):
     "This module provides an interface to the Posix calls for tty I/O control.\n\
@@ -25,6 +28,13 @@
         'tcsetattr'   : 'interp_termios.tcsetattr',
     }
 
+    def startup(self, space):
+        # XXX nasty annotation trick
+        try:
+            raise termios_error(NonConstant(-3), NonConstant("xxx"))
+        except termios.error, e:
+            pass
+
 import termios
 from pypy.module.termios import interp_termios
 

Modified: pypy/branch/kill-ctypes/pypy/module/termios/interp_termios.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/module/termios/interp_termios.py	(original)
+++ pypy/branch/kill-ctypes/pypy/module/termios/interp_termios.py	Tue May 29 22:46:05 2007
@@ -5,9 +5,9 @@
 
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.interpreter.error import OperationError
+from pypy.rpython.module import ll_termios
 import os
 import termios
-from pypy.rlib.objectmodel import we_are_translated
 
 # proper semantics are to have termios.error, but since it's not documented
 # anyway, let's have it as OSError on interplevel. We need to have
@@ -30,23 +30,15 @@
 tcsetattr.unwrap_spec = [ObjSpace, int, int, W_Root]
 
 def tcgetattr(space, fd):
-    # XXX Argh argh argh argh. ARGH!
-    if we_are_translated():
-        try:
-            tup_w = termios.tcgetattr(fd)
-        except OSError, e:
-            raise convert_error(space, e)
-    else:
-        try:
-            tup_w = termios.tcgetattr(fd)
-        except termios.error, e:
-            e.errno = e.args[0]
-            raise convert_error(space, e)
-    l_w = []
-    for w_item in tup_w[:-1]:
-        l_w.append(space.wrap(w_item))
+    try:
+        tup = termios.tcgetattr(fd)
+    except termios.error, e:
+        e.errno = e.args[0]
+        raise convert_error(space, e)
+    iflag, oflag, cflag, lflag, ispeed, ospeed, cc = tup
+    l_w = [space.wrap(i) for i in [iflag, oflag, cflag, lflag, ispeed, ospeed]]
     # last one need to be chosen carefully
-    w_cc = space.newlist([space.wrap(i) for i in tup_w[-1]])
+    w_cc = space.newlist([space.wrap(i) for i in cc])
     l_w.append(w_cc)
     return space.newlist(l_w)
 tcgetattr.unwrap_spec = [ObjSpace, int]

Modified: pypy/branch/kill-ctypes/pypy/module/termios/test/test_termios.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/module/termios/test/test_termios.py	(original)
+++ pypy/branch/kill-ctypes/pypy/module/termios/test/test_termios.py	Tue May 29 22:46:05 2007
@@ -52,7 +52,6 @@
         cls.w_orig_module_dict = cls.space.appexec([], "(): return %r" % (d,))
 
     def test_values(self):
-        skip("Not supported yet")
         import termios
         d = {}
         for name in dir(termios):

Modified: pypy/branch/kill-ctypes/pypy/rpython/module/ll_termios.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/module/ll_termios.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/module/ll_termios.py	Tue May 29 22:46:05 2007
@@ -10,6 +10,9 @@
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.extfunc import register_external
 from pypy.rlib.rarithmetic import intmask
+from pypy.rpython.extregistry import ExtRegistryEntry
+from pypy.annotation import model as annmodel
+from pypy.rpython import rclass
 
 # XXX is this portable? well.. not at all, ideally
 # I would like to have NCCS = CLaterConstant(NCCS)
@@ -20,10 +23,6 @@
 
 includes = ['termios.h', 'unistd.h']
 
-# XXX all functions here raise OSError, because they cannot
-# raise termios.error (lack of translation possibilities). hence
-# I don't know how to solve this, the tests will probably don't work
-
 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)))
@@ -35,12 +34,16 @@
 c_cfgetospeed = rffi.llexternal('cfgetospeed', [TERMIOSP], SPEED_T,
                                 includes=includes)
 
+class termios_error(termios.error):
+    def __init__(self, num, msg):
+        self.args = (num, msg)
+
 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')
+        raise termios_error(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)

Modified: pypy/branch/kill-ctypes/pypy/rpython/module/test/test_ll_termios.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/rpython/module/test/test_ll_termios.py	(original)
+++ pypy/branch/kill-ctypes/pypy/rpython/module/test/test_ll_termios.py	Tue May 29 22:46:05 2007
@@ -32,7 +32,7 @@
     def spawn(self, argv):
         return self._spawn(sys.executable, argv)
 
-    def test_getattr(self):
+    def test_tcgetattr(self):
         source = py.code.Source("""
         import sys
         sys.path.insert(0, '%s')
@@ -58,14 +58,29 @@
         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???
+    def test_tcgetattr2(self):
+        source = py.code.Source("""
+        import sys
+        sys.path.insert(0, '%s')
+        from pypy.translator.c.test.test_genc import compile
+        from pypy.rpython.module import ll_termios
+        import termios
+        def runs_tcgetattr():
+            try:
+                termios.tcgetattr(338)
+            except termios.error, e:
+                return 2
+            return 3
+
+        fn = compile(runs_tcgetattr, [], backendopt=False)
+        res = fn()
+        if res == 2:
+            print 'OK!'
+        else:
+            print 'fail!'
+        """ % os.path.dirname(pypydir))
+        f = udir.join("test_tcgetattr.py")
+        f.write(source)
+        child = self.spawn([str(f)])
+        child.expect("OK!")
+



More information about the Pypy-commit mailing list