Commits

Maciej Fijalkowski committed 0716fde Merge

merge default

Comments (0)

Files changed (69)

ctypes_configure/cbuild.py

     cfiles += eci.separate_module_files
     include_dirs = list(eci.include_dirs)
     library_dirs = list(eci.library_dirs)
-    if sys.platform == 'darwin':    # support Fink & Darwinports
-        for s in ('/sw/', '/opt/local/'):
+    if (sys.platform == 'darwin' or    # support Fink & Darwinports
+            sys.platform.startswith('freebsd')):
+        for s in ('/sw/', '/opt/local/', '/usr/local/'):
             if s + 'include' not in include_dirs and \
                os.path.exists(s + 'include'):
                 include_dirs.append(s + 'include')
             self.link_extra += ['-pthread']
         if sys.platform == 'win32':
             self.link_extra += ['/DEBUG'] # generate .pdb file
-        if sys.platform == 'darwin':
-            # support Fink & Darwinports
-            for s in ('/sw/', '/opt/local/'):
+        if (sys.platform == 'darwin' or    # support Fink & Darwinports
+                sys.platform.startswith('freebsd')):
+            for s in ('/sw/', '/opt/local/', '/usr/local/'):
                 if s + 'include' not in self.include_dirs and \
                    os.path.exists(s + 'include'):
                     self.include_dirs.append(s + 'include')
             self.outputfilename = py.path.local(cfilenames[0]).new(ext=ext)
         else: 
             self.outputfilename = py.path.local(outputfilename)
-        self.eci = eci
 
     def build(self, noerr=False):
         basename = self.outputfilename.new(ext='')
             old = cfile.dirpath().chdir() 
             try: 
                 res = compiler.compile([cfile.basename], 
-                                       include_dirs=self.eci.include_dirs,
+                                       include_dirs=self.include_dirs,
                                        extra_preargs=self.compile_extra)
                 assert len(res) == 1
                 cobjfile = py.path.local(res[0]) 
             finally: 
                 old.chdir() 
         compiler.link_executable(objects, str(self.outputfilename),
-                                 libraries=self.eci.libraries,
+                                 libraries=self.libraries,
                                  extra_preargs=self.link_extra,
-                                 library_dirs=self.eci.library_dirs)
+                                 library_dirs=self.library_dirs)
 
 def build_executable(*args, **kwds):
     noerr = kwds.pop('noerr', False)

lib-python/modified-2.7/UserDict.py

     def __iter__(self):
         return iter(self.data)
 
-import _abcoll
-_abcoll.MutableMapping.register(IterableUserDict)
+try:
+    import _abcoll
+except ImportError:
+    pass    # e.g. no '_weakref' module on this pypy
+else:
+    _abcoll.MutableMapping.register(IterableUserDict)
 
 
 class DictMixin:

lib_pypy/_subprocess.py

 
 # Now the _subprocess module implementation 
 
-from ctypes import c_int as _c_int, byref as _byref
+from ctypes import c_int as _c_int, byref as _byref, WinError as _WinError
 
 class _handle:
     def __init__(self, handle):
     res = _CreatePipe(_byref(read), _byref(write), None, size)
 
     if not res:
-        raise WindowsError("Error")
+        raise _WinError()
 
     return _handle(read.value), _handle(write.value)
 
                            access, inherit, options)
 
     if not res:
-        raise WindowsError("Error")
+        raise _WinError()
 
     return _handle(target.value)
 DUPLICATE_SAME_ACCESS = 2
                         start_dir, _byref(si), _byref(pi))
 
     if not res:
-        raise WindowsError("Error")
+        raise _WinError()
 
     return _handle(pi.hProcess), _handle(pi.hThread), pi.dwProcessID, pi.dwThreadID
 STARTF_USESHOWWINDOW = 0x001
     res = _WaitForSingleObject(int(handle), milliseconds)
 
     if res < 0:
-        raise WindowsError("Error")
+        raise _WinError()
 
     return res
 INFINITE = 0xffffffff
     res = _GetExitCodeProcess(int(handle), _byref(code))
 
     if not res:
-        raise WindowsError("Error")
+        raise _WinError()
 
     return code.value
 
     res = _TerminateProcess(int(handle), exitcode)
 
     if not res:
-        raise WindowsError("Error")
+        raise _WinError()
 
 def GetStdHandle(stdhandle):
     res = _GetStdHandle(stdhandle)

lib_pypy/ctypes_config_cache/pyexpat.ctc.py

-"""
-'ctypes_configure' source for pyexpat.py.
-Run this to rebuild _pyexpat_cache.py.
-"""
-
-import ctypes
-from ctypes import c_char_p, c_int, c_void_p, c_char
-from ctypes_configure import configure
-import dumpcache
-
-
-class CConfigure:
-    _compilation_info_ = configure.ExternalCompilationInfo(
-        includes = ['expat.h'],
-        libraries = ['expat'],
-        pre_include_lines = [
-        '#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)'],
-        )
-
-    XML_Char = configure.SimpleType('XML_Char', c_char)
-    XML_COMBINED_VERSION = configure.ConstantInteger('XML_COMBINED_VERSION')
-    for name in ['XML_PARAM_ENTITY_PARSING_NEVER',
-                 'XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE',
-                 'XML_PARAM_ENTITY_PARSING_ALWAYS']:
-        locals()[name] = configure.ConstantInteger(name)
-
-    XML_Encoding = configure.Struct('XML_Encoding',[
-                                    ('data', c_void_p),
-                                    ('convert', c_void_p),
-                                    ('release', c_void_p),
-                                    ('map', c_int * 256)])
-    XML_Content = configure.Struct('XML_Content',[
-        ('numchildren', c_int),
-        ('children', c_void_p),
-        ('name', c_char_p),
-        ('type', c_int),
-        ('quant', c_int),
-    ])
-    # this is insanely stupid
-    XML_FALSE = configure.ConstantInteger('XML_FALSE')
-    XML_TRUE = configure.ConstantInteger('XML_TRUE')
-
-config = configure.configure(CConfigure)
-
-dumpcache.dumpcache2('pyexpat', config)

lib_pypy/ctypes_config_cache/test/test_cache.py

     d = run('resource.ctc.py', '_resource_cache.py')
     assert 'RLIM_NLIMITS' in d
 
-def test_pyexpat():
-    d = run('pyexpat.ctc.py', '_pyexpat_cache.py')
-    assert 'XML_COMBINED_VERSION' in d
-
 def test_locale():
     d = run('locale.ctc.py', '_locale_cache.py')
     assert 'LC_ALL' in d

lib_pypy/datetime.py

     raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
 
 def _check_date_fields(year, month, day):
-    if not isinstance(year, (int, long)):
-        raise TypeError('int expected')
+    for value in [year, day]:
+        if not isinstance(value, (int, long)):
+            raise TypeError('int expected')
     if not MINYEAR <= year <= MAXYEAR:
         raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
     if not 1 <= month <= 12:
         raise ValueError('day must be in 1..%d' % dim, day)
 
 def _check_time_fields(hour, minute, second, microsecond):
-    if not isinstance(hour, (int, long)):
-        raise TypeError('int expected')
+    for value in [hour, minute, second, microsecond]:
+        if not isinstance(value, (int, long)):
+            raise TypeError('int expected')
     if not 0 <= hour <= 23:
         raise ValueError('hour must be in 0..23', hour)
     if not 0 <= minute <= 59:
     def utcfromtimestamp(cls, t):
         "Construct a UTC datetime from a POSIX timestamp (like time.time())."
         t, frac = divmod(t, 1.0)
-        us = round(frac * 1e6)
+        us = int(round(frac * 1e6))
 
         # If timestamp is less than one microsecond smaller than a
         # full second, us can be rounded up to 1000000.  In this case,

lib_pypy/numpy.py

+raise ImportError(
+    "The 'numpy' module of PyPy is in-development and not complete. "
+    "To try it out anyway, you can either import from 'numpypy', "
+    "or just write 'import numpypy' first in your program and then "
+    "import from 'numpy' as usual.")

lib_pypy/numpypy/__init__.py

 from _numpypy import *
 from .core import *
+
+import sys
+sys.modules.setdefault('numpy', sys.modules['numpypy'])

lib_pypy/numpypy/core/numeric.py

 
-from _numpypy import array, ndarray, int_, float_ #, complex_# , longlong
+from _numpypy import array, ndarray, int_, float_, bool_ #, complex_# , longlong
 from _numpypy import concatenate
+import math
 import sys
 import _numpypy as multiarray # ARGH
 from numpypy.core.arrayprint import array2string
 set_string_function(array_repr, 1)
 
 little_endian = (sys.byteorder == 'little')
+
+Inf = inf = infty = Infinity = PINF = float('inf')
+NINF = float('-inf')
+PZERO = 0.0
+NZERO = -0.0
+nan = NaN = NAN = float('nan')
+False_ = bool_(False)
+True_ = bool_(True)
+e = math.e
+pi = math.pi

lib_pypy/pyexpat.py

