Author: Brian Kearns <bdkea...@gmail.com>
Branch: 
Changeset: r73329:34ddb6cb97b1
Date: 2014-09-05 15:23 -0400
http://bitbucket.org/pypy/pypy/changeset/34ddb6cb97b1/

Log:    properly support EnvironmentError in rpython

diff --git a/rpython/annotator/builtin.py b/rpython/annotator/builtin.py
--- a/rpython/annotator/builtin.py
+++ b/rpython/annotator/builtin.py
@@ -255,12 +255,8 @@
         BUILTIN_ANALYZERS[original] = value
 
 
-@analyzer_for(getattr(IOError.__init__, 'im_func', IOError.__init__))
-def IOError_init(s_self, *args):
-    pass
-
-@analyzer_for(getattr(OSError.__init__, 'im_func', OSError.__init__))
-def OSError_init(s_self, *args):
+@analyzer_for(getattr(EnvironmentError.__init__, 'im_func', 
EnvironmentError.__init__))
+def EnvironmentError_init(s_self, *args):
     pass
 
 try:
diff --git a/rpython/annotator/classdef.py b/rpython/annotator/classdef.py
--- a/rpython/annotator/classdef.py
+++ b/rpython/annotator/classdef.py
@@ -438,8 +438,7 @@
 # ____________________________________________________________
 
 FORCE_ATTRIBUTES_INTO_CLASSES = {
-    IOError: {'errno': SomeInteger()},
-    OSError: {'errno': SomeInteger()},
+    EnvironmentError: {'errno': SomeInteger(), 'strerror': 
SomeString(can_be_None=True), 'filename': SomeString(can_be_None=True)},
 }
 
 try:
diff --git a/rpython/rtyper/rbuiltin.py b/rpython/rtyper/rbuiltin.py
--- a/rpython/rtyper/rbuiltin.py
+++ b/rpython/rtyper/rbuiltin.py
@@ -3,9 +3,10 @@
 from rpython.rlib import rarithmetic, objectmodel
 from rpython.rtyper import raddress, rptr, extregistry, rrange
 from rpython.rtyper.error import TyperError
-from rpython.rtyper.lltypesystem import lltype, llmemory, rclass
+from rpython.rtyper.lltypesystem import lltype, llmemory, rclass, rstr
 from rpython.rtyper.lltypesystem.rdict import rtype_r_dict
 from rpython.rtyper.rmodel import Repr
+from rpython.rtyper.rstr import AbstractStringRepr
 from rpython.tool.pairtype import pairtype
 
 
@@ -264,27 +265,21 @@
 def rtype_object__init__(hop):
     hop.exception_cannot_occur()
 
-def rtype_IOError__init__(hop):
+def rtype_EnvironmentError__init__(hop):
     hop.exception_cannot_occur()
-    if hop.nb_args == 2:
-        raise TyperError("IOError() should not be called with "
-                         "a single argument")
+    if hop.nb_args <= 1:
+        raise TyperError("EnvironmentError() should be called with "
+                         "at least one argument")
+    v_self = hop.args_v[0]
+    r_self = hop.args_r[0]
+    v_errno = hop.inputarg(lltype.Signed, arg=1)
+    r_self.setfield(v_self, 'errno', v_errno, hop.llops)
     if hop.nb_args >= 3:
-        v_self = hop.args_v[0]
-        r_self = hop.args_r[0]
-        v_errno = hop.inputarg(lltype.Signed, arg=1)
-        r_self.setfield(v_self, 'errno', v_errno, hop.llops)
-
-def rtype_OSError__init__(hop):
-    hop.exception_cannot_occur()
-    if hop.nb_args == 2:
-        raise TyperError("OSError() should not be called with "
-                         "a single argument")
-    if hop.nb_args >= 3:
-        v_self = hop.args_v[0]
-        r_self = hop.args_r[0]
-        v_errno = hop.inputarg(lltype.Signed, arg=1)
-        r_self.setfield(v_self, 'errno', v_errno, hop.llops)
+        v_strerror = hop.inputarg(rstr.string_repr, arg=2)
+        r_self.setfield(v_self, 'strerror', v_strerror, hop.llops)
+        if hop.nb_args >= 4:
+            v_filename = hop.inputarg(rstr.string_repr, arg=3)
+            r_self.setfield(v_self, 'filename', v_filename, hop.llops)
 
 def rtype_WindowsError__init__(hop):
     hop.exception_cannot_occur()
@@ -344,11 +339,8 @@
         original = getattr(__builtin__, name[14:])
         BUILTIN_TYPER[original] = value
 
-BUILTIN_TYPER[getattr(IOError.__init__, 'im_func', IOError.__init__)] = (
-    rtype_IOError__init__)
-
-BUILTIN_TYPER[getattr(OSError.__init__, 'im_func', OSError.__init__)] = (
-    rtype_OSError__init__)
+BUILTIN_TYPER[getattr(EnvironmentError.__init__, 'im_func', 
EnvironmentError.__init__)] = (
+    rtype_EnvironmentError__init__)
 
 try:
     WindowsError
diff --git a/rpython/rtyper/test/test_exception.py 
b/rpython/rtyper/test/test_exception.py
--- a/rpython/rtyper/test/test_exception.py
+++ b/rpython/rtyper/test/test_exception.py
@@ -36,22 +36,44 @@
 class TestException(BaseRtypingTest):
     def test_exception_with_arg(self):
         def g(n):
-            raise IOError(n, "?")
+            raise IOError(n)
         def h(n):
-            raise OSError(n, "?")
+            raise OSError(n, "?", None)
+        def i(n):
+            raise EnvironmentError(n, "?", "test")
+        def j(n):
+            raise IOError(0, "test")
         def f(n):
             try:
                 g(n)
             except IOError, e:
                 assert e.errno == 42
+                assert e.strerror is None
+                assert e.filename is None
             else:
                 assert False
             try:
                 h(n)
             except OSError, e:
                 assert e.errno == 42
+                assert e.strerror == "?"
+                assert e.filename is None
             else:
                 assert False
+            try:
+                i(n)
+            except EnvironmentError as e:
+                assert e.errno == 42
+                assert e.strerror == "?"
+                assert e.filename == "test"
+            else:
+                assert False
+            try:
+                j(n)
+            except (IOError, OSError) as e:
+                assert e.errno == 0
+                assert e.strerror == "test"
+                assert e.filename is None
         self.interpret(f, [42])
 
     def test_catch_incompatible_class(self):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to