[pypy-commit] pypy vendor/stdlib-3.6: Fix v3.6.1 import so that the process described in stdlib-upgrade.txt works as intended

rlamy pypy.commits at gmail.com
Fri Aug 2 11:42:23 EDT 2019


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: vendor/stdlib-3.6
Changeset: r97050:058b99d6e81f
Date: 2019-08-02 16:41 +0100
http://bitbucket.org/pypy/pypy/changeset/058b99d6e81f/

Log:	Fix v3.6.1 import so that the process described in stdlib-
	upgrade.txt works as intended

diff too long, truncating to 2000 out of 5488 lines

diff --git a/lib-python/3/idlelib/AutoComplete.py b/lib-python/3/idlelib/autocomplete.py
rename from lib-python/3/idlelib/AutoComplete.py
rename to lib-python/3/idlelib/autocomplete.py
diff --git a/lib-python/3/idlelib/AutoExpand.py b/lib-python/3/idlelib/autoexpand.py
rename from lib-python/3/idlelib/AutoExpand.py
rename to lib-python/3/idlelib/autoexpand.py
diff --git a/lib-python/3/idlelib/CallTips.py b/lib-python/3/idlelib/calltips.py
rename from lib-python/3/idlelib/CallTips.py
rename to lib-python/3/idlelib/calltips.py
diff --git a/lib-python/3/idlelib/CodeContext.py b/lib-python/3/idlelib/codecontext.py
rename from lib-python/3/idlelib/CodeContext.py
rename to lib-python/3/idlelib/codecontext.py
diff --git a/lib-python/3/idlelib/configDialog.py b/lib-python/3/idlelib/configdialog.py
rename from lib-python/3/idlelib/configDialog.py
rename to lib-python/3/idlelib/configdialog.py
diff --git a/lib-python/3/idlelib/Debugger.py b/lib-python/3/idlelib/debugger.py
rename from lib-python/3/idlelib/Debugger.py
rename to lib-python/3/idlelib/debugger.py
diff --git a/lib-python/3/idlelib/Delegator.py b/lib-python/3/idlelib/delegator.py
rename from lib-python/3/idlelib/Delegator.py
rename to lib-python/3/idlelib/delegator.py
diff --git a/lib-python/3/idlelib/FileList.py b/lib-python/3/idlelib/filelist.py
rename from lib-python/3/idlelib/FileList.py
rename to lib-python/3/idlelib/filelist.py
diff --git a/lib-python/3/idlelib/HyperParser.py b/lib-python/3/idlelib/hyperparser.py
rename from lib-python/3/idlelib/HyperParser.py
rename to lib-python/3/idlelib/hyperparser.py
diff --git a/lib-python/3/idlelib/MultiCall.py b/lib-python/3/idlelib/multicall.py
rename from lib-python/3/idlelib/MultiCall.py
rename to lib-python/3/idlelib/multicall.py
diff --git a/lib-python/3/idlelib/ParenMatch.py b/lib-python/3/idlelib/parenmatch.py
rename from lib-python/3/idlelib/ParenMatch.py
rename to lib-python/3/idlelib/parenmatch.py
diff --git a/lib-python/3/idlelib/PathBrowser.py b/lib-python/3/idlelib/pathbrowser.py
rename from lib-python/3/idlelib/PathBrowser.py
rename to lib-python/3/idlelib/pathbrowser.py
diff --git a/lib-python/3/idlelib/Percolator.py b/lib-python/3/idlelib/percolator.py
rename from lib-python/3/idlelib/Percolator.py
rename to lib-python/3/idlelib/percolator.py
diff --git a/lib-python/3/idlelib/PyParse.py b/lib-python/3/idlelib/pyparse.py
rename from lib-python/3/idlelib/PyParse.py
rename to lib-python/3/idlelib/pyparse.py
diff --git a/lib-python/3/idlelib/PyShell.py b/lib-python/3/idlelib/pyshell.py
rename from lib-python/3/idlelib/PyShell.py
rename to lib-python/3/idlelib/pyshell.py
--- a/lib-python/3/idlelib/PyShell.py
+++ b/lib-python/3/idlelib/pyshell.py
@@ -5,15 +5,15 @@
 except ImportError:
     print("** IDLE can't import Tkinter.\n"
           "Your Python may not be configured for Tk. **", file=sys.__stderr__)
-    sys.exit(1)
+    raise SystemExit(1)
 import tkinter.messagebox as tkMessageBox
 if TkVersion < 8.5:
     root = Tk()  # otherwise create root in main
     root.withdraw()
     tkMessageBox.showerror("Idle Cannot Start",
-            "Idle requires tcl/tk 8.5+, not $s." % TkVersion,
+            "Idle requires tcl/tk 8.5+, not %s." % TkVersion,
             parent=root)
-    sys.exit(1)
+    raise SystemExit(1)
 
 from code import InteractiveInterpreter
 import getopt