-
-import ctypes
-import ctypes.util
-from ctypes import c_char_p, c_int, c_void_p, POINTER, c_char, c_wchar_p
-import sys
-
-# load the platform-specific cache made by running pyexpat.ctc.py
-from ctypes_config_cache._pyexpat_cache import *
-
-try: from __pypy__ import builtinify
-except ImportError: builtinify = lambda f: f
-
-
-lib = ctypes.CDLL(ctypes.util.find_library('expat'))
-
-
-XML_Content.children = POINTER(XML_Content)
-XML_Parser = ctypes.c_void_p # an opaque pointer
-assert XML_Char is ctypes.c_char # this assumption is everywhere in
-# cpython's expat, let's explode
-
-def declare_external(name, args, res):
-    func = getattr(lib, name)
-    func.args = args
-    func.restype = res
-    globals()[name] = func
-
-declare_external('XML_ParserCreate', [c_char_p], XML_Parser)
-declare_external('XML_ParserCreateNS', [c_char_p, c_char], XML_Parser)
-declare_external('XML_Parse', [XML_Parser, c_char_p, c_int, c_int], c_int)
-currents = ['CurrentLineNumber', 'CurrentColumnNumber',
-            'CurrentByteIndex']
-for name in currents:
-    func = getattr(lib, 'XML_Get' + name)
-    func.args = [XML_Parser]
-    func.restype = c_int
-
-declare_external('XML_SetReturnNSTriplet', [XML_Parser, c_int], None)
-declare_external('XML_GetSpecifiedAttributeCount', [XML_Parser], c_int)
-declare_external('XML_SetParamEntityParsing', [XML_Parser, c_int], None)
-declare_external('XML_GetErrorCode', [XML_Parser], c_int)
-declare_external('XML_StopParser', [XML_Parser, c_int], None)
-declare_external('XML_ErrorString', [c_int], c_char_p)
-declare_external('XML_SetBase', [XML_Parser, c_char_p], None)
-if XML_COMBINED_VERSION >= 19505:
-    declare_external('XML_UseForeignDTD', [XML_Parser, c_int], None)
-
-declare_external('XML_SetUnknownEncodingHandler', [XML_Parser, c_void_p,
-                                                   c_void_p], None)
-declare_external('XML_FreeContentModel', [XML_Parser, POINTER(XML_Content)],
-                 None)
-declare_external('XML_ExternalEntityParserCreate', [XML_Parser,c_char_p,
-                                                    c_char_p],
-                 XML_Parser)
-
-handler_names = [
-    'StartElement',
-    'EndElement',
-    'ProcessingInstruction',
-    'CharacterData',
-    'UnparsedEntityDecl',
-    'NotationDecl',
-    'StartNamespaceDecl',
-    'EndNamespaceDecl',
-    'Comment',
-    'StartCdataSection',
-    'EndCdataSection',
-    'Default',
-    'DefaultHandlerExpand',
-    'NotStandalone',
-    'ExternalEntityRef',
-    'StartDoctypeDecl',
-    'EndDoctypeDecl',
-    'EntityDecl',
-    'XmlDecl',
-    'ElementDecl',
-    'AttlistDecl',
-    ]
-if XML_COMBINED_VERSION >= 19504:
-    handler_names.append('SkippedEntity')
-setters = {}
-
-for name in handler_names:
-    if name == 'DefaultHandlerExpand':
-        newname = 'XML_SetDefaultHandlerExpand'
-    else:
-        name += 'Handler'
-        newname = 'XML_Set' + name
-    cfunc = getattr(lib, newname)
-    cfunc.args = [XML_Parser, ctypes.c_void_p]
-    cfunc.result = ctypes.c_int
-    setters[name] = cfunc
-
-class ExpatError(Exception):
-    def __str__(self):
-        return self.s
-
-error = ExpatError
-
-class XMLParserType(object):
-    specified_attributes = 0
-    ordered_attributes = 0
-    returns_unicode = 1
-    encoding = 'utf-8'
-    def __init__(self, encoding, namespace_separator, _hook_external_entity=False):
-        self.returns_unicode = 1
-        if encoding:
-            self.encoding = encoding
-        if not _hook_external_entity:
-            if namespace_separator is None:
-                self.itself = XML_ParserCreate(encoding)
-            else:
-                self.itself = XML_ParserCreateNS(encoding, ord(namespace_separator))
-            if not self.itself:
-                raise RuntimeError("Creating parser failed")
-            self._set_unknown_encoding_handler()
-        self.storage = {}
-        self.buffer = None
-        self.buffer_size = 8192
-        self.character_data_handler = None
-        self.intern = {}
-        self.__exc_info = None
-
-    def _flush_character_buffer(self):
-        if not self.buffer:
-            return
-        res = self._call_character_handler(''.join(self.buffer))
-        self.buffer = []
-        return res
-
-    def _call_character_handler(self, buf):
-        if self.character_data_handler:
-            self.character_data_handler(buf)
-
-    def _set_unknown_encoding_handler(self):
-        def UnknownEncoding(encodingData, name, info_p):
-            info = info_p.contents
-            s = ''.join([chr(i) for i in range(256)])
-            u = s.decode(self.encoding, 'replace')
-            for i in range(len(u)):
-                if u[i] == u'\xfffd':
-                    info.map[i] = -1
-                else:
-                    info.map[i] = ord(u[i])
-            info.data = None
-            info.convert = None
-            info.release = None
-            return 1
-        
-        CB = ctypes.CFUNCTYPE(c_int, c_void_p, c_char_p, POINTER(XML_Encoding))
-        cb = CB(UnknownEncoding)
-        self._unknown_encoding_handler = (cb, UnknownEncoding)
-        XML_SetUnknownEncodingHandler(self.itself, cb, None)
-
-    def _set_error(self, code):
-        e = ExpatError()
-        e.code = code
-        lineno = lib.XML_GetCurrentLineNumber(self.itself)
-        colno = lib.XML_GetCurrentColumnNumber(self.itself)
-        e.offset = colno
-        e.lineno = lineno
-        err = XML_ErrorString(code)[:200]
-        e.s = "%s: line: %d, column: %d" % (err, lineno, colno)
-        e.message = e.s
-        self._error = e
-
-    def Parse(self, data, is_final=0):
-        res = XML_Parse(self.itself, data, len(data), is_final)
-        if res == 0:
-            self._set_error(XML_GetErrorCode(self.itself))
-            if self.__exc_info:
-                exc_info = self.__exc_info
-                self.__exc_info = None
-                raise exc_info[0], exc_info[1], exc_info[2]
-            else:
-                raise self._error
-        self._flush_character_buffer()
-        return res
-
-    def _sethandler(self, name, real_cb):
-        setter = setters[name]
-        try:
-            cb = self.storage[(name, real_cb)]
-        except KeyError:
-            cb = getattr(self, 'get_cb_for_%s' % name)(real_cb)
-            self.storage[(name, real_cb)] = cb
-        except TypeError:
-            # weellll...
-            cb = getattr(self, 'get_cb_for_%s' % name)(real_cb)
-        setter(self.itself, cb)
-
-    def _wrap_cb(self, cb):
-        def f(*args):
-            try:
-                return cb(*args)
-            except:
-                self.__exc_info = sys.exc_info()
-                XML_StopParser(self.itself, XML_FALSE)
-        return f
-
-    def get_cb_for_StartElementHandler(self, real_cb):
-        def StartElement(unused, name, attrs):
-            # unpack name and attrs
-            conv = self.conv
-            self._flush_character_buffer()
-            if self.specified_attributes:
-                max = XML_GetSpecifiedAttributeCount(self.itself)
-            else:
-                max = 0
-            while attrs[max]:
-                max += 2 # copied
-            if self.ordered_attributes:
-                res = [attrs[i] for i in range(max)]
-            else:
-                res = {}
-                for i in range(0, max, 2):
-                    res[conv(attrs[i])] = conv(attrs[i + 1])
-            real_cb(conv(name), res)
-        StartElement = self._wrap_cb(StartElement)
-        CB = ctypes.CFUNCTYPE(None, c_void_p, c_char_p, POINTER(c_char_p))
-        return CB(StartElement)
-
-    def get_cb_for_ExternalEntityRefHandler(self, real_cb):
-        def ExternalEntity(unused, context, base, sysId, pubId):
-            self._flush_character_buffer()
-            conv = self.conv
-            res = real_cb(conv(context), conv(base), conv(sysId),
-                          conv(pubId))
-            if res is None:
-                return 0
-            return res
-        ExternalEntity = self._wrap_cb(ExternalEntity)
-        CB = ctypes.CFUNCTYPE(c_int, c_void_p, *([c_char_p] * 4))
-        return CB(ExternalEntity)
-
-    def get_cb_for_CharacterDataHandler(self, real_cb):
-        def CharacterData(unused, s, lgt):
-            if self.buffer is None:
-                self._call_character_handler(self.conv(s[:lgt]))
-            else:
-                if len(self.buffer) + lgt > self.buffer_size:
-                    self._flush_character_buffer()
-                    if self.character_data_handler is None:
-                        return
-                if lgt >= self.buffer_size:
-                    self._call_character_handler(s[:lgt])
-                    self.buffer = []
-                else:
-                    self.buffer.append(s[:lgt])
-        CharacterData = self._wrap_cb(CharacterData)
-        CB = ctypes.CFUNCTYPE(None, c_void_p, POINTER(c_char), c_int)
-        return CB(CharacterData)
-
-    def get_cb_for_NotStandaloneHandler(self, real_cb):
-        def NotStandaloneHandler(unused):
-            return real_cb()
-        NotStandaloneHandler = self._wrap_cb(NotStandaloneHandler)
-        CB = ctypes.CFUNCTYPE(c_int, c_void_p)
-        return CB(NotStandaloneHandler)
-
-    def get_cb_for_EntityDeclHandler(self, real_cb):
-        def EntityDecl(unused, ename, is_param, value, value_len, base,
-                       system_id, pub_id, not_name):
-            self._flush_character_buffer()
-            if not value:
-                value = None
-            else:
-                value = value[:value_len]
-            args = [ename, is_param, value, base, system_id,
-                    pub_id, not_name]
-            args = [self.conv(arg) for arg in args]
-            real_cb(*args)
-        EntityDecl = self._wrap_cb(EntityDecl)
-        CB = ctypes.CFUNCTYPE(None, c_void_p, c_char_p, c_int, c_char_p,
-                               c_int, c_char_p, c_char_p, c_char_p, c_char_p)
-        return CB(EntityDecl)
-
-    def _conv_content_model(self, model):
-        children = tuple([self._conv_content_model(model.children[i])
-                          for i in range(model.numchildren)])
-        return (model.type, model.quant, self.conv(model.name),
-                children)
-
-    def get_cb_for_ElementDeclHandler(self, real_cb):
-        def ElementDecl(unused, name, model):
-            self._flush_character_buffer()
-            modelobj = self._conv_content_model(model[0])
-            real_cb(name, modelobj)
-            XML_FreeContentModel(self.itself, model)
-
-        ElementDecl = self._wrap_cb(ElementDecl)
-        CB = ctypes.CFUNCTYPE(None, c_void_p, c_char_p, POINTER(XML_Content))
-        return CB(ElementDecl)
-
-    def _new_callback_for_string_len(name, sign):
-        def get_callback_for_(self, real_cb):
-            def func(unused, s, len):
-                self._flush_character_buffer()
-                arg = self.conv(s[:len])
-                real_cb(arg)
-            func.func_name = name
-            func = self._wrap_cb(func)
-            CB = ctypes.CFUNCTYPE(*sign)
-            return CB(func)
-        get_callback_for_.func_name = 'get_cb_for_' + name
-        return get_callback_for_
-    
-    for name in ['DefaultHandlerExpand',
-                 'DefaultHandler']:
-        sign = [None, c_void_p, POINTER(c_char), c_int]
-        name = 'get_cb_for_' + name
-        locals()[name] = _new_callback_for_string_len(name, sign)
-
-    def _new_callback_for_starargs(name, sign):
-        def get_callback_for_(self, real_cb):
-            def func(unused, *args):
-                self._flush_character_buffer()
-                args = [self.conv(arg) for arg in args]
-                real_cb(*args)
-            func.func_name = name
-            func = self._wrap_cb(func)
-            CB = ctypes.CFUNCTYPE(*sign)
-            return CB(func)
-        get_callback_for_.func_name = 'get_cb_for_' + name
-        return get_callback_for_
-    
-    for name, num_or_sign in [
-        ('EndElementHandler', 1),
-        ('ProcessingInstructionHandler', 2),
-        ('UnparsedEntityDeclHandler', 5),
-        ('NotationDeclHandler', 4),
-        ('StartNamespaceDeclHandler', 2),
-        ('EndNamespaceDeclHandler', 1),
-        ('CommentHandler', 1),
-        ('StartCdataSectionHandler', 0),
-        ('EndCdataSectionHandler', 0),
-        ('StartDoctypeDeclHandler', [None, c_void_p] + [c_char_p] * 3 + [c_int]),
-        ('XmlDeclHandler', [None, c_void_p, c_char_p, c_char_p, c_int]),
-        ('AttlistDeclHandler', [None, c_void_p] + [c_char_p] * 4 + [c_int]),
-        ('EndDoctypeDeclHandler', 0),
-        ('SkippedEntityHandler', [None, c_void_p, c_char_p, c_int]),
-        ]:
-        if isinstance(num_or_sign, int):
-            sign = [None, c_void_p] + [c_char_p] * num_or_sign
-        else:
-            sign = num_or_sign
-        name = 'get_cb_for_' + name
-        locals()[name] = _new_callback_for_starargs(name, sign)
-
-    def conv_unicode(self, s):
-        if s is None or isinstance(s, int):
-            return s
-        return s.decode(self.encoding, "strict")
-
-    def __setattr__(self, name, value):
-        # forest of ifs...
-        if name in ['ordered_attributes',
-                    'returns_unicode', 'specified_attributes']:
-            if value:
-                if name == 'returns_unicode':
-                    self.conv = self.conv_unicode
-                self.__dict__[name] = 1
-            else:
-                if name == 'returns_unicode':
-                    self.conv = lambda s: s
-                self.__dict__[name] = 0
-        elif name == 'buffer_text':
-            if value:
-                self.buffer = []
-            else:
-                self._flush_character_buffer()
-                self.buffer = None
-        elif name == 'buffer_size':
-            if not isinstance(value, int):
-                raise TypeError("Expected int")
-            if value <= 0:
-                raise ValueError("Expected positive int")
-            self.__dict__[name] = value
-        elif name == 'namespace_prefixes':
-            XML_SetReturnNSTriplet(self.itself, int(bool(value)))
-        elif name in setters:
-            if name == 'CharacterDataHandler':
-                # XXX we need to flush buffer here
-                self._flush_character_buffer()
-                self.character_data_handler = value
-            #print name
-            #print value
-            #print
-            self._sethandler(name, value)
-        else:
-            self.__dict__[name] = value
-
-    def SetParamEntityParsing(self, arg):
-        XML_SetParamEntityParsing(self.itself, arg)
-
-    if XML_COMBINED_VERSION >= 19505:
-        def UseForeignDTD(self, arg=True):
-            if arg:
-                flag = XML_TRUE
-            else:
-                flag = XML_FALSE
-            XML_UseForeignDTD(self.itself, flag)
-
-    def __getattr__(self, name):
-        if name == 'buffer_text':
-            return self.buffer is not None
-        elif name in currents:
-            return getattr(lib, 'XML_Get' + name)(self.itself)
-        elif name == 'ErrorColumnNumber':
-            return lib.XML_GetCurrentColumnNumber(self.itself)
-        elif name == 'ErrorLineNumber':
-            return lib.XML_GetCurrentLineNumber(self.itself)
-        return self.__dict__[name]
-
-    def ParseFile(self, file):
-        return self.Parse(file.read(), False)
-
-    def SetBase(self, base):
-        XML_SetBase(self.itself, base)
-
-    def ExternalEntityParserCreate(self, context, encoding=None):
-        """ExternalEntityParserCreate(context[, encoding])
-        Create a parser for parsing an external entity based on the
-        information passed to the ExternalEntityRefHandler."""
-        new_parser = XMLParserType(encoding, None, True)
-        new_parser.itself = XML_ExternalEntityParserCreate(self.itself,
-                                                           context, encoding)
-        new_parser._set_unknown_encoding_handler()
-        return new_parser
-
-@builtinify
-def ErrorString(errno):
-    return XML_ErrorString(errno)[:200]
-
-@builtinify
-def ParserCreate(encoding=None, namespace_separator=None, intern=None):
-    if (not isinstance(encoding, str) and
-        not encoding is None):
-        raise TypeError("ParserCreate() argument 1 must be string or None, not %s" % encoding.__class__.__name__)
-    if (not isinstance(namespace_separator, str) and
-        not namespace_separator is None):
-        raise TypeError("ParserCreate() argument 2 must be string or None, not %s" % namespace_separator.__class__.__name__)
-    if namespace_separator is not None:
-        if len(namespace_separator) > 1:
-            raise ValueError('namespace_separator must be at most one character, omitted, or None')
-        if len(namespace_separator) == 0:
-            namespace_separator = None
-    return XMLParserType(encoding, namespace_separator)

