[pypy-commit] pypy rffi-parser: INT vs INT_real mess

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89134:49c4b821f3fe
Date: 2016-12-18 03:42 +
http://bitbucket.org/pypy/pypy/changeset/49c4b821f3fe/

Log:INT vs INT_real mess

diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -657,6 +657,7 @@
 CNAME_TO_LLTYPE[name] = rfficache.platform.types[rname]
 
 add_inttypes()
+CNAME_TO_LLTYPE['int'] = rffi.INT_real
 
 def cname_to_lltype(name):
 return CNAME_TO_LLTYPE[name]
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy rffi-parser: Add buffer to the pseudo-header, handle pointers to primitive types and fixed-size arrays

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89128:456561b7910a
Date: 2016-12-18 01:24 +
http://bitbucket.org/pypy/pypy/changeset/456561b7910a/

Log:Add buffer to the pseudo-header, handle pointers to primitive types
and fixed-size arrays

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
@@ -623,6 +623,38 @@
 typedef struct _typeobject PyTypeObject;
 
 typedef void (*freefunc)(void *);
+
+/* Py3k buffer interface, adapted for PyPy */
+#define Py_MAX_NDIMS 32
+#define Py_MAX_FMT 128
+typedef struct bufferinfo {
+void *buf;
+PyObject *obj;/* owned reference */
+Py_ssize_t len;
+
+/* This is Py_ssize_t so it can be
+   pointed to by strides in simple case.*/
+Py_ssize_t itemsize;
+int readonly;
+int ndim;
+char *format;
+Py_ssize_t *shape;
+Py_ssize_t *strides;
+Py_ssize_t *suboffsets; /* alway NULL for app-level objects*/
+unsigned char _format[Py_MAX_FMT];
+Py_ssize_t _strides[Py_MAX_NDIMS];
+Py_ssize_t _shape[Py_MAX_NDIMS];
+/* static store for shape and strides of
+   mono-dimensional buffers. */
+/* Py_ssize_t smalltable[2]; */
+void *internal; /* always NULL for app-level objects */
+} Py_buffer;
+
+
+typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
+typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
+/* end Py3k buffer interface */
+
 """)
 h.configure_types()
 
@@ -645,26 +677,10 @@
 PyVarObjectStruct = h.definitions['PyVarObject'].OF
 PyVarObject = lltype.Ptr(PyVarObjectStruct)
 
-Py_buffer = cpython_struct(
-"Py_buffer", (
-('buf', rffi.VOIDP),
-('obj', PyObject),
-('len', Py_ssize_t),
-('itemsize', Py_ssize_t),
+Py_buffer = h.definitions['Py_buffer']
+getbufferproc = h.definitions['getbufferproc']
+releasebufferproc = h.definitions['releasebufferproc']
 
-('readonly', lltype.Signed),
-('ndim', lltype.Signed),
-('format', rffi.CCHARP),
-('shape', Py_ssize_tP),
-('strides', Py_ssize_tP),
-('_format', rffi.CFixedArray(rffi.UCHAR, Py_MAX_FMT)),
-('_shape', rffi.CFixedArray(Py_ssize_t, Py_MAX_NDIMS)),
-('_strides', rffi.CFixedArray(Py_ssize_t, Py_MAX_NDIMS)),
-('suboffsets', Py_ssize_tP),
-#('smalltable', rffi.CFixedArray(Py_ssize_t, 2)),
-('internal', rffi.VOIDP)
-))
-Py_bufferP = lltype.Ptr(Py_buffer)
 
 @specialize.memo()
 def is_PyObject(TYPE):
diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -732,6 +732,8 @@
 return rffi.VOIDP
 elif isinstance(TO, DelayedStruct):
 TO = TO.TYPE
+elif isinstance(obj.totype, model.PrimitiveType):
+return rffi.CArrayPtr(TO)
 return lltype.Ptr(TO)
 elif isinstance(obj, model.FunctionPtrType):
 if obj.ellipsis:
@@ -741,6 +743,8 @@
 return lltype.Ptr(lltype.FuncType(args, res))
 elif isinstance(obj, model.VoidType):
 return lltype.Void
+elif isinstance(obj, model.ArrayType):
+return rffi.CFixedArray(self.convert_type(obj.item), obj.length)
 else:
 raise NotImplementedError
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy rffi-parser: Handle FILE*

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89132:5d78c74e573b
Date: 2016-12-18 02:56 +
http://bitbucket.org/pypy/pypy/changeset/5d78c74e573b/

Log:Handle FILE*

diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -3,6 +3,7 @@
 from cffi.commontypes import COMMON_TYPES, resolve_common_type
 import pycparser
 import weakref, re
+from rpython.rlib.rfile import FILEP
 from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.rtyper.tool import rfficache, rffi_platform
 
@@ -644,7 +645,7 @@
 CNAME_TO_LLTYPE = {
 'char': rffi.CHAR,
 'double': rffi.DOUBLE, 'long double': rffi.LONGDOUBLE,
-'float': rffi.FLOAT}
+'float': rffi.FLOAT, 'FILE': FILEP.TO}
 
 def add_inttypes():
 for name in rffi.TYPES:
@@ -695,6 +696,8 @@
 self.macros[name] = value
 
 def new_struct(self, obj):
+if obj.name == '_IO_FILE':  # cffi weirdness
+return cname_to_lltype('FILE')
 struct = DelayedStruct(obj.name, None, lltype.ForwardReference())
 # Cache it early, to avoid infinite recursion
 self.structs[obj] = struct
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy rffi-parser: Complete the declaration of PyTypeObject, fix some of the issues

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89130:b3e206c8fe8c
Date: 2016-12-18 02:07 +
http://bitbucket.org/pypy/pypy/changeset/b3e206c8fe8c/

Log:Complete the declaration of PyTypeObject, fix some of the issues

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
@@ -620,7 +620,7 @@
PyObject_VAR_HEAD
 } PyVarObject;
 
-typedef struct _typeobject PyTypeObject;
+struct _typeobject;
 
 typedef void (*freefunc)(void *);
 typedef void (*destructor)(PyObject *);
@@ -700,6 +700,178 @@
 typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
 /* end Py3k buffer interface */
 
+typedef int (*objobjproc)(PyObject *, PyObject *);
+typedef int (*visitproc)(PyObject *, void *);
+typedef int (*traverseproc)(PyObject *, visitproc, void *);
+
+typedef struct {
+   /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
+  arguments are guaranteed to be of the object's type (modulo
+  coercion hacks -- i.e. if the type's coercion function
+  returns other types, then these are allowed as well).  Numbers that
+  have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
+  arguments for proper type and implement the necessary conversions
+  in the slot functions themselves. */
+
+   binaryfunc nb_add;
+   binaryfunc nb_subtract;
+   binaryfunc nb_multiply;
+   binaryfunc nb_divide;
+   binaryfunc nb_remainder;
+   binaryfunc nb_divmod;
+   ternaryfunc nb_power;
+   unaryfunc nb_negative;
+   unaryfunc nb_positive;
+   unaryfunc nb_absolute;
+   inquiry nb_nonzero;
+   unaryfunc nb_invert;
+   binaryfunc nb_lshift;
+   binaryfunc nb_rshift;
+   binaryfunc nb_and;
+   binaryfunc nb_xor;
+   binaryfunc nb_or;
+   coercion nb_coerce;
+   unaryfunc nb_int;
+   unaryfunc nb_long;
+   unaryfunc nb_float;
+   unaryfunc nb_oct;
+   unaryfunc nb_hex;
+   /* Added in release 2.0 */
+   binaryfunc nb_inplace_add;
+   binaryfunc nb_inplace_subtract;
+   binaryfunc nb_inplace_multiply;
+   binaryfunc nb_inplace_divide;
+   binaryfunc nb_inplace_remainder;
+   ternaryfunc nb_inplace_power;
+   binaryfunc nb_inplace_lshift;
+   binaryfunc nb_inplace_rshift;
+   binaryfunc nb_inplace_and;
+   binaryfunc nb_inplace_xor;
+   binaryfunc nb_inplace_or;
+
+   /* Added in release 2.2 */
+   /* The following require the Py_TPFLAGS_HAVE_CLASS flag */
+   binaryfunc nb_floor_divide;
+   binaryfunc nb_true_divide;
+   binaryfunc nb_inplace_floor_divide;
+   binaryfunc nb_inplace_true_divide;
+
+   /* Added in release 2.5 */
+   unaryfunc nb_index;
+} PyNumberMethods;
+
+typedef struct {
+   lenfunc sq_length;
+   binaryfunc sq_concat;
+   ssizeargfunc sq_repeat;
+   ssizeargfunc sq_item;
+   ssizessizeargfunc sq_slice;
+   ssizeobjargproc sq_ass_item;
+   ssizessizeobjargproc sq_ass_slice;
+   objobjproc sq_contains;
+   /* Added in release 2.0 */
+   binaryfunc sq_inplace_concat;
+   ssizeargfunc sq_inplace_repeat;
+} PySequenceMethods;
+
+typedef struct {
+   lenfunc mp_length;
+   binaryfunc mp_subscript;
+   objobjargproc mp_ass_subscript;
+} PyMappingMethods;
+
+typedef struct {
+   readbufferproc bf_getreadbuffer;
+   writebufferproc bf_getwritebuffer;
+   segcountproc bf_getsegcount;
+   charbufferproc bf_getcharbuffer;
+   getbufferproc bf_getbuffer;
+   releasebufferproc bf_releasebuffer;
+} PyBufferProcs;
+
+
+
+typedef struct _typeobject {
+   PyObject_VAR_HEAD
+   const char *tp_name; /* For printing, in format "." */
+   Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
+
+   /* Methods to implement standard operations */
+
+   destructor tp_dealloc;
+   printfunc tp_print;
+   getattrfunc tp_getattr;
+   setattrfunc tp_setattr;
+   cmpfunc tp_compare;
+   reprfunc tp_repr;
+
+   /* Method suites for standard classes */
+
+   PyNumberMethods *tp_as_number;
+   PySequenceMethods *tp_as_sequence;
+   PyMappingMethods *tp_as_mapping;
+
+   /* More standard operations (here for binary compatibility) */
+
+   hashfunc tp_hash;
+   ternaryfunc tp_call;
+   reprfunc tp_str;
+   getattrofunc tp_getattro;
+   setattrofunc tp_setattro;
+
+   /* Functions to access object as input/output buffer */
+   PyBufferProcs *tp_as_buffer;
+
+   /* Flags to define presence of optional/expanded features */
+   long tp_flags;
+
+   const char *tp_doc; /* Documentation string */
+
+   /* Assigned meaning in release 2.0 */
+   /* call function for all accessible objects */
+   traverseproc tp_traverse;
+
+   /* delete references to contained objects */
+   inquiry tp_clear;
+
+   /* Assigned 

[pypy-commit] pypy rffi-parser: pointers to DelayedStruct

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89127:69eba1948840
Date: 2016-12-18 01:16 +
http://bitbucket.org/pypy/pypy/changeset/69eba1948840/

Log:pointers to DelayedStruct

diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -661,12 +661,13 @@
 return CNAME_TO_LLTYPE[name]
 
 class DelayedStruct(object):
-def __init__(self, name, fields):
+def __init__(self, name, fields, TYPE):
 self.struct_name = name
 self.fields = fields
+self.TYPE = TYPE
 
 def __repr__(self):
-return "".format(vars(self))
+return "".format(**vars(self))
 
 
 class ParsedSource(object):
@@ -695,12 +696,12 @@
 
 def new_struct(self, obj):
 if obj.fldtypes is None:
-return lltype.ForwardReference()
+fields = None
 else:
 fields = zip(
 obj.fldnames,
 [self.convert_type(field) for field in obj.fldtypes])
-return DelayedStruct(obj.name, fields)
+return DelayedStruct(obj.name, fields, lltype.ForwardReference())
 
 def realize_struct(self, struct, type_name):
 from pypy.module.cpyext.api import CConfig, TYPES
@@ -729,6 +730,8 @@
 TO = self.convert_type(obj.totype)
 if TO is lltype.Void:
 return rffi.VOIDP
+elif isinstance(TO, DelayedStruct):
+TO = TO.TYPE
 return lltype.Ptr(TO)
 elif isinstance(obj, model.FunctionPtrType):
 if obj.ellipsis:
diff --git a/pypy/module/cpyext/typeobjectdefs.py 
b/pypy/module/cpyext/typeobjectdefs.py
--- a/pypy/module/cpyext/typeobjectdefs.py
+++ b/pypy/module/cpyext/typeobjectdefs.py
@@ -31,6 +31,7 @@
 initproc = P(FT([PyO, PyO, PyO], rffi.INT_real))
 newfunc = P(FT([PyTypeObjectPtr, PyO, PyO], PyO))
 allocfunc = P(FT([PyTypeObjectPtr, Py_ssize_t], PyO))
+
 unaryfunc = P(FT([PyO], PyO))
 binaryfunc = P(FT([PyO, PyO], PyO))
 ternaryfunc = P(FT([PyO, PyO, PyO], PyO))
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy rffi-parser: fix

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89131:b9189e339e2d
Date: 2016-12-18 02:25 +
http://bitbucket.org/pypy/pypy/changeset/b9189e339e2d/

Log:fix

diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -686,8 +686,8 @@
 assert name not in self.definitions
 tp = self.convert_type(obj)
 if isinstance(tp, DelayedStruct):
-tp = self.realize_struct(tp, name)
-self.structs[obj] = tp
+self.realize_struct(tp, name)
+tp = self.structs[obj] = tp.TYPE
 self.definitions[name] = lltype.Typedef(tp, name)
 
 def add_macro(self, name, value):
@@ -705,13 +705,10 @@
 return struct
 
 def realize_struct(self, struct, type_name):
-from pypy.module.cpyext.api import CConfig, TYPES
 configname = type_name.replace(' ', '__')
 setattr(self._Config, configname,
 rffi_platform.Struct(type_name, struct.fields))
-forward = lltype.ForwardReference()
-self._TYPES[configname] = forward
-return forward
+self._TYPES[configname] = struct.TYPE
 
 def configure_types(self):
 for name, TYPE in rffi_platform.configure(self._Config).iteritems():
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy rffi-parser: Add missing declarations needed by PyTypeObject

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89133:62289b903d10
Date: 2016-12-18 03:40 +
http://bitbucket.org/pypy/pypy/changeset/62289b903d10/

Log:Add missing declarations needed by PyTypeObject

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
@@ -789,6 +789,42 @@
releasebufferproc bf_releasebuffer;
 } PyBufferProcs;
 
+/* from descrobject.h */
+typedef PyObject *(*getter)(PyObject *, void *);
+typedef int (*setter)(PyObject *, PyObject *, void *);
+
+typedef struct PyGetSetDef {
+   char *name;
+   getter get;
+   setter set;
+   char *doc;
+   void *closure;
+} PyGetSetDef;
+
+/* from methodobject.h */
+typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
+typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
+ PyObject *);
+typedef PyObject *(*PyNoArgsFunction)(PyObject *);
+
+struct PyMethodDef {
+const char  *ml_name;   /* The name of the built-in function/method */
+PyCFunction  ml_meth;   /* The C function that implements it */
+int  ml_flags;  /* Combination of METH_xxx flags, which mostly
+   describe the args expected by the C func */
+const char  *ml_doc;/* The __doc__ attribute, or NULL */
+};
+typedef struct PyMethodDef PyMethodDef;
+
+/* from structmember.h */
+typedef struct PyMemberDef {
+/* Current version, use this */
+char *name;
+int type;
+Py_ssize_t offset;
+int flags;
+char *doc;
+} PyMemberDef;
 
 
 typedef struct _typeobject {
diff --git a/pypy/module/cpyext/modsupport.py b/pypy/module/cpyext/modsupport.py
--- a/pypy/module/cpyext/modsupport.py
+++ b/pypy/module/cpyext/modsupport.py
@@ -50,7 +50,7 @@
 cache.  CPython includes some extra checking here to make sure the module
 being initialized lines up with what's expected, but we don't.
 """
-from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
+from pypy.module.cpyext.api import PyTypeObjectPtr
 modname = rffi.charp2str(name)
 state = space.fromcache(State)
 f_name, f_path = state.package_context
diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -6,9 +6,9 @@
 from rpython.rlib.rarithmetic import widen
 from pypy.module.cpyext.api import (
 cpython_api, generic_cpy_call, PyObject, Py_ssize_t, Py_TPFLAGS_CHECKTYPES,
-mangle_name, pypy_decl, Py_buffer, Py_bufferP)
+mangle_name, pypy_decl, Py_buffer, Py_bufferP, PyTypeObjectPtr)
 from pypy.module.cpyext.typeobjectdefs import (
-unaryfunc, ternaryfunc, PyTypeObjectPtr, binaryfunc,
+unaryfunc, ternaryfunc, binaryfunc,
 getattrfunc, getattrofunc, setattrofunc, lenfunc, ssizeargfunc, inquiry,
 ssizessizeargfunc, ssizeobjargproc, iternextfunc, initproc, richcmpfunc,
 cmpfunc, hashfunc, descrgetfunc, descrsetfunc, objobjproc, objobjargproc,
diff --git a/pypy/module/cpyext/test/test_bytesobject.py 
b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -5,7 +5,7 @@
 from pypy.module.cpyext.bytesobject import new_empty_str, PyBytesObject
 from pypy.module.cpyext.api import PyObjectP, PyObject, Py_ssize_tP, 
generic_cpy_call
 from pypy.module.cpyext.pyobject import Py_DecRef, from_ref, make_ref
-from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
+from pypy.module.cpyext.api import PyTypeObjectPtr
 
 import py
 import sys
diff --git a/pypy/module/cpyext/typeobjectdefs.py 
b/pypy/module/cpyext/typeobjectdefs.py
--- a/pypy/module/cpyext/typeobjectdefs.py
+++ b/pypy/module/cpyext/typeobjectdefs.py
@@ -1,239 +1,62 @@
-from rpython.rtyper.lltypesystem import rffi, lltype
-from rpython.rtyper.lltypesystem.lltype import Ptr, FuncType, Void
-from pypy.module.cpyext.api import (cpython_struct, Py_ssize_t, Py_ssize_tP,
-PyVarObjectFields, PyTypeObject, PyTypeObjectPtr, FILEP,
-Py_TPFLAGS_READYING, Py_TPFLAGS_READY, Py_TPFLAGS_HEAPTYPE)
-from pypy.module.cpyext.pyobject import PyObject, make_ref, from_ref
-from pypy.module.cpyext.modsupport import PyMethodDef
-from pypy.module.cpyext.api import Py_bufferP, h
+from pypy.module.cpyext.api import h
 
 
-P, FT, PyO = Ptr, FuncType, PyObject
-PyOPtr = Ptr(lltype.Array(PyO, hints={'nolength': True}))
+freefunc = h.definitions['freefunc']
+destructor = h.definitions['destructor']
+printfunc = h.definitions['printfunc']
+getattrfunc = h.definitions['getattrfunc']
+getattrofunc = h.definitions['getattrofunc']
+setattrfunc = h.definitions['setattrfunc']
+setattrofunc = h.definitions['setattrofunc']
+cmpfunc = h.definitions['cmpfunc']
+reprfunc = h.definitions['reprfunc']
+hashfunc = h.definitions['hashfunc']
+richcmpfunc = h.definitions['richcmpfunc']
+getiterfunc = 

[pypy-commit] pypy rffi-parser: fix

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89135:1c1d3f89270c
Date: 2016-12-18 03:53 +
http://bitbucket.org/pypy/pypy/changeset/1c1d3f89270c/

Log:fix

diff --git a/pypy/module/cpyext/typeobjectdefs.py 
b/pypy/module/cpyext/typeobjectdefs.py
--- a/pypy/module/cpyext/typeobjectdefs.py
+++ b/pypy/module/cpyext/typeobjectdefs.py
@@ -1,57 +1,57 @@
 from pypy.module.cpyext.api import h
 
 
-freefunc = h.definitions['freefunc']
-destructor = h.definitions['destructor']
-printfunc = h.definitions['printfunc']
-getattrfunc = h.definitions['getattrfunc']
-getattrofunc = h.definitions['getattrofunc']
-setattrfunc = h.definitions['setattrfunc']
-setattrofunc = h.definitions['setattrofunc']
-cmpfunc = h.definitions['cmpfunc']
-reprfunc = h.definitions['reprfunc']
-hashfunc = h.definitions['hashfunc']
-richcmpfunc = h.definitions['richcmpfunc']
-getiterfunc = h.definitions['getiterfunc']
-iternextfunc = h.definitions['iternextfunc']
-descrgetfunc = h.definitions['descrgetfunc']
-descrsetfunc = h.definitions['descrsetfunc']
-initproc = h.definitions['initproc']
-newfunc = h.definitions['newfunc']
-allocfunc = h.definitions['allocfunc']
+freefunc = h.definitions['freefunc'].OF
+destructor = h.definitions['destructor'].OF
+printfunc = h.definitions['printfunc'].OF
+getattrfunc = h.definitions['getattrfunc'].OF
+getattrofunc = h.definitions['getattrofunc'].OF
+setattrfunc = h.definitions['setattrfunc'].OF
+setattrofunc = h.definitions['setattrofunc'].OF
+cmpfunc = h.definitions['cmpfunc'].OF
+reprfunc = h.definitions['reprfunc'].OF
+hashfunc = h.definitions['hashfunc'].OF
+richcmpfunc = h.definitions['richcmpfunc'].OF
+getiterfunc = h.definitions['getiterfunc'].OF
+iternextfunc = h.definitions['iternextfunc'].OF
+descrgetfunc = h.definitions['descrgetfunc'].OF
+descrsetfunc = h.definitions['descrsetfunc'].OF
+initproc = h.definitions['initproc'].OF
+newfunc = h.definitions['newfunc'].OF
+allocfunc = h.definitions['allocfunc'].OF
 
-unaryfunc = h.definitions['unaryfunc']
-binaryfunc = h.definitions['binaryfunc']
-ternaryfunc = h.definitions['ternaryfunc']
-inquiry = h.definitions['inquiry']
-lenfunc = h.definitions['lenfunc']
-coercion = h.definitions['coercion']
-intargfunc = h.definitions['intargfunc']
-intintargfunc = h.definitions['intintargfunc']
-ssizeargfunc = h.definitions['ssizeargfunc']
-ssizessizeargfunc = h.definitions['ssizessizeargfunc']
-intobjargproc = h.definitions['intobjargproc']
-intintobjargproc = h.definitions['intintobjargproc']
-ssizeobjargproc = h.definitions['ssizeobjargproc']
-ssizessizeobjargproc = h.definitions['ssizessizeobjargproc']
-objobjargproc = h.definitions['objobjargproc']
+unaryfunc = h.definitions['unaryfunc'].OF
+binaryfunc = h.definitions['binaryfunc'].OF
+ternaryfunc = h.definitions['ternaryfunc'].OF
+inquiry = h.definitions['inquiry'].OF
+lenfunc = h.definitions['lenfunc'].OF
+coercion = h.definitions['coercion'].OF
+intargfunc = h.definitions['intargfunc'].OF
+intintargfunc = h.definitions['intintargfunc'].OF
+ssizeargfunc = h.definitions['ssizeargfunc'].OF
+ssizessizeargfunc = h.definitions['ssizessizeargfunc'].OF
+intobjargproc = h.definitions['intobjargproc'].OF
+intintobjargproc = h.definitions['intintobjargproc'].OF
+ssizeobjargproc = h.definitions['ssizeobjargproc'].OF
+ssizessizeobjargproc = h.definitions['ssizessizeobjargproc'].OF
+objobjargproc = h.definitions['objobjargproc'].OF
 
-objobjproc = h.definitions['objobjproc']
-visitproc = h.definitions['visitproc']
-traverseproc = h.definitions['traverseproc']
+objobjproc = h.definitions['objobjproc'].OF
+visitproc = h.definitions['visitproc'].OF
+traverseproc = h.definitions['traverseproc'].OF
 
-getter = h.definitions['getter']
-setter = h.definitions['setter']
+getter = h.definitions['getter'].OF
+setter = h.definitions['setter'].OF
 
 #wrapperfunc = h.definitions['wrapperfunc']
 #wrapperfunc_kwds = h.definitions['wrapperfunc_kwds']
 
-readbufferproc = h.definitions['readbufferproc']
-writebufferproc = h.definitions['writebufferproc']
-segcountproc = h.definitions['segcountproc']
-charbufferproc = h.definitions['charbufferproc']
-getbufferproc = h.definitions['getbufferproc']
-releasebufferproc = h.definitions['releasebufferproc']
+readbufferproc = h.definitions['readbufferproc'].OF
+writebufferproc = h.definitions['writebufferproc'].OF
+segcountproc = h.definitions['segcountproc'].OF
+charbufferproc = h.definitions['charbufferproc'].OF
+getbufferproc = h.definitions['getbufferproc'].OF
+releasebufferproc = h.definitions['releasebufferproc'].OF
 
 
 PyGetSetDef = h.definitions['PyGetSetDef'].OF
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy.org extradoc: update the values

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: extradoc
Changeset: r835:99c5d63b977a
Date: 2016-12-17 22:38 +0100
http://bitbucket.org/pypy/pypy.org/changeset/99c5d63b977a/

Log:update the values

diff --git a/don1.html b/don1.html
--- a/don1.html
+++ b/don1.html
@@ -15,7 +15,7 @@
 
 

-   $66464 of $105000 (63.3%)
+   $66474 of $105000 (63.3%)


 
@@ -23,7 +23,7 @@
   
   This donation goes towards supporting Python 3 in 
PyPy.
   Current status:
-we have $2305 left
+we have $2314 left
   in the account. Read proposal
   
   
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy better-PyDict_Next: mrege known good default into branch

2016-12-17 Thread mattip
Author: Matti Picus 
Branch: better-PyDict_Next
Changeset: r89126:6f9cd556bd03
Date: 2016-12-17 21:54 +0200
http://bitbucket.org/pypy/pypy/changeset/6f9cd556bd03/

Log:mrege known good default into branch

diff too long, truncating to 2000 out of 13024 lines

diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -77,3 +77,5 @@
 ^.hypothesis/
 ^release/
 ^rpython/_cache$
+
+pypy/module/cppyy/.+/*\.pcm
diff --git a/pypy/config/pypyoption.py b/pypy/config/pypyoption.py
--- a/pypy/config/pypyoption.py
+++ b/pypy/config/pypyoption.py
@@ -190,6 +190,12 @@
"make sure that all calls go through space.call_args",
default=False),
 
+BoolOption("disable_entrypoints",
+   "Disable external entry points, notably the"
+   " cpyext module and cffi's embedding mode.",
+   default=False,
+   requires=[("objspace.usemodules.cpyext", False)]),
+
 OptionDescription("std", "Standard Object Space Options", [
 BoolOption("withtproxy", "support transparent proxies",
default=True),
diff --git a/pypy/doc/contributor.rst b/pypy/doc/contributor.rst
--- a/pypy/doc/contributor.rst
+++ b/pypy/doc/contributor.rst
@@ -1,3 +1,9 @@
+#encoding utf-8
+
+Contributors
+
+::
+
   Armin Rigo
   Maciej Fijalkowski
   Carl Friedrich Bolz
@@ -307,7 +313,7 @@
   Mads Kiilerich
   Antony Lee
   Jason Madden
-  Daniel Neuhuser
+  Daniel Neuhuser
   reub...@gmail.com
   Yaroslav Fedevych
   Jim Hunziker
diff --git a/pypy/doc/cppyy.rst b/pypy/doc/cppyy.rst
--- a/pypy/doc/cppyy.rst
+++ b/pypy/doc/cppyy.rst
@@ -1,145 +1,61 @@
 cppyy: C++ bindings for PyPy
 
 
-The cppyy module creates, at run-time, Python-side classes and functions for
-C++, by querying a C++ reflection system.
-The default system used is `Reflex`_, which extracts the needed information
-from C++ header files.
-Another current backend is based on `CINT`_, and yet another, more important
-one for the medium- to long-term will be based on `cling`_.
-The latter sits on top of `llvm`_'s `clang`_, and will therefore allow the use
-of C++11.
-The work on the cling backend has so far been done only for CPython, but
-bringing it to PyPy is a lot less work than developing it in the first place.
+The cppyy module delivers dynamic Python-C++ bindings.
+It is designed for automation, high performance, scale, interactivity, and
+handling all of modern C++ (11, 14, etc.).
+It is based on `Cling`_ which, through `LLVM`_/`clang`_, provides C++
+reflection and interactivity.
+Reflection information is extracted from C++ header files.
+Cppyy itself is built into PyPy (an alternative exists for CPython), but
+it requires a `backend`_, installable through pip, to interface with Cling.
 
-.. _Reflex: https://root.cern.ch/how/how-use-reflex
-.. _CINT: https://root.cern.ch/introduction-cint
-.. _cling: https://root.cern.ch/cling
-.. _llvm: http://llvm.org/
+.. _Cling: https://root.cern.ch/cling
+.. _LLVM: http://llvm.org/
 .. _clang: http://clang.llvm.org/
-
-This document describes the version of cppyy that lives in the main branch of
-PyPy.
-The development of cppyy happens in the "reflex-support" branch.
-
-
-Motivation
---
-
-To provide bindings to another language in CPython, you program to a
-generic C-API that exposes many of the interpreter features.
-With PyPy, however, there is no such generic C-API, because several of the
-interpreter features (e.g. the memory model) are pluggable and therefore
-subject to change.
-Furthermore, a generic API does not allow any assumptions about the calls
-into another language, forcing the JIT to behave conservatively around these
-calls and with the objects that cross language boundaries.
-In contrast, cppyy does not expose an API, but expects one to be implemented
-by a backend.
-It makes strong assumptions about the semantics of the API that it uses and
-that in turn allows the JIT to make equally strong assumptions.
-This is possible, because the expected API is only for providing C++ language
-bindings, and does not provide generic programmability.
-
-The cppyy module further offers two features, which result in improved
-performance as well as better functionality and cross-language integration.
-First, cppyy itself is written in RPython and therefore open to optimizations
-by the JIT up until the actual point of call into C++.
-This means for example, that if variables are already unboxed by the JIT, they
-can be passed through directly to C++.
-Second, a backend such as Reflex (and cling far more so) adds dynamic features
-to C++, thus greatly reducing impedance mismatches between the two languages.
-For example, Reflex is dynamic enough to allow writing runtime bindings
-generation in python (as opposed to RPython) and this is used to create very
-natural "pythonizations" of the bound code.
-As another example, cling allows automatic instantiations of 

[pypy-commit] pypy better-PyDict_Next: dealloc at first opportunity, rather than wait for dict_dealloc (cfbolz)

2016-12-17 Thread mattip
Author: Matti Picus 
Branch: better-PyDict_Next
Changeset: r89125:61a5f611cb3a
Date: 2016-12-17 21:52 +0200
http://bitbucket.org/pypy/pypy/changeset/61a5f611cb3a/

Log:dealloc at first opportunity, rather than wait for dict_dealloc
(cfbolz)

diff --git a/pypy/module/cpyext/dictobject.py b/pypy/module/cpyext/dictobject.py
--- a/pypy/module/cpyext/dictobject.py
+++ b/pypy/module/cpyext/dictobject.py
@@ -10,6 +10,7 @@
 from pypy.module.cpyext.pyobject import (PyObject, PyObjectP, as_pyobj, 
 make_typedescr, track_reference, create_ref, from_ref, decref,
 Py_IncRef)
+from pypy.module.cpyext.object import _dealloc
 from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
 
 PyDictObjectStruct = lltype.ForwardReference()
@@ -58,7 +59,7 @@
 def dict_dealloc(space, py_obj):
 py_dict = rffi.cast(PyDictObject, py_obj)
 decref(space, py_dict.c_ob_keys)
-from pypy.module.cpyext.object import _dealloc
+py_dict.c_ob_keys = lltype.nullptr(PyObject.TO)
 _dealloc(space, py_obj)
 
 @cpython_api([], PyObject)
@@ -270,6 +271,8 @@
 w_keys = from_ref(space, py_dict.c_ob_keys)
 ppos[0] += 1
 if pos >= space.len_w(w_keys):
+decref(space, py_dict.c_ob_keys)
+py_dict.c_ob_keys = lltype.nullptr(PyObject.TO)
 return 0
 w_key = space.listview(w_keys)[pos]
 w_value = space.getitem(w_dict, w_key)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy rffi-parser: Handle function pointers and void

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89124:3ef445c0db43
Date: 2016-12-17 18:58 +
http://bitbucket.org/pypy/pypy/changeset/3ef445c0db43/

Log:Handle function pointers and void

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
@@ -621,6 +621,8 @@
 } PyVarObject;
 
 typedef struct _typeobject PyTypeObject;
+
+typedef void (*freefunc)(void *);
 """)
 h.configure_types()
 
diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -726,7 +726,18 @@
 self.structs[obj] = result
 return result
 elif isinstance(obj, model.PointerType):
-return lltype.Ptr(self.convert_type(obj.totype))
+TO = self.convert_type(obj.totype)
+if TO is lltype.Void:
+return rffi.VOIDP
+return lltype.Ptr(TO)
+elif isinstance(obj, model.FunctionPtrType):
+if obj.ellipsis:
+raise NotImplementedError
+args = [self.convert_type(arg) for arg in obj.args]
+res = self.convert_type(obj.result)
+return lltype.Ptr(lltype.FuncType(args, res))
+elif isinstance(obj, model.VoidType):
+return lltype.Void
 else:
 raise NotImplementedError
 
diff --git a/pypy/module/cpyext/typeobjectdefs.py 
b/pypy/module/cpyext/typeobjectdefs.py
--- a/pypy/module/cpyext/typeobjectdefs.py
+++ b/pypy/module/cpyext/typeobjectdefs.py
@@ -5,13 +5,15 @@
 Py_TPFLAGS_READYING, Py_TPFLAGS_READY, Py_TPFLAGS_HEAPTYPE)
 from pypy.module.cpyext.pyobject import PyObject, make_ref, from_ref
 from pypy.module.cpyext.modsupport import PyMethodDef
-from pypy.module.cpyext.api import Py_bufferP
+from pypy.module.cpyext.api import Py_bufferP, h
 
 
 P, FT, PyO = Ptr, FuncType, PyObject
 PyOPtr = Ptr(lltype.Array(PyO, hints={'nolength': True}))
 
-freefunc = P(FT([rffi.VOIDP], Void))
+#freefunc = P(FT([rffi.VOIDP], Void))
+freefunc = h.definitions['freefunc']
+
 destructor = P(FT([PyO], Void))
 printfunc = P(FT([PyO, FILEP, rffi.INT_real], rffi.INT))
 getattrfunc = P(FT([PyO, rffi.CCHARP], PyO))
@@ -200,7 +202,7 @@
 ("tp_clear", inquiry),#U
 
 # Assigned meaning in release 2.1
-# rich comparisons 
+# rich comparisons
 ("tp_richcompare", richcmpfunc), #N
 
 # weak reference enabler
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Add a minimal amount of ll_assert_not_none(), notably on the popvalue()

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89123:7904f63e30ef
Date: 2016-12-17 18:36 +0100
http://bitbucket.org/pypy/pypy/changeset/7904f63e30ef/

Log:Add a minimal amount of ll_assert_not_none(), notably on the
popvalue() method

diff --git a/pypy/interpreter/pyframe.py b/pypy/interpreter/pyframe.py
--- a/pypy/interpreter/pyframe.py
+++ b/pypy/interpreter/pyframe.py
@@ -4,6 +4,7 @@
 import sys
 from rpython.rlib import jit
 from rpython.rlib.debug import make_sure_not_resized, check_nonneg
+from rpython.rlib.debug import ll_assert_not_none
 from rpython.rlib.jit import hint
 from rpython.rlib.objectmodel import instantiate, specialize, we_are_translated
 from rpython.rlib.rarithmetic import intmask, r_uint
@@ -298,7 +299,13 @@
 # stack manipulation helpers
 def pushvalue(self, w_object):
 depth = self.valuestackdepth
-self.locals_cells_stack_w[depth] = w_object
+self.locals_cells_stack_w[depth] = ll_assert_not_none(w_object)
+self.valuestackdepth = depth + 1
+
+def pushvalue_none(self):
+depth = self.valuestackdepth
+# the entry is already None, and remains None
+assert self.locals_cells_stack_w[depth] is None
 self.valuestackdepth = depth + 1
 
 def _check_stack_index(self, index):
@@ -311,6 +318,9 @@
 return index >= stackstart
 
 def popvalue(self):
+return ll_assert_not_none(self.popvalue_maybe_none())
+
+def popvalue_maybe_none(self):
 depth = self.valuestackdepth - 1
 assert self._check_stack_index(depth)
 assert depth >= 0
@@ -385,6 +395,9 @@
 def peekvalue(self, index_from_top=0):
 # NOTE: top of the stack is peekvalue(0).
 # Contrast this with CPython where it's PEEK(-1).
+return ll_assert_not_none(self.peekvalue_maybe_none(index_from_top))
+
+def peekvalue_maybe_none(self, index_from_top=0):
 index_from_top = hint(index_from_top, promote=True)
 index = self.valuestackdepth + ~index_from_top
 assert self._check_stack_index(index)
@@ -396,7 +409,7 @@
 index = self.valuestackdepth + ~index_from_top
 assert self._check_stack_index(index)
 assert index >= 0
-self.locals_cells_stack_w[index] = w_object
+self.locals_cells_stack_w[index] = ll_assert_not_none(w_object)
 
 @jit.unroll_safe
 def dropvaluesuntil(self, finaldepth):
diff --git a/pypy/objspace/std/callmethod.py b/pypy/objspace/std/callmethod.py
--- a/pypy/objspace/std/callmethod.py
+++ b/pypy/objspace/std/callmethod.py
@@ -80,14 +80,14 @@
 if w_value is None:
 w_value = space.getattr(w_obj, w_name)
 f.pushvalue(w_value)
-f.pushvalue(None)
+f.pushvalue_none()
 
 @jit.unroll_safe
 def CALL_METHOD(f, oparg, *ignored):
 # opargs contains the arg, and kwarg count, excluding the implicit 'self'
 n_args = oparg & 0xff
 n_kwargs = (oparg >> 8) & 0xff
-w_self = f.peekvalue(n_args + (2 * n_kwargs))
+w_self = f.peekvalue_maybe_none(n_args + (2 * n_kwargs))
 n = n_args + (w_self is not None)
 
 if not n_kwargs:
@@ -115,7 +115,7 @@
 arguments, keywords, keywords_w, None, None,
 methodcall=w_self is not None)
 if w_self is None:
-f.popvalue()# removes w_self, which is None
+f.popvalue_maybe_none()# removes w_self, which is None
 w_callable = f.popvalue()
 if f.get_is_being_profiled() and function.is_builtin_code(w_callable):
 w_result = f.space.call_args_and_c_profile(f, w_callable, args)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Propagate debug.ll_assert_not_none() through the JIT, using the same

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89122:5efae655f1ce
Date: 2016-12-17 18:20 +0100
http://bitbucket.org/pypy/pypy/changeset/5efae655f1ce/

Log:Propagate debug.ll_assert_not_none() through the JIT, using the same
technique as jit.record_exact_class(). If we use it a bit inside
PyPy it could remove a good number of guard_nonnull or
guard_nonnull_class.

diff --git a/rpython/jit/codewriter/jtransform.py 
b/rpython/jit/codewriter/jtransform.py
--- a/rpython/jit/codewriter/jtransform.py
+++ b/rpython/jit/codewriter/jtransform.py
@@ -283,6 +283,12 @@
 def rewrite_op_jit_record_exact_class(self, op):
 return SpaceOperation("record_exact_class", [op.args[0], op.args[1]], 
None)
 
+def rewrite_op_debug_assert_not_none(self, op):
+if isinstance(op.args[0], Variable):
+return SpaceOperation('assert_not_none', [op.args[0]], None)
+else:
+return []
+
 def rewrite_op_cast_bool_to_int(self, op): pass
 def rewrite_op_cast_bool_to_uint(self, op): pass
 def rewrite_op_cast_char_to_int(self, op): pass
diff --git a/rpython/jit/codewriter/test/test_flatten.py 
b/rpython/jit/codewriter/test/test_flatten.py
--- a/rpython/jit/codewriter/test/test_flatten.py
+++ b/rpython/jit/codewriter/test/test_flatten.py
@@ -402,7 +402,7 @@
 
 self.encoding_test(f, [65], """
 raise $<* struct object>
-""")
+""", transform=True)
 
 def test_exc_raise_2(self):
 def g(i):
diff --git a/rpython/jit/metainterp/blackhole.py 
b/rpython/jit/metainterp/blackhole.py
--- a/rpython/jit/metainterp/blackhole.py
+++ b/rpython/jit/metainterp/blackhole.py
@@ -563,6 +563,10 @@
 ll_assert((i & 1) == 1, "bhimpl_cast_int_to_ptr: not an odd int")
 return lltype.cast_int_to_ptr(llmemory.GCREF, i)
 
+@arguments("r")
+def bhimpl_assert_not_none(a):
+assert a
+
 @arguments("r", "i")
 def bhimpl_record_exact_class(a, b):
 pass
diff --git a/rpython/jit/metainterp/executor.py 
b/rpython/jit/metainterp/executor.py
--- a/rpython/jit/metainterp/executor.py
+++ b/rpython/jit/metainterp/executor.py
@@ -5,6 +5,7 @@
 from rpython.rlib.rarithmetic import ovfcheck, r_longlong, is_valid_int
 from rpython.rlib.unroll import unrolling_iterable
 from rpython.rlib.objectmodel import specialize
+from rpython.rlib.debug import fatalerror
 from rpython.jit.metainterp.history import check_descr
 from rpython.jit.metainterp.history import INT, REF, FLOAT, VOID, AbstractDescr
 from rpython.jit.metainterp.history import ConstInt, ConstFloat, ConstPtr
@@ -321,6 +322,10 @@
 def do_keepalive(cpu, _, x):
 pass
 
+def do_assert_not_none(cpu, _, box):
+if not box.getref_base():
+fatalerror("found during JITting: ll_assert_not_none() failed")
+
 # 
 
 
diff --git a/rpython/jit/metainterp/optimizeopt/rewrite.py 
b/rpython/jit/metainterp/optimizeopt/rewrite.py
--- a/rpython/jit/metainterp/optimizeopt/rewrite.py
+++ b/rpython/jit/metainterp/optimizeopt/rewrite.py
@@ -499,6 +499,9 @@
 box = self.get_box_replacement(op.getarg(0))
 self.make_constant(box, CONST_0)
 
+def optimize_ASSERT_NOT_NONE(self, op):
+self.make_nonnull(op.getarg(0))
+
 def optimize_RECORD_EXACT_CLASS(self, op):
 opinfo = self.getptrinfo(op.getarg(0))
 expectedclassbox = op.getarg(1)
diff --git a/rpython/jit/metainterp/optimizeopt/simplify.py 
b/rpython/jit/metainterp/optimizeopt/simplify.py
--- a/rpython/jit/metainterp/optimizeopt/simplify.py
+++ b/rpython/jit/metainterp/optimizeopt/simplify.py
@@ -42,6 +42,9 @@
 # but it's a bit hard to implement robustly if heap.py is also run
 pass
 
+def optimize_ASSERT_NOT_NONE(self, op):
+pass
+
 def optimize_RECORD_EXACT_CLASS(self, op):
 pass
 
diff --git a/rpython/jit/metainterp/optimizeopt/test/test_optimizebasic.py 
b/rpython/jit/metainterp/optimizeopt/test/test_optimizebasic.py
--- a/rpython/jit/metainterp/optimizeopt/test/test_optimizebasic.py
+++ b/rpython/jit/metainterp/optimizeopt/test/test_optimizebasic.py
@@ -5595,6 +5595,19 @@
 """
 self.optimize_loop(ops, expected)
 
+def test_assert_not_none(self):
+ops = """
+[p0]
+assert_not_none(p0)
+guard_nonnull(p0) []
+finish()
+"""
+expected = """
+[p0]
+finish()
+"""
+self.optimize_loop(ops, expected)
+
 
 class TestLLtype(BaseTestOptimizeBasic, LLtypeMixin):
 pass
diff --git a/rpython/jit/metainterp/pyjitpl.py 
b/rpython/jit/metainterp/pyjitpl.py
--- a/rpython/jit/metainterp/pyjitpl.py
+++ b/rpython/jit/metainterp/pyjitpl.py
@@ -275,12 +275,18 @@
 def opimpl_ptr_iszero(self, box):
 return self.execute(rop.PTR_EQ, box, history.CONST_NULL)
 
+@arguments("box")
+def opimpl_assert_not_none(self, box):
+if 

[pypy-commit] pypy issue2444: use FinalizerQueue (with minor hack for tests)

2016-12-17 Thread mattip
Author: Matti Picus 
Branch: issue2444
Changeset: r89121:f66ee40b4bc5
Date: 2016-12-17 20:14 +0200
http://bitbucket.org/pypy/pypy/changeset/f66ee40b4bc5/

Log:use FinalizerQueue (with minor hack for tests)

diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -4,6 +4,7 @@
 
 from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.rlib.rarithmetic import widen
+from rpython.rlib import rgc # Force registration of gc.collect
 from pypy.module.cpyext.api import (
 cpython_api, generic_cpy_call, PyObject, Py_ssize_t, Py_TPFLAGS_CHECKTYPES,
 mangle_name, pypy_decl, Py_buffer, Py_bufferP)
@@ -343,7 +344,6 @@
 self.releasebufferproc = releasebuffer
 
 def releasebuffer(self):
-print '--'
 if self.releasebufferproc:
 func_target = rffi.cast(releasebufferproc, self.releasebufferproc)
 with lltype.scoped_alloc(Py_buffer) as pybuf:
@@ -385,6 +385,17 @@
 # absolutely no safety checks, what could go wrong?
 self.ptr[index] = char
 
+class FQ(rgc.FinalizerQueue):
+Class = CPyBuffer
+def finalizer_trigger(self):
+while 1:
+buf  = self.next_dead()
+if not buf:
+break
+buf.releasebuffer()
+
+fq = FQ()
+
 def wrap_getreadbuffer(space, w_self, w_args, func):
 func_target = rffi.cast(readbufferproc, func)
 py_obj = make_ref(space, w_self)
@@ -395,8 +406,10 @@
 size = generic_cpy_call(space, func_target, w_self, index, ptr)
 if size < 0:
 space.fromcache(State).check_and_raise_exception(always=True)
-return space.newbuffer(CPyBuffer(space, ptr[0], size, w_self, 
-   releasebuffer=releasebuffer))
+buf = CPyBuffer(space, ptr[0], size, w_self, 
+   releasebuffer=releasebuffer)
+fq.register_finalizer(buf)
+return space.newbuffer(buf)
 
 def wrap_getwritebuffer(space, w_self, w_args, func):
 func_target = rffi.cast(readbufferproc, func)
@@ -408,8 +421,10 @@
 size = generic_cpy_call(space, func_target, w_self, index, ptr)
 if size < 0:
 space.fromcache(State).check_and_raise_exception(always=True)
-return space.newbuffer(CPyBuffer(space, ptr[0], size, w_self, 
readonly=False,
-   releasebuffer=releasebuffer))
+buf = CPyBuffer(space, ptr[0], size, w_self, readonly=False,
+   releasebuffer=releasebuffer)
+fq.register_finalizer(buf)
+return space.newbuffer(buf)
 
 def wrap_getbuffer(space, w_self, w_args, func):
 func_target = rffi.cast(getbufferproc, func)
@@ -436,11 +451,13 @@
 format = rffi.charp2str(pybuf.c_format)
 else:
 format = 'B'
-return space.newbuffer(CPyBuffer(space, ptr, size, w_self, 
format=format,
+buf = CPyBuffer(space, ptr, size, w_self, format=format,
 ndim=ndim, shape=shape, strides=strides,
 itemsize=pybuf.c_itemsize,
 readonly=widen(pybuf.c_readonly),
-releasebuffer = releasebuffer))
+releasebuffer = releasebuffer)
+fq.register_finalizer(buf)
+return space.newbuffer(buf)
 
 def get_richcmp_func(OP_CONST):
 def inner(space, w_self, w_args, func):
diff --git a/pypy/module/cpyext/test/test_bufferobject.py 
b/pypy/module/cpyext/test/test_bufferobject.py
--- a/pypy/module/cpyext/test/test_bufferobject.py
+++ b/pypy/module/cpyext/test/test_bufferobject.py
@@ -114,9 +114,8 @@
 """, )
 import gc
 assert module.get_cnt() == 0
-print '++'
 a = memoryview(module.create_test())
-print 'xxx'
 assert module.get_cnt() == 1
 del a
+gc.collect(); gc.collect(); gc.collect()
 assert module.get_cnt() == 0
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
@@ -323,7 +323,6 @@
 
 def newbuffer(self, obj):
 ret = W_Buffer(obj)
-ret.register_finalizer(self)
 return ret
 
 def newbytes(self, s):
diff --git a/rpython/rlib/rgc.py b/rpython/rlib/rgc.py
--- a/rpython/rlib/rgc.py
+++ b/rpython/rlib/rgc.py
@@ -450,6 +450,7 @@
 "the object must have a __dict__" % (obj,))
 assert (not hasattr(obj, '__slots__') or
 type(obj).__slots__ == () or
+type(obj).__slots__ == ['readonly'] or
 type(obj).__slots__ == ('__weakref__',)), (
 "%r: to run register_finalizer() untranslated, "
 "the object must not have __slots__" % (obj,))

[pypy-commit] pypy rffi-parser: Do one configure_types call per ParsedSource instance

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89120:d8f4db529986
Date: 2016-12-17 17:47 +
http://bitbucket.org/pypy/pypy/changeset/d8f4db529986/

Log:Do one configure_types call per ParsedSource instance

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
@@ -268,8 +268,6 @@
 self.gil = gil
 self.result_borrowed = result_borrowed
 self.result_is_ll = result_is_ll
-if result_is_ll:# means 'returns a low-level PyObject pointer'
-assert is_PyObject(restype)
 #
 def get_llhelper(space):
 return llhelper(self.functype, self.get_wrapper(space))
@@ -624,6 +622,7 @@
 
 typedef struct _typeobject PyTypeObject;
 """)
+h.configure_types()
 
 Py_ssize_t = h.definitions['Py_ssize_t']
 Py_ssize_tP = rffi.CArrayPtr(Py_ssize_t)
diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -671,11 +671,15 @@
 
 class ParsedSource(object):
 def __init__(self, source, parser, definitions=None, macros=None):
+from pypy.module.cpyext.api import CConfig
 self.source = source
 self.definitions = definitions if definitions is not None else {}
 self.macros = macros if macros is not None else {}
 self.structs = {}
 self.ctx = parser
+self._Config = type('Config', (object,), {})
+self._Config._compilation_info_ = CConfig._compilation_info_
+self._TYPES = {}
 
 def add_typedef(self, name, obj):
 assert name not in self.definitions
@@ -701,12 +705,17 @@
 def realize_struct(self, struct, type_name):
 from pypy.module.cpyext.api import CConfig, TYPES
 configname = type_name.replace(' ', '__')
-setattr(CConfig, configname,
+setattr(self._Config, configname,
 rffi_platform.Struct(type_name, struct.fields))
 forward = lltype.ForwardReference()
-TYPES[configname] = forward
+self._TYPES[configname] = forward
 return forward
 
+def configure_types(self):
+for name, TYPE in rffi_platform.configure(self._Config).iteritems():
+if name in self._TYPES:
+self._TYPES[name].become(TYPE)
+
 def convert_type(self, obj):
 if isinstance(obj, model.PrimitiveType):
 return cname_to_lltype(obj.name)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Change https://www.verisign.net/, which has been failing for two days,

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89119:ecfd182b1eb3
Date: 2016-12-17 18:15 +0100
http://bitbucket.org/pypy/pypy/changeset/ecfd182b1eb3/

Log:Change https://www.verisign.net/, which has been failing for two
days, to https://gmail.com/

diff --git a/pypy/module/_ssl/test/test_ssl.py 
b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -169,8 +169,8 @@
 }
 
 def setup_method(self, method):
-# https://www.verisign.net/
-ADDR = "www.verisign.net", 443
+# https://gmail.com/
+ADDR = "gmail.com", 443
 
 self.w_s = self.space.appexec([self.space.wrap(ADDR)], """(ADDR):
 import socket
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: flow space: only emit ll_assert_not_none() in some forms of raise,

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89118:e58b69ffe5aa
Date: 2016-12-17 17:59 +0100
http://bitbucket.org/pypy/pypy/changeset/e58b69ffe5aa/

Log:flow space: only emit ll_assert_not_none() in some forms of raise,
not if we built the exception instance just now

diff --git a/rpython/flowspace/flowcontext.py b/rpython/flowspace/flowcontext.py
--- a/rpython/flowspace/flowcontext.py
+++ b/rpython/flowspace/flowcontext.py
@@ -599,6 +599,7 @@
 """
 from rpython.rlib.debug import ll_assert_not_none
 
+check_not_none = False
 w_is_type = op.isinstance(w_arg1, const(type)).eval(self)
 if self.guessbool(w_is_type):
 # this is for all cases of the form (Class, something)
@@ -610,6 +611,7 @@
 if self.guessbool(op.issubtype(w_valuetype, 
w_arg1).eval(self)):
 # raise Type, Instance: let etype be the exact type of 
value
 w_value = w_arg2
+check_not_none = True
 else:
 # raise Type, X: assume X is the constructor argument
 w_value = op.simple_call(w_arg1, w_arg2).eval(self)
@@ -620,7 +622,10 @@
 "separate value")
 raise Raise(const(exc))
 w_value = w_arg1
-w_value = op.simple_call(const(ll_assert_not_none), w_value).eval(self)
+check_not_none = True
+if check_not_none:
+w_value = op.simple_call(const(ll_assert_not_none),
+ w_value).eval(self)
 w_type = op.type(w_value).eval(self)
 return FSException(w_type, w_value)
 
diff --git a/rpython/jit/codewriter/test/test_flatten.py 
b/rpython/jit/codewriter/test/test_flatten.py
--- a/rpython/jit/codewriter/test/test_flatten.py
+++ b/rpython/jit/codewriter/test/test_flatten.py
@@ -466,6 +466,14 @@
 int_return $True
 """, transform=True)
 
+def test_assert_disappears(self):
+def f(i):
+assert i > 5
+return i
+self.encoding_test(f, [7], """
+int_return %i0
+""")
+
 def test_int_floordiv_ovf_zer(self):
 def f(i, j):
 assert i >= 0
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy rffi-parser: Inline cpython_struct() into ParsedSource

2016-12-17 Thread rlamy
Author: Ronan Lamy 
Branch: rffi-parser
Changeset: r89117:768d5bba506e
Date: 2016-12-17 16:09 +
http://bitbucket.org/pypy/pypy/changeset/768d5bba506e/

Log:Inline cpython_struct() into ParsedSource

diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -4,11 +4,11 @@
 import pycparser
 import weakref, re
 from rpython.rtyper.lltypesystem import rffi, lltype
-from rpython.rtyper.tool import rfficache
+from rpython.rtyper.tool import rfficache, rffi_platform
 
 _r_comment = re.compile(r"/\*.*?\*/|//([^\n\\]|\\.)*?$",
 re.DOTALL | re.MULTILINE)
-_r_define  = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)"
+_r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)"
 r"\b((?:[^\n\\]|\\.)*?)$",
 re.DOTALL | re.MULTILINE)
 _r_words = re.compile(r"\w+|\S")
@@ -665,10 +665,6 @@
 self.struct_name = name
 self.fields = fields
 
-def realize(self, type_name):
-from pypy.module.cpyext.api import cpython_struct
-return cpython_struct(type_name, self.fields)
-
 def __repr__(self):
 return "".format(vars(self))
 
@@ -685,7 +681,7 @@
 assert name not in self.definitions
 tp = self.convert_type(obj)
 if isinstance(tp, DelayedStruct):
-tp = tp.realize(name)
+tp = self.realize_struct(tp, name)
 self.structs[obj] = tp
 self.definitions[name] = lltype.Typedef(tp, name)
 
@@ -693,20 +689,31 @@
 assert name not in self.macros
 self.macros[name] = value
 
+def new_struct(self, obj):
+if obj.fldtypes is None:
+return lltype.ForwardReference()
+else:
+fields = zip(
+obj.fldnames,
+[self.convert_type(field) for field in obj.fldtypes])
+return DelayedStruct(obj.name, fields)
+
+def realize_struct(self, struct, type_name):
+from pypy.module.cpyext.api import CConfig, TYPES
+configname = type_name.replace(' ', '__')
+setattr(CConfig, configname,
+rffi_platform.Struct(type_name, struct.fields))
+forward = lltype.ForwardReference()
+TYPES[configname] = forward
+return forward
+
 def convert_type(self, obj):
 if isinstance(obj, model.PrimitiveType):
 return cname_to_lltype(obj.name)
 elif isinstance(obj, model.StructType):
-from pypy.module.cpyext.api import cpython_struct
 if obj in self.structs:
 return self.structs[obj]
-if obj.fldtypes is None:
-result = lltype.ForwardReference()
-else:
-fields = zip(
-obj.fldnames,
-[self.convert_type(field) for field in obj.fldtypes])
-result = DelayedStruct(obj.name, fields)
+result = self.new_struct(obj)
 self.structs[obj] = result
 return result
 elif isinstance(obj, model.PointerType):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Add the config option 'disable_entrypoints' for embedding PyPy together

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89114:17a956e1c059
Date: 2016-12-17 14:10 +0100
http://bitbucket.org/pypy/pypy/changeset/17a956e1c059/

Log:Add the config option 'disable_entrypoints' for embedding PyPy
together with another RPython VM

diff --git a/pypy/config/pypyoption.py b/pypy/config/pypyoption.py
--- a/pypy/config/pypyoption.py
+++ b/pypy/config/pypyoption.py
@@ -190,6 +190,12 @@
"make sure that all calls go through space.call_args",
default=False),
 
+BoolOption("disable_entrypoints",
+   "Disable external entry points, notably the"
+   " cpyext module and cffi's embedding mode.",
+   default=False,
+   requires=[("objspace.usemodules.cpyext", False)]),
+
 OptionDescription("std", "Standard Object Space Options", [
 BoolOption("withtproxy", "support transparent proxies",
default=True),
diff --git a/pypy/goal/targetpypystandalone.py 
b/pypy/goal/targetpypystandalone.py
--- a/pypy/goal/targetpypystandalone.py
+++ b/pypy/goal/targetpypystandalone.py
@@ -83,12 +83,18 @@
 return 1
 return exitcode
 
+return entry_point, get_additional_entrypoints(space)
+
+
+def get_additional_entrypoints(space):
 # register the minimal equivalent of running a small piece of code. This
 # should be used as sparsely as possible, just to register callbacks
-
 from rpython.rlib.entrypoint import entrypoint_highlevel
 from rpython.rtyper.lltypesystem import rffi, lltype
 
+if space.config.objspace.disable_entrypoints:
+return {}
+
 @entrypoint_highlevel('main', [rffi.CCHARP, rffi.INT],
   c_name='pypy_setup_home')
 def pypy_setup_home(ll_home, verbose):
@@ -188,11 +194,11 @@
 return -1
 return 0
 
-return entry_point, {'pypy_execute_source': pypy_execute_source,
- 'pypy_execute_source_ptr': pypy_execute_source_ptr,
- 'pypy_init_threads': pypy_init_threads,
- 'pypy_thread_attach': pypy_thread_attach,
- 'pypy_setup_home': pypy_setup_home}
+return {'pypy_execute_source': pypy_execute_source,
+'pypy_execute_source_ptr': pypy_execute_source_ptr,
+'pypy_init_threads': pypy_init_threads,
+'pypy_thread_attach': pypy_thread_attach,
+'pypy_setup_home': pypy_setup_home}
 
 
 # _ Define and setup target ___
diff --git a/pypy/module/_cffi_backend/__init__.py 
b/pypy/module/_cffi_backend/__init__.py
--- a/pypy/module/_cffi_backend/__init__.py
+++ b/pypy/module/_cffi_backend/__init__.py
@@ -71,10 +71,11 @@
 def __init__(self, space, *args):
 MixedModule.__init__(self, space, *args)
 #
-# import 'embedding', which has the side-effect of registering
-# the 'pypy_init_embedded_cffi_module' entry point
-from pypy.module._cffi_backend import embedding
-embedding.glob.space = space
+if not space.config.objspace.disable_entrypoints:
+# import 'embedding', which has the side-effect of registering
+# the 'pypy_init_embedded_cffi_module' entry point
+from pypy.module._cffi_backend import embedding
+embedding.glob.space = space
 
 
 def get_dict_rtld_constants():
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: Try to import 'embedding' from __init__(), which might allow us

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89113:be4a524fa746
Date: 2016-12-17 12:45 +0100
http://bitbucket.org/pypy/pypy/changeset/be4a524fa746/

Log:Try to import 'embedding' from __init__(), which might allow us to
check a space flag

diff --git a/pypy/module/_cffi_backend/__init__.py 
b/pypy/module/_cffi_backend/__init__.py
--- a/pypy/module/_cffi_backend/__init__.py
+++ b/pypy/module/_cffi_backend/__init__.py
@@ -1,6 +1,6 @@
 import sys
 from pypy.interpreter.mixedmodule import MixedModule
-from rpython.rlib import rdynload, clibffi, entrypoint
+from rpython.rlib import rdynload, clibffi
 from rpython.rtyper.lltypesystem import rffi
 
 VERSION = "1.9.1"
@@ -68,7 +68,11 @@
 if has_stdcall:
 interpleveldefs['FFI_STDCALL'] = 'space.wrap(%d)' % FFI_STDCALL
 
-def startup(self, space):
+def __init__(self, space, *args):
+MixedModule.__init__(self, space, *args)
+#
+# import 'embedding', which has the side-effect of registering
+# the 'pypy_init_embedded_cffi_module' entry point
 from pypy.module._cffi_backend import embedding
 embedding.glob.space = space
 
@@ -85,11 +89,3 @@
 
 for _name, _value in get_dict_rtld_constants().items():
 Module.interpleveldefs[_name] = 'space.wrap(%d)' % _value
-
-
-# write this entrypoint() here, to make sure it is registered early enough
-@entrypoint.entrypoint_highlevel('main', [rffi.INT, rffi.VOIDP],
- c_name='pypy_init_embedded_cffi_module')
-def pypy_init_embedded_cffi_module(version, init_struct):
-from pypy.module._cffi_backend import embedding
-return embedding.pypy_init_embedded_cffi_module(version, init_struct)
diff --git a/pypy/module/_cffi_backend/embedding.py 
b/pypy/module/_cffi_backend/embedding.py
--- a/pypy/module/_cffi_backend/embedding.py
+++ b/pypy/module/_cffi_backend/embedding.py
@@ -1,4 +1,5 @@
 import os
+from rpython.rlib import entrypoint
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 
@@ -46,6 +47,8 @@
 glob = Global()
 
 
+@entrypoint.entrypoint_highlevel('main', [rffi.INT, rffi.VOIDP],
+ c_name='pypy_init_embedded_cffi_module')
 def pypy_init_embedded_cffi_module(version, init_struct):
 # called from __init__.py
 name = "?"
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy py3.5: missing files

2016-12-17 Thread plan_rich
Author: Richard Plangger 
Branch: py3.5
Changeset: r89112:a357008bf0d3
Date: 2016-12-17 12:03 +0100
http://bitbucket.org/pypy/pypy/changeset/a357008bf0d3/

Log:missing files

diff --git a/lib_pypy/_cffi_ssl/_cffi_src/openssl/osrandom_engine.py 
b/lib_pypy/_cffi_ssl/_cffi_src/openssl/osrandom_engine.py
new file mode 100644
--- /dev/null
+++ b/lib_pypy/_cffi_ssl/_cffi_src/openssl/osrandom_engine.py
@@ -0,0 +1,29 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import absolute_import, division, print_function
+
+import os
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+
+with open(os.path.join(HERE, "src/osrandom_engine.h")) as f:
+INCLUDES = f.read()
+
+TYPES = """
+static const char *const Cryptography_osrandom_engine_name;
+static const char *const Cryptography_osrandom_engine_id;
+"""
+
+FUNCTIONS = """
+int Cryptography_add_osrandom_engine(void);
+"""
+
+MACROS = """
+"""
+
+with open(os.path.join(HERE, "src/osrandom_engine.c")) as f:
+CUSTOMIZATIONS = f.read()
+
+CONDITIONAL_NAMES = {}
diff --git a/lib_pypy/_cffi_ssl/_cffi_src/openssl/src/osrandom_engine.c 
b/lib_pypy/_cffi_ssl/_cffi_src/openssl/src/osrandom_engine.c
new file mode 100644
--- /dev/null
+++ b/lib_pypy/_cffi_ssl/_cffi_src/openssl/src/osrandom_engine.c
@@ -0,0 +1,576 @@
+/* osurandom engine
+ *
+ * Windows CryptGenRandom()
+ * macOS >= 10.12  getentropy()
+ * OpenBSD 5.6+getentropy()
+ * other BSD   getentropy() if SYS_getentropy is defined
+ * Linux 3.4.17+   getrandom() with fallback to /dev/urandom
+ * other   /dev/urandom with cached fd
+ *
+ * The /dev/urandom, getrandom and getentropy code is derived from Python's
+ * Python/random.c, written by Antoine Pitrou and Victor Stinner.
+ *
+ * Copyright 2001-2016 Python Software Foundation; All Rights Reserved.
+ */
+
+static const char *Cryptography_osrandom_engine_id = "osrandom";
+
+/
+ * Windows
+ */
+#if CRYPTOGRAPHY_OSRANDOM_ENGINE == CRYPTOGRAPHY_OSRANDOM_ENGINE_CRYPTGENRANDOM
+static const char *Cryptography_osrandom_engine_name = "osrandom_engine 
CryptGenRandom()";
+static HCRYPTPROV hCryptProv = 0;
+
+static int osrandom_init(ENGINE *e) {
+if (hCryptProv != 0) {
+return 1;
+}
+if (CryptAcquireContext(, NULL, NULL,
+PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
+return 1;
+} else {
+ERR_Cryptography_OSRandom_error(
+CRYPTOGRAPHY_OSRANDOM_F_INIT,
+CRYPTOGRAPHY_OSRANDOM_R_CRYPTACQUIRECONTEXT,
+__FILE__, __LINE__
+);
+return 0;
+}
+}
+
+static int osrandom_rand_bytes(unsigned char *buffer, int size) {
+if (hCryptProv == 0) {
+return 0;
+}
+
+if (!CryptGenRandom(hCryptProv, (DWORD)size, buffer)) {
+ERR_Cryptography_OSRandom_error(
+CRYPTOGRAPHY_OSRANDOM_F_RAND_BYTES,
+CRYPTOGRAPHY_OSRANDOM_R_CRYPTGENRANDOM,
+__FILE__, __LINE__
+);
+return 0;
+}
+return 1;
+}
+
+static int osrandom_finish(ENGINE *e) {
+if (CryptReleaseContext(hCryptProv, 0)) {
+hCryptProv = 0;
+return 1;
+} else {
+ERR_Cryptography_OSRandom_error(
+CRYPTOGRAPHY_OSRANDOM_F_FINISH,
+CRYPTOGRAPHY_OSRANDOM_R_CRYPTRELEASECONTEXT,
+__FILE__, __LINE__
+);
+return 0;
+}
+}
+
+static int osrandom_rand_status(void) {
+return hCryptProv != 0;
+}
+
+static const char *osurandom_get_implementation(void) {
+return "CryptGenRandom";
+}
+
+#endif /* CRYPTOGRAPHY_OSRANDOM_ENGINE_CRYPTGENRANDOM */
+
+/
+ * BSD getentropy
+ */
+#if CRYPTOGRAPHY_OSRANDOM_ENGINE == CRYPTOGRAPHY_OSRANDOM_ENGINE_GETENTROPY
+static const char *Cryptography_osrandom_engine_name = "osrandom_engine 
getentropy()";
+
+static int osrandom_init(ENGINE *e) {
+return 1;
+}
+
+static int osrandom_rand_bytes(unsigned char *buffer, int size) {
+int len, res;
+while (size > 0) {
+/* OpenBSD and macOS restrict maximum buffer size to 256. */
+len = size > 256 ? 256 : size;
+res = getentropy(buffer, len);
+if (res < 0) {
+ERR_Cryptography_OSRandom_error(
+CRYPTOGRAPHY_OSRANDOM_F_RAND_BYTES,
+CRYPTOGRAPHY_OSRANDOM_R_GETENTROPY_FAILED,
+__FILE__, __LINE__
+);
+return 0;
+}
+buffer += len;
+size -= len;
+}
+return 1;
+}
+
+static int osrandom_finish(ENGINE *e) {
+return 1;
+}
+
+static int osrandom_rand_status(void) {
+return 1;
+}
+
+static const char *osurandom_get_implementation(void) {
+return "getentropy";
+}
+#endif /* 

[pypy-commit] pypy py3.5: update comments

2016-12-17 Thread plan_rich
Author: Richard Plangger 
Branch: py3.5
Changeset: r89111:699a508dff49
Date: 2016-12-17 11:50 +0100
http://bitbucket.org/pypy/pypy/changeset/699a508dff49/

Log:update comments

diff --git a/lib_pypy/_cffi_ssl/_stdssl/__init__.py 
b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
--- a/lib_pypy/_cffi_ssl/_stdssl/__init__.py
+++ b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
@@ -100,7 +100,8 @@
 lib.OpenSSL_add_all_algorithms()
 
 def check_signals():
-# TODO PyErr_CheckSignal equivalent for pypy?
+# nothing to do, we are on python level, signals are
+# checked frequently in the bytecode dispatch loop
 pass
 
 def _socket_timeout(s):
@@ -347,7 +348,6 @@
 peer_cert = ffi.gc(peer_cert, lib.X509_free)
 self.peer_cert = peer_cert
 
-#PySSL_END_ALLOW_THREADS
 self.handshake_done = 1
 return None
 
diff --git a/lib_pypy/_cffi_ssl/_stdssl/certificate.py 
b/lib_pypy/_cffi_ssl/_stdssl/certificate.py
--- a/lib_pypy/_cffi_ssl/_stdssl/certificate.py
+++ b/lib_pypy/_cffi_ssl/_stdssl/certificate.py
@@ -151,7 +151,6 @@
 value = lib.X509_NAME_ENTRY_get_data(entry);
 attr = _create_tuple_for_attribute(name, value);
 if attr == ffi.NULL:
-pass # TODO error
 raise NotImplementedError
 rdn.append(attr)
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy py3.5: call _setup_ssl_threads()

2016-12-17 Thread plan_rich
Author: Richard Plangger 
Branch: py3.5
Changeset: r89110:94808865e615
Date: 2016-12-17 11:05 +0100
http://bitbucket.org/pypy/pypy/changeset/94808865e615/

Log:call _setup_ssl_threads()

diff --git a/lib_pypy/_cffi_ssl/_stdssl/__init__.py 
b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
--- a/lib_pypy/_cffi_ssl/_stdssl/__init__.py
+++ b/lib_pypy/_cffi_ssl/_stdssl/__init__.py
@@ -96,7 +96,7 @@
 # init open ssl
 lib.SSL_load_error_strings()
 lib.SSL_library_init()
-# TODO threads?
+lib._setup_ssl_threads()
 lib.OpenSSL_add_all_algorithms()
 
 def check_signals():
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy py3.5: update cryptography cffi backend

2016-12-17 Thread plan_rich
Author: Richard Plangger 
Branch: py3.5
Changeset: r89109:09da85689a2d
Date: 2016-12-17 10:40 +0100
http://bitbucket.org/pypy/pypy/changeset/09da85689a2d/

Log:update cryptography cffi backend

diff --git a/lib_pypy/_cffi_ssl/README.md b/lib_pypy/_cffi_ssl/README.md
--- a/lib_pypy/_cffi_ssl/README.md
+++ b/lib_pypy/_cffi_ssl/README.md
@@ -14,3 +14,7 @@
 Copy over all the sources into the folder `lib_pypy/_cffi_ssl/*`. Updating the 
cffi backend can be simply done by the following command:
 
 $ cp -r /src/_cffi_src/* .
+
+# Crpytography version
+
+`c8f47ad2122efdd5e772aee13ed5d4c64e7d6086`
diff --git a/lib_pypy/_cffi_ssl/_cffi_src/build_openssl.py 
b/lib_pypy/_cffi_ssl/_cffi_src/build_openssl.py
--- a/lib_pypy/_cffi_ssl/_cffi_src/build_openssl.py
+++ b/lib_pypy/_cffi_ssl/_cffi_src/build_openssl.py
@@ -68,6 +68,7 @@
 "objects",
 "ocsp",
 "opensslv",
+"osrandom_engine",
 "pem",
 "pkcs12",
 "rand",
diff --git a/lib_pypy/_cffi_ssl/_cffi_src/openssl/aes.py 
b/lib_pypy/_cffi_ssl/_cffi_src/openssl/aes.py
--- a/lib_pypy/_cffi_ssl/_cffi_src/openssl/aes.py
+++ b/lib_pypy/_cffi_ssl/_cffi_src/openssl/aes.py
@@ -10,7 +10,6 @@
 
 TYPES = """
 static const int Cryptography_HAS_AES_WRAP;
-static const int Cryptography_HAS_AES_CTR128_ENCRYPT;
 
 struct aes_key_st {
 ...;
@@ -29,22 +28,8 @@
 """
 
 MACROS = """
-/* The ctr128_encrypt function is only useful in 1.0.0. We can use EVP for
-   this in 1.0.1+. */
-void AES_ctr128_encrypt(const unsigned char *, unsigned char *,
-size_t, const AES_KEY *, unsigned char[],
-unsigned char[], unsigned int *);
 """
 
 CUSTOMIZATIONS = """
 static const long Cryptography_HAS_AES_WRAP = 1;
-#if CRYPTOGRAPHY_OPENSSL_110_OR_GREATER && !defined(LIBRESSL_VERSION_NUMBER)
-static const int Cryptography_HAS_AES_CTR128_ENCRYPT = 0;
-void (*AES_ctr128_encrypt)(const unsigned char *, unsigned char *,
-   size_t, const AES_KEY *,
-   unsigned char[], unsigned char[],
-   unsigned int *) = NULL;
-#else
-static const int Cryptography_HAS_AES_CTR128_ENCRYPT = 1;
-#endif
 """
diff --git a/lib_pypy/_cffi_ssl/_cffi_src/openssl/cmac.py 
b/lib_pypy/_cffi_ssl/_cffi_src/openssl/cmac.py
--- a/lib_pypy/_cffi_ssl/_cffi_src/openssl/cmac.py
+++ b/lib_pypy/_cffi_ssl/_cffi_src/openssl/cmac.py
@@ -5,7 +5,7 @@
 from __future__ import absolute_import, division, print_function
 
 INCLUDES = """
-#if !defined(OPENSSL_NO_CMAC) && CRYPTOGRAPHY_OPENSSL_101_OR_GREATER
+#if !defined(OPENSSL_NO_CMAC)
 #include 
 #endif
 """
@@ -28,7 +28,7 @@
 """
 
 CUSTOMIZATIONS = """
-#if !defined(OPENSSL_NO_CMAC) && CRYPTOGRAPHY_OPENSSL_101_OR_GREATER
+#if !defined(OPENSSL_NO_CMAC)
 static const long Cryptography_HAS_CMAC = 1;
 #else
 static const long Cryptography_HAS_CMAC = 0;
diff --git a/lib_pypy/_cffi_ssl/_cffi_src/openssl/cryptography.py 
b/lib_pypy/_cffi_ssl/_cffi_src/openssl/cryptography.py
--- a/lib_pypy/_cffi_ssl/_cffi_src/openssl/cryptography.py
+++ b/lib_pypy/_cffi_ssl/_cffi_src/openssl/cryptography.py
@@ -17,8 +17,6 @@
 #include 
 #endif
 
-#define CRYPTOGRAPHY_OPENSSL_101_OR_GREATER \
-(OPENSSL_VERSION_NUMBER >= 0x10001000)
 #define CRYPTOGRAPHY_OPENSSL_102_OR_GREATER \
 (OPENSSL_VERSION_NUMBER >= 0x10002000)
 #define CRYPTOGRAPHY_OPENSSL_102BETA2_OR_GREATER \
@@ -26,8 +24,6 @@
 #define CRYPTOGRAPHY_OPENSSL_110_OR_GREATER \
 (OPENSSL_VERSION_NUMBER >= 0x1010)
 
-#define CRYPTOGRAPHY_OPENSSL_LESS_THAN_101 \
-(OPENSSL_VERSION_NUMBER < 0x10001000)
 #define CRYPTOGRAPHY_OPENSSL_LESS_THAN_102 \
 (OPENSSL_VERSION_NUMBER < 0x10002000)
 #define CRYPTOGRAPHY_OPENSSL_LESS_THAN_102BETA3 \
@@ -51,12 +47,8 @@
 """
 
 TYPES = """
-static const int CRYPTOGRAPHY_OPENSSL_101_OR_GREATER;
-
 static const int CRYPTOGRAPHY_OPENSSL_110_OR_GREATER;
 
-static const int CRYPTOGRAPHY_OPENSSL_LESS_THAN_101;
-
 static const int CRYPTOGRAPHY_OPENSSL_LESS_THAN_102I;
 
 static const int CRYPTOGRAPHY_IS_LIBRESSL;
diff --git a/lib_pypy/_cffi_ssl/_cffi_src/openssl/ec.py 
b/lib_pypy/_cffi_ssl/_cffi_src/openssl/ec.py
--- a/lib_pypy/_cffi_ssl/_cffi_src/openssl/ec.py
+++ b/lib_pypy/_cffi_ssl/_cffi_src/openssl/ec.py
@@ -14,7 +14,6 @@
 
 TYPES = """
 static const int Cryptography_HAS_EC;
-static const int Cryptography_HAS_EC_1_0_1;
 static const int Cryptography_HAS_EC2M;
 static const int Cryptography_HAS_EC_1_0_2;
 
@@ -327,13 +326,6 @@
 
 int (*EC_METHOD_get_field_type)(const EC_METHOD *) = NULL;
 
-#else
-static const long Cryptography_HAS_EC = 1;
-#endif
-
-#if defined(OPENSSL_NO_EC) || CRYPTOGRAPHY_OPENSSL_LESS_THAN_101
-static const long Cryptography_HAS_EC_1_0_1 = 0;
-
 int (*EC_KEY_get_flags)(const EC_KEY *) = NULL;
 void (*EC_KEY_set_flags)(EC_KEY *, int) = NULL;
 void (*EC_KEY_clear_flags)(EC_KEY *, int) = NULL;
@@ -341,10 +333,9 @@
 int (*EC_KEY_set_public_key_affine_coordinates)(
 EC_KEY *, BIGNUM *, BIGNUM *) = NULL;
 

[pypy-commit] pypy default: Translation fix

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89108:51d7e78bf66c
Date: 2016-12-17 09:33 +0100
http://bitbucket.org/pypy/pypy/changeset/51d7e78bf66c/

Log:Translation fix

diff --git a/pypy/module/cpyext/import_.py b/pypy/module/cpyext/import_.py
--- a/pypy/module/cpyext/import_.py
+++ b/pypy/module/cpyext/import_.py
@@ -131,16 +131,14 @@
 in different threads to return with a partially loaded module.
 These calls are serialized by the global interpreter lock."""
 try:
-w_func = space.getbuiltinmodule('imp').get('acquire_lock')
-space.call_function(w_func)
+space.call_method(space.getbuiltinmodule('imp'), 'acquire_lock')
 except OperationError as e:
 e.write_unraisable(space, "_PyImport_AcquireLock")
 
 @cpython_api([], rffi.INT_real, error=CANNOT_FAIL)
 def _PyImport_ReleaseLock(space):
 try:
-w_func = space.getbuiltinmodule('imp').get('release_lock')
-space.call_function(w_func)
+space.call_method(space.getbuiltinmodule('imp'), 'release_lock')
 return 1
 except OperationError as e:
 e.write_unraisable(space, "_PyImport_ReleaseLock")
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] pypy default: _PyImport_{Acquire,Release}Lock()

2016-12-17 Thread arigo
Author: Armin Rigo 
Branch: 
Changeset: r89107:836ec0a42c70
Date: 2016-12-17 09:24 +0100
http://bitbucket.org/pypy/pypy/changeset/836ec0a42c70/

Log:_PyImport_{Acquire,Release}Lock()

diff --git a/pypy/module/cpyext/import_.py b/pypy/module/cpyext/import_.py
--- a/pypy/module/cpyext/import_.py
+++ b/pypy/module/cpyext/import_.py
@@ -1,6 +1,6 @@
 from pypy.interpreter import module
 from pypy.module.cpyext.api import (
-generic_cpy_call, cpython_api, PyObject, CONST_STRING)
+generic_cpy_call, cpython_api, PyObject, CONST_STRING, CANNOT_FAIL)
 from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.module import Module
@@ -124,3 +124,24 @@
 w_mod = importing.add_module(space, w_name)
 space.setattr(w_mod, space.wrap('__file__'), space.wrap(pathname))
 return importing.exec_code_module(space, w_mod, code, w_name)
+
+@cpython_api([], lltype.Void, error=CANNOT_FAIL)
+def _PyImport_AcquireLock(space):
+"""Locking primitive to prevent parallel imports of the same module
+in different threads to return with a partially loaded module.
+These calls are serialized by the global interpreter lock."""
+try:
+w_func = space.getbuiltinmodule('imp').get('acquire_lock')
+space.call_function(w_func)
+except OperationError as e:
+e.write_unraisable(space, "_PyImport_AcquireLock")
+
+@cpython_api([], rffi.INT_real, error=CANNOT_FAIL)
+def _PyImport_ReleaseLock(space):
+try:
+w_func = space.getbuiltinmodule('imp').get('release_lock')
+space.call_function(w_func)
+return 1
+except OperationError as e:
+e.write_unraisable(space, "_PyImport_ReleaseLock")
+return -1
diff --git a/pypy/module/cpyext/test/test_import.py 
b/pypy/module/cpyext/test/test_import.py
--- a/pypy/module/cpyext/test/test_import.py
+++ b/pypy/module/cpyext/test/test_import.py
@@ -37,6 +37,14 @@
 stat = api.PyImport_ReloadModule(stat)
 assert space.getattr(stat, space.wrap("S_IMODE"))
 
+def test_lock(self, space, api):
+# "does not crash"
+api._PyImport_AcquireLock()
+api._PyImport_AcquireLock()
+api._PyImport_ReleaseLock()
+api._PyImport_ReleaseLock()
+
+
 class AppTestImportLogic(AppTestCpythonExtensionBase):
 def test_import_logic(self):
 import sys, os
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit