[pypy-commit] pypy default: Port some tests from CPython.

arigo noreply at buildbot.pypy.org
Mon Sep 24 18:10:23 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r57499:95d15eb62e18
Date: 2012-09-24 15:46 +0200
http://bitbucket.org/pypy/pypy/changeset/95d15eb62e18/

Log:	Port some tests from CPython.

diff --git a/lib-python/conftest.py b/lib-python/conftest.py
--- a/lib-python/conftest.py
+++ b/lib-python/conftest.py
@@ -183,7 +183,7 @@
     RegrTest('test_cpickle.py', core=True),
     RegrTest('test_cprofile.py'), 
     RegrTest('test_crypt.py', usemodules='crypt', skip=skip_win32),
-    RegrTest('test_csv.py'),
+    RegrTest('test_csv.py', usemodules='_csv'),
 
     RegrTest('test_curses.py', skip="unsupported extension module"),
     RegrTest('test_datetime.py'),
diff --git a/pypy/module/_csv/interp_reader.py b/pypy/module/_csv/interp_reader.py
--- a/pypy/module/_csv/interp_reader.py
+++ b/pypy/module/_csv/interp_reader.py
@@ -13,15 +13,6 @@
  EAT_CRNL) = range(8)
 
 
-def error(space, msg):
-    w_module = space.getbuiltinmodule('_csv')
-    w_error = space.getattr(w_module, space.wrap('Error'))
-    raise OperationError(w_error, space.wrap(msg))
-
-def new_field_builder():
-    return StringBuilder(64)
-
-
 class W_Reader(Wrappable):
 
     def __init__(self, space, dialect, w_iter):
@@ -33,6 +24,13 @@
     def iter_w(self):
         return self.space.wrap(self)
 
+    def error(self, msg):
+        space = self.space
+        msg = 'line %d: %s' % (self.line_num, msg)
+        w_module = space.getbuiltinmodule('_csv')
+        w_error = space.getattr(w_module, space.wrap('Error'))
+        raise OperationError(w_error, space.wrap(msg))
+
     def save_field(self, field_builder):
         field = field_builder.build()
         if self.numeric_field:
@@ -63,13 +61,13 @@
             except OperationError, e:
                 if e.match(space, space.w_StopIteration):
                     if field_builder is not None:
-                        raise error("newline inside string")
+                        raise self.error("newline inside string")
                 raise
             self.line_num += 1
             line = space.str_w(w_line)
             for c in line:
                 if c == '\0':
-                    raise error("line contains NULL byte")
+                    raise self.error("line contains NULL byte")
 
                 if state == START_RECORD:
                     if c == '\n' or c == '\r':
@@ -80,7 +78,7 @@
                     # fall-through to the next case
 
                 if state == START_FIELD:
-                    field_builder = new_field_builder()
+                    field_builder = StringBuilder(64)
                     # expecting field
                     if c == '\n' or c == '\r':
                         # save empty field
@@ -168,20 +166,19 @@
                         state = IN_FIELD
                     else:
                         # illegal
-                        raise error("'%s' expected after '%s'" %
-                                    dialect.delimiter,
-                                    dialect.quotechar)
+                        raise self.error("'%s' expected after '%s'" % (
+                            dialect.delimiter, dialect.quotechar))
 
                 elif state == EAT_CRNL:
                     if not (c == '\n' or c == '\r'):
-                        raise error("new-line character seen in unquoted "
-                                    "field - do you need to open the file "
-                                    "in universal-newline mode?")
+                        raise self.error("new-line character seen in unquoted "
+                                        "field - do you need to open the file "
+                                        "in universal-newline mode?")
 
             if (state == START_FIELD or
                   state == IN_FIELD or
                   state == QUOTE_IN_QUOTED_FIELD):
-                self.save_field()
+                self.save_field(field_builder)
                 break
             elif state == ESCAPED_CHAR:
                 field_builder.append('\n')
diff --git a/pypy/module/_csv/test/test_reader.py b/pypy/module/_csv/test/test_reader.py
--- a/pypy/module/_csv/test/test_reader.py
+++ b/pypy/module/_csv/test/test_reader.py
@@ -5,8 +5,29 @@
     def setup_class(cls):
         cls.space = gettestobjspace(usemodules=['_csv'])
 
+        w__read_test = cls.space.appexec([], r"""():
+            import _csv
+            def _read_test(input, expect, **kwargs):
+                reader = _csv.reader(input, **kwargs)
+                if expect == 'Error':
+                    raises(_csv.Error, list, reader)
+                    return
+                result = list(reader)
+                assert result == expect, 'result: %r\nexpect: %r' % (
+                    result, expect)
+            return _read_test
+        """)
+        if type(w__read_test) is type(lambda:0):
+            w__read_test = staticmethod(w__read_test)
+        cls.w__read_test = w__read_test
+
     def test_simple_reader(self):
-        import _csv
-        r = _csv.reader(['foo:bar\n'], delimiter=':')
-        lst = list(r)
-        assert lst == [['foo', 'bar']]
+        self._read_test(['foo:bar\n'], [['foo', 'bar']], delimiter=':')
+
+    def test_read_oddinputs(self):
+        self._read_test([], [])
+        self._read_test([''], [[]])
+        self._read_test(['"ab"c'], 'Error', strict = 1)
+        # cannot handle null bytes for the moment
+        self._read_test(['ab\0c'], 'Error', strict = 1)
+        self._read_test(['"ab"c'], [['abc']], doublequote = 0)


More information about the pypy-commit mailing list