lib_pypy/pypy_test/test_pyexpat.py

-# XXX TypeErrors on calling handlers, or on bad return values from a
-# handler, are obscure and unhelpful.
-
-from __future__ import absolute_import
-import StringIO, sys
-import unittest, py
-
-from lib_pypy.ctypes_config_cache import rebuild
-rebuild.rebuild_one('pyexpat.ctc.py')
-
-from lib_pypy import pyexpat
-#from xml.parsers import expat
-expat = pyexpat
-
-from test.test_support import sortdict, run_unittest
-
-
-class TestSetAttribute:
-    def setup_method(self, meth):
-        self.parser = expat.ParserCreate(namespace_separator='!')
-        self.set_get_pairs = [
-            [0, 0],
-            [1, 1],
-            [2, 1],
-            [0, 0],
-            ]
-
-    def test_returns_unicode(self):
-        for x, y in self.set_get_pairs:
-            self.parser.returns_unicode = x
-            assert self.parser.returns_unicode == y
-
-    def test_ordered_attributes(self):
-        for x, y in self.set_get_pairs:
-            self.parser.ordered_attributes = x
-            assert self.parser.ordered_attributes == y
-
-    def test_specified_attributes(self):
-        for x, y in self.set_get_pairs:
-            self.parser.specified_attributes = x
-            assert self.parser.specified_attributes == y
-
-
-data = '''\
-<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
-<?xml-stylesheet href="stylesheet.css"?>
-<!-- comment data -->
-<!DOCTYPE quotations SYSTEM "quotations.dtd" [
-<!ELEMENT root ANY>
-<!ATTLIST root attr1 CDATA #REQUIRED attr2 CDATA #IMPLIED>
-<!NOTATION notation SYSTEM "notation.jpeg">
-<!ENTITY acirc "&#226;">
-<!ENTITY external_entity SYSTEM "entity.file">
-<!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation>
-%unparsed_entity;
-]>
-
-<root attr1="value1" attr2="value2&#8000;">
-<myns:subelement xmlns:myns="http://www.python.org/namespace">
-     Contents of subelements
-</myns:subelement>
-<sub2><![CDATA[contents of CDATA section]]></sub2>
-&external_entity;
-&skipped_entity;
-</root>
-'''
-
-
-# Produce UTF-8 output
-class TestParse:
-    class Outputter:
-        def __init__(self):
-            self.out = []
-
-        def StartElementHandler(self, name, attrs):
-            self.out.append('Start element: ' + repr(name) + ' ' +
-                            sortdict(attrs))
-
-        def EndElementHandler(self, name):
-            self.out.append('End element: ' + repr(name))
-
-        def CharacterDataHandler(self, data):
-            data = data.strip()
-            if data:
-                self.out.append('Character data: ' + repr(data))
-
-        def ProcessingInstructionHandler(self, target, data):
-            self.out.append('PI: ' + repr(target) + ' ' + repr(data))
-
-        def StartNamespaceDeclHandler(self, prefix, uri):
-            self.out.append('NS decl: ' + repr(prefix) + ' ' + repr(uri))
-
-        def EndNamespaceDeclHandler(self, prefix):
-            self.out.append('End of NS decl: ' + repr(prefix))
-
-        def StartCdataSectionHandler(self):
-            self.out.append('Start of CDATA section')
-
-        def EndCdataSectionHandler(self):
-            self.out.append('End of CDATA section')
-
-        def CommentHandler(self, text):
-            self.out.append('Comment: ' + repr(text))
-
-        def NotationDeclHandler(self, *args):
-            name, base, sysid, pubid = args
-            self.out.append('Notation declared: %s' %(args,))
-
-        def UnparsedEntityDeclHandler(self, *args):
-            entityName, base, systemId, publicId, notationName = args
-            self.out.append('Unparsed entity decl: %s' %(args,))
-
-        def NotStandaloneHandler(self):
-            self.out.append('Not standalone')
-            return 1
-
-        def ExternalEntityRefHandler(self, *args):
-            context, base, sysId, pubId = args
-            self.out.append('External entity ref: %s' %(args[1:],))
-            return 1
-
-        def StartDoctypeDeclHandler(self, *args):
-            self.out.append(('Start doctype', args))
-            return 1
-
-        def EndDoctypeDeclHandler(self):
-            self.out.append("End doctype")
-            return 1
-
-        def EntityDeclHandler(self, *args):
-            self.out.append(('Entity declaration', args))
-            return 1
-
-        def XmlDeclHandler(self, *args):
-            self.out.append(('XML declaration', args))
-            return 1
-
-        def ElementDeclHandler(self, *args):
-            self.out.append(('Element declaration', args))
-            return 1
-
-        def AttlistDeclHandler(self, *args):
-            self.out.append(('Attribute list declaration', args))
-            return 1
-
-        def SkippedEntityHandler(self, *args):
-            self.out.append(("Skipped entity", args))
-            return 1
-
-        def DefaultHandler(self, userData):
-            pass
-
-        def DefaultHandlerExpand(self, userData):
-            pass
-
-    handler_names = [
-        'StartElementHandler', 'EndElementHandler', 'CharacterDataHandler',
-        'ProcessingInstructionHandler', 'UnparsedEntityDeclHandler',
-        'NotationDeclHandler', 'StartNamespaceDeclHandler',
-        'EndNamespaceDeclHandler', 'CommentHandler',
-        'StartCdataSectionHandler', 'EndCdataSectionHandler', 'DefaultHandler',
-        'DefaultHandlerExpand', 'NotStandaloneHandler',
-        'ExternalEntityRefHandler', 'StartDoctypeDeclHandler',
-        'EndDoctypeDeclHandler', 'EntityDeclHandler', 'XmlDeclHandler',
-        'ElementDeclHandler', 'AttlistDeclHandler', 'SkippedEntityHandler',
-        ]
-
-    def test_utf8(self):
-
-        out = self.Outputter()
-        parser = expat.ParserCreate(namespace_separator='!')
-        for name in self.handler_names:
-            setattr(parser, name, getattr(out, name))
-        parser.returns_unicode = 0
-        parser.Parse(data, 1)
-
-        # Verify output
-        operations = out.out
-        expected_operations = [
-            ('XML declaration', (u'1.0', u'iso-8859-1', 0)),
-            'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'',
-            "Comment: ' comment data '",
-            "Not standalone",
-            ("Start doctype", ('quotations', 'quotations.dtd', None, 1)),
-            ('Element declaration', (u'root', (2, 0, None, ()))),
-            ('Attribute list declaration', ('root', 'attr1', 'CDATA', None,
-                1)),
-            ('Attribute list declaration', ('root', 'attr2', 'CDATA', None,
-                0)),
-            "Notation declared: ('notation', None, 'notation.jpeg', None)",
-            ('Entity declaration', ('acirc', 0, '\xc3\xa2', None, None, None, None)),
-            ('Entity declaration', ('external_entity', 0, None, None,
-                'entity.file', None, None)),
-            "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')",
-            "Not standalone",
-            "End doctype",
-            "Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\\xe1\\xbd\\x80'}",
-            "NS decl: 'myns' 'http://www.python.org/namespace'",
-            "Start element: 'http://www.python.org/namespace!subelement' {}",
-            "Character data: 'Contents of subelements'",
-            "End element: 'http://www.python.org/namespace!subelement'",
-            "End of NS decl: 'myns'",
-            "Start element: 'sub2' {}",
-            'Start of CDATA section',
-            "Character data: 'contents of CDATA section'",
-            'End of CDATA section',
-            "End element: 'sub2'",
-            "External entity ref: (None, 'entity.file', None)",
-            ('Skipped entity', ('skipped_entity', 0)),
-            "End element: 'root'",
-        ]
-        for operation, expected_operation in zip(operations, expected_operations):
-            assert operation == expected_operation
-
-    def test_unicode(self):
-        # Try the parse again, this time producing Unicode output
-        out = self.Outputter()
-        parser = expat.ParserCreate(namespace_separator='!')
-        parser.returns_unicode = 1
-        for name in self.handler_names:
-            setattr(parser, name, getattr(out, name))
-
-        parser.Parse(data, 1)
-
-        operations = out.out
-        expected_operations = [
-            ('XML declaration', (u'1.0', u'iso-8859-1', 0)),
-            'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'',
-            "Comment: u' comment data '",
-            "Not standalone",
-            ("Start doctype", ('quotations', 'quotations.dtd', None, 1)),
-            ('Element declaration', (u'root', (2, 0, None, ()))),
-            ('Attribute list declaration', ('root', 'attr1', 'CDATA', None,
-                1)),
-            ('Attribute list declaration', ('root', 'attr2', 'CDATA', None,
-                0)),
-            "Notation declared: (u'notation', None, u'notation.jpeg', None)",
-            ('Entity declaration', (u'acirc', 0, u'\xe2', None, None, None,
-                None)),
-            ('Entity declaration', (u'external_entity', 0, None, None,
-                 u'entity.file', None, None)),
-            "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')",
-            "Not standalone",
-            "End doctype",
-            "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}",
-            "NS decl: u'myns' u'http://www.python.org/namespace'",
-            "Start element: u'http://www.python.org/namespace!subelement' {}",
-            "Character data: u'Contents of subelements'",
-            "End element: u'http://www.python.org/namespace!subelement'",
-            "End of NS decl: u'myns'",
-            "Start element: u'sub2' {}",
-            'Start of CDATA section',
-            "Character data: u'contents of CDATA section'",
-            'End of CDATA section',
-            "End element: u'sub2'",
-            "External entity ref: (None, u'entity.file', None)",
-            ('Skipped entity', ('skipped_entity', 0)),
-            "End element: u'root'",
-        ]
-        for operation, expected_operation in zip(operations, expected_operations):
-            assert operation == expected_operation
-
-    def test_parse_file(self):
-        # Try parsing a file
-        out = self.Outputter()
-        parser = expat.ParserCreate(namespace_separator='!')
-        parser.returns_unicode = 1
-        for name in self.handler_names:
-            setattr(parser, name, getattr(out, name))
-        file = StringIO.StringIO(data)
-
-        parser.ParseFile(file)
-
-        operations = out.out
-        expected_operations = [
-            ('XML declaration', (u'1.0', u'iso-8859-1', 0)),
-            'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'',
-            "Comment: u' comment data '",
-            "Not standalone",
-            ("Start doctype", ('quotations', 'quotations.dtd', None, 1)),
-            ('Element declaration', (u'root', (2, 0, None, ()))),
-            ('Attribute list declaration', ('root', 'attr1', 'CDATA', None,
-                1)),
-            ('Attribute list declaration', ('root', 'attr2', 'CDATA', None,
-                0)),
-            "Notation declared: (u'notation', None, u'notation.jpeg', None)",
-            ('Entity declaration', ('acirc', 0, u'\xe2', None, None, None, None)),
-            ('Entity declaration', (u'external_entity', 0, None, None, u'entity.file', None, None)),
-            "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')",
-            "Not standalone",
-            "End doctype",
-            "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}",
-            "NS decl: u'myns' u'http://www.python.org/namespace'",
-            "Start element: u'http://www.python.org/namespace!subelement' {}",
-            "Character data: u'Contents of subelements'",
-            "End element: u'http://www.python.org/namespace!subelement'",
-            "End of NS decl: u'myns'",
-            "Start element: u'sub2' {}",
-            'Start of CDATA section',
-            "Character data: u'contents of CDATA section'",
-            'End of CDATA section',
-            "End element: u'sub2'",
-            "External entity ref: (None, u'entity.file', None)",
-            ('Skipped entity', ('skipped_entity', 0)),
-            "End element: u'root'",
-        ]
-        for operation, expected_operation in zip(operations, expected_operations):
-            assert operation == expected_operation
-
-
-class TestNamespaceSeparator:
-    def test_legal(self):
-        # Tests that make sure we get errors when the namespace_separator value
-        # is illegal, and that we don't for good values:
-        expat.ParserCreate()
-        expat.ParserCreate(namespace_separator=None)
-        expat.ParserCreate(namespace_separator=' ')
-
-    def test_illegal(self):
-        try:
-            expat.ParserCreate(namespace_separator=42)
-            raise AssertionError
-        except TypeError, e:
-            assert str(e) == (
-                'ParserCreate() argument 2 must be string or None, not int')
-
-        try:
-            expat.ParserCreate(namespace_separator='too long')
-            raise AssertionError
-        except ValueError, e:
-            assert str(e) == (
-                'namespace_separator must be at most one character, omitted, or None')
-
-    def test_zero_length(self):
-        # ParserCreate() needs to accept a namespace_separator of zero length
-        # to satisfy the requirements of RDF applications that are required
-        # to simply glue together the namespace URI and the localname.  Though
-        # considered a wart of the RDF specifications, it needs to be supported.
-        #
-        # See XML-SIG mailing list thread starting with
-        # http://mail.python.org/pipermail/xml-sig/2001-April/005202.html
-        #
-        expat.ParserCreate(namespace_separator='') # too short
-
-
-class TestInterning:
-    def test(self):
-        py.test.skip("Not working")
-        # Test the interning machinery.
-        p = expat.ParserCreate()
-        L = []
-        def collector(name, *args):
-            L.append(name)
-        p.StartElementHandler = collector
-        p.EndElementHandler = collector
-        p.Parse("<e> <e/> <e></e> </e>", 1)
-        tag = L[0]
-        assert len(L) == 6
-        for entry in L:
-            # L should have the same string repeated over and over.
-            assert tag is entry
-
-
-class TestBufferText:
-    def setup_method(self, meth):
-        self.stuff = []
-        self.parser = expat.ParserCreate()
-        self.parser.buffer_text = 1
-        self.parser.CharacterDataHandler = self.CharacterDataHandler
-
-    def check(self, expected, label):
-        assert self.stuff == expected, (
-                "%s\nstuff    = %r\nexpected = %r"
-                % (label, self.stuff, map(unicode, expected)))
-
-    def CharacterDataHandler(self, text):
-        self.stuff.append(text)
-
-    def StartElementHandler(self, name, attrs):
-        self.stuff.append("<%s>" % name)
-        bt = attrs.get("buffer-text")
-        if bt == "yes":
-            self.parser.buffer_text = 1
-        elif bt == "no":
-            self.parser.buffer_text = 0
-
-    def EndElementHandler(self, name):
-        self.stuff.append("</%s>" % name)
-
-    def CommentHandler(self, data):
-        self.stuff.append("<!--%s-->" % data)
-
-    def setHandlers(self, handlers=[]):
-        for name in handlers:
-            setattr(self.parser, name, getattr(self, name))
-
-    def test_default_to_disabled(self):
-        parser = expat.ParserCreate()
-        assert not parser.buffer_text
-
-    def test_buffering_enabled(self):
-        # Make sure buffering is turned on
-        assert self.parser.buffer_text
-        self.parser.Parse("<a>1<b/>2<c/>3</a>", 1)
-        assert self.stuff == ['123'], (
-                          "buffered text not properly collapsed")
-
-    def test1(self):
-        # XXX This test exposes more detail of Expat's text chunking than we
-        # XXX like, but it tests what we need to concisely.
-        self.setHandlers(["StartElementHandler"])
-        self.parser.Parse("<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4\n5</a>", 1)
-        assert self.stuff == (
-                          ["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"]), (
-                          "buffering control not reacting as expected")
-
-    def test2(self):
-        self.parser.Parse("<a>1<b/>&lt;2&gt;<c/>&#32;\n&#x20;3</a>", 1)
-        assert self.stuff == ["1<2> \n 3"], (
-                          "buffered text not properly collapsed")
-
-    def test3(self):
-        self.setHandlers(["StartElementHandler"])
-        self.parser.Parse("<a>1<b/>2<c/>3</a>", 1)
-        assert self.stuff == ["<a>", "1", "<b>", "2", "<c>", "3"], (
-                          "buffered text not properly split")
-
-    def test4(self):
-        self.setHandlers(["StartElementHandler", "EndElementHandler"])
-        self.parser.CharacterDataHandler = None
-        self.parser.Parse("<a>1<b/>2<c/>3</a>", 1)
-        assert self.stuff == (
-                          ["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"])
-
-    def test5(self):
-        self.setHandlers(["StartElementHandler", "EndElementHandler"])
-        self.parser.Parse("<a>1<b></b>2<c/>3</a>", 1)
-        assert self.stuff == (
-            ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"])
-
-    def test6(self):
-        self.setHandlers(["CommentHandler", "EndElementHandler",
-                    "StartElementHandler"])
-        self.parser.Parse("<a>1<b/>2<c></c>345</a> ", 1)
-        assert self.stuff == (
-            ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"]), (
-            "buffered text not properly split")
-
-    def test7(self):
-        self.setHandlers(["CommentHandler", "EndElementHandler",
-                    "StartElementHandler"])
-        self.parser.Parse("<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", 1)
-        assert self.stuff == (
-                          ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3",
-                           "<!--abc-->", "4", "<!--def-->", "5", "</a>"]), (
-                          "buffered text not properly split")
-
-
-# Test handling of exception from callback:
-class TestHandlerException:
-    def StartElementHandler(self, name, attrs):
-        raise RuntimeError(name)
-
-    def test(self):
-        parser = expat.ParserCreate()
-        parser.StartElementHandler = self.StartElementHandler
-        try:
-            parser.Parse("<a><b><c/></b></a>", 1)
-            raise AssertionError
-        except RuntimeError, e:
-            assert e.args[0] == 'a', (
-                              "Expected RuntimeError for element 'a', but" + \
-                              " found %r" % e.args[0])
-
-
-# Test Current* members:
-class TestPosition:
-    def StartElementHandler(self, name, attrs):
-        self.check_pos('s')
-
-    def EndElementHandler(self, name):
-        self.check_pos('e')
-
-    def check_pos(self, event):
-        pos = (event,
-               self.parser.CurrentByteIndex,
-               self.parser.CurrentLineNumber,
-               self.parser.CurrentColumnNumber)
-        assert self.upto < len(self.expected_list)
-        expected = self.expected_list[self.upto]
-        assert pos == expected, (
-                'Expected position %s, got position %s' %(pos, expected))
-        self.upto += 1
-
-    def test(self):
-        self.parser = expat.ParserCreate()
-        self.parser.StartElementHandler = self.StartElementHandler
-        self.parser.EndElementHandler = self.EndElementHandler
-        self.upto = 0
-        self.expected_list = [('s', 0, 1, 0), ('s', 5, 2, 1), ('s', 11, 3, 2),
-                              ('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)]
-
-        xml = '<a>\n <b>\n  <c/>\n </b>\n</a>'
-        self.parser.Parse(xml, 1)
-
-
-class Testsf1296433:
-    def test_parse_only_xml_data(self):
-        # http://python.org/sf/1296433
-        #
-        xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025)
-        # this one doesn't crash
-        #xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000)
-
-        class SpecificException(Exception):
-            pass
-
-        def handler(text):
-            raise SpecificException
-
-        parser = expat.ParserCreate()
-        parser.CharacterDataHandler = handler
-
-        py.test.raises(Exception, parser.Parse, xml)
-
-class TestChardataBuffer:
-    """
-    test setting of chardata buffer size
-    """
-
-    def test_1025_bytes(self):
-        assert self.small_buffer_test(1025) == 2
-
-    def test_1000_bytes(self):
-        assert self.small_buffer_test(1000) == 1
-
-    def test_wrong_size(self):
-        parser = expat.ParserCreate()
-        parser.buffer_text = 1
-        def f(size):
-            parser.buffer_size = size
-
-        py.test.raises(TypeError, f, sys.maxint+1)
-        py.test.raises(ValueError, f, -1)
-        py.test.raises(ValueError, f, 0)
-
-    def test_unchanged_size(self):
-        xml1 = ("<?xml version='1.0' encoding='iso8859'?><s>%s" % ('a' * 512))
-        xml2 = 'a'*512 + '</s>'
-        parser = expat.ParserCreate()
-        parser.CharacterDataHandler = self.counting_handler
-        parser.buffer_size = 512
-        parser.buffer_text = 1
-
-        # Feed 512 bytes of character data: the handler should be called
-        # once.
-        self.n = 0
-        parser.Parse(xml1)
-        assert self.n == 1
-
-        # Reassign to buffer_size, but assign the same size.
-        parser.buffer_size = parser.buffer_size
-        assert self.n == 1
-
-        # Try parsing rest of the document
-        parser.Parse(xml2)
-        assert self.n == 2
-
-
-    def test_disabling_buffer(self):
-        xml1 = "<?xml version='1.0' encoding='iso8859'?><a>%s" % ('a' * 512)
-        xml2 = ('b' * 1024)
-        xml3 = "%s</a>" % ('c' * 1024)
-        parser = expat.ParserCreate()
-        parser.CharacterDataHandler = self.counting_handler
-        parser.buffer_text = 1
-        parser.buffer_size = 1024
-        assert parser.buffer_size == 1024
-
-        # Parse one chunk of XML
-        self.n = 0
-        parser.Parse(xml1, 0)
-        assert parser.buffer_size == 1024
-        assert self.n == 1
-
-        # Turn off buffering and parse the next chunk.
-        parser.buffer_text = 0
-        assert not parser.buffer_text
-        assert parser.buffer_size == 1024
-        for i in range(10):
-            parser.Parse(xml2, 0)
-        assert self.n == 11
-
-        parser.buffer_text = 1
-        assert parser.buffer_text
-        assert parser.buffer_size == 1024
-        parser.Parse(xml3, 1)
-        assert self.n == 12
-
-
-
-    def make_document(self, bytes):
-        return ("<?xml version='1.0'?><tag>" + bytes * 'a' + '</tag>')
-
-    def counting_handler(self, text):
-        self.n += 1
-
-    def small_buffer_test(self, buffer_len):
-        xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * buffer_len)
-        parser = expat.ParserCreate()
-        parser.CharacterDataHandler = self.counting_handler
-        parser.buffer_size = 1024
-        parser.buffer_text = 1
-
-        self.n = 0
-        parser.Parse(xml)
-        return self.n
-
-    def test_change_size_1(self):
-        xml1 = "<?xml version='1.0' encoding='iso8859'?><a><s>%s" % ('a' * 1024)
-        xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025)
-        parser = expat.ParserCreate()
-        parser.CharacterDataHandler = self.counting_handler
-        parser.buffer_text = 1
-        parser.buffer_size = 1024
-        assert parser.buffer_size == 1024
-
-        self.n = 0
-        parser.Parse(xml1, 0)
-        parser.buffer_size *= 2
-        assert parser.buffer_size == 2048
-        parser.Parse(xml2, 1)
-        assert self.n == 2
-
-    def test_change_size_2(self):
-        xml1 = "<?xml version='1.0' encoding='iso8859'?><a>a<s>%s" % ('a' * 1023)
-        xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025)
-        parser = expat.ParserCreate()
-        parser.CharacterDataHandler = self.counting_handler
-        parser.buffer_text = 1
-        parser.buffer_size = 2048
-        assert parser.buffer_size == 2048
-
-        self.n=0
-        parser.Parse(xml1, 0)
-        parser.buffer_size /= 2
-        assert parser.buffer_size == 1024
-        parser.Parse(xml2, 1)
-        assert self.n == 4
-
-    def test_segfault(self):
-        py.test.raises(TypeError, expat.ParserCreate, 1234123123)
-
-def test_invalid_data():
-    parser = expat.ParserCreate()
-    parser.Parse('invalid.xml', 0)
-    try:
-        parser.Parse("", 1)
-    except expat.ExpatError, e:
-        assert e.code == 2 # XXX is this reliable?
-        assert e.lineno == 1
-        assert e.message.startswith('syntax error')
-    else:
-        py.test.fail("Did not raise")
-

