Commits

mattip committed 7be0c69 Merge

merge default into branch

Comments (0)

Files changed (16)

lib_pypy/pyrepl/readline.py

         try:
             return unicode(line, ENCODING)
         except UnicodeDecodeError:   # bah, silently fall back...
-            return unicode(line, 'utf-8')
+            return unicode(line, 'utf-8', 'replace')
 
     def get_history_length(self):
         return self.saved_history_length

lib_pypy/pyrepl/unix_console.py

             if iscode:
                 self.__tputs(text)
             else:
-                os.write(self.output_fd, text.encode(self.encoding))
+                os.write(self.output_fd, text.encode(self.encoding, 'replace'))
         del self.__buffer[:]
 
     def __tputs(self, fmt, prog=delayprog):

pypy/annotation/annrpython.py

                         if cell.is_constant():
                             newcell.const = cell.const
                         cell = newcell
-                        cell.knowntypedata = renamed_knowntypedata
+                        cell.set_knowntypedata(renamed_knowntypedata)
 
                     cells.append(cell)
 

pypy/annotation/binaryop.py

         # XXX HACK HACK HACK
         bk = getbookkeeper()
         if bk is not None: # for testing
-            knowntypedata = r.knowntypedata = {}
+            knowntypedata = {}
             fn, block, i = bk.position_key
 
             annotator = bk.annotator
 
             bind(obj2, obj1, 0)
             bind(obj1, obj2, 1)
+            r.set_knowntypedata(knowntypedata)
 
         return r
 
             case = opname in ('gt', 'ge', 'eq')
             add_knowntypedata(knowntypedata, case, [op.args[0]],
                               SomeInteger(nonneg=True, knowntype=tointtype(int1)))
-        if knowntypedata:
-            r.knowntypedata = knowntypedata
+        r.set_knowntypedata(knowntypedata)
         # a special case for 'x < 0' or 'x >= 0',
         # where 0 is a flow graph Constant
         # (in this case we are sure that it cannot become a r_uint later)
         if hasattr(boo1, 'knowntypedata') and \
            hasattr(boo2, 'knowntypedata'):
             ktd = merge_knowntypedata(boo1.knowntypedata, boo2.knowntypedata)
-            if ktd:
-                s.knowntypedata = ktd
+            s.set_knowntypedata(ktd)
         return s 
 
     def and_((boo1, boo2)):

pypy/annotation/builtin.py

             variables = [op.args[1]]
         for variable in variables:
             assert bk.annotator.binding(variable) == s_obj
-        r.knowntypedata = {}
-        
+        knowntypedata = {}
         if not hasattr(typ, '_freeze_') and isinstance(s_type, SomePBC):
-            add_knowntypedata(r.knowntypedata, True, variables, bk.valueoftype(typ))
+            add_knowntypedata(knowntypedata, True, variables, bk.valueoftype(typ))
+        r.set_knowntypedata(knowntypedata)
     return r
 
 # note that this one either needs to be constant, or we will create SomeObject

pypy/annotation/model.py

     unsigned = False
     def __init__(self):
         pass
+    def set_knowntypedata(self, knowntypedata):
+        assert not hasattr(self, 'knowntypedata')
+        if knowntypedata:
+            self.knowntypedata = knowntypedata
 
 class SomeStringOrUnicode(SomeObject):
     immutable = True

pypy/annotation/unaryop.py

         s_obj.is_true_behavior(r)
 
         bk = getbookkeeper()
-        knowntypedata = r.knowntypedata = {}
+        knowntypedata = {}
         fn, block, i = bk.position_key
         op = block.operations[i]
         assert op.opname == "is_true" or op.opname == "nonzero"
         if s_obj.can_be_none():
             s_nonnone_obj = s_obj.nonnoneify()
         add_knowntypedata(knowntypedata, True, [arg], s_nonnone_obj)
+        r.set_knowntypedata(knowntypedata)
         return r
-        
 
     def nonzero(obj):
         return obj.is_true()

