[pypy-commit] pypy default: move some code out of descr_init, cleanup

pjenvey noreply at buildbot.pypy.org
Fri Apr 26 03:32:07 CEST 2013


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: 
Changeset: r63618:0bf46697f8e7
Date: 2013-04-25 18:15 -0700
http://bitbucket.org/pypy/pypy/changeset/0bf46697f8e7/

Log:	move some code out of descr_init, cleanup

diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -1,16 +1,16 @@
-from pypy.module._io.interp_iobase import W_IOBase
-from pypy.interpreter.typedef import (
-    TypeDef, GetSetProperty, interp_attrproperty_w, interp_attrproperty,
-    generic_new_descr)
-from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
+import sys
+
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.error import OperationError
-from rpython.rlib.rarithmetic import intmask, r_ulonglong, r_uint
+from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
+from pypy.interpreter.typedef import (
+    GetSetProperty, TypeDef, generic_new_descr, interp_attrproperty,
+    interp_attrproperty_w)
+from pypy.module._codecs import interp_codecs
+from pypy.module._io.interp_iobase import W_IOBase, convert_size
+from rpython.rlib.rarithmetic import intmask, r_uint, r_ulonglong
 from rpython.rlib.rbigint import rbigint
 from rpython.rlib.rstring import UnicodeBuilder
-from pypy.module._codecs import interp_codecs
-from pypy.module._io.interp_iobase import convert_size
-import sys
 
 
 STATE_ZERO, STATE_OK, STATE_DETACHED = range(3)
@@ -270,6 +270,27 @@
     errors = GetSetProperty(W_TextIOBase.errors_get_w),
 )
 
+
+def _determine_encoding(space, encoding):
+    if encoding is not None:
+        return space.wrap(encoding)
+
+    try:
+        w_locale = space.call_method(space.builtin, '__import__',
+                                     space.wrap('locale'))
+        w_encoding = space.call_method(w_locale, 'getpreferredencoding')
+    except OperationError as e:
+        # getpreferredencoding() may also raise ImportError
+        if not e.match(space, space.w_ImportError):
+            raise
+        return space.wrap('ascii')
+    else:
+        if space.isinstance_w(w_encoding, space.w_str):
+            return w_encoding
+
+    raise OperationError(space.w_IOError, space.wrap(
+            "could not determine default encoding"))
+
 class PositionCookie(object):
     def __init__(self, bigint):
         self.start_pos = bigint.ulonglongmask()
@@ -337,32 +358,8 @@
     def descr_init(self, space, w_buffer, encoding=None,
                    w_errors=None, w_newline=None, line_buffering=0):
         self.state = STATE_ZERO
-
         self.w_buffer = w_buffer
-
-        # Set encoding
-        self.w_encoding = None
-        if encoding is None:
-            try:
-                w_locale = space.call_method(space.builtin, '__import__',
-                                             space.wrap("locale"))
-                self.w_encoding = space.call_method(w_locale,
-                                                    "getpreferredencoding")
-            except OperationError, e:
-                # getpreferredencoding() may also raise ImportError
-                if not e.match(space, space.w_ImportError):
-                    raise
-                self.w_encoding = space.wrap("ascii")
-            else:
-                if not space.isinstance_w(self.w_encoding, space.w_str):
-                    self.w_encoding = None
-        if self.w_encoding:
-            pass
-        elif encoding is not None:
-            self.w_encoding = space.wrap(encoding)
-        else:
-            raise OperationError(space.w_IOError, space.wrap(
-                "could not determine default encoding"))
+        self.w_encoding = _determine_encoding(space, encoding)
 
         if space.is_none(w_errors):
             w_errors = space.wrap("strict")


More information about the pypy-commit mailing list