py/_io/terminalwriter.py

                     ('srWindow', SMALL_RECT),
                     ('dwMaximumWindowSize', COORD)]
 
+    _GetStdHandle = ctypes.windll.kernel32.GetStdHandle
+    _GetStdHandle.argtypes = [wintypes.DWORD]
+    _GetStdHandle.restype = wintypes.HANDLE
     def GetStdHandle(kind):
-        return ctypes.windll.kernel32.GetStdHandle(kind)
+        return _GetStdHandle(kind)
 
-    SetConsoleTextAttribute = \
-        ctypes.windll.kernel32.SetConsoleTextAttribute
-
+    SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
+    SetConsoleTextAttribute.argtypes = [wintypes.HANDLE, wintypes.WORD]
+    SetConsoleTextAttribute.restype = wintypes.BOOL
+        
+    _GetConsoleScreenBufferInfo = \
+        ctypes.windll.kernel32.GetConsoleScreenBufferInfo
+    _GetConsoleScreenBufferInfo.argtypes = [wintypes.HANDLE, 
+                                ctypes.POINTER(CONSOLE_SCREEN_BUFFER_INFO)]
+    _GetConsoleScreenBufferInfo.restype = wintypes.BOOL
     def GetConsoleInfo(handle):
         info = CONSOLE_SCREEN_BUFFER_INFO()