pypy/config/translationoption.py

                          ("translation.gcrootfinder", DEFL_ROOTFINDER_WITHJIT),
                          ("translation.list_comprehension_operations", True)]),
     ChoiceOption("jit_backend", "choose the backend for the JIT",
-                 ["auto", "x86", "x86-without-sse2", "llvm", 'arm'],
+                 ["auto", "x86", "x86-without-sse2", 'arm'],
                  default="auto", cmdline="--jit-backend"),
     ChoiceOption("jit_profiler", "integrate profiler support into the JIT",
                  ["off", "oprofile"],

pypy/jit/backend/detect_cpu.py

         return "pypy.jit.backend.x86.runner", "CPU_X86_64"
     elif backend_name == 'cli':
         return "pypy.jit.backend.cli.runner", "CliCPU"
-    elif backend_name == 'llvm':
-        return "pypy.jit.backend.llvm.runner", "LLVMCPU"
     elif backend_name == 'arm':
         return "pypy.jit.backend.arm.runner", "CPU_ARM"
     elif backend_name == 'armhf':

pypy/module/_cffi_backend/ctypefunc.py

 
 import sys
 from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.interpreter.error import wrap_oserror
 from pypy.rpython.lltypesystem import lltype, llmemory, rffi
-from pypy.rlib import jit, clibffi, jit_libffi, rposix
+from pypy.rlib import jit, clibffi, jit_libffi
 from pypy.rlib.jit_libffi import CIF_DESCRIPTION, CIF_DESCRIPTION_P
 from pypy.rlib.jit_libffi import FFI_TYPE, FFI_TYPE_P, FFI_TYPE_PP
 from pypy.rlib.jit_libffi import SIZE_OF_FFI_ARG
                     if flag == 1:
                         raw_string = rffi.cast(rffi.CCHARPP, data)[0]
                         lltype.free(raw_string, flavor='raw')
-                    elif flag == 2:
-                        file = rffi.cast(rffi.CCHARPP, data)[0]
-                        rffi_fclose(file)
             lltype.free(buffer, flavor='raw')
         return w_res
 
     assert isinstance(abi, int)
     return space.wrap(abi)
 
-rffi_fdopen = rffi.llexternal("fdopen", [rffi.INT, rffi.CCHARP], rffi.CCHARP)
-rffi_fclose = rffi.llexternal("fclose", [rffi.CCHARP], rffi.INT)
-
-def prepare_file_call_argument(fileobj):
-    import os
-    space = fileobj.space
-    fileobj.direct_flush()
-    fd = fileobj.direct_fileno()
-    if fd < 0:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("file has no OS file descriptor"))
-    try:
-        fd2 = os.dup(fd)
-        f = rffi_fdopen(fd2, fileobj.mode)
-        if not f:
-            os.close(fd2)
-            raise OSError(rposix.get_errno(), "fdopen failed")
-    except OSError, e:
-        raise wrap_oserror(space, e)
-    return f
-
 # ____________________________________________________________
 
 

