Author: Philip Jenvey <pjen...@underboss.org>
Branch: py3k
Changeset: r59271:078592628907
Date: 2012-12-02 15:14 -0800
http://bitbucket.org/pypy/pypy/changeset/078592628907/

Log:    merge

diff --git a/lib_pypy/_testcapimodule.c b/lib_pypy/_testcapimodule.c
--- a/lib_pypy/_testcapimodule.c
+++ b/lib_pypy/_testcapimodule.c
@@ -1,5 +1,4 @@
-/* Verbatim copy of Modules/_testcapimodule.c from CPython 3.2 w/ use
-   of PyInstanceMethod disabled */
+/* Verbatim copy of Modules/_testcapimodule.c from CPython 3.2 */
 /*
  * C Extension module to test Python interpreter C APIs.
  *
@@ -2626,10 +2625,8 @@
     PyModule_AddObject(m, "PY_SSIZE_T_MAX", 
PyLong_FromSsize_t(PY_SSIZE_T_MAX));
     PyModule_AddObject(m, "PY_SSIZE_T_MIN", 
PyLong_FromSsize_t(PY_SSIZE_T_MIN));
     PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", 
PyLong_FromSsize_t(sizeof(PyGC_Head)));
-#if 0 /* XXX: disabled for PyPy (for now) */
     Py_INCREF(&PyInstanceMethod_Type);
     PyModule_AddObject(m, "instancemethod", (PyObject 
*)&PyInstanceMethod_Type);
-#endif
 
     TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
     Py_INCREF(TestError);
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -650,8 +650,12 @@
         lambda space: init_capsule(),
     ])
     from pypy.module.posix.interp_posix import add_fork_hook
-    reinit_tls = rffi.llexternal('PyThread_ReInitTLS', [], lltype.Void,
-                                 compilation_info=eci)    
+    if we_are_translated():
+        reinit_tls = rffi.llexternal('PyThread_ReInitTLS', [], lltype.Void,
+                                     compilation_info=eci)
+    else:
+        reinit_tls = rffi.llexternal('PyPyThread_ReInitTLS', [], lltype.Void,
+                                     compilation_info=eci)
     add_fork_hook('child', reinit_tls)
 
 def init_function(func):
diff --git a/pypy/module/cpyext/buffer.py b/pypy/module/cpyext/buffer.py
--- a/pypy/module/cpyext/buffer.py
+++ b/pypy/module/cpyext/buffer.py
@@ -1,8 +1,8 @@
-from pypy.interpreter.error import OperationError
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import (
     cpython_api, CANNOT_FAIL, Py_buffer)
-from pypy.module.cpyext.pyobject import PyObject
+from pypy.module.cpyext.pyobject import PyObject, Py_DecRef
+from pypy.interpreter import buffer
 
 @cpython_api([lltype.Ptr(Py_buffer), lltype.Char], rffi.INT_real, 
error=CANNOT_FAIL)
 def PyBuffer_IsContiguous(space, view, fortran):