-        ctypes.windll.kernel32.GetConsoleScreenBufferInfo(\
-            handle, ctypes.byref(info))
+        _GetConsoleScreenBufferInfo(handle, ctypes.byref(info))
         return info
 
     def _getdimensions():

pypy/doc/Makefile

 	      "run these through (pdf)latex."
 
 man:
+	python config/generate.py
 	$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
 	@echo
 	@echo "Build finished. The manual pages are in $(BUILDDIR)/man"

pypy/doc/coding-guide.rst

   In a few cases (e.g. hash table manipulation), we need machine-sized unsigned
   arithmetic.  For these cases there is the r_uint class, which is a pure
   Python implementation of word-sized unsigned integers that silently wrap
-  around.  The purpose of this class (as opposed to helper functions as above)
+  around.  ("word-sized" and "machine-sized" are used equivalently and mean
+  the native size, which you get using "unsigned long" in C.)
+  The purpose of this class (as opposed to helper functions as above)
   is consistent typing: both Python and the annotator will propagate r_uint
   instances in the program and interpret all the operations between them as
   unsigned.  Instances of r_uint are special-cased by the code generators to

pypy/doc/commandline_ref.rst

+Command line reference
+======================
+
+Manual pages
+------------
+
+.. toctree::
+   :maxdepth: 1
+
+   man/pypy.1.rst
 # built documents.
 #
 # The short X.Y version.