pypy/module/_cffi_backend/ctypeptr.py

 """
 
 from pypy.interpreter.error import OperationError, operationerrfmt
+from pypy.interpreter.error import wrap_oserror
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.rlib.objectmodel import keepalive_until_here
 from pypy.rlib.rarithmetic import ovfcheck
+from pypy.rlib import rposix
 
 from pypy.module._cffi_backend.ctypeobj import W_CType
 from pypy.module._cffi_backend import cdataobj, misc, ctypeprim
         p = rffi.ptradd(cdata, i * self.ctitem.size)
         return cdataobj.W_CData(space, p, self)
 
+    def cast(self, w_ob):
+        if self.is_file:
+            value = self.prepare_file(w_ob)
+            if value:
+                return cdataobj.W_CData(self.space, value, self)
+        return W_CTypePtrBase.cast(self, w_ob)
+
+    def prepare_file(self, w_ob):
+        from pypy.module._file.interp_file import W_File
+        from pypy.module._cffi_backend import ctypefunc
+        ob = self.space.interpclass_w(w_ob)
+        if isinstance(ob, W_File):
+            return prepare_file_argument(self.space, ob)
+        else:
+            return lltype.nullptr(rffi.CCHARP.TO)
+
     def _prepare_pointer_call_argument(self, w_init, cdata):
         space = self.space
         if (space.isinstance_w(w_init, space.w_list) or
             # from a string, we add the null terminator
             length = space.int_w(space.len(w_init)) + 1
         elif self.is_file:
-            from pypy.module._file.interp_file import W_File
-            from pypy.module._cffi_backend import ctypefunc
-            ob = space.interpclass_w(w_init)
-            if isinstance(ob, W_File):
-                result = ctypefunc.prepare_file_call_argument(ob)
+            result = self.prepare_file(w_init)
+            if result:
                 rffi.cast(rffi.CCHARPP, cdata)[0] = result
                 return 2
             return 0
         else:
             raise OperationError(space.w_TypeError,
                      space.wrap("expected a 'cdata struct-or-union' object"))
+
+# ____________________________________________________________
+
+
+rffi_fdopen = rffi.llexternal("fdopen", [rffi.INT, rffi.CCHARP], rffi.CCHARP)
+rffi_fclose = rffi.llexternal("fclose", [rffi.CCHARP], rffi.INT)
+
+class CffiFileObj(object):
+    _immutable_ = True
+    def __init__(self, fd, mode):
+        self.llf = rffi_fdopen(fd, mode)
+        if not self.llf:
+            raise OSError(rposix.get_errno(), "fdopen failed")
+    def close(self):
+        rffi_fclose(self.llf)
+
+def prepare_file_argument(space, fileobj):
+    fileobj.direct_flush()
+    if fileobj.cffi_fileobj is None:
+        fd = fileobj.direct_fileno()
+        if fd < 0:
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("file has no OS file descriptor"))
+        try:
+            fileobj.cffi_fileobj = CffiFileObj(fd, fileobj.mode)
+        except OSError, e:
+            raise wrap_oserror(space, e)
+    return fileobj.cffi_fileobj.llf

pypy/module/_cffi_backend/test/_backend_test_c.py

     e = py.test.raises(TypeError, fputs, b"hello world\n", fw1)
     assert str(e.value) == ("initializer for ctype 'struct NOT_FILE *' must "
                             "be a cdata pointer, not file")
+
+def test_FILE_object():
+    if sys.platform == "win32":
+        py.test.skip("testing FILE not implemented")
+    #
+    BFILE = new_struct_type("_IO_FILE")
+    BFILEP = new_pointer_type(BFILE)
+    BChar = new_primitive_type("char")
+    BCharP = new_pointer_type(BChar)
+    BInt = new_primitive_type("int")
+    BFunc = new_function_type((BCharP, BFILEP), BInt, False)
+    BFunc2 = new_function_type((BFILEP,), BInt, False)
+    ll = find_and_load_library('c')
+    fputs = ll.load_function(BFunc, "fputs")
+    fileno = ll.load_function(BFunc2, "fileno")
+    #
+    import posix
+    fdr, fdw = posix.pipe()
+    fw1 = posix.fdopen(fdw, 'wb', 256)
+    #
+    fw1p = cast(BFILEP, fw1)
+    fw1.write(b"X")
+    fw1.flush()
+    res = fputs(b"hello\n", fw1p)
+    assert res >= 0
+    res = fileno(fw1p)
+    assert res == fdw
+    fw1.close()
+    #
+    data = posix.read(fdr, 256)
+    assert data == b"Xhello\n"
+    posix.close(fdr)

pypy/module/_cffi_backend/test/test_ztranslation.py

 from pypy.objspace.fake.checkmodule import checkmodule
+from pypy.module._cffi_backend import ctypeptr
+from pypy.rpython.lltypesystem import lltype, rffi
 
 # side-effect: FORMAT_LONGDOUBLE must be built before test_checkmodule()
 from pypy.module._cffi_backend import misc
 
 
 def test_checkmodule():
-    checkmodule('_cffi_backend')
+    # prepare_file_argument() is not working without translating the _file
+    # module too
+    def dummy_prepare_file_argument(space, fileobj):
+        return lltype.nullptr(rffi.CCHARP.TO)
+    old = ctypeptr.prepare_file_argument
+    try:
+        ctypeptr.prepare_file_argument = dummy_prepare_file_argument
+        #
+        checkmodule('_cffi_backend')
+        #
+    finally:
+        ctypeptr.prepare_file_argument = old

pypy/module/_file/interp_file.py

     encoding = None
     errors   = None
     fd       = -1
+    cffi_fileobj = None    # pypy/module/_cffi_backend
 
     newlines = 0     # Updated when the stream is closed
 
                 del openstreams[stream]
             except KeyError:
                 pass
-            stream.close()
+            # close the stream.  If cffi_fileobj is None, we close the
+            # underlying fileno too.  Otherwise, we leave that to
+            # cffi_fileobj.close().
+            cffifo = self.cffi_fileobj
+            self.cffi_fileobj = None
+            stream.close1(cffifo is None)
+            if cffifo is not None:
+                cffifo.close()
 
     def direct_fileno(self):
         self.getstream()    # check if the file is still open

pypy/module/micronumpy/interp_ufuncs.py

         return interp_dtype.get_dtype_cache(space).builtin_dtypes[dtypenum]
 
 
+@jit.unroll_safe
 def find_unaryop_result_dtype(space, dt, promote_to_float=False,
     promote_bools=False, promote_to_largest=False, allow_complex=True):
     if promote_bools and (dt.kind == interp_dtype.BOOLLTR):

pypy/rlib/streamio.py

         return False
 
     def close(self):
+        self.close1(True)
+
+    def close1(self, closefileno):
         pass
 
     def peek(self):
             else:
                 data = data[n:]
 
-    def close(self):
-        os.close(self.fd)
+    def close1(self, closefileno):
+        if closefileno:
+            os.close(self.fd)
 
     if sys.platform == "win32":
         def truncate(self, size):
         size = os.fstat(self.fd).st_size
         self.mm = mmap.mmap(self.fd, size, access=self.access)
 
-    def close(self):
+    def close1(self, closefileno):
         self.mm.close()
-        os.close(self.fd)
+        if closefileno:
+            os.close(self.fd)
 
     def tell(self):
         return self.pos
     ("truncate", [r_longlong]),
     ("flush", []),
     ("flushable", []),
-    ("close", []),
+    ("close1", [int]),
     ("peek", []),
     ("try_to_find_file_descriptor", []),
     ("getnewlines", []),
     truncate   = PassThrough("truncate",  flush_buffers=True)
     flush      = PassThrough("flush",     flush_buffers=True)
     flushable  = PassThrough("flushable", flush_buffers=False)
-    close      = PassThrough("close",     flush_buffers=False)
+    close1     = PassThrough("close1",    flush_buffers=False)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)
 
     seek       = PassThrough("seek",     flush_buffers=True)
     truncate   = PassThrough("truncate", flush_buffers=True)
     flush      = PassThrough("flush",    flush_buffers=True)
-    close      = PassThrough("close",    flush_buffers=True)
+    close1     = PassThrough("close1",   flush_buffers=True)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)
 
 
     flush    = PassThrough("flush", flush_buffers=False)
     flushable= PassThrough("flushable", flush_buffers=False)
-    close    = PassThrough("close", flush_buffers=False)
+    close1   = PassThrough("close1", flush_buffers=False)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)
 
     truncate = PassThrough("truncate", flush_buffers=True)
     flush    = PassThrough("flush", flush_buffers=False)
     flushable= PassThrough("flushable", flush_buffers=False)
-    close    = PassThrough("close", flush_buffers=False)
+    close1   = PassThrough("close1", flush_buffers=False)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)
     
     truncate   = PassThrough("truncate",  flush_buffers=True)
     flush      = PassThrough("flush",     flush_buffers=True)
     flushable  = PassThrough("flushable", flush_buffers=False)
-    close      = PassThrough("close",     flush_buffers=False)
+    close1     = PassThrough("close1",    flush_buffers=False)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)
 
     truncate   = PassThrough("truncate",  flush_buffers=False)
     flush      = PassThrough("flush",     flush_buffers=False)
     flushable  = PassThrough("flushable", flush_buffers=False)
-    close      = PassThrough("close",     flush_buffers=False)
+    close1     = PassThrough("close1",    flush_buffers=False)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)
 
     peek       = PassThrough("peek",      flush_buffers=False)
     flush      = PassThrough("flush",     flush_buffers=False)
     flushable  = PassThrough("flushable", flush_buffers=False)
-    close      = PassThrough("close",     flush_buffers=False)
+    close1     = PassThrough("close1",    flush_buffers=False)
     write      = PassThrough("write",     flush_buffers=False)
     truncate   = PassThrough("truncate",  flush_buffers=False)
     getnewlines= PassThrough("getnewlines",flush_buffers=False)
     truncate   = PassThrough("truncate",  flush_buffers=False)
     flush      = PassThrough("flush",     flush_buffers=False)
     flushable  = PassThrough("flushable", flush_buffers=False)
-    close      = PassThrough("close",     flush_buffers=False)
+    close1     = PassThrough("close1",    flush_buffers=False)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)
 
     truncate   = PassThrough("truncate",  flush_buffers=False)
     flush      = PassThrough("flush",     flush_buffers=False)
     flushable  = PassThrough("flushable", flush_buffers=False)
-    close      = PassThrough("close",     flush_buffers=False)
+    close1     = PassThrough("close1",    flush_buffers=False)
     try_to_find_file_descriptor = PassThrough("try_to_find_file_descriptor",
                                               flush_buffers=False)