Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3.3
Changeset: r75226:f93c5b218a24
Date: 2015-01-02 12:21 +0100
http://bitbucket.org/pypy/pypy/changeset/f93c5b218a24/

Log:    Easy additions to cpyext, needed for _testcapi

diff --git a/pypy/module/cpyext/include/Python.h 
b/pypy/module/cpyext/include/Python.h
--- a/pypy/module/cpyext/include/Python.h
+++ b/pypy/module/cpyext/include/Python.h
@@ -86,6 +86,7 @@
 #include "object.h"
 #include "abstract.h"
 #include "pyport.h"
+#include "pymacro.h"
 #include "warnings.h"
 
 #include <stdarg.h>
diff --git a/pypy/module/cpyext/include/pymacro.h 
b/pypy/module/cpyext/include/pymacro.h
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/include/pymacro.h
@@ -0,0 +1,26 @@
+#ifndef Py_PYMACRO_H
+#define Py_PYMACRO_H
+
+/* Get the number of elements in a visible array
+
+   This does not work on pointers, or arrays declared as [], or function
+   parameters. With correct compiler support, such usage will cause a build
+   error (see Py_BUILD_ASSERT_EXPR).
+
+   Written by Rusty Russell, public domain, http://ccodearchive.net/
+
+   Requires at GCC 3.1+ */
+#if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
+    (((__GNUC__ == 3) && (__GNU_MINOR__ >= 1)) || (__GNUC__ >= 4)))
+/* Two gcc extensions.
+   &a[0] degrades to a pointer: a different type from an array */
+#define Py_ARRAY_LENGTH(array) \
+    (sizeof(array) / sizeof((array)[0]) \
+     + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
+                                                          
typeof(&(array)[0]))))
+#else
+#define Py_ARRAY_LENGTH(array) \
+    (sizeof(array) / sizeof((array)[0]))
+#endif
+
+#endif /* Py_PYMACRO_H */
diff --git a/pypy/module/cpyext/longobject.py b/pypy/module/cpyext/longobject.py
--- a/pypy/module/cpyext/longobject.py
+++ b/pypy/module/cpyext/longobject.py
@@ -84,6 +84,15 @@
     """
     return space.int_w(w_long)
 
+@cpython_api([PyObject], rffi.SIZE_T, error=-1)
+def PyLong_AsSize_t(space, w_long):
+    """Return a C size_t representation of of pylong.  pylong must be
+    an instance of PyLongObject.
+    
+    Raise OverflowError if the value of pylong is out of range for a
+    size_t."""
+    return space.uint_w(w_long)
+
 @cpython_api([PyObject], rffi.LONGLONG, error=-1)
 def PyLong_AsLongLong(space, w_long):
     """
diff --git a/pypy/module/cpyext/pythonrun.py b/pypy/module/cpyext/pythonrun.py
--- a/pypy/module/cpyext/pythonrun.py
+++ b/pypy/module/cpyext/pythonrun.py
@@ -1,6 +1,8 @@
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL
 from pypy.module.cpyext.state import State
+from pypy.module.cpyext.pyobject import PyObject
+from pypy.module.cpyext.pyerrors import PyErr_SetNone
 
 @cpython_api([], rffi.INT_real, error=CANNOT_FAIL)
 def Py_IsInitialized(space):
@@ -46,3 +48,8 @@
     except ValueError:
         return -1
     return 0
+
+@cpython_api([], PyObject, error=CANNOT_FAIL)
+def PyThread_exit_thread(space):
+    PyErr_SetNone(space, space.w_SystemExit)
+
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
@@ -1511,16 +1511,6 @@
     raise NotImplementedError
     
 
-@cpython_api([PyObject], rffi.SIZE_T, error=-1)
-def PyLong_AsSize_t(space, pylong):
-    """Return a C size_t representation of of pylong.  pylong must be
-    an instance of PyLongObject.
-    
-    Raise OverflowError if the value of pylong is out of range for a
-    size_t."""
-    raise NotImplementedError
-    
-
 @cpython_api([PyObject, rffi.CCHARP], rffi.INT_real, error=-1)
 def PyMapping_DelItemString(space, o, key):
     """Remove the mapping for object key from the object o. Return -1 on
diff --git a/pypy/module/cpyext/unicodeobject.py 
b/pypy/module/cpyext/unicodeobject.py
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -201,6 +201,19 @@
     assert isinstance(w_obj, unicodeobject.W_UnicodeObject)
     return space.len_w(w_obj)
 
+@cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL)
+def PyUnicode_GET_LENGTH(space, w_obj):
+    """Return the length of the Unicode string, in code points.
+    o has to be a Unicode object in the "canonical" representation
+    (not checked)."""
+    assert isinstance(w_obj, unicodeobject.W_UnicodeObject)
+    return space.len_w(w_obj)
+
+@cpython_api([PyObject], rffi.INT, error=CANNOT_FAIL)
+def PyUnicode_IS_READY(space, w_obj):
+    # PyPy is always ready.
+    return space.w_True
+
 @cpython_api([PyObject], rffi.CWCHARP, error=CANNOT_FAIL)
 def PyUnicode_AS_UNICODE(space, ref):
     """Return a pointer to the internal Py_UNICODE buffer of the object.  ref
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to