-version = '1.7'
+version = '1.8'
 # The full version, including alpha/beta/rc tags.
-release = '1.7'
+release = '1.8'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.

pypy/doc/config/objspace.usemodules.pyexpat.txt

-Use (experimental) pyexpat module written in RPython, instead of CTypes
-version which is used by default.
+Use the pyexpat module, written in RPython.

pypy/doc/config/translation.log.txt

 
 These must be enabled by setting the PYPYLOG environment variable.
 The exact set of features supported by PYPYLOG is described in
-pypy/translation/c/src/debug.h.
+pypy/translation/c/src/debug_print.h.

pypy/doc/garbage_collection.rst

 So as a first approximation, when compared to the Hybrid GC, the
 Minimark GC saves one word of memory per old object.
 
-There are a number of environment variables that can be tweaked to
-influence the GC.  (Their default value should be ok for most usages.)
-You can read more about them at the start of
-`pypy/rpython/memory/gc/minimark.py`_.
+There are :ref:`a number of environment variables
+<minimark-environment-variables>` that can be tweaked to influence the
+GC.  (Their default value should be ok for most usages.)
 
 In more detail:
 
   are preserved.  If the object dies then the pre-reserved location
   becomes free garbage, to be collected at the next major collection.
 
-
 .. include:: _ref.txt