@@ -11,3 +11,27 @@
     (fortran is 'A').  Return 0 otherwise."""
     # PyPy only supports contiguous Py_buffers for now.
     return 1
+
+class CBufferMixin(object):
+    _mixin_ = True
+
+    def __init__(self, space, c_buf, c_len, w_obj):
+        self.space = space
+        self.c_buf = c_buf
+        self.c_len = c_len
+        self.w_obj = w_obj
+
+    def __del__(self):
+        Py_DecRef(self.space, self.w_obj)
+
+    def getlength(self):
+        return self.c_len
+
+    def getitem(self, index):
+        return self.c_buf[index]
+
+    def as_str(self):
+        return rffi.charpsize2str(self.c_buf, self.c_len)
+        
+class CBuffer(CBufferMixin, buffer.Buffer):
+    pass
diff --git a/pypy/module/cpyext/memoryobject.py 
b/pypy/module/cpyext/memoryobject.py
--- a/pypy/module/cpyext/memoryobject.py
+++ b/pypy/module/cpyext/memoryobject.py
@@ -1,6 +1,19 @@
-from pypy.module.cpyext.api import cpython_api
-from pypy.module.cpyext.pyobject import PyObject
+from pypy.module.cpyext.api import cpython_api, Py_buffer
+from pypy.module.cpyext.pyobject import PyObject, from_ref
+from pypy.module.cpyext.buffer import CBuffer
+from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.module.__builtin__.interp_memoryview import W_MemoryView
 
 @cpython_api([PyObject], PyObject)
 def PyMemoryView_FromObject(space, w_obj):
     return space.call_method(space.builtin, "memoryview", w_obj)
+
+@cpython_api([lltype.Ptr(Py_buffer)], PyObject)
+def PyMemoryView_FromBuffer(space, view):
+    """Create a memoryview object wrapping the given buffer structure view.
+    The memoryview object then owns the buffer represented by view, which
+    means you shouldn't try to call PyBuffer_Release() yourself: it
+    will be done on deallocation of the memoryview object."""
+    w_obj = from_ref(space, view.c_obj)
+    buf = CBuffer(space, view.c_buf, view.c_len, w_obj)
+    return space.wrap(W_MemoryView(space.wrap(buf)))
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -1610,15 +1610,6 @@
     raise NotImplementedError
     
 
-@cpython_api([Py_buffer], PyObject)
-def PyMemoryView_FromBuffer(space, view):
-    """Create a memoryview object wrapping the given buffer structure view.
-    The memoryview object then owns the buffer represented by view, which
-    means you shouldn't try to call PyBuffer_Release() yourself: it
-    will be done on deallocation of the memoryview object."""
-    raise NotImplementedError
-    
-
 @cpython_api([PyObject, rffi.INT_real, lltype.Char], PyObject)
 def PyMemoryView_GetContiguous(space, obj, buffertype, order):
     """Create a memoryview object to a contiguous chunk of memory (in either
@@ -1645,33 +1636,6 @@
     raise NotImplementedError
     
 
-@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
-def PyInstanceMethod_Check(space, o):
-    """Return true if o is an instance method object (has type
-    PyInstanceMethod_Type).  The parameter must not be NULL."""
-    raise NotImplementedError
-    
-
-@cpython_api([PyObject], PyObject)
-def PyInstanceMethod_New(space, func):
-    """Return a new instance method object, with func being any callable object
-    func is the function that will be called when the instance method is
-    called."""
-    raise NotImplementedError
-    
-
-@cpython_api([PyObject], PyObject)
-def PyInstanceMethod_Function(space, im):
-    """Return the function object associated with the instance method im."""
-    raise NotImplementedError
-    
-
-@cpython_api([PyObject], PyObject)
-def PyInstanceMethod_GET_FUNCTION(space, im):
-    """Macro version of PyInstanceMethod_Function() which avoids error 
checking."""
-    raise NotImplementedError
-    
-
 @cpython_api([], rffi.INT_real, error=-1)
 def PyMethod_ClearFreeList(space, ):
     """Clear the free list. Return the total number of freed items."""
diff --git a/pypy/module/cpyext/test/test_memoryobject.py 
b/pypy/module/cpyext/test/test_memoryobject.py
--- a/pypy/module/cpyext/test/test_memoryobject.py
+++ b/pypy/module/cpyext/test/test_memoryobject.py
@@ -1,5 +1,6 @@
 import py
 from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 
 class TestMemoryViewObject(BaseApiTest):
     def test_fromobject(self, space, api):
@@ -11,3 +12,29 @@
         w_view = api.PyMemoryView_FromObject(w_hello)
         w_bytes = space.call_method(w_view, "tobytes")
         assert space.unwrap(w_bytes) == "hello"
+
+class AppTestPyBuffer_FillInfo(AppTestCpythonExtensionBase):
+    def test_fillWithObject(self):
+        module = self.import_extension('foo', [
+                ("fillinfo", "METH_VARARGS",
+                 """
+                 Py_buffer buf;
+                 PyObject *str = PyBytes_FromString("hello, world.");
+                 PyObject *result;
+
+                 if (PyBuffer_FillInfo(&buf, str, PyBytes_AsString(str), 13,
+                                       0, 0)) {
+                     return NULL;
+                 }
+
+                 /* Get rid of our own reference to the object, but
+                  * the Py_buffer should still have a reference.
+                  */
+                 Py_DECREF(str);
+
+                 return PyMemoryView_FromBuffer(&buf);
+                 """)])
+        result = module.fillinfo()
+        assert b"hello, world." == result
+
+
diff --git a/pypy/module/imp/interp_imp.py b/pypy/module/imp/interp_imp.py
--- a/pypy/module/imp/interp_imp.py
+++ b/pypy/module/imp/interp_imp.py
@@ -73,14 +73,15 @@
     stream = find_info.stream
 
     if stream is not None:
-        # try to find the declared encoding
         encoding = None
-        firstline = stream.readline()
-        stream.seek(0, 0) # reset position
-        if firstline.startswith('#'):
-            encoding = pytokenizer.match_encoding_declaration(firstline)
-        if encoding is None:
-            encoding = unicodetype.getdefaultencoding(space)
+        if find_info.modtype == importing.PY_SOURCE:
+            # try to find the declared encoding
+            firstline = stream.readline()
+            stream.seek(0, 0) # reset position
+            if firstline.startswith('#'):
+                encoding = pytokenizer.match_encoding_declaration(firstline)
+            if encoding is None:
+                encoding = unicodetype.getdefaultencoding(space)
         #
         # in python2, both CPython and PyPy pass the filename to
         # open(). However, CPython 3 just passes the fd, so the returned file
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -50,7 +50,6 @@
 
         self.FrameClass = frame.build_frame(self)
         self.StringObjectCls = W_StringObject
-
         self.UnicodeObjectCls = W_UnicodeObject
 
         self._install_multimethods()
diff --git a/pypy/tool/pytest/objspace.py b/pypy/tool/pytest/objspace.py
--- a/pypy/tool/pytest/objspace.py
+++ b/pypy/tool/pytest/objspace.py
@@ -10,7 +10,7 @@
     """ helper for instantiating and caching space's for testing.
     """
     try:
-        config = make_config(option,**kwds)
+        config = make_config(option, **kwds)
     except ConflictConfigError as e:
         # this exception is typically only raised if a module is not available.
         # in this case the test should be skipped
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to