diff --git a/lib-python/3/idlelib/ScrolledList.py b/lib-python/3/idlelib/scrolledlist.py
rename from lib-python/3/idlelib/ScrolledList.py
rename to lib-python/3/idlelib/scrolledlist.py
diff --git a/lib-python/3/idlelib/SearchEngine.py b/lib-python/3/idlelib/searchengine.py
rename from lib-python/3/idlelib/SearchEngine.py
rename to lib-python/3/idlelib/searchengine.py
diff --git a/lib-python/3/idlelib/StackViewer.py b/lib-python/3/idlelib/stackviewer.py
rename from lib-python/3/idlelib/StackViewer.py
rename to lib-python/3/idlelib/stackviewer.py
diff --git a/lib-python/3/idlelib/textView.py b/lib-python/3/idlelib/textview.py
rename from lib-python/3/idlelib/textView.py
rename to lib-python/3/idlelib/textview.py
diff --git a/lib-python/3/idlelib/ToolTip.py b/lib-python/3/idlelib/tooltip.py
rename from lib-python/3/idlelib/ToolTip.py
rename to lib-python/3/idlelib/tooltip.py
diff --git a/lib-python/3/idlelib/ZoomHeight.py b/lib-python/3/idlelib/zoomheight.py
rename from lib-python/3/idlelib/ZoomHeight.py
rename to lib-python/3/idlelib/zoomheight.py
diff --git a/lib-python/3/site-packages/README b/lib-python/3/site-packages/README
deleted file mode 100644
--- a/lib-python/3/site-packages/README
+++ /dev/null
@@ -1,2 +0,0 @@
-This directory exists so that 3rd party packages can be installed
-here.  Read the source for site.py for more details.
diff --git a/lib-python/3/test/mod_generics_cache.py b/lib-python/3/test/mod_generics_cache.py
new file mode 100644
--- /dev/null
+++ b/lib-python/3/test/mod_generics_cache.py
@@ -0,0 +1,14 @@
+"""Module for testing the behavior of generics across different modules."""
+
+from typing import TypeVar, Generic
+
+T = TypeVar('T')
+
+
+class A(Generic[T]):
+    pass
+
+
+class B(Generic[T]):
+    class A(Generic[T]):
+        pass
diff --git a/lib-python/3/test/mp_preload.py b/lib-python/3/test/mp_preload.py
new file mode 100644
--- /dev/null
+++ b/lib-python/3/test/mp_preload.py
@@ -0,0 +1,18 @@
+import multiprocessing
+
+multiprocessing.Lock()
+
+
+def f():
+    print("ok")
+
+
+if __name__ == "__main__":
+    ctx = multiprocessing.get_context("forkserver")
+    modname = "test.mp_preload"
+    # Make sure it's importable
+    __import__(modname)
+    ctx.set_forkserver_preload([modname])
+    proc = ctx.Process(target=f)
+    proc.start()
+    proc.join()
diff --git a/lib-python/3/test/regrtest.py b/lib-python/3/test/regrtest.py
old mode 100644
new mode 100755
diff --git a/lib-python/3/timeit.py b/lib-python/3/timeit.py
old mode 100644
new mode 100755
diff --git a/lib-python/3/venv/scripts/posix/activate b/lib-python/3/venv/scripts/common/activate
rename from lib-python/3/venv/scripts/posix/activate
rename to lib-python/3/venv/scripts/common/activate
diff --git a/lib_pypy/_ctypes_test.c b/lib_pypy/_ctypes_test.c
new file mode 100644
--- /dev/null
+++ b/lib_pypy/_ctypes_test.c
@@ -0,0 +1,687 @@
+#include <Python.h>
+
+#ifdef MS_WIN32
+#include <windows.h>
+#endif
+
+#if defined(MS_WIN32) || defined(__CYGWIN__)
+#define EXPORT(x) __declspec(dllexport) x
+#else
+#define EXPORT(x) x
+#endif
+
+/* some functions handy for testing */
+
+EXPORT(int)
+_testfunc_cbk_reg_int(int a, int b, int c, int d, int e,
+                      int (*func)(int, int, int, int, int))
+{
+    return func(a*a, b*b, c*c, d*d, e*e);
+}
+
+EXPORT(double)
+_testfunc_cbk_reg_double(double a, double b, double c, double d, double e,
+                         double (*func)(double, double, double, double, double))
+{
+    return func(a*a, b*b, c*c, d*d, e*e);
+}
+
+/*
+ * This structure should be the same as in test_callbacks.py and the
+ * method test_callback_large_struct. See issues 17310 and 20160: the
+ * structure must be larger than 8 bytes long.
+ */
+
+typedef struct {
+    unsigned long first;
+    unsigned long second;
+    unsigned long third;
+} Test;
+
+EXPORT(void)
+_testfunc_cbk_large_struct(Test in, void (*func)(Test))
+{
+    func(in);
+}
+
+/*
+ * See issue 29565. Update a structure passed by value;
+ * the caller should not see any change.
+ */
+
+EXPORT(void)
+_testfunc_large_struct_update_value(Test in)
+{
+    in.first = 0x0badf00d;
+    in.second = 0x0badf00d;
+    in.third = 0x0badf00d;
+}
+
+EXPORT(void)testfunc_array(int values[4])
+{
+    printf("testfunc_array %d %d %d %d\n",
+           values[0],
+           values[1],
+           values[2],
+           values[3]);
+}
+
+EXPORT(long double)testfunc_Ddd(double a, double b)
+{
+    long double result = (long double)(a * b);
+    printf("testfunc_Ddd(%p, %p)\n", &a, &b);
+    printf("testfunc_Ddd(%g, %g)\n", a, b);
+    return result;
+}
+
+EXPORT(long double)testfunc_DDD(long double a, long double b)
+{
+    long double result = a * b;
+    printf("testfunc_DDD(%p, %p)\n", &a, &b);
+    printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
+    return result;
+}
+
+EXPORT(int)testfunc_iii(int a, int b)
+{
+    int result = a * b;
+    printf("testfunc_iii(%p, %p)\n", &a, &b);
+    return result;
+}
+
+EXPORT(int)myprintf(char *fmt, ...)
+{
+    int result;
+    va_list argptr;
+    va_start(argptr, fmt);
+    result = vprintf(fmt, argptr);
+    va_end(argptr);
+    return result;
+}
+
+EXPORT(char *)my_strtok(char *token, const char *delim)
+{
+    return strtok(token, delim);
+}
+
+EXPORT(char *)my_strchr(const char *s, int c)
+{
+    return strchr(s, c);
+}
+
+
+EXPORT(double) my_sqrt(double a)
+{
+    return sqrt(a);
+}
+
+EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
+{
+    qsort(base, num, width, compare);
+}
+
+EXPORT(int *) _testfunc_ai8(int a[8])
+{
+    return a;
+}
+
+EXPORT(void) _testfunc_v(int a, int b, int *presult)
+{
+    *presult = a + b;
+}
+
+EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
+{
+/*      printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
+               b, h, i, l, f, d);
+*/
+    return (int)(b + h + i + l + f + d);
+}
+
+EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
+{
+/*      printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
+               b, h, i, l, f, d);
+*/
+    return (float)(b + h + i + l + f + d);
+}
+
+EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
+{
+/*      printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
+               b, h, i, l, f, d);
+*/
+    return (double)(b + h + i + l + f + d);
+}
+
+EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
+{
+/*      printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
+               b, h, i, l, f, d);
+*/
+    return (long double)(b + h + i + l + f + d);
+}
+
+EXPORT(char *) _testfunc_p_p(void *s)
+{
+    return (char *)s;
+}
+
+EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
+{
+    return argv[(*argcp)-1];
+}
+
+EXPORT(void *) get_strchr(void)
+{
+    return (void *)strchr;
+}
+
+EXPORT(char *) my_strdup(char *src)
+{
+    char *dst = (char *)malloc(strlen(src)+1);
+    if (!dst)
+        return NULL;
+    strcpy(dst, src);
+    return dst;
+}
+
+EXPORT(void)my_free(void *ptr)
+{
+    free(ptr);
+}
+
+#ifdef HAVE_WCHAR_H
+EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
+{
+    size_t len = wcslen(src);
+    wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
+    if (ptr == NULL)
+        return NULL;
+    memcpy(ptr, src, (len+1) * sizeof(wchar_t));
+    return ptr;
+}
+
+EXPORT(size_t) my_wcslen(wchar_t *src)
+{
+    return wcslen(src);
+}
+#endif
+
+#ifndef MS_WIN32
+# ifndef __stdcall
+#  define __stdcall /* */
+# endif
+#endif
+
+typedef struct {
+    int (*c)(int, int);
+    int (__stdcall *s)(int, int);
+} FUNCS;
+
+EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
+{
+    fp->c(1, 2);
+    fp->s(3, 4);
+    return 0;
+}
+
+EXPORT(int) _testfunc_deref_pointer(int *pi)
+{
+    return *pi;
+}
+
+#ifdef MS_WIN32
+EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
+{
+    piunk->lpVtbl->AddRef(piunk);
+    return piunk->lpVtbl->Release(piunk);
+}
+#endif
+
+EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
+{
+    int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+
+    return (*func)(table);
+}
+
+EXPORT(long long) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
+                                      double d, long long q)
+{
+    return (long long)(b + h + i + l + f + d + q);
+}
+
+EXPORT(long long) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
+{
+    return (long long)(b + h + i + l + f + d);
+}
+
+EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
+{
+    int sum = 0;
+    while (value != 0) {
+        sum += func(value);
+        value /= 2;
+    }
+    return sum;
+}
+
+EXPORT(long long) _testfunc_callback_q_qf(long long value,
+                                          long long (*func)(long long))
+{
+    long long sum = 0;
+
+    while (value != 0) {
+        sum += func(value);
+        value /= 2;
+    }
+    return sum;
+}
+
+typedef struct {
+    char *name;
+    char *value;
+} SPAM;
+
+typedef struct {
+    char *name;
+    int num_spams;
+    SPAM *spams;
+} EGG;
+
+SPAM my_spams[2] = {
+    { "name1", "value1" },
+    { "name2", "value2" },
+};
+
+EGG my_eggs[1] = {
+    { "first egg", 1, my_spams }
+};
+
+EXPORT(int) getSPAMANDEGGS(EGG **eggs)
+{
+    *eggs = my_eggs;
+    return 1;
+}
+
+typedef struct tagpoint {
+    int x;
+    int y;
+} point;
+
+EXPORT(int) _testfunc_byval(point in, point *pout)
+{
+    if (pout) {
+        pout->x = in.x;
+        pout->y = in.y;
+    }
+    return in.x + in.y;
+}
+
+EXPORT (int) an_integer = 42;
+
+EXPORT(int) get_an_integer(void)
+{
+    return an_integer;
+}
+
+EXPORT(double)
+integrate(double a, double b, double (*f)(double), long nstep)
+{
+    double x, sum=0.0, dx=(b-a)/(double)nstep;
+    for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
+        sum += f(x);
+    return sum/(double)nstep;
+}
+
+typedef struct {
+    void (*initialize)(void *(*)(int), void(*)(void *));
+} xxx_library;
+
+static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
+{
+    void *ptr;
+
+    printf("_xxx_init got %p %p\n", Xalloc, Xfree);
+    printf("calling\n");
+    ptr = Xalloc(32);
+    Xfree(ptr);
+    printf("calls done, ptr was %p\n", ptr);
+}
+
+xxx_library _xxx_lib = {
+    _xxx_init
+};
+
+EXPORT(xxx_library) *library_get(void)
+{
+    return &_xxx_lib;
+}
+
+#ifdef MS_WIN32
+/* See Don Box (german), pp 79ff. */
+EXPORT(void) GetString(BSTR *pbstr)
+{
+    *pbstr = SysAllocString(L"Goodbye!");
+}
+#endif
+
+/*
+ * Some do-nothing functions, for speed tests
+ */
+PyObject *py_func_si(PyObject *self, PyObject *args)
+{
+    char *name;
+    int i;
+    if (!PyArg_ParseTuple(args, "si", &name, &i))
+        return NULL;
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+EXPORT(void) _py_func_si(char *s, int i)
+{
+}
+
+PyObject *py_func(PyObject *self, PyObject *args)
+{
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+EXPORT(void) _py_func(void)
+{
+}
+
+EXPORT(long long) last_tf_arg_s;
+EXPORT(unsigned long long) last_tf_arg_u;
+
+struct BITS {
+    int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
+    short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
+};
+
+EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
+{
+    switch (name) {
+    case 'A': bits->A = value; break;
+    case 'B': bits->B = value; break;
+    case 'C': bits->C = value; break;
+    case 'D': bits->D = value; break;
+    case 'E': bits->E = value; break;
+    case 'F': bits->F = value; break;
+    case 'G': bits->G = value; break;
+    case 'H': bits->H = value; break;
+    case 'I': bits->I = value; break;
+
+    case 'M': bits->M = value; break;
+    case 'N': bits->N = value; break;
+    case 'O': bits->O = value; break;
+    case 'P': bits->P = value; break;
+    case 'Q': bits->Q = value; break;
+    case 'R': bits->R = value; break;
+    case 'S': bits->S = value; break;
+    }
+}
+
+EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
+{
+    switch (name) {
+    case 'A': return bits->A;
+    case 'B': return bits->B;
+    case 'C': return bits->C;
+    case 'D': return bits->D;
+    case 'E': return bits->E;
+    case 'F': return bits->F;
+    case 'G': return bits->G;
+    case 'H': return bits->H;
+    case 'I': return bits->I;
+
+    case 'M': return bits->M;
+    case 'N': return bits->N;
+    case 'O': return bits->O;
+    case 'P': return bits->P;
+    case 'Q': return bits->Q;
+    case 'R': return bits->R;
+    case 'S': return bits->S;
+    }
+    return 0;
+}
+
+static PyMethodDef module_methods[] = {
+/*      {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
+    {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
+*/
+    {"func_si", py_func_si, METH_VARARGS},
+    {"func", py_func, METH_NOARGS},
+    { NULL, NULL, 0, NULL},
+};
+
+#define S last_tf_arg_s = (long long)c
+#define U last_tf_arg_u = (unsigned long long)c
+
+EXPORT(signed char) tf_b(signed char c) { S; return c/3; }
+EXPORT(unsigned char) tf_B(unsigned char c) { U; return c/3; }
+EXPORT(short) tf_h(short c) { S; return c/3; }
+EXPORT(unsigned short) tf_H(unsigned short c) { U; return c/3; }
+EXPORT(int) tf_i(int c) { S; return c/3; }
+EXPORT(unsigned int) tf_I(unsigned int c) { U; return c/3; }
+EXPORT(long) tf_l(long c) { S; return c/3; }
+EXPORT(unsigned long) tf_L(unsigned long c) { U; return c/3; }
+EXPORT(long long) tf_q(long long c) { S; return c/3; }
+EXPORT(unsigned long long) tf_Q(unsigned long long c) { U; return c/3; }
+EXPORT(float) tf_f(float c) { S; return c/3; }
+EXPORT(double) tf_d(double c) { S; return c/3; }
+EXPORT(long double) tf_D(long double c) { S; return c/3; }
+
+#ifdef MS_WIN32
+EXPORT(signed char) __stdcall s_tf_b(signed char c) { S; return c/3; }
+EXPORT(unsigned char) __stdcall s_tf_B(unsigned char c) { U; return c/3; }
+EXPORT(short) __stdcall s_tf_h(short c) { S; return c/3; }
+EXPORT(unsigned short) __stdcall s_tf_H(unsigned short c) { U; return c/3; }
+EXPORT(int) __stdcall s_tf_i(int c) { S; return c/3; }
+EXPORT(unsigned int) __stdcall s_tf_I(unsigned int c) { U; return c/3; }
+EXPORT(long) __stdcall s_tf_l(long c) { S; return c/3; }
+EXPORT(unsigned long) __stdcall s_tf_L(unsigned long c) { U; return c/3; }
+EXPORT(long long) __stdcall s_tf_q(long long c) { S; return c/3; }
+EXPORT(unsigned long long) __stdcall s_tf_Q(unsigned long long c) { U; return c/3; }
+EXPORT(float) __stdcall s_tf_f(float c) { S; return c/3; }
+EXPORT(double) __stdcall s_tf_d(double c) { S; return c/3; }
+EXPORT(long double) __stdcall s_tf_D(long double c) { S; return c/3; }
+#endif
+/*******/
+
+EXPORT(signed char) tf_bb(signed char x, signed char c) { S; return c/3; }
+EXPORT(unsigned char) tf_bB(signed char x, unsigned char c) { U; return c/3; }
+EXPORT(short) tf_bh(signed char x, short c) { S; return c/3; }
+EXPORT(unsigned short) tf_bH(signed char x, unsigned short c) { U; return c/3; }
+EXPORT(int) tf_bi(signed char x, int c) { S; return c/3; }
+EXPORT(unsigned int) tf_bI(signed char x, unsigned int c) { U; return c/3; }
+EXPORT(long) tf_bl(signed char x, long c) { S; return c/3; }
+EXPORT(unsigned long) tf_bL(signed char x, unsigned long c) { U; return c/3; }
+EXPORT(long long) tf_bq(signed char x, long long c) { S; return c/3; }
+EXPORT(unsigned long long) tf_bQ(signed char x, unsigned long long c) { U; return c/3; }
+EXPORT(float) tf_bf(signed char x, float c) { S; return c/3; }
+EXPORT(double) tf_bd(signed char x, double c) { S; return c/3; }
+EXPORT(long double) tf_bD(signed char x, long double c) { S; return c/3; }
+EXPORT(void) tv_i(int c) { S; return; }
+
+#ifdef MS_WIN32
+EXPORT(signed char) __stdcall s_tf_bb(signed char x, signed char c) { S; return c/3; }
+EXPORT(unsigned char) __stdcall s_tf_bB(signed char x, unsigned char c) { U; return c/3; }
+EXPORT(short) __stdcall s_tf_bh(signed char x, short c) { S; return c/3; }
+EXPORT(unsigned short) __stdcall s_tf_bH(signed char x, unsigned short c) { U; return c/3; }
+EXPORT(int) __stdcall s_tf_bi(signed char x, int c) { S; return c/3; }
+EXPORT(unsigned int) __stdcall s_tf_bI(signed char x, unsigned int c) { U; return c/3; }
+EXPORT(long) __stdcall s_tf_bl(signed char x, long c) { S; return c/3; }
+EXPORT(unsigned long) __stdcall s_tf_bL(signed char x, unsigned long c) { U; return c/3; }
+EXPORT(long long) __stdcall s_tf_bq(signed char x, long long c) { S; return c/3; }
+EXPORT(unsigned long long) __stdcall s_tf_bQ(signed char x, unsigned long long c) { U; return c/3; }
+EXPORT(float) __stdcall s_tf_bf(signed char x, float c) { S; return c/3; }
+EXPORT(double) __stdcall s_tf_bd(signed char x, double c) { S; return c/3; }
+EXPORT(long double) __stdcall s_tf_bD(signed char x, long double c) { S; return c/3; }
+EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
+#endif
+
+/********/
+
+#ifndef MS_WIN32
+
+typedef struct {
+    long x;
+    long y;
+} POINT;
+
+typedef struct {
+    long left;
+    long top;
+    long right;
+    long bottom;
+} RECT;
+
+#endif
+
+EXPORT(int) PointInRect(RECT *prc, POINT pt)
+{
+    if (pt.x < prc->left)
+        return 0;
+    if (pt.x > prc->right)
+        return 0;
+    if (pt.y < prc->top)
+        return 0;
+    if (pt.y > prc->bottom)
+        return 0;
+    return 1;
+}
+
+EXPORT(long left = 10);
+EXPORT(long top = 20);
+EXPORT(long right = 30);
+EXPORT(long bottom = 40);
+
+EXPORT(RECT) ReturnRect(int i, RECT ar, RECT* br, POINT cp, RECT dr,
+                        RECT *er, POINT fp, RECT gr)
+{
+    /*Check input */
+    if (ar.left + br->left + dr.left + er->left + gr.left != left * 5)
+    {
+        ar.left = 100;
+        return ar;
+    }
+    if (ar.right + br->right + dr.right + er->right + gr.right != right * 5)
+    {
+        ar.right = 100;
+        return ar;
+    }
+    if (cp.x != fp.x)
+    {
+        ar.left = -100;
+    }
+    if (cp.y != fp.y)
+    {
+        ar.left = -200;
+    }
+    switch(i)
+    {
+    case 0:
+        return ar;
+        break;
+    case 1:
+        return dr;
+        break;
+    case 2:
+        return gr;
+        break;
+
+    }
+    return ar;
+}
+
+typedef struct {
+    short x;
+    short y;
+} S2H;
+
+EXPORT(S2H) ret_2h_func(S2H inp)
+{
+    inp.x *= 2;
+    inp.y *= 3;
+    return inp;
+}
+
+typedef struct {
+    int a, b, c, d, e, f, g, h;
+} S8I;
+
+EXPORT(S8I) ret_8i_func(S8I inp)
+{
+    inp.a *= 2;
+    inp.b *= 3;
+    inp.c *= 4;
+    inp.d *= 5;
+    inp.e *= 6;
+    inp.f *= 7;
+    inp.g *= 8;
+    inp.h *= 9;
+    return inp;
+}
+
+EXPORT(int) GetRectangle(int flag, RECT *prect)
+{
+    if (flag == 0)
+        return 0;
+    prect->left = (int)flag;
+    prect->top = (int)flag + 1;
+    prect->right = (int)flag + 2;
+    prect->bottom = (int)flag + 3;
+    return 1;
+}
+
+EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
+{
+    *pi += a;
+    *pj += b;
+}
+
+#ifdef MS_WIN32
+EXPORT(S2H) __stdcall s_ret_2h_func(S2H inp) { return ret_2h_func(inp); }
+EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
+#endif
+
+#ifdef MS_WIN32
+/* Should port this */
+#include <stdlib.h>
+#include <search.h>
+
+EXPORT (HRESULT) KeepObject(IUnknown *punk)
+{
+    static IUnknown *pobj;
+    if (punk)
+        punk->lpVtbl->AddRef(punk);
+    if (pobj)
+        pobj->lpVtbl->Release(pobj);
+    pobj = punk;
+    return S_OK;
+}
+
+#endif
+
+
+static struct PyModuleDef _ctypes_testmodule = {
+    PyModuleDef_HEAD_INIT,
+    "_ctypes_test",
+    NULL,
+    -1,
+    module_methods,
+    NULL,
+    NULL,
+    NULL,
+    NULL
+};
+
+PyMODINIT_FUNC
+PyInit__ctypes_test(void)
+{
+    return PyModule_Create(&_ctypes_testmodule);
+}
diff --git a/lib_pypy/_testcapimodule.c b/lib_pypy/_testcapimodule.c
new file mode 100644
--- /dev/null
+++ b/lib_pypy/_testcapimodule.c
@@ -0,0 +1,4649 @@
+/*
+ * C Extension module to test Python interpreter C APIs.
+ *
+ * The 'test_*' functions exported by this module are run as part of the
+ * standard Python regression test, via Lib/test/test_capi.py.
+ */
+
+#define PY_SSIZE_T_CLEAN
+
+#include "Python.h"
+#include <float.h>
+#include "structmember.h"
+#include "datetime.h"
+#include "marshal.h"
+#include <signal.h>
+
+#ifdef MS_WINDOWS
+#  include <winsock2.h>         /* struct timeval */
+#endif
+
+#ifdef WITH_THREAD
+#include "pythread.h"
+#endif /* WITH_THREAD */
+static PyObject *TestError;     /* set to exception object in init */
+
+/* Raise TestError with test_name + ": " + msg, and return NULL. */
+
+static PyObject *
+raiseTestError(const char* test_name, const char* msg)
+{
+    PyErr_Format(TestError, "%s: %s", test_name, msg);
+    return NULL;
+}
+
+/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
+
+   The ones derived from autoconf on the UNIX-like OSes can be relied
+   upon (in the absence of sloppy cross-compiling), but the Windows
+   platforms have these hardcoded.  Better safe than sorry.
+*/
+static PyObject*
+sizeof_error(const char* fatname, const char* typname,
+    int expected, int got)
+{
+    PyErr_Format(TestError,
+        "%s #define == %d but sizeof(%s) == %d",
+        fatname, expected, typname, got);
+    return (PyObject*)NULL;
+}
+
+static PyObject*
+test_config(PyObject *self)
+{
+#define CHECK_SIZEOF(FATNAME, TYPE) \
+            if (FATNAME != sizeof(TYPE)) \
+                return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
+
+    CHECK_SIZEOF(SIZEOF_SHORT, short);
+    CHECK_SIZEOF(SIZEOF_INT, int);
+    CHECK_SIZEOF(SIZEOF_LONG, long);
+    CHECK_SIZEOF(SIZEOF_VOID_P, void*);
+    CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
+    CHECK_SIZEOF(SIZEOF_LONG_LONG, long long);
+
+#undef CHECK_SIZEOF
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+static PyObject*
+test_sizeof_c_types(PyObject *self)
+{
+#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wtype-limits"
+#endif
+#define CHECK_SIZEOF(TYPE, EXPECTED)         \
+    if (EXPECTED != sizeof(TYPE))  {         \
+        PyErr_Format(TestError,              \
+            "sizeof(%s) = %u instead of %u", \
+            #TYPE, sizeof(TYPE), EXPECTED);  \
+        return (PyObject*)NULL;              \
+    }
+#define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0)
+#define CHECK_SIGNNESS(TYPE, SIGNED)         \
+    if (IS_SIGNED(TYPE) != SIGNED) {         \
+        PyErr_Format(TestError,              \
+            "%s signness is, instead of %i",  \
+            #TYPE, IS_SIGNED(TYPE), SIGNED); \
+        return (PyObject*)NULL;              \
+    }
+
+    /* integer types */
+    CHECK_SIZEOF(Py_UCS1, 1);
+    CHECK_SIZEOF(Py_UCS2, 2);
+    CHECK_SIZEOF(Py_UCS4, 4);
+    CHECK_SIGNNESS(Py_UCS1, 0);
+    CHECK_SIGNNESS(Py_UCS2, 0);
+    CHECK_SIGNNESS(Py_UCS4, 0);
+    CHECK_SIZEOF(int32_t, 4);
+    CHECK_SIGNNESS(int32_t, 1);
+    CHECK_SIZEOF(uint32_t, 4);
+    CHECK_SIGNNESS(uint32_t, 0);
+    CHECK_SIZEOF(int64_t, 8);
+    CHECK_SIGNNESS(int64_t, 1);
+    CHECK_SIZEOF(uint64_t, 8);
+    CHECK_SIGNNESS(uint64_t, 0);
+
+    /* pointer/size types */
+    CHECK_SIZEOF(size_t, sizeof(void *));
+    CHECK_SIGNNESS(size_t, 0);
+    CHECK_SIZEOF(Py_ssize_t, sizeof(void *));
+    CHECK_SIGNNESS(Py_ssize_t, 1);
+
+    CHECK_SIZEOF(uintptr_t, sizeof(void *));
+    CHECK_SIGNNESS(uintptr_t, 0);
+    CHECK_SIZEOF(intptr_t, sizeof(void *));
+    CHECK_SIGNNESS(intptr_t, 1);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+
+#undef IS_SIGNED
+#undef CHECK_SIGNESS
+#undef CHECK_SIZEOF
+#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
+#pragma GCC diagnostic pop
+#endif
+}
+
+
+static PyObject*
+test_list_api(PyObject *self)
+{
+    PyObject* list;
+    int i;
+
+    /* SF bug 132008:  PyList_Reverse segfaults */
+#define NLIST 30
+    list = PyList_New(NLIST);
+    if (list == (PyObject*)NULL)
+        return (PyObject*)NULL;
+    /* list = range(NLIST) */
+    for (i = 0; i < NLIST; ++i) {
+        PyObject* anint = PyLong_FromLong(i);
+        if (anint == (PyObject*)NULL) {
+            Py_DECREF(list);
+            return (PyObject*)NULL;
+        }
+        PyList_SET_ITEM(list, i, anint);
+    }
+    /* list.reverse(), via PyList_Reverse() */
+    i = PyList_Reverse(list);   /* should not blow up! */
+    if (i != 0) {
+        Py_DECREF(list);
+        return (PyObject*)NULL;
+    }
+    /* Check that list == range(29, -1, -1) now */
+    for (i = 0; i < NLIST; ++i) {
+        PyObject* anint = PyList_GET_ITEM(list, i);
+        if (PyLong_AS_LONG(anint) != NLIST-1-i) {
+            PyErr_SetString(TestError,
+                            "test_list_api: reverse screwed up");
+            Py_DECREF(list);
+            return (PyObject*)NULL;
+        }
+    }
+    Py_DECREF(list);
+#undef NLIST
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+static int
+test_dict_inner(int count)
+{
+    Py_ssize_t pos = 0, iterations = 0;
+    int i;
+    PyObject *dict = PyDict_New();
+    PyObject *v, *k;
+
+    if (dict == NULL)
+        return -1;
+
+    for (i = 0; i < count; i++) {
+        v = PyLong_FromLong(i);
+        if (v == NULL) {
+            return -1;
+        }
+        if (PyDict_SetItem(dict, v, v) < 0) {
+            Py_DECREF(v);
+            return -1;
+        }
+        Py_DECREF(v);
+    }
+
+    while (PyDict_Next(dict, &pos, &k, &v)) {
+        PyObject *o;
+        iterations++;
+
+        i = PyLong_AS_LONG(v) + 1;
+        o = PyLong_FromLong(i);
+        if (o == NULL)
+            return -1;
+        if (PyDict_SetItem(dict, k, o) < 0) {
+            Py_DECREF(o);
+            return -1;
+        }
+        Py_DECREF(o);
+    }
+
+    Py_DECREF(dict);
+
+    if (iterations != count) {
+        PyErr_SetString(
+            TestError,
+            "test_dict_iteration: dict iteration went wrong ");
+        return -1;
+    } else {
+        return 0;
+    }
+}
+
+static PyObject*
+test_dict_iteration(PyObject* self)
+{
+    int i;
+
+    for (i = 0; i < 200; i++) {
+        if (test_dict_inner(i) < 0) {
+            return NULL;
+        }
+    }
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+static PyObject*
+dict_getitem_knownhash(PyObject *self, PyObject *args)
+{
+    PyObject *mp, *key, *result;
+    Py_ssize_t hash;
+
+    if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
+                          &mp, &key, &hash)) {
+        return NULL;
+    }
+
+    result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
+    if (result == NULL && !PyErr_Occurred()) {
+        _PyErr_SetKeyError(key);
+        return NULL;
+    }
+
+    Py_XINCREF(result);
+    return result;
+}
+
+static PyObject*
+dict_hassplittable(PyObject *self, PyObject *arg)
+{
+    if (!PyDict_Check(arg)) {
+        PyErr_Format(PyExc_TypeError,
+                     "dict_hassplittable() argument must be dict, not '%s'",
+                     arg->ob_type->tp_name);
+        return NULL;
+    }
+
+    return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg));
+}
+
+/* Issue #4701: Check that PyObject_Hash implicitly calls
+ *   PyType_Ready if it hasn't already been called
+ */
+static PyTypeObject _HashInheritanceTester_Type = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "hashinheritancetester",            /* Name of this type */
+    sizeof(PyObject),           /* Basic object size */
+    0,                          /* Item size for varobject */
+    (destructor)PyObject_Del, /* tp_dealloc */
+    0,                          /* tp_print */
+    0,                          /* tp_getattr */
+    0,                          /* tp_setattr */
+    0,                          /* tp_reserved */
+    0,                          /* tp_repr */
+    0,                          /* tp_as_number */
+    0,                          /* tp_as_sequence */
+    0,                          /* tp_as_mapping */
+    0,                          /* tp_hash */
+    0,                          /* tp_call */
+    0,                          /* tp_str */
+    PyObject_GenericGetAttr,  /* tp_getattro */
+    0,                          /* tp_setattro */
+    0,                          /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,         /* tp_flags */
+    0,                          /* tp_doc */
+    0,                          /* tp_traverse */
+    0,                          /* tp_clear */
+    0,                          /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    0,                          /* tp_iter */
+    0,                          /* tp_iternext */
+    0,                          /* tp_methods */
+    0,                          /* tp_members */
+    0,                          /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    0,                          /* tp_dictoffset */
+    0,                          /* tp_init */
+    0,                          /* tp_alloc */
+    PyType_GenericNew,                  /* tp_new */
+};
+
+static PyObject*
+test_lazy_hash_inheritance(PyObject* self)
+{
+    PyTypeObject *type;
+    PyObject *obj;
+    Py_hash_t hash;
+
+    type = &_HashInheritanceTester_Type;
+
+    if (type->tp_dict != NULL)
+        /* The type has already been initialized. This probably means
+           -R is being used. */
+        Py_RETURN_NONE;
+
+
+    obj = PyObject_New(PyObject, type);
+    if (obj == NULL) {
+        PyErr_Clear();
+        PyErr_SetString(
+            TestError,
+            "test_lazy_hash_inheritance: failed to create object");
+        return NULL;
+    }
+
+    if (type->tp_dict != NULL) {
+        PyErr_SetString(
+            TestError,
+            "test_lazy_hash_inheritance: type initialised too soon");
+        Py_DECREF(obj);
+        return NULL;
+    }
+
+    hash = PyObject_Hash(obj);
+    if ((hash == -1) && PyErr_Occurred()) {
+        PyErr_Clear();
+        PyErr_SetString(
+            TestError,
+            "test_lazy_hash_inheritance: could not hash object");
+        Py_DECREF(obj);
+        return NULL;
+    }
+
+    if (type->tp_dict == NULL) {
+        PyErr_SetString(
+            TestError,
+            "test_lazy_hash_inheritance: type not initialised by hash()");
+        Py_DECREF(obj);
+        return NULL;
+    }
+
+    if (type->tp_hash != PyType_Type.tp_hash) {
+        PyErr_SetString(
+            TestError,
+            "test_lazy_hash_inheritance: unexpected hash function");
+        Py_DECREF(obj);
+        return NULL;
+    }
+
+    Py_DECREF(obj);
+
+    Py_RETURN_NONE;
+}
+
+
+/* Tests of PyLong_{As, From}{Unsigned,}Long(), and
+   PyLong_{As, From}{Unsigned,}LongLong().
+
+   Note that the meat of the test is contained in testcapi_long.h.
+   This is revolting, but delicate code duplication is worse:  "almost
+   exactly the same" code is needed to test long long, but the ubiquitous
+   dependence on type names makes it impossible to use a parameterized
+   function.  A giant macro would be even worse than this.  A C++ template
+   would be perfect.
+
+   The "report an error" functions are deliberately not part of the #include
+   file:  if the test fails, you can set a breakpoint in the appropriate
+   error function directly, and crawl back from there in the debugger.
+*/
+
+#define UNBIND(X)  Py_DECREF(X); (X) = NULL
+
+static PyObject *
+raise_test_long_error(const char* msg)
+{
+    return raiseTestError("test_long_api", msg);
+}
+
+#define TESTNAME        test_long_api_inner
+#define TYPENAME        long
+#define F_S_TO_PY       PyLong_FromLong
+#define F_PY_TO_S       PyLong_AsLong
+#define F_U_TO_PY       PyLong_FromUnsignedLong
+#define F_PY_TO_U       PyLong_AsUnsignedLong
+
+#include "testcapi_long.h"
+
+static PyObject *
+test_long_api(PyObject* self)
+{
+    return TESTNAME(raise_test_long_error);
+}
+
+#undef TESTNAME
+#undef TYPENAME
+#undef F_S_TO_PY
+#undef F_PY_TO_S
+#undef F_U_TO_PY
+#undef F_PY_TO_U
+
+static PyObject *
+raise_test_longlong_error(const char* msg)
+{
+    return raiseTestError("test_longlong_api", msg);
+}
+
+#define TESTNAME        test_longlong_api_inner
+#define TYPENAME        long long
+#define F_S_TO_PY       PyLong_FromLongLong
+#define F_PY_TO_S       PyLong_AsLongLong
+#define F_U_TO_PY       PyLong_FromUnsignedLongLong
+#define F_PY_TO_U       PyLong_AsUnsignedLongLong
+
+#include "testcapi_long.h"
+
+static PyObject *
+test_longlong_api(PyObject* self, PyObject *args)
+{
+    return TESTNAME(raise_test_longlong_error);
+}
+
+#undef TESTNAME
+#undef TYPENAME
+#undef F_S_TO_PY
+#undef F_PY_TO_S
+#undef F_U_TO_PY
+#undef F_PY_TO_U
+
+/* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
+   is tested by test_long_api_inner. This test will concentrate on proper
+   handling of overflow.
+*/
+
+static PyObject *
+test_long_and_overflow(PyObject *self)
+{
+    PyObject *num, *one, *temp;
+    long value;
+    int overflow;
+
+    /* Test that overflow is set properly for a large value. */
+    /* num is a number larger than LONG_MAX even on 64-bit platforms */
+    num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != 1)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was not set to 1");
+
+    /* Same again, with num = LONG_MAX + 1 */
+    num = PyLong_FromLong(LONG_MAX);
+    if (num == NULL)
+        return NULL;
+    one = PyLong_FromLong(1L);
+    if (one == NULL) {
+        Py_DECREF(num);
+        return NULL;
+    }
+    temp = PyNumber_Add(num, one);
+    Py_DECREF(one);
+    Py_DECREF(num);
+    num = temp;
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != 1)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was not set to 1");
+
+    /* Test that overflow is set properly for a large negative value. */
+    /* num is a number smaller than LONG_MIN even on 64-bit platforms */
+    num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != -1)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was not set to -1");
+
+    /* Same again, with num = LONG_MIN - 1 */
+    num = PyLong_FromLong(LONG_MIN);
+    if (num == NULL)
+        return NULL;
+    one = PyLong_FromLong(1L);
+    if (one == NULL) {
+        Py_DECREF(num);
+        return NULL;
+    }
+    temp = PyNumber_Subtract(num, one);
+    Py_DECREF(one);
+    Py_DECREF(num);
+    num = temp;
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != -1)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was not set to -1");
+
+    /* Test that overflow is cleared properly for small values. */
+    num = PyLong_FromString("FF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != 0xFF)
+        return raiseTestError("test_long_and_overflow",
+            "expected return value 0xFF");
+    if (overflow != 0)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was not cleared");
+
+    num = PyLong_FromString("-FF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -0xFF)
+        return raiseTestError("test_long_and_overflow",
+            "expected return value 0xFF");
+    if (overflow != 0)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was set incorrectly");
+
+    num = PyLong_FromLong(LONG_MAX);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != LONG_MAX)
+        return raiseTestError("test_long_and_overflow",
+            "expected return value LONG_MAX");
+    if (overflow != 0)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was not cleared");
+
+    num = PyLong_FromLong(LONG_MIN);
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != LONG_MIN)
+        return raiseTestError("test_long_and_overflow",
+            "expected return value LONG_MIN");
+    if (overflow != 0)
+        return raiseTestError("test_long_and_overflow",
+            "overflow was not cleared");
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/* Test the PyLong_AsLongLongAndOverflow API. General conversion to
+   long long is tested by test_long_api_inner. This test will
+   concentrate on proper handling of overflow.
+*/
+
+static PyObject *
+test_long_long_and_overflow(PyObject *self)
+{
+    PyObject *num, *one, *temp;
+    long long value;
+    int overflow;
+
+    /* Test that overflow is set properly for a large value. */
+    /* num is a number larger than PY_LLONG_MAX on a typical machine. */
+    num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != 1)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was not set to 1");
+
+    /* Same again, with num = PY_LLONG_MAX + 1 */
+    num = PyLong_FromLongLong(PY_LLONG_MAX);
+    if (num == NULL)
+        return NULL;
+    one = PyLong_FromLong(1L);
+    if (one == NULL) {
+        Py_DECREF(num);
+        return NULL;
+    }
+    temp = PyNumber_Add(num, one);
+    Py_DECREF(one);
+    Py_DECREF(num);
+    num = temp;
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != 1)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was not set to 1");
+
+    /* Test that overflow is set properly for a large negative value. */
+    /* num is a number smaller than PY_LLONG_MIN on a typical platform */
+    num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != -1)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was not set to -1");
+
+    /* Same again, with num = PY_LLONG_MIN - 1 */
+    num = PyLong_FromLongLong(PY_LLONG_MIN);
+    if (num == NULL)
+        return NULL;
+    one = PyLong_FromLong(1L);
+    if (one == NULL) {
+        Py_DECREF(num);
+        return NULL;
+    }
+    temp = PyNumber_Subtract(num, one);
+    Py_DECREF(one);
+    Py_DECREF(num);
+    num = temp;
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -1)
+        return raiseTestError("test_long_long_and_overflow",
+            "return value was not set to -1");
+    if (overflow != -1)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was not set to -1");
+
+    /* Test that overflow is cleared properly for small values. */
+    num = PyLong_FromString("FF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != 0xFF)
+        return raiseTestError("test_long_long_and_overflow",
+            "expected return value 0xFF");
+    if (overflow != 0)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was not cleared");
+
+    num = PyLong_FromString("-FF", NULL, 16);
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != -0xFF)
+        return raiseTestError("test_long_long_and_overflow",
+            "expected return value 0xFF");
+    if (overflow != 0)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was set incorrectly");
+
+    num = PyLong_FromLongLong(PY_LLONG_MAX);
+    if (num == NULL)
+        return NULL;
+    overflow = 1234;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != PY_LLONG_MAX)
+        return raiseTestError("test_long_long_and_overflow",
+            "expected return value PY_LLONG_MAX");
+    if (overflow != 0)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was not cleared");
+
+    num = PyLong_FromLongLong(PY_LLONG_MIN);
+    if (num == NULL)
+        return NULL;
+    overflow = 0;
+    value = PyLong_AsLongLongAndOverflow(num, &overflow);
+    Py_DECREF(num);
+    if (value == -1 && PyErr_Occurred())
+        return NULL;
+    if (value != PY_LLONG_MIN)
+        return raiseTestError("test_long_long_and_overflow",
+            "expected return value PY_LLONG_MIN");
+    if (overflow != 0)
+        return raiseTestError("test_long_long_and_overflow",
+            "overflow was not cleared");
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that
+   non-integer arguments are handled correctly. It should be extended to
+   test overflow handling.
+ */
+
+static PyObject *
+test_long_as_size_t(PyObject *self)
+{
+    size_t out_u;
+    Py_ssize_t out_s;
+
+    Py_INCREF(Py_None);
+
+    out_u = PyLong_AsSize_t(Py_None);
+    if (out_u != (size_t)-1 || !PyErr_Occurred())
+        return raiseTestError("test_long_as_size_t",
+                              "PyLong_AsSize_t(None) didn't complain");
+    if (!PyErr_ExceptionMatches(PyExc_TypeError))
+        return raiseTestError("test_long_as_size_t",
+                              "PyLong_AsSize_t(None) raised "
+                              "something other than TypeError");
+    PyErr_Clear();
+
+    out_s = PyLong_AsSsize_t(Py_None);
+    if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred())
+        return raiseTestError("test_long_as_size_t",
+                              "PyLong_AsSsize_t(None) didn't complain");
+    if (!PyErr_ExceptionMatches(PyExc_TypeError))
+        return raiseTestError("test_long_as_size_t",
+                              "PyLong_AsSsize_t(None) raised "
+                              "something other than TypeError");
+    PyErr_Clear();
+
+    /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
+    return Py_None;
+}
+
+/* Test the PyLong_AsDouble API. At present this just tests that
+   non-integer arguments are handled correctly.
+ */
+
+static PyObject *
+test_long_as_double(PyObject *self)
+{
+    double out;
+
+    Py_INCREF(Py_None);
+
+    out = PyLong_AsDouble(Py_None);
+    if (out != -1.0 || !PyErr_Occurred())
+        return raiseTestError("test_long_as_double",
+                              "PyLong_AsDouble(None) didn't complain");
+    if (!PyErr_ExceptionMatches(PyExc_TypeError))
+        return raiseTestError("test_long_as_double",
+                              "PyLong_AsDouble(None) raised "
+                              "something other than TypeError");
+    PyErr_Clear();
+
+    /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
+    return Py_None;
+}
+
+/* Test the L code for PyArg_ParseTuple.  This should deliver a long long
+   for both long and int arguments.  The test may leak a little memory if
+   it fails.
+*/
+static PyObject *
+test_L_code(PyObject *self)
+{
+    PyObject *tuple, *num;
+    long long value;
+
+    tuple = PyTuple_New(1);
+    if (tuple == NULL)
+        return NULL;
+
+    num = PyLong_FromLong(42);
+    if (num == NULL)
+        return NULL;
+
+    PyTuple_SET_ITEM(tuple, 0, num);
+
+    value = -1;
+    if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
+        return NULL;
+    if (value != 42)
+        return raiseTestError("test_L_code",
+            "L code returned wrong value for long 42");
+
+    Py_DECREF(num);
+    num = PyLong_FromLong(42);
+    if (num == NULL)
+        return NULL;
+
+    PyTuple_SET_ITEM(tuple, 0, num);
+
+    value = -1;
+    if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
+        return NULL;
+    if (value != 42)
+        return raiseTestError("test_L_code",
+            "L code returned wrong value for int 42");
+
+    Py_DECREF(tuple);
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+static PyObject *
+return_none(void *unused)
+{
+    Py_RETURN_NONE;
+}
+
+static PyObject *
+raise_error(void *unused)
+{
+    PyErr_SetNone(PyExc_ValueError);
+    return NULL;
+}
+
+static int
+test_buildvalue_N_error(const char *fmt)
+{
+    PyObject *arg, *res;
+
+    arg = PyList_New(0);
+    if (arg == NULL) {
+        return -1;
+    }
+
+    Py_INCREF(arg);
+    res = Py_BuildValue(fmt, return_none, NULL, arg);
+    if (res == NULL) {
+        return -1;
+    }
+    Py_DECREF(res);
+    if (Py_REFCNT(arg) != 1) {
+        PyErr_Format(TestError, "test_buildvalue_N: "
+                     "arg was not decrefed in successful "
+                     "Py_BuildValue(\"%s\")", fmt);
+        return -1;
+    }
+
+    Py_INCREF(arg);
+    res = Py_BuildValue(fmt, raise_error, NULL, arg);
+    if (res != NULL || !PyErr_Occurred()) {
+        PyErr_Format(TestError, "test_buildvalue_N: "
+                     "Py_BuildValue(\"%s\") didn't complain", fmt);
+        return -1;
+    }
+    PyErr_Clear();
+    if (Py_REFCNT(arg) != 1) {
+        PyErr_Format(TestError, "test_buildvalue_N: "
+                     "arg was not decrefed in failed "
+                     "Py_BuildValue(\"%s\")", fmt);
+        return -1;
+    }
+    Py_DECREF(arg);
+    return 0;
+}
+
+static PyObject *
+test_buildvalue_N(PyObject *self, PyObject *noargs)
+{
+    PyObject *arg, *res;
+
+    arg = PyList_New(0);
+    if (arg == NULL) {
+        return NULL;
+    }
+    Py_INCREF(arg);
+    res = Py_BuildValue("N", arg);
+    if (res == NULL) {
+        return NULL;
+    }
+    if (res != arg) {
+        return raiseTestError("test_buildvalue_N",
+                              "Py_BuildValue(\"N\") returned wrong result");
+    }
+    if (Py_REFCNT(arg) != 2) {
+        return raiseTestError("test_buildvalue_N",
+                              "arg was not decrefed in Py_BuildValue(\"N\")");
+    }
+    Py_DECREF(res);
+    Py_DECREF(arg);
+
+    if (test_buildvalue_N_error("O&N") < 0)
+        return NULL;
+    if (test_buildvalue_N_error("(O&N)") < 0)
+        return NULL;
+    if (test_buildvalue_N_error("[O&N]") < 0)
+        return NULL;
+    if (test_buildvalue_N_error("{O&N}") < 0)
+        return NULL;
+    if (test_buildvalue_N_error("{()O&(())N}") < 0)
+        return NULL;
+
+    Py_RETURN_NONE;
+}
+
+
+static PyObject *
+get_args(PyObject *self, PyObject *args)
+{
+    if (args == NULL) {
+        args = Py_None;
+    }
+    Py_INCREF(args);
+    return args;
+}
+
+static PyObject *
+get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    if (kwargs == NULL) {
+        kwargs = Py_None;
+    }
+    Py_INCREF(kwargs);
+    return kwargs;
+}
+
+/* Test tuple argument processing */
+static PyObject *
+getargs_tuple(PyObject *self, PyObject *args)
+{
+    int a, b, c;
+    if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
+        return NULL;
+    return Py_BuildValue("iii", a, b, c);
+}
+
+/* test PyArg_ParseTupleAndKeywords */
+static PyObject *
+getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
+    static const char fmt[] = "(ii)i|(i(ii))(iii)i";
+    int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
+        &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
+        &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
+        return NULL;
+    return Py_BuildValue("iiiiiiiiii",
+        int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
+        int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
+}
+
+/* test PyArg_ParseTupleAndKeywords keyword-only arguments */
+static PyObject *
+getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *keywords[] = {"required", "optional", "keyword_only", NULL};
+    int required = -1;
+    int optional = -1;
+    int keyword_only = -1;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
+                                     &required, &optional, &keyword_only))
+        return NULL;
+    return Py_BuildValue("iii", required, optional, keyword_only);
+}
+
+/* test PyArg_ParseTupleAndKeywords positional-only arguments */
+static PyObject *
+getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *keywords[] = {"", "", "keyword", NULL};
+    int required = -1;
+    int optional = -1;
+    int keyword = -1;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
+                                     &required, &optional, &keyword))
+        return NULL;
+    return Py_BuildValue("iii", required, optional, keyword);
+}
+
+/* Functions to call PyArg_ParseTuple with integer format codes,
+   and return the result.
+*/
+static PyObject *
+getargs_b(PyObject *self, PyObject *args)
+{
+    unsigned char value;
+    if (!PyArg_ParseTuple(args, "b", &value))
+        return NULL;
+    return PyLong_FromUnsignedLong((unsigned long)value);
+}
+
+static PyObject *
+getargs_B(PyObject *self, PyObject *args)
+{
+    unsigned char value;
+    if (!PyArg_ParseTuple(args, "B", &value))
+        return NULL;
+    return PyLong_FromUnsignedLong((unsigned long)value);
+}
+
+static PyObject *
+getargs_h(PyObject *self, PyObject *args)
+{
+    short value;
+    if (!PyArg_ParseTuple(args, "h", &value))
+        return NULL;
+    return PyLong_FromLong((long)value);
+}
+
+static PyObject *
+getargs_H(PyObject *self, PyObject *args)
+{
+    unsigned short value;
+    if (!PyArg_ParseTuple(args, "H", &value))
+        return NULL;
+    return PyLong_FromUnsignedLong((unsigned long)value);
+}
+
+static PyObject *
+getargs_I(PyObject *self, PyObject *args)
+{
+    unsigned int value;
+    if (!PyArg_ParseTuple(args, "I", &value))
+        return NULL;
+    return PyLong_FromUnsignedLong((unsigned long)value);
+}
+
+static PyObject *
+getargs_k(PyObject *self, PyObject *args)
+{
+    unsigned long value;
+    if (!PyArg_ParseTuple(args, "k", &value))
+        return NULL;
+    return PyLong_FromUnsignedLong(value);
+}
+
+static PyObject *
+getargs_i(PyObject *self, PyObject *args)
+{
+    int value;
+    if (!PyArg_ParseTuple(args, "i", &value))
+        return NULL;
+    return PyLong_FromLong((long)value);
+}
+
+static PyObject *
+getargs_l(PyObject *self, PyObject *args)
+{
+    long value;
+    if (!PyArg_ParseTuple(args, "l", &value))
+        return NULL;
+    return PyLong_FromLong(value);
+}
+
+static PyObject *
+getargs_n(PyObject *self, PyObject *args)
+{
+    Py_ssize_t value;
+    if (!PyArg_ParseTuple(args, "n", &value))
+        return NULL;
+    return PyLong_FromSsize_t(value);
+}
+
+static PyObject *
+getargs_p(PyObject *self, PyObject *args)
+{
+    int value;
+    if (!PyArg_ParseTuple(args, "p", &value))
+        return NULL;
+    return PyLong_FromLong(value);
+}
+
+static PyObject *
+getargs_L(PyObject *self, PyObject *args)
+{
+    long long value;


More information about the pypy-commit mailing list