pypy/doc/gc_info.rst

+Garbage collector configuration
+===============================
+
+.. _minimark-environment-variables:
+
+Minimark
+--------
+
+PyPy's default ``minimark`` garbage collector is configurable through
+several environment variables:
+
+``PYPY_GC_NURSERY``
+    The nursery size.
+    Defaults to ``4MB``.
+    Small values (like 1 or 1KB) are useful for debugging.
+
+``PYPY_GC_MAJOR_COLLECT``
+    Major collection memory factor.
+    Default is ``1.82``, which means trigger a major collection when the
+    memory consumed equals 1.82 times the memory really used at the end
+    of the previous major collection.
+
+``PYPY_GC_GROWTH``
+    Major collection threshold's max growth rate.
+    Default is ``1.4``.
+    Useful to collect more often than normally on sudden memory growth,
+    e.g. when there is a temporary peak in memory usage.
+
+``PYPY_GC_MAX``
+    The max heap size.
+    If coming near this limit, it will first collect more often, then
+    raise an RPython MemoryError, and if that is not enough, crash the
+    program with a fatal error.
+    Try values like ``1.6GB``.
+
+``PYPY_GC_MAX_DELTA``
+    The major collection threshold will never be set to more than
+    ``PYPY_GC_MAX_DELTA`` the amount really used after a collection.
+    Defaults to 1/8th of the total RAM size (which is constrained to be
+    at most 2/3/4GB on 32-bit systems).
+    Try values like ``200MB``.
+
+``PYPY_GC_MIN``
+    Don't collect while the memory size is below this limit.
+    Useful to avoid spending all the time in the GC in very small
+    programs.
+    Defaults to 8 times the nursery.
+
+``PYPY_GC_DEBUG``
+    Enable extra checks around collections that are too slow for normal
+    use.
+    Values are ``0`` (off), ``1`` (on major collections) or ``2`` (also
+    on minor collections).

pypy/doc/getting-started-python.rst

 executable. The executable behaves mostly like a normal Python interpreter::
 
     $ ./pypy-c
-    Python 2.7.0 (61ef2a11b56a, Mar 02 2011, 03:00:11)
-    [PyPy 1.6.0 with GCC 4.4.3] on linux2
+    Python 2.7.2 (0e28b379d8b3, Feb 09 2012, 19:41:03)
+    [PyPy 1.8.0 with GCC 4.4.3] on linux2
     Type "help", "copyright", "credits" or "license" for more information.
     And now for something completely different: ``this sentence is false''
     >>>> 46 - 4
     42
     >>>> from test import pystone
     >>>> pystone.main()
-    Pystone(1.1) time for 50000 passes = 0.280017
-    This machine benchmarks at 178561 pystones/second
-    >>>>
+    Pystone(1.1) time for 50000 passes = 0.220015
+    This machine benchmarks at 227257 pystones/second
+    >>>> pystone.main()
+    Pystone(1.1) time for 50000 passes = 0.060004
+    This machine benchmarks at 833278 pystones/second
+    >>>> 
 
+Note that pystone gets faster as the JIT kicks in.
 This executable can be moved around or copied on other machines; see
 Installation_ below.
 

pypy/doc/getting-started.rst

 PyPy is ready to be executed as soon as you unpack the tarball or the zip
 file, with no need to install it in any specific location::
 
-    $ tar xf pypy-1.7-linux.tar.bz2
-
-    $ ./pypy-1.7/bin/pypy
-    Python 2.7.1 (?, Apr 27 2011, 12:44:21)
-    [PyPy 1.7.0 with GCC 4.4.3] on linux2
+    $ tar xf pypy-1.8-linux.tar.bz2
+    $ ./pypy-1.8/bin/pypy
+    Python 2.7.2 (0e28b379d8b3, Feb 09 2012, 19:41:03)
+    [PyPy 1.8.0 with GCC 4.4.3] on linux2
     Type "help", "copyright", "credits" or "license" for more information.
-    And now for something completely different: ``implementing LOGO in LOGO:
-    "turtles all the way down"''
+    And now for something completely different: ``it seems to me that once you
+    settle on an execution / object model and / or bytecode format, you've already
+    decided what languages (where the 's' seems superfluous) support is going to be
+    first class for''
     >>>>
 
 If you want to make PyPy available system-wide, you can put a symlink to the
 
     $ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
 
-    $ ./pypy-1.7/bin/pypy distribute_setup.py
+    $ ./pypy-1.8/bin/pypy distribute_setup.py
 
-    $ ./pypy-1.7/bin/pypy get-pip.py
+    $ ./pypy-1.8/bin/pypy get-pip.py
 
-    $ ./pypy-1.7/bin/pip install pygments  # for example
+    $ ./pypy-1.8/bin/pip install pygments  # for example
 
-3rd party libraries will be installed in ``pypy-1.7/site-packages``, and
-the scripts in ``pypy-1.7/bin``.
+3rd party libraries will be installed in ``pypy-1.8/site-packages``, and
+the scripts in ``pypy-1.8/bin``.
 
 Installing using virtualenv
 ---------------------------

pypy/doc/index.rst

 
 * `FAQ`_: some frequently asked questions.
 
-* `Release 1.7`_: the latest official release
+* `Release 1.8`_: the latest official release
 
 * `PyPy Blog`_: news and status info about PyPy 
 
 .. _`Getting Started`: getting-started.html
 .. _`Papers`: extradoc.html
 .. _`Videos`: video-index.html
-.. _`Release 1.7`: http://pypy.org/download.html
+.. _`Release 1.8`: http://pypy.org/download.html
 .. _`speed.pypy.org`: http://speed.pypy.org
 .. _`RPython toolchain`: translation.html
 .. _`potential project ideas`: project-ideas.html
 Windows, on top of .NET, and on top of Java.
 To dig into PyPy it is recommended to try out the current
 Mercurial default branch, which is always working or mostly working,
-instead of the latest release, which is `1.7`__.
+instead of the latest release, which is `1.8`__.
 
-.. __: release-1.7.0.html
+.. __: release-1.8.0.html
 
 PyPy is mainly developed on Linux and Mac OS X.  Windows is supported,
 but platform-specific bugs tend to take longer before we notice and fix
    getting-started-dev.rst
    windows.rst
    faq.rst
+   commandline_ref.rst
    architecture.rst
    coding-guide.rst
    cpython_differences.rst
    garbage_collection.rst
+   gc_info.rst
    interpreter.rst
    objspace.rst
    __pypy__-module.rst

pypy/doc/jit-hooks.rst

+JIT hooks in PyPy
+=================
+
+There are several hooks in the `pypyjit` module that may help you with
+understanding what's pypy's JIT doing while running your program. There
+are three functions related to that coming from the `pypyjit` module:
+
+* `set_optimize_hook`::
+
+    Set a compiling hook that will be called each time a loop is optimized,
+    but before assembler compilation. This allows to add additional
+    optimizations on Python level.
+    
+    The hook will be called with the following signature:
+    hook(jitdriver_name, loop_type, greenkey or guard_number, operations)
+
+    jitdriver_name is the name of this particular jitdriver, 'pypyjit' is
+    the main interpreter loop
+
+    loop_type can be either `loop` `entry_bridge` or `bridge`
+    in case loop is not `bridge`, greenkey will be a tuple of constants
+    or a string describing it.
+
+    for the interpreter loop` it'll be a tuple
+    (code, offset, is_being_profiled)
+
+    Note that jit hook is not reentrant. It means that if the code
+    inside the jit hook is itself jitted, it will get compiled, but the
+    jit hook won't be called for that.
+
+    Result value will be the resulting list of operations, or None
+
+* `set_compile_hook`::
+
+    Set a compiling hook that will be called each time a loop is compiled.
+    The hook will be called with the following signature:
+    hook(jitdriver_name, loop_type, greenkey or guard_number, operations,
+         assembler_addr, assembler_length)
+
+    jitdriver_name is the name of this particular jitdriver, 'pypyjit' is
+    the main interpreter loop
+
+    loop_type can be either `loop` `entry_bridge` or `bridge`
+    in case loop is not `bridge`, greenkey will be a tuple of constants
+    or a string describing it.
+
+    for the interpreter loop` it'll be a tuple
+    (code, offset, is_being_profiled)
+
+    assembler_addr is an integer describing where assembler starts,
+    can be accessed via ctypes, assembler_lenght is the lenght of compiled
+    asm
+
+    Note that jit hook is not reentrant. It means that if the code
+    inside the jit hook is itself jitted, it will get compiled, but the
+    jit hook won't be called for that.
+
+* `set_abort_hook`::
+
+    Set a hook (callable) that will be called each time there is tracing
+    aborted due to some reason.
+
+    The hook will be called as in: hook(jitdriver_name, greenkey, reason)
+
+    Where reason is the reason for abort, see documentation for set_compile_hook
+    for descriptions of other arguments.

