Commits

Benjamin Peterson committed 836e7a0

merge from trunk

  • Participants
  • Parent commits 649967f
  • Branches fast-forward

Comments (0)

Files changed (549)

File lib-python/conftest.py

                                       regrtestdir, modregrtestdir, testresultdir
 
 pytest_plugins = "resultlog",
-rsyncdirs = ['.', '../pypy']
-    
+rsyncdirs = ['.', '../pypy/']
+
 # 
 # Interfacing/Integrating with py.test's collection process 
 #

File lib-python/modified-2.5.2/distutils/cmd.py

     # return a list of 'site' dirs for easy_install
     from pkg_resources import normalize_path
     sitedirs = filter(None,os.environ.get('PYTHONPATH','').split(os.pathsep))
-    sitedirs.append(os.path.join(sys.pypy_prefix, 'site-packages'))
+    pypylib = 'pypy%d.%d' % sys.pypy_version_info[:2]
+    sitedirs.append(os.path.join(sys.prefix, 'lib', pypylib, 'site-packages'))
     sitedirs = map(normalize_path, sitedirs)
     return sitedirs
 

File lib-python/modified-2.5.2/distutils/command/build_ext.py

         # for Release and Debug builds.
         # also Python's library directory must be appended to library_dirs
         if os.name == 'nt':
-            self.library_dirs.append(os.path.join(sys.pypy_prefix, 'pypy', '_interfaces'))
+            self.library_dirs.append(os.path.join(sys.prefix, 'pypy', '_interfaces'))
             if self.debug:
                 self.build_temp = os.path.join(self.build_temp, "Debug")
             else:

File lib-python/modified-2.5.2/distutils/command/install.py

         'data'   : '$base',
         },
     'pypy': {
-        'purelib': '$base/site-packages',
-        'platlib': '$base/site-packages',
+        'purelib': '$base/lib/pypy$pypy_version_short/site-packages',
+        'platlib': '$base/lib/pypy$pypy_version_short/site-packages',
         'headers': '$base/include',
         'scripts': '$base/bin',
         'data'   : '$base',
                             'dist_fullname': self.distribution.get_fullname(),
                             'py_version': py_version,
                             'py_version_short': py_version[0:3],
+                            'pypy_version_short': '%d.%d' % sys.pypy_version_info[:2],
                             'sys_prefix': prefix,
                             'prefix': prefix,
                             'sys_exec_prefix': exec_prefix,
                 if self.exec_prefix is not None:
                     raise DistutilsOptionError, \
                           "must not supply exec-prefix without prefix"
-
-                if hasattr(sys, 'pypy_prefix'):
-                    self.prefix = os.path.normpath(sys.pypy_prefix)
-                    self.exec_prefix = self.prefix
-                else:
-                    self.prefix = os.path.normpath(sys.prefix)
-                    self.exec_prefix = os.path.normpath(sys.exec_prefix)
+                
+                self.prefix = os.path.normpath(sys.prefix)
+                self.exec_prefix = os.path.normpath(sys.exec_prefix)
 
             else:
                 if self.exec_prefix is None:
             self.select_scheme("unix_home")
         else:
             if self.prefix is None:
-                if hasattr(sys, 'pypy_prefix'):
-                    self.prefix = os.path.normpath(sys.pypy_prefix)
-                else:
-                    self.prefix = os.path.normpath(sys.prefix)
+                self.prefix = os.path.normpath(sys.prefix)
 
             self.install_base = self.install_platbase = self.prefix
             try:
 
     def select_scheme (self, name):
         # it's the caller's problem if they supply a bad name!
-        if hasattr(sys, 'pypy_prefix'):
+        if hasattr(sys, 'pypy_version_info'):
             name = 'pypy'
         scheme = INSTALL_SCHEMES[name]
         for key in SCHEME_KEYS:

File lib-python/modified-2.5.2/distutils/sysconfig.py

 
 if '__pypy__' in sys.builtin_module_names:
     from distutils.sysconfig_pypy import *
+    from distutils.sysconfig_pypy import _config_vars # needed by setuptools
 else:
     from distutils.sysconfig_cpython import *
+    from distutils.sysconfig_pypy import _config_vars # needed by setuptools

File lib-python/modified-2.5.2/distutils/sysconfig_pypy.py

 from distutils.errors import DistutilsPlatformError
 
 
-PYPY_PREFIX = os.path.normpath(sys.pypy_prefix)
+PREFIX = os.path.normpath(sys.prefix)
 python_build = False
 
 
     if os.path.exists(cand):
         return cand
     if plat_specific:
-        return j(sys.pypy_prefix, "pypy", "_interfaces")
-    return j(sys.pypy_prefix, 'pypy', 'module', 'cpyext', 'include')
+        return j(sys.prefix, "pypy", "_interfaces")
+    return j(sys.prefix, 'pypy', 'module', 'cpyext', 'include')
 
 def get_python_version():
     """Return a string containing the major and minor Python version,
         raise DistutilsPlatformError(
             "calls to get_python_lib(standard_lib=1) cannot succeed")
     if prefix is None:
-        prefix = PYPY_PREFIX
-    return os.path.join(prefix, "site-packages")
+        prefix = PREFIX
+    pypylib = 'pypy%d.%d' % sys.pypy_version_info[:2]
+    return os.path.join(prefix, 'lib', pypylib, 'site-packages')
 
 
 _config_vars = None

File lib-python/modified-2.5.2/site.py

 
 def addsitepackages(known_paths):
     """Add site-packages to sys.path, in a PyPy-specific way."""
-    if hasattr(sys, 'pypy_prefix'):
-        sitedir = os.path.join(sys.pypy_prefix, "site-packages")
+    if hasattr(sys, 'pypy_version_info'):
+        pypylib = 'pypy%d.%d' % sys.pypy_version_info[:2]
+        sitedir = os.path.join(sys.prefix, 'lib', pypylib, 'site-packages')
         if os.path.isdir(sitedir):
             addsitedir(sitedir, known_paths)
     return None

File lib-python/modified-2.5.2/test/regrtest.py

         random.shuffle(tests)
     if trace:
         import trace
-        if hasattr(sys, 'prefix'):
-            ignoredirs = [sys.prefix, sys.exec_prefix]
-        else:
-            ignoredirs = [sys.pypy_prefix]     # PyPy only
-        tracer = trace.Trace(ignoredirs=ignoredirs, trace=False, count=True)
+        tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
+                             trace=False, count=True)
     test_support.verbose = verbose      # Tell tests to be moderately quiet
     test_support.use_resources = use_resources
     save_modules = sys.modules.keys()

File lib-python/modified-2.5.2/test/test_doctest.py

 
 import trace, sys, re, StringIO
 def test_coverage(coverdir):
-    if hasattr(sys, 'prefix'):
-        ignoredirs = [sys.prefix, sys.exec_prefix]
-    else:
-        ignoredirs = [sys.pypy_prefix]     # PyPy only
-    tracer = trace.Trace(ignoredirs=ignoredirs, trace=0, count=1)
+    tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
+                         trace=0, count=1)
     tracer.run('reload(doctest); test_main()')
     r = tracer.results()
     print 'Writing coverage results...'

File lib-python/modified-2.5.2/test/test_sys.py

         if test.test_support.have_unicode:
             self.assert_(isinstance(sys.maxunicode, int))
         self.assert_(isinstance(sys.platform, basestring))
-        if hasattr(sys, 'prefix'):
-            self.assert_(isinstance(sys.prefix, basestring))
-            self.assert_(isinstance(sys.exec_prefix, basestring))
-        else:
-            self.assert_(isinstance(sys.pypy_prefix, basestring))  # PyPy only
+        self.assert_(isinstance(sys.prefix, basestring))
+        self.assert_(isinstance(sys.exec_prefix, basestring))
         self.assert_(isinstance(sys.version, basestring))
         vi = sys.version_info
         self.assert_(isinstance(vi, tuple))

File lib_pypy/PyQt4.py

+from _rpyc_support import proxy_sub_module, remote_eval
+
+
+for name in ("QtCore", "QtGui", "QtWebKit"):
+    proxy_sub_module(globals(), name)
+
+s = "__import__('PyQt4').QtGui.QDialogButtonBox."
+QtGui.QDialogButtonBox.Cancel = remote_eval("%sCancel | %sCancel" % (s, s))
+QtGui.QDialogButtonBox.Ok = remote_eval("%sOk | %sOk" % (s, s))

File lib_pypy/__init__.py

+# This __init__.py shows up in PyPy's app-level standard library.
+# Let's try to prevent that confusion...
+if __name__ != 'pypy.lib':
+    raise ImportError, '__init__'

File lib_pypy/_collections.py

+"""High performance data structures
+"""
+#
+# Copied and completed from the sandbox of CPython
+#   (nondist/sandbox/collections/pydeque.py rev 1.1, Raymond Hettinger)
+#
+
+import operator
+try:
+    from thread import get_ident as _thread_ident
+except ImportError:
+    def _thread_ident():
+        return -1
+
+
+n = 30
+LFTLNK = n
+RGTLNK = n+1
+BLOCKSIZ = n+2
+
+class deque(object):
+
+    def __new__(cls, iterable=(), *args, **kw):
+        self = super(deque, cls).__new__(cls, *args, **kw)
+        self.clear()
+        return self
+
+    def __init__(self, iterable=()):
+        add = self.append
+        for elem in iterable:
+            add(elem)
+
+    def clear(self):
+        self.right = self.left = [None] * BLOCKSIZ
+        self.rightndx = n//2   # points to last written element
+        self.leftndx = n//2+1
+        self.length = 0
+        self.state = 0
+
+    def append(self, x):
+        self.state += 1
+        self.rightndx += 1
+        if self.rightndx == n:
+            newblock = [None] * BLOCKSIZ
+            self.right[RGTLNK] = newblock
+            newblock[LFTLNK] = self.right
+            self.right = newblock
+            self.rightndx = 0
+        self.length += 1
+        self.right[self.rightndx] = x
+
+    def appendleft(self, x):
+        self.state += 1
+        self.leftndx -= 1
+        if self.leftndx == -1:
+            newblock = [None] * BLOCKSIZ
+            self.left[LFTLNK] = newblock
+            newblock[RGTLNK] = self.left
+            self.left = newblock
+            self.leftndx = n-1
+        self.length += 1
+        self.left[self.leftndx] = x
+
+    def extend(self, iterable):
+        for elem in iterable:
+            self.append(elem)
+
+    def extendleft(self, iterable):
+        for elem in iterable:
+            self.appendleft(elem)
+
+    def pop(self):
+        if self.left is self.right and self.leftndx > self.rightndx:
+            raise IndexError, "pop from an empty deque"
+        x = self.right[self.rightndx]
+        self.right[self.rightndx] = None
+        self.length -= 1
+        self.rightndx -= 1
+        self.state += 1
+        if self.rightndx == -1:
+            prevblock = self.right[LFTLNK]
+            if prevblock is None:
+                # the deque has become empty; recenter instead of freeing block
+                self.rightndx = n//2
+                self.leftndx = n//2+1
+            else:
+                prevblock[RGTLNK] = None
+                self.right[LFTLNK] = None
+                self.right = prevblock
+                self.rightndx = n-1
+        return x
+
+    def popleft(self):
+        if self.left is self.right and self.leftndx > self.rightndx:
+            raise IndexError, "pop from an empty deque"
+        x = self.left[self.leftndx]
+        self.left[self.leftndx] = None
+        self.length -= 1
+        self.leftndx += 1
+        self.state += 1
+        if self.leftndx == n:
+            prevblock = self.left[RGTLNK]
+            if prevblock is None:
+                # the deque has become empty; recenter instead of freeing block
+                self.rightndx = n//2
+                self.leftndx = n//2+1
+            else:
+                prevblock[LFTLNK] = None
+                self.left[RGTLNK] = None
+                self.left = prevblock
+                self.leftndx = 0
+        return x
+
+    def remove(self, value):
+        # Need to be defensive for mutating comparisons
+        for i in range(len(self)):
+            if self[i] == value:
+                del self[i]
+                return
+        raise ValueError("deque.remove(x): x not in deque")
+
+    def rotate(self, n=1):
+        length = len(self)
+        if length == 0:
+            return
+        halflen = (length+1) >> 1
+        if n > halflen or n < -halflen:
+            n %= length
+            if n > halflen:
+                n -= length
+            elif n < -halflen:
+                n += length
+        while n > 0:
+            self.appendleft(self.pop())
+            n -= 1
+        while n < 0:
+            self.append(self.popleft())
+            n += 1
+
+    def __repr__(self):
+        threadlocalattr = '__repr' + str(_thread_ident())
+        if threadlocalattr in self.__dict__:
+            return 'deque([...])'
+        else:
+            self.__dict__[threadlocalattr] = True
+            try:
+                return 'deque(%r)' % (list(self),)
+            finally:
+                del self.__dict__[threadlocalattr]
+
+    def __iter__(self):
+        return deque_iterator(self, self._iter_impl)
+
+    def _iter_impl(self, original_state, giveup):
+        if self.state != original_state:
+            giveup()
+        block = self.left
+        while block:
+            l, r = 0, n
+            if block is self.left:
+                l = self.leftndx
+            if block is self.right:
+                r = self.rightndx + 1
+            for elem in block[l:r]:
+                yield elem
+                if self.state != original_state:
+                    giveup()
+            block = block[RGTLNK]
+
+    def __reversed__(self):
+        return deque_iterator(self, self._reversed_impl)
+
+    def _reversed_impl(self, original_state, giveup):
+        if self.state != original_state:
+            giveup()
+        block = self.right
+        while block:
+            l, r = 0, n
+            if block is self.left:
+                l = self.leftndx
+            if block is self.right:
+                r = self.rightndx + 1
+            for elem in reversed(block[l:r]):
+                yield elem
+                if self.state != original_state:
+                    giveup()
+            block = block[LFTLNK]
+
+    def __len__(self):
+        #sum = 0
+        #block = self.left
+        #while block:
+        #    sum += n
+        #    block = block[RGTLNK]
+        #return sum + self.rightndx - self.leftndx + 1 - n
+        return self.length
+
+    def __getref(self, index):
+        if index >= 0:
+            block = self.left
+            while block:
+                l, r = 0, n
+                if block is self.left:
+                    l = self.leftndx
+                if block is self.right:
+                    r = self.rightndx + 1
+                span = r-l
+                if index < span:
+                    return block, l+index
+                index -= span
+                block = block[RGTLNK]
+        else:
+            block = self.right
+            while block:
+                l, r = 0, n
+                if block is self.left:
+                    l = self.leftndx
+                if block is self.right:
+                    r = self.rightndx + 1
+                negative_span = l-r
+                if index >= negative_span:
+                    return block, r+index
+                index -= negative_span
+                block = block[LFTLNK]
+        raise IndexError("deque index out of range")
+
+    def __getitem__(self, index):
+        block, index = self.__getref(index)
+        return block[index]
+
+    def __setitem__(self, index, value):
+        block, index = self.__getref(index)
+        block[index] = value
+
+    def __delitem__(self, index):
+        length = len(self)
+        if index >= 0:
+            if index >= length:
+                raise IndexError("deque index out of range")
+            self.rotate(-index)
+            self.popleft()
+            self.rotate(index)
+        else:
+            index = ~index
+            if index >= length:
+                raise IndexError("deque index out of range")
+            self.rotate(index)
+            self.pop()
+            self.rotate(-index)
+
+    def __reduce_ex__(self, proto):
+        return type(self), (), self.__dict__, iter(self), None
+
+    def __hash__(self):
+        raise TypeError, "deque objects are unhashable"
+
+    def __copy__(self):
+        return self.__class__(self)
+
+    # XXX make comparison more efficient
+    def __eq__(self, other):
+        if isinstance(other, deque):
+            return list(self) == list(other)
+        else:
+            return NotImplemented
+
+    def __ne__(self, other):
+        if isinstance(other, deque):
+            return list(self) != list(other)
+        else:
+            return NotImplemented
+
+    def __lt__(self, other):
+        if isinstance(other, deque):
+            return list(self) < list(other)
+        else:
+            return NotImplemented
+
+    def __le__(self, other):
+        if isinstance(other, deque):
+            return list(self) <= list(other)
+        else:
+            return NotImplemented
+
+    def __gt__(self, other):
+        if isinstance(other, deque):
+            return list(self) > list(other)
+        else:
+            return NotImplemented
+
+    def __ge__(self, other):
+        if isinstance(other, deque):
+            return list(self) >= list(other)
+        else:
+            return NotImplemented
+
+class deque_iterator(object):
+
+    def __init__(self, deq, itergen):
+        self.counter = len(deq)
+        def giveup():
+            self.counter = 0
+            raise RuntimeError, "deque mutated during iteration"
+        self._gen = itergen(deq.state, giveup)
+
+    def next(self):
+        res =  self._gen.next()
+        self.counter -= 1
+        return res
+
+    def __iter__(self):
+        return self
+
+class defaultdict(dict):
+    
+    def __init__(self, *args, **kwds):
+        self.default_factory = None
+        if 'default_factory' in kwds:
+            self.default_factory = kwds.pop('default_factory')
+        elif len(args) > 0 and callable(args[0]):
+            self.default_factory = args[0]
+            args = args[1:]
+        super(defaultdict, self).__init__(*args, **kwds)
+ 
+    def __missing__(self, key):
+        # from defaultdict docs
+        if self.default_factory is None: 
+            raise KeyError(key)
+        self[key] = value = self.default_factory()
+        return value
+
+    def __repr__(self, recurse=set()):
+        if id(self) in recurse:
+            return "defaultdict(...)"
+        try:
+            recurse.add(id(self))
+            return "defaultdict(%s, %s)" % (repr(self.default_factory), super(defaultdict, self).__repr__())
+        finally:
+            recurse.remove(id(self))
+
+    def copy(self):
+        return type(self)(self, default_factory=self.default_factory)
+    
+    def __copy__(self):
+        return self.copy()
+
+    def __reduce__(self):
+        """
+        __reduce__ must return a 5-tuple as follows:
+
+           - factory function
+           - tuple of args for the factory function
+           - additional state (here None)
+           - sequence iterator (here None)
+           - dictionary iterator (yielding successive (key, value) pairs
+
+           This API is used by pickle.py and copy.py.
+        """
+        return (type(self), (self.default_factory,), None, None, self.iteritems())
+

File lib_pypy/_csv.py

+"""CSV parsing and writing.
+
+This module provides classes that assist in the reading and writing
+of Comma Separated Value (CSV) files, and implements the interface
+described by PEP 305.  Although many CSV files are simple to parse,
+the format is not formally defined by a stable specification and
+is subtle enough that parsing lines of a CSV file with something
+like line.split(\",\") is bound to fail.  The module supports three
+basic APIs: reading, writing, and registration of dialects.
+
+
+DIALECT REGISTRATION:
+
+Readers and writers support a dialect argument, which is a convenient
+handle on a group of settings.  When the dialect argument is a string,
+it identifies one of the dialects previously registered with the module.
+If it is a class or instance, the attributes of the argument are used as
+the settings for the reader or writer:
+
+    class excel:
+        delimiter = ','
+        quotechar = '\"'
+        escapechar = None
+        doublequote = True
+        skipinitialspace = False
+        lineterminator = '\\r\\n'
+        quoting = QUOTE_MINIMAL
+
+SETTINGS:
+
+    * quotechar - specifies a one-character string to use as the 
+        quoting character.  It defaults to '\"'.
+    * delimiter - specifies a one-character string to use as the 
+        field separator.  It defaults to ','.
+    * skipinitialspace - specifies how to interpret whitespace which
+        immediately follows a delimiter.  It defaults to False, which
+        means that whitespace immediately following a delimiter is part
+        of the following field.
+    * lineterminator -  specifies the character sequence which should 
+        terminate rows.
+    * quoting - controls when quotes should be generated by the writer.
+        It can take on any of the following module constants:
+
+        csv.QUOTE_MINIMAL means only when required, for example, when a
+            field contains either the quotechar or the delimiter
+        csv.QUOTE_ALL means that quotes are always placed around fields.
+        csv.QUOTE_NONNUMERIC means that quotes are always placed around
+            fields which do not parse as integers or floating point
+            numbers.
+        csv.QUOTE_NONE means that quotes are never placed around fields.
+    * escapechar - specifies a one-character string used to escape 
+        the delimiter when quoting is set to QUOTE_NONE.
+    * doublequote - controls the handling of quotes inside fields.  When
+        True, two consecutive quotes are interpreted as one during read,
+        and when writing, each quote character embedded in the data is
+        written as two quotes.
+"""
+
+__version__ = "1.0"
+
+QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE = range(4)
+_dialects = {}
+_field_limit = 128 * 1024 # max parsed field size
+
+class Error(Exception):
+    pass
+
+class Dialect(object):
+    """CSV dialect
+
+    The Dialect type records CSV parsing and generation options."""
+
+    __slots__ = ["delimiter", "doublequote", "escapechar", "lineterminator",
+                 "quotechar", "quoting", "skipinitialspace", "strict"]
+
+    def __new__(cls, dialect, **kwargs):
+
+        for name in kwargs:
+            if name not in Dialect.__slots__:
+                raise TypeError("unexpected keyword argument '%s'" %
+                                (name,))
+
+        if dialect is not None:
+            if isinstance(dialect, basestring):
+                dialect = get_dialect(dialect)
+        
+            # Can we reuse this instance?
+            if (isinstance(dialect, Dialect)
+                and all(value is None for value in kwargs.itervalues())):
+                return dialect
+
+        self = object.__new__(cls)
+
+
+        def set_char(x):
+            if x is None:
+                return None
+            if isinstance(x, str) and len(x) <= 1:
+                return x
+            raise TypeError("%r must be a 1-character string" % (name,))
+        def set_str(x):
+            if isinstance(x, str):
+                return x
+            raise TypeError("%r must be a string" % (name,))
+        def set_quoting(x):
+            if x in range(4):
+                return x
+            raise TypeError("bad 'quoting' value")
+        
+        attributes = {"delimiter": (',', set_char),
+                      "doublequote": (True, bool),
+                      "escapechar": (None, set_char),
+                      "lineterminator": ("\r\n", set_str),
+                      "quotechar": ('"', set_char),
+                      "quoting": (QUOTE_MINIMAL, set_quoting),
+                      "skipinitialspace": (False, bool),
+                      "strict": (False, bool),
+                      }
+
+        # Copy attributes
+        notset = object()
+        for name in Dialect.__slots__:
+            value = notset
+            if name in kwargs:
+                value = kwargs[name]
+            elif dialect is not None:
+                value = getattr(dialect, name, notset)
+
+            # mapping by name: (default, converter)
+            if value is notset:
+                value = attributes[name][0]
+                if name == 'quoting' and not self.quotechar:
+                    value = QUOTE_NONE
+            else:
+                converter = attributes[name][1]
+                if converter:
+                    value = converter(value)
+                
+            setattr(self, name, value)
+
+        if not self.delimiter:
+            raise TypeError("delimiter must be set")
+
+        if self.quoting != QUOTE_NONE and not self.quotechar:
+            raise TypeError("quotechar must be set if quoting enabled")
+
+        if not self.lineterminator:
+            raise TypeError("lineterminator must be set")
+
+        return self
+        
+
+def _call_dialect(dialect_inst, kwargs):
+    return Dialect(dialect_inst, **kwargs)
+
+def register_dialect(name, dialect=None, **kwargs):
+    """Create a mapping from a string name to a dialect class.
+    dialect = csv.register_dialect(name, dialect)"""
+    if not isinstance(name, basestring):
+        raise TypeError("dialect name must be a string or unicode")
+
+    dialect = _call_dialect(dialect, kwargs)
+    _dialects[name] = dialect
+
+def unregister_dialect(name):
+    """Delete the name/dialect mapping associated with a string name.\n
+    csv.unregister_dialect(name)"""
+    try:
+        del _dialects[name]
+    except KeyError:
+        raise Error("unknown dialect")
+
+def get_dialect(name):
+    """Return the dialect instance associated with name.
+    dialect = csv.get_dialect(name)"""
+    try:
+        return _dialects[name]
+    except KeyError:
+        raise Error("unknown dialect")
+
+def list_dialects():
+    """Return a list of all know dialect names
+    names = csv.list_dialects()"""
+    return list(_dialects)
+
+class Reader:
+
+    """CSV reader
+
+    Reader objects are responsible for reading and parsing tabular data
+    in CSV format."""
+    
+
+    (START_RECORD, START_FIELD, ESCAPED_CHAR, IN_FIELD,
+     IN_QUOTED_FIELD, ESCAPE_IN_QUOTED_FIELD, QUOTE_IN_QUOTED_FIELD,
+     EAT_CRNL) = range(8)
+    
+    def __init__(self, iterator, dialect=None, **kwargs):
+        self.dialect = _call_dialect(dialect, kwargs)
+        self.input_iter = iter(iterator)
+        self.line_num = 0
+
+        self._parse_reset()
+
+    def _parse_reset(self):
+        self.field = []
+        self.fields = []
+        self.state = self.START_RECORD
+        self.numeric_field = False
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        self._parse_reset()
+        while True:
+            try:
+                line = self.input_iter.next()
+            except StopIteration:
+                # End of input OR exception
+                if len(self.field) > 0:
+                    raise Error("newline inside string")
+                raise
+
+            self.line_num += 1
+
+            for c in line:
+                if c == '\0':
+                    raise Error("line contains NULL byte")
+                self._parse_process_char(c)
+            self._parse_process_char('\0')
+
+            if self.state == self.START_RECORD:
+                break
+
+        fields = self.fields
+        self.fields = []
+        return fields
+            
+    def _parse_process_char(self, c):
+        if self.state == self.IN_FIELD:
+            # in unquoted field
+            if c in ('\n', '\r', '\0'):
+                # end of line - return [fields]
+                self._parse_save_field()
+                if c == '\0':
+                    self.state = self.START_RECORD
+                else:
+                    self.state = self.EAT_CRNL
+            elif c == self.dialect.escapechar:
+                # possible escaped character
+                self.state = self.ESCAPED_CHAR
+            elif c == self.dialect.delimiter:
+                # save field - wait for new field
+                self._parse_save_field()
+                self.state = self.START_FIELD
+            else:
+                # normal character - save in field
+                self._parse_add_char(c)
+                
+        elif self.state == self.START_RECORD:
+            if c == '\0':
+                # empty line - return []
+                pass
+            elif c in ('\n', '\r'):
+                self.state = self.EAT_CRNL
+            else:
+                self.state = self.START_FIELD
+                # restart process
+                self._parse_process_char(c)
+
+        elif self.state == self.START_FIELD:
+            if c in ('\n', '\r', '\0'):
+                # save empty field - return [fields]
+                self._parse_save_field()
+                if c == '\0':
+                    self.state = self.START_RECORD
+                else:
+                    self.state = self.EAT_CRNL
+            elif (c == self.dialect.quotechar
+                  and self.dialect.quoting != QUOTE_NONE):
+                # start quoted field
+                self.state = self.IN_QUOTED_FIELD
+            elif c == self.dialect.escapechar:
+                # possible escaped character
+                self.state = self.ESCAPED_CHAR
+            elif c == ' ' and self.dialect.skipinitialspace:
+                # ignore space at start of field
+                pass
+            elif c == self.dialect.delimiter:
+                # save empty field
+                self._parse_save_field()
+            else:
+                # begin new unquoted field
+                if self.dialect.quoting == QUOTE_NONNUMERIC:
+                    self.numeric_field = True
+                self._parse_add_char(c)
+                self.state = self.IN_FIELD
+        
+        elif self.state == self.ESCAPED_CHAR:
+            if c == '\0':
+                c = '\n'
+            self._parse_add_char(c)
+            self.state = self.IN_FIELD
+        
+        elif self.state == self.IN_QUOTED_FIELD:
+            if c == '\0':
+                pass
+            elif c == self.dialect.escapechar:
+                # possible escape character
+                self.state = self.ESCAPE_IN_QUOTED_FIELD
+            elif (c == self.dialect.quotechar
+                  and self.dialect.quoting != QUOTE_NONE):
+                if self.dialect.doublequote:
+                    # doublequote; " represented by ""
+                    self.state = self.QUOTE_IN_QUOTED_FIELD
+                else:
+                    #end of quote part of field
+                    self.state = self.IN_FIELD
+            else:
+                # normal character - save in field
+                self._parse_add_char(c)
+                
+        elif self.state == self.ESCAPE_IN_QUOTED_FIELD:
+            if c == '\0':
+                c = '\n'
+            self._parse_add_char(c)
+            self.state = self.IN_QUOTED_FIELD
+                
+        elif self.state == self.QUOTE_IN_QUOTED_FIELD:
+            # doublequote - seen a quote in a quoted field
+            if (c == self.dialect.quotechar
+                and self.dialect.quoting != QUOTE_NONE):
+                # save "" as "
+                self._parse_add_char(c)
+                self.state = self.IN_QUOTED_FIELD
+            elif c == self.dialect.delimiter:
+                # save field - wait for new field
+                self._parse_save_field()
+                self.state = self.START_FIELD
+            elif c in ('\r', '\n', '\0'):
+                # end of line - return [fields]
+                self._parse_save_field()
+                if c == '\0':
+                    self.state = self.START_RECORD
+                else:
+                    self.state = self.EAT_CRNL
+            elif not self.dialect.strict:
+                self._parse_add_char(c)
+                self.state = self.IN_FIELD
+            else:
+                raise Error("'%c' expected after '%c'" %
+                            (self.dialect.delimiter, self.dialect.quotechar))
+
+        elif self.state == self.EAT_CRNL:
+            if c in ('\r', '\n'):
+                pass
+            elif c == '\0':
+                self.state = self.START_RECORD
+            else:
+                raise Error("new-line character seen in unquoted field - "
+                            "do you need to open the file "
+                            "in universal-newline mode?")
+
+        else:
+            raise RuntimeError("unknown state: %r" % (self.state,))
+
+    def _parse_save_field(self):
+        field, self.field = self.field, []
+        field = ''.join(field)
+        if self.numeric_field:
+            self.numeric_field = False
+            field = float(field)
+        self.fields.append(field)
+
+    def _parse_add_char(self, c):
+        if len(self.field) >= _field_limit:
+            raise Error("field larget than field limit (%d)" % (_field_limit))
+        self.field.append(c)
+        
+
+class Writer:
+    """CSV writer
+
+    Writer objects are responsible for generating tabular data
+    in CSV format from sequence input."""
+
+    def __init__(self, file, dialect=None, **kwargs):
+        if not (hasattr(file, 'write') and callable(file.write)):
+            raise TypeError("argument 1 must have a 'write' method")
+        self.writeline = file.write
+        self.dialect = _call_dialect(dialect, kwargs)
+
+    def _join_reset(self):
+        self.rec = []
+        self.num_fields = 0
+
+    def _join_append(self, field, quoted, quote_empty):
+        dialect = self.dialect
+        # If this is not the first field we need a field separator
+        if self.num_fields > 0:
+            self.rec.append(dialect.delimiter)
+
+        if dialect.quoting == QUOTE_NONE:
+            need_escape = tuple(dialect.lineterminator) + (
+                dialect.escapechar,  # escapechar always first
+                dialect.delimiter, dialect.quotechar)
+                
+        else:
+            for c in tuple(dialect.lineterminator) + (
+                dialect.delimiter, dialect.escapechar):
+                if c and c in field:
+                    quoted = True
+
+            need_escape = ()
+            if dialect.quotechar in field:
+                if dialect.doublequote:
+                    field = field.replace(dialect.quotechar,
+                                          dialect.quotechar * 2)
+                    quoted = True
+                else:
+                    need_escape = (dialect.quotechar,)
+
+
+        for c in need_escape:
+            if c and c in field:
+                if not dialect.escapechar:
+                    raise Error("need to escape, but no escapechar set")
+                field = field.replace(c, dialect.escapechar + c)
+
+        # If field is empty check if it needs to be quoted
+        if field == '' and quote_empty:
+            if dialect.quoting == QUOTE_NONE:
+                raise Error("single empty field record must be quoted")
+            quoted = 1
+
+        if quoted:
+            field = dialect.quotechar + field + dialect.quotechar
+
+        self.rec.append(field)
+        self.num_fields += 1
+
+
+
+    def writerow(self, row):
+        dialect = self.dialect
+        try:
+            rowlen = len(row)
+        except TypeError:
+            raise Error("sequence expected")
+
+        # join all fields in internal buffer
+        self._join_reset()
+        
+        for field in row:
+            quoted = False
+            if dialect.quoting == QUOTE_NONNUMERIC:
+                try:
+                    float(field)
+                except:
+                    quoted = True
+                # This changed since 2.5:
+                # quoted = not isinstance(field, (int, long, float))
+            elif dialect.quoting == QUOTE_ALL:
+                quoted = True
+
+            if field is None:
+                self._join_append("", quoted, rowlen == 1)
+            else:
+                self._join_append(str(field), quoted, rowlen == 1)
+
+        # add line terminator
+        self.rec.append(dialect.lineterminator)
+
+        self.writeline(''.join(self.rec))
+
+    def writerows(self, rows):
+        for row in rows:
+            self.writerow(row)
+
+def reader(*args, **kwargs):
+    """
+    csv_reader = reader(iterable [, dialect='excel']
+                       [optional keyword args])
+    for row in csv_reader:
+        process(row)
+
+    The "iterable" argument can be any object that returns a line
+    of input for each iteration, such as a file object or a list.  The
+    optional \"dialect\" parameter is discussed below.  The function
+    also accepts optional keyword arguments which override settings
+    provided by the dialect.
+
+    The returned object is an iterator.  Each iteration returns a row
+    of the CSV file (which can span multiple input lines)"""
+    
+    return Reader(*args, **kwargs)
+
+def writer(*args, **kwargs):
+    """
+    csv_writer = csv.writer(fileobj [, dialect='excel']
+                            [optional keyword args])
+    for row in sequence:
+        csv_writer.writerow(row)
+
+    [or]
+
+    csv_writer = csv.writer(fileobj [, dialect='excel']
+                            [optional keyword args])
+    csv_writer.writerows(rows)
+
+    The \"fileobj\" argument can be any object that supports the file API."""
+    return Writer(*args, **kwargs)
+
+
+undefined = object()
+def field_size_limit(limit=undefined):
+    """Sets an upper limit on parsed fields.
+    csv.field_size_limit([limit])
+
+    Returns old limit. If limit is not given, no new limit is set and
+    the old limit is returned"""
+
+    global _field_limit
+    old_limit = _field_limit
+    
+    if limit is not undefined:
+        if not isinstance(limit, (int, long)):
+            raise TypeError("int expected, got %s" %
+                            (limit.__class__.__name__,))
+        _field_limit = limit
+
+    return old_limit

File lib_pypy/_ctypes/__init__.py

+from _ctypes.dummy import resize
+from _ctypes.basics import _CData, sizeof, alignment, byref, addressof,\
+     ArgumentError, COMError
+from _ctypes.primitive import _SimpleCData
+from _ctypes.pointer import _Pointer, _cast_addr
+from _ctypes.function import CFuncPtr
+from _ctypes.dll import dlopen
+from _ctypes.structure import Structure
+from _ctypes.array import Array
+from _ctypes.builtin import _memmove_addr, _string_at_addr, _memset_addr,\
+     set_conversion_mode, _wstring_at_addr
+from _ctypes.union import Union
+
+import os as _os
+
+if _os.name in ("nt", "ce"):
+    from _rawffi import FormatError
+    from _rawffi import check_HRESULT as _check_HRESULT
+    CopyComPointer = None # XXX
+
+from _rawffi import FUNCFLAG_STDCALL, FUNCFLAG_CDECL, FUNCFLAG_PYTHONAPI
+
+__version__ = '1.0.3'
+#XXX platform dependant?
+RTLD_LOCAL = 0
+RTLD_GLOBAL = 256

File lib_pypy/_ctypes/array.py

+
+import _rawffi
+
+from _ctypes.basics import _CData, cdata_from_address, _CDataMeta, sizeof
+from _ctypes.basics import keepalive_key, store_reference, ensure_objects
+from _ctypes.basics import CArgObject
+from _ctypes.builtin import _string_at_addr, _wstring_at_addr
+
+def _create_unicode(buffer, maxlength):
+    res = []
+    for i in range(maxlength):
+        if buffer[i] == '\x00':
+            break
+        res.append(buffer[i])
+    return u''.join(res)
+
+class ArrayMeta(_CDataMeta):
+    def __new__(self, name, cls, typedict):
+        res = type.__new__(self, name, cls, typedict)
+        if '_type_' in typedict:
+            ffiarray = _rawffi.Array(typedict['_type_']._ffishape)
+            res._ffiarray = ffiarray
+            subletter = getattr(typedict['_type_'], '_type_', None)
+            if subletter == 'c':
+                def getvalue(self):
+                    return _rawffi.charp2string(self._buffer.buffer,
+                                                self._length_)
+                def setvalue(self, val):
+                    # we don't want to have buffers here
+                    if len(val) > self._length_:
+                        raise ValueError("%r too long" % (val,))
+                    for i in range(len(val)):
+                        self[i] = val[i]
+                    if len(val) < self._length_:
+                        self[len(val)] = '\x00'
+                res.value = property(getvalue, setvalue)
+
+                def getraw(self):
+                    return _rawffi.charp2rawstring(self._buffer.buffer,
+                                                   self._length_)
+
+                def setraw(self, buffer):
+                    for i in range(len(buffer)):
+                        self[i] = buffer[i]
+                res.raw = property(getraw, setraw)
+            elif subletter == 'u':
+                def getvalue(self):
+                    # rawffi support anyone?
+                    return _create_unicode(self._buffer, self._length_)
+
+                def setvalue(self, val):
+                    # we don't want to have buffers here
+                    if len(val) > self._length_:
+                        raise ValueError("%r too long" % (val,))
+                    for i in range(len(val)):
+                        self[i] = val[i]
+                    if len(val) < self._length_:
+                        self[len(val)] = '\x00'
+                res.value = property(getvalue, setvalue)
+                
+            if '_length_' in typedict:
+                res._ffishape = (ffiarray, typedict['_length_'])
+                res._fficompositesize = res._sizeofinstances()
+        else:
+            res._ffiarray = None
+        return res
+
+    from_address = cdata_from_address
+
+    def _sizeofinstances(self):
+        size, alignment = self._ffiarray.size_alignment(self._length_)
+        return size
+
+    def _alignmentofinstances(self):
+        return self._type_._alignmentofinstances()
+
+    def _CData_output(self, resarray, base=None, index=-1):
+        # this seems to be a string if we're array of char, surprise!
+        from ctypes import c_char, c_wchar, c_char_p, c_wchar_p
+        if self._type_ is c_char:
+            return _rawffi.charp2string(resarray.buffer, self._length_)
+        if self._type_ is c_wchar:
+            xxx
+        res = self.__new__(self)
+        ffiarray = self._ffiarray.fromaddress(resarray.buffer, self._length_)
+        res._buffer = ffiarray
+        res._base = base
+        res._index = index
+        return res
+
+    def _CData_retval(self, resbuffer):
+        raise NotImplementedError
+
+    def from_param(self, value):
+        # array accepts very strange parameters as part of structure
+        # or function argument...
+        from ctypes import c_char, c_wchar
+        if issubclass(self._type_, (c_char, c_wchar)):
+            if isinstance(value, basestring):
+                if len(value) > self._length_:
+                    raise ValueError("Invalid length")
+                value = self(*value)
+        else:
+            if isinstance(value, tuple):
+                if len(value) > self._length_:
+                    raise RuntimeError("Invalid length")
+                value = self(*value)
+        return _CDataMeta.from_param(self, value)
+
+def array_get_slice_params(self, index):
+    if index.step is not None:
+        raise TypeError("3 arg slices not supported (for no reason)")
+    start = index.start or 0
+    stop = index.stop or self._length_
+    return start, stop
+
+def array_slice_setitem(self, index, value):
+    start, stop = self._get_slice_params(index)
+    if stop - start != len(value):
+        raise ValueError("Can only assign slices of the same length")
+    for i in range(start, stop):
+        self[i] = value[i - start]
+
+def array_slice_getitem(self, index):
+    start, stop = self._get_slice_params(index)
+    l = [self[i] for i in range(start, stop)]
+    letter = getattr(self._type_, '_type_', None)
+    if letter == 'c':
+        return "".join(l)
+    if letter == 'u':
+        return u"".join(l)
+    return l
+
+class Array(_CData):
+    __metaclass__ = ArrayMeta
+    _ffiargshape = 'P'
+
+    def __init__(self, *args):
+        self._buffer = self._ffiarray(self._length_, autofree=True)
+        for i, arg in enumerate(args):
+            self[i] = arg
+
+    def _fix_index(self, index):
+        if index < 0:
+            index += self._length_
+        if 0 <= index < self._length_:
+            return index
+        else:
+            raise IndexError
+
+    _get_slice_params = array_get_slice_params
+    _slice_getitem = array_slice_getitem
+    _slice_setitem = array_slice_setitem
+
+    def _subarray(self, index):
+        """Return a _rawffi array of length 1 whose address is the same as
+        the index'th item of self."""
+        address = self._buffer.itemaddress(index)
+        return self._ffiarray.fromaddress(address, 1)
+
+    def __setitem__(self, index, value):
+        if isinstance(index, slice):
+            self._slice_setitem(index, value)
+            return
+        index = self._fix_index(index)
+        if ensure_objects(value) is not None:
+            store_reference(self, index, value._objects)
+        arg = self._type_._CData_value(value)
+        if self._type_._fficompositesize is None:
+            self._buffer[index] = arg
+            # something more sophisticated, cannot set field directly
+        else:
+            from ctypes import memmove
+            dest = self._buffer.itemaddress(index)
+            memmove(dest, arg, self._type_._fficompositesize)
+
+    def __getitem__(self, index):
+        if isinstance(index, slice):
+            return self._slice_getitem(index)
+        index = self._fix_index(index)
+        return self._type_._CData_output(self._subarray(index), self, index)
+
+    def __len__(self):
+        return self._length_
+
+    def _get_buffer_for_param(self):
+        return CArgObject(self._buffer.byptr())
+
+    def _get_buffer_value(self):
+        return self._buffer.buffer
+
+ARRAY_CACHE = {}
+
+def create_array_type(base, length):
+    if not isinstance(length, (int, long)):
+        raise TypeError("Can't multiply a ctypes type by a non-integer")
+    if length < 0:
+        raise ValueError("Array length must be >= 0")
+    key = (base, length)