[pypy-svn] r26566 - in pypy/dist/pypy: rpython/rctypes rpython/rctypes/test rpython/rctypes/tool translator/c/src

arigo at codespeak.net arigo at codespeak.net
Sat Apr 29 10:50:28 CEST 2006


Author: arigo
Date: Sat Apr 29 10:50:26 2006
New Revision: 26566

Added:
   pypy/dist/pypy/rpython/rctypes/aerrno.py   (contents, props changed)
   pypy/dist/pypy/rpython/rctypes/test/test_rerrno.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
   pypy/dist/pypy/rpython/rctypes/tool/util.py
   pypy/dist/pypy/translator/c/src/ll_os.h
Log:
Support geterrno() from rctypes to genc.


Added: pypy/dist/pypy/rpython/rctypes/aerrno.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/aerrno.py	Sat Apr 29 10:50:26 2006
@@ -0,0 +1,36 @@
+"""
+Helpers to access the C-level 'errno' variable.
+"""
+from pypy.rpython.extregistry import ExtRegistryEntry
+from pypy.annotation import model as annmodel
+from ctypes import pythonapi, py_object
+
+
+##def setfromerrno(exc=OSError):
+##    """Raise an exception of the given class with the last failed C library
+##    function's errno."""
+##    pythonapi.PyErr_SetFromErrno(py_object(exc))
+
+def geterrno():
+    """Return the current 'errno' value."""
+    try:
+        pythonapi.PyErr_SetFromErrno(py_object(OSError))
+    except OSError, e:
+        return e.errno
+    else:
+        raise RuntimeError("setfromerrno() should have raised")
+
+
+class GetErrnoFnEntry(ExtRegistryEntry):
+    "Annotation and rtyping of calls to geterrno()"
+    _about_ = geterrno
+
+    def compute_result_annotation(self):
+        return annmodel.SomeInteger()
+
+    def specialize_call(self, hop):
+        from pypy.rpython.lltypesystem import lltype
+        return hop.llops.gencapicall('geterrno', [],
+                                     resulttype = lltype.Signed,
+                                     includes = (),
+                                     _callable = geterrno)

Added: pypy/dist/pypy/rpython/rctypes/test/test_rerrno.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rerrno.py	Sat Apr 29 10:50:26 2006
@@ -0,0 +1,91 @@
+"""
+Test errno.
+"""
+
+import py
+import sys, errno
+import pypy.rpython.rctypes.implementation
+from pypy.annotation.annrpython import RPythonAnnotator
+from pypy.translator.translator import TranslationContext
+from pypy.rpython.test.test_llinterp import interpret
+from pypy.translator.c.test.test_genc import compile
+from pypy import conftest
+from pypy.rpython.rctypes import aerrno
+from pypy.rpython.rctypes.test.test_rfunc import mylib
+
+from ctypes import c_char_p, c_int
+
+
+if sys.platform == 'win32':
+    py.test.skip("Unix only for now")
+
+open = mylib.open
+open.argtypes = [c_char_p, c_int, c_int]
+open.restype = c_int
+def ll_open(p, flags, mode):
+    s = ''
+    i = 0
+    while p[i] != '\x00':
+        s += p[i]
+        i += 1
+    return open(s, flags, mode)
+open.llinterp_friendly_version = ll_open
+open.includes = ('fcntl.h',)
+
+
+def test_open():
+    if sys.platform == 'win32':
+        py.test.skip("Unix only")
+    open = mylib.open
+    fd = open("/_rctypes_test_rfunc/this/directory/does/not/exist/at/all!",
+              0, 0)
+    result = aerrno.geterrno()
+    assert fd == -1
+    assert result == errno.ENOENT
+
+class Test_annotation:
+    def test_annotate_geterrno(self):
+        def f():
+            return aerrno.geterrno()
+        a = RPythonAnnotator()
+        s = a.build_types(f, [])
+        if conftest.option.view:
+            a.translator.view()
+        assert s.knowntype == int
+
+class Test_specialization:
+    def test_specialize_geterrno(self):
+        if sys.platform == 'win32':
+            py.test.skip("Unix only")
+        open = mylib.open
+        open.argtypes = [c_char_p, c_int, c_int]
+        open.restype = c_int
+        def func():
+            fd = open(
+                "/_rctypes_test_rfunc/this/directory/does/not/exist/at/all!",
+                0, 0)
+            return fd, aerrno.geterrno()
+
+        res = interpret(func, [])
+        assert res.item0 == -1
+        # the following doesn't work because the llinterp calls many C library
+        # functions between open() and geterrno()
+        ##assert res.item1 == errno.ENOENT
+
+class Test_compile:
+    def test_compile_geterrno(self):
+        if sys.platform == 'win32':
+            py.test.skip("Unix only")
+        open = mylib.open
+        open.argtypes = [c_char_p, c_int, c_int]
+        open.restype = c_int
+        def func():
+            fd = open(
+                "/_rctypes_test_rfunc/this/directory/does/not/exist/at/all!",
+                0, 0)
+            return fd, aerrno.geterrno()
+
+        fn = compile(func, [])
+        fd, result = fn()
+        assert fd == -1
+        assert result == errno.ENOENT

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	Sat Apr 29 10:50:26 2006
@@ -3,7 +3,7 @@
 """
 
 import py
-import sys, errno
+import sys
 import pypy.rpython.rctypes.implementation
 from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.translator.translator import TranslationContext, graphof
@@ -119,21 +119,6 @@
     s2 = ctime(byref(c_long(N)))
     assert s1.strip() == s2.strip()
 
-def test_open():
-    if sys.platform == 'win32':
-        py.test.skip("Unix only")
-    open = mylib.open
-    fd = open("/_rctypes_test_rfunc/this/directory/does/not/exist/at/all!",
-              0, 0)
-    try:
-        util.setfromerrno()
-    except OSError, e:
-        pass
-    else:
-        raise AssertionError("util.setfromerrno() should have raised")
-    assert fd == -1
-    assert e.errno == errno.ENOENT
-
 ##def test_callback():
 ##    assert callback(100) == 103
 ##    assert pycallback(100) == 103
@@ -197,7 +182,6 @@
         if conftest.option.view:
             a.translator.view()
         assert s.knowntype == str
-        
 
 ##    def test_annotate_callback(self):
 ##        def fn(n):

Modified: pypy/dist/pypy/rpython/rctypes/tool/util.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/tool/util.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/tool/util.py	Sat Apr 29 10:50:26 2006
@@ -1,16 +1,9 @@
 """
-This is the module 'ctypes.util', copied from ctypes 0.9.9.6,
-with an extra setfromerrno() helper.
+This is the module 'ctypes.util', copied from ctypes 0.9.9.6.
 """
 import sys, os
 import ctypes
 
-def setfromerrno(exc=OSError):
-    """Raise an exception of the given class with the last failed C library
-    function's errno."""
-    ctypes.pythonapi.PyErr_SetFromErrno(ctypes.py_object(exc))
-
-
 # find_library(name) returns the pathname of a library, or None.
 if os.name == "nt":
     def find_library(name):

Modified: pypy/dist/pypy/translator/c/src/ll_os.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/ll_os.h	(original)
+++ pypy/dist/pypy/translator/c/src/ll_os.h	Sat Apr 29 10:50:26 2006
@@ -62,6 +62,8 @@
 RPyString *LL_os_readdir(struct RPyOpaque_DIR *dir);
 void LL_os_closedir(struct RPyOpaque_DIR *dir);
 
+#define geterrno()  errno
+
 /* implementations */
 
 #ifndef PYPY_NOT_MAIN_FILE



More information about the Pypy-commit mailing list