pypy/doc/jit/index.rst

 
 - Notes_ about the current work in PyPy
 
+- Hooks_ debugging facilities available to a python programmer
+
 
 .. _Overview: overview.html
 .. _Notes: pyjitpl5.html
+.. _Hooks: ../jit-hooks.html

pypy/doc/man/pypy.1.rst

 -S
     Do not ``import site`` on initialization.
 
+-s
+    Don't add the user site directory to `sys.path`.
+
 -u
     Unbuffered binary ``stdout`` and ``stderr``.
 
 -E
     Ignore environment variables (such as ``PYTHONPATH``).
 
+-B
+    Disable writing bytecode (``.pyc``) files.
+
 --version
     Print the PyPy version.
 
         Optimizations to enabled or ``all``.
         Warning, this option is dangerous, and should be avoided.
 
+ENVIRONMENT
+===========
+
+``PYTHONPATH``
+    Add directories to pypy's module search path.
+    The format is the same as shell's ``PATH``.
+
+``PYTHONSTARTUP``
+    A script referenced by this variable will be executed before the
+    first prompt is displayed, in interactive mode.
+
+``PYTHONDONTWRITEBYTECODE``
+    If set to a non-empty value, equivalent to the ``-B`` option.
+    Disable writing ``.pyc`` files.
+
+``PYTHONINSPECT``
+    If set to a non-empty value, equivalent to the ``-i`` option.
+    Inspect interactively after running the specified script.
+
+``PYTHONIOENCODING``
+    If this is set, it overrides the encoding used for
+    *stdin*/*stdout*/*stderr*.
+    The syntax is *encodingname*:*errorhandler*
+    The *errorhandler* part is optional and has the same meaning as in
+    `str.encode`.
+
+``PYTHONNOUSERSITE``
+    If set to a non-empty value, equivalent to the ``-s`` option.
+    Don't add the user site directory to `sys.path`.
+
+``PYTHONWARNINGS``
+    If set, equivalent to the ``-W`` option (warning control).
+    The value should be a comma-separated list of ``-W`` parameters.
+
+``PYPYLOG``
+    If set to a non-empty value, enable logging, the format is:
+
+    *fname*
+        logging for profiling: includes all
+        ``debug_start``/``debug_stop`` but not any nested
+        ``debug_print``.
+        *fname* can be ``-`` to log to *stderr*.
+
+    ``:``\ *fname*
+        Full logging, including ``debug_print``.
+
+    *prefix*\ ``:``\ *fname*
+        Conditional logging.
+        Multiple prefixes can be specified, comma-separated.
+        Only sections whose name match the prefix will be logged.
+
+    ``PYPYLOG``\ =\ ``jit-log-opt,jit-backend:``\ *logfile* will
+    generate a log suitable for *jitviewer*, a tool for debugging
+    performance issues under PyPy.
+
+.. include:: ../gc_info.rst
+   :start-line: 7
+
 SEE ALSO
 ========
 

pypy/doc/release-1.8.0.rst

 ============================
-PyPy 1.7 - business as usual
+PyPy 1.8 - business as usual
 ============================
 
-We're pleased to announce the 1.8 release of PyPy. As became a habit, this
-release brings a lot of bugfixes, performance and memory improvements over
-the 1.7 release. The main highlight of the release is the introduction of
-list strategies which makes homogenous lists more efficient both in terms
-of performance and memory. Otherwise it's "business as usual" in the sense
-that performance improved roughly 10% on average since the previous release.
-You can download the PyPy 1.8 release here:
+We're pleased to announce the 1.8 release of PyPy. As habitual this
+release brings a lot of bugfixes, together with performance and memory
+improvements over the 1.7 release. The main highlight of the release
+is the introduction of `list strategies`_ which makes homogenous lists
+more efficient both in terms of performance and memory. This release
+also upgrades us from Python 2.7.1 compatibility to 2.7.2. Otherwise
+it's "business as usual" in the sense that performance improved
+roughly 10% on average since the previous release.
+
+you can download the PyPy 1.8 release here:
 
     http://pypy.org/download.html
 
+.. _`list strategies`: http://morepypy.blogspot.com/2011/10/more-compact-lists-with-list-strategies.html
+
 What is PyPy?
 =============
 
 due to its integrated tracing JIT compiler.
 
 This release supports x86 machines running Linux 32/64, Mac OS X 32/64 or
-Windows 32. Windows 64 work is ongoing, but not yet natively supported.
+Windows 32. Windows 64 work has been stalled, we would welcome a volunteer
+to handle that.
 
 .. _`pypy 1.8 and cpython 2.7.1`: http://speed.pypy.org
 
   the JIT performance in places that use such lists. There are also special
   strategies for unicode and string lists.
 
-* As usual, numerous performance improvements. There are too many examples
-  which python constructs now should behave faster to list them.
+* As usual, numerous performance improvements. There are many examples
+  of python constructs that now should be faster; too many to list them.
 
 * Bugfixes and compatibility fixes with CPython.
 
 * Windows fixes.
 
-* NumPy effort progress, for the exact list of things that have been done,
+* NumPy effort progress; for the exact list of things that have been done,
   consult the `numpy status page`_. A tentative list of things that has
   been done:
 
-  xxxx # list it, multidim arrays in particular
+  * multi dimensional arrays
 
-* Fundraising XXX
+  * various sizes of dtypes
 
-.. _`numpy status page`: xxx
-.. _`numpy status update blog report`: xxx
+  * a lot of ufuncs
+
+  * a lot of other minor changes
+
+  Right now the `numpy` module is available under both `numpy` and `numpypy`
+  names. However, because it's incomplete, you have to `import numpypy` first
+  before doing any imports from `numpy`.
+
+* New JIT hooks that allow you to hook into the JIT process from your python
+  program. There is a `brief overview`_ of what they offer.
+
+* Standard library upgrade from 2.7.1 to 2.7.2.
+
+Ongoing work
+============
+
+As usual, there is quite a bit of ongoing work that either didn't make it to
+the release or is not ready yet. Highlights include:
+
+* Non-x86 backends for the JIT: ARMv7 (almost ready) and PPC64 (in progress)
+
+* Specialized type instances - allocate instances as efficient as C structs,
+  including type specialization
+
+* More numpy work
+
+* Since the last release there was a significant breakthrough in PyPy's
+  fundraising. We now have enough funds to work on first stages of `numpypy`_
+  and `py3k`_. We would like to thank again to everyone who donated.
+
+* It's also probably worth noting, we're considering donations for the
+  Software Transactional Memory project. You can read more about `our plans`_
+
+Cheers,
+The PyPy Team
+
+.. _`brief overview`: http://doc.pypy.org/en/latest/jit-hooks.html
+.. _`numpy status page`: http://buildbot.pypy.org/numpy-status/latest.html
+.. _`numpy status update blog report`: http://morepypy.blogspot.com/2012/01/numpypy-status-update.html
+.. _`numpypy`: http://pypy.org/numpydonate.html
+.. _`py3k`: http://pypy.org/py3donate.html
+.. _`our plans`: http://morepypy.blogspot.com/2012/01/transactional-memory-ii.html

pypy/interpreter/astcompiler/optimize.py

                     # narrow builds will return a surrogate.  In both
                     # the cases skip the optimization in order to
                     # produce compatible pycs.
-                    if (self.space.isinstance_w(w_obj, self.space.w_unicode)
-                        and 
+                    if (self.space.isinstance_w(w_obj, self.space.w_unicode) and
                         self.space.isinstance_w(w_const, self.space.w_unicode)):
                         unistr = self.space.unicode_w(w_const)
                         if len(unistr) == 1:
                         else:
                             ch = 0
                         if (ch > 0xFFFF or
-                            (MAXUNICODE == 0xFFFF and 0xD800 <= ch <= 0xDFFFF)):
+                            (MAXUNICODE == 0xFFFF and 0xD800 <= ch <= 0xDFFF)):
                             return subs
 
                     return ast.Const(w_const, subs.lineno, subs.col_offset)

pypy/interpreter/astcompiler/test/test_compiler.py

         # Just checking this doesn't crash out
         self.count_instructions(source)
 
-    def test_const_fold_unicode_subscr(self):
+    def test_const_fold_unicode_subscr(self, monkeypatch):
         source = """def f():
         return u"abc"[0]
         """
         assert counts == {ops.LOAD_CONST: 2, ops.BINARY_SUBSCR: 1,
                           ops.RETURN_VALUE: 1}
 
+        monkeypatch.setattr(optimize, "MAXUNICODE", 0xFFFF)
+        source = """def f():
+        return u"\uE01F"[0]
+        """
+        counts = self.count_instructions(source)
+        assert counts == {ops.LOAD_