[pypy-commit] cffi default: Add GCC/Clang -Wall -Wextra flags in tests

2014-12-22 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1596:03f384b183d1
Date: 2014-11-28 12:05 +0300
http://bitbucket.org/cffi/cffi/changeset/03f384b183d1/

Log:Add GCC/Clang -Wall -Wextra flags in tests

diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -15,12 +15,12 @@
 if (sys.platform == 'darwin' and
   [int(x) for x in os.uname()[2].split('.')] = [11, 0, 0]):
 # assume a standard clang or gcc
-extra_compile_args = ['-Werror', '-Wconversion']
+extra_compile_args = ['-Werror', '-Wall', '-Wextra', '-Wconversion']
 # special things for clang
 extra_compile_args.append('-Qunused-arguments')
 else:
 # assume a standard gcc
-extra_compile_args = ['-Werror', '-Wconversion']
+extra_compile_args = ['-Werror', '-Wall', '-Wextra', '-Wconversion']
 
 class FFI(FFI):
 def verify(self, *args, **kwds):
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: Silence GCC/Clang -Wunused-parameter warnings

2014-12-22 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1595:25bdcb810dc0
Date: 2014-11-24 21:31 +0300
http://bitbucket.org/cffi/cffi/changeset/25bdcb810dc0/

Log:Silence GCC/Clang -Wunused-parameter warnings

diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -65,7 +65,7 @@
 # The following two 'chained_list_constants' items contains
 # the head of these two chained lists, as a string that gives the
 # call to do, if any.
-self._chained_list_constants = ['0', '0']
+self._chained_list_constants = ['((void)lib,0)', '((void)lib,0)']
 #
 prnt = self._prnt
 # first paste some standard set of lines that are mostly '#define'
@@ -337,7 +337,7 @@
 prnt = self._prnt
 numargs = len(tp.args)
 if numargs == 0:
-argname = 'no_arg'
+argname = 'noarg'
 elif numargs == 1:
 argname = 'arg0'
 else:
@@ -387,6 +387,9 @@
 prnt('  Py_END_ALLOW_THREADS')
 prnt()
 #
+prnt('  (void)self; /* unused */')
+if numargs == 0:
+prnt('  (void)noarg; /* unused */')
 if result_code:
 prnt('  return %s;' %
  self._convert_expr_from_c(tp.result, 'result', 'result type'))
@@ -453,6 +456,7 @@
 prnt('static void %s(%s *p)' % (checkfuncname, cname))
 prnt('{')
 prnt('  /* only to generate compile-time warnings or errors */')
+prnt('  (void)p;')
 for fname, ftype, fbitsize in tp.enumfields():
 if (isinstance(ftype, model.PrimitiveType)
 and ftype.is_integer_type()) or fbitsize = 0:
@@ -483,6 +487,8 @@
 prnt('sizeof(((%s *)0)-%s),' % (cname, fname))
 prnt('-1')
 prnt('  };')
+prnt('  (void)self; /* unused */')
+prnt('  (void)noarg; /* unused */')
 prnt('  return _cffi_get_struct_layout(nums);')
 prnt('  /* the next line is not executed, but compiled */')
 prnt('  %s(0);' % (checkfuncname,))
@@ -911,6 +917,7 @@
 {
 PyObject *library;
 int was_alive = (_cffi_types != NULL);
+(void)self; /* unused */
 if (!PyArg_ParseTuple(args, OOO, _cffi_types, _cffi_VerificationError,
library))
 return NULL;
diff --git a/cffi/vengine_gen.py b/cffi/vengine_gen.py
--- a/cffi/vengine_gen.py
+++ b/cffi/vengine_gen.py
@@ -235,6 +235,7 @@
 prnt('static void %s(%s *p)' % (checkfuncname, cname))
 prnt('{')
 prnt('  /* only to generate compile-time warnings or errors */')
+prnt('  (void)p;')
 for fname, ftype, fbitsize in tp.enumfields():
 if (isinstance(ftype, model.PrimitiveType)
 and ftype.is_integer_type()) or fbitsize = 0:
diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -430,7 +430,7 @@
 ffi = FFI()
 ffi.cdef(typedef struct foo_s foo_t; int bar(foo_t *);)
 lib = ffi.verify(typedef struct foo_s foo_t;\n
- int bar(foo_t *f) { return 42; }\n)
+ int bar(foo_t *f) { (void)f; return 42; }\n)
 assert lib.bar(ffi.NULL) == 42
 
 def test_ffi_full_struct():
@@ -1810,7 +1810,7 @@
 def test_typeof_function():
 ffi = FFI()
 ffi.cdef(int foo(int, char);)
-lib = ffi.verify(int foo(int x, char y) { return 42; })
+lib = ffi.verify(int foo(int x, char y) { (void)x; (void)y; return 42; })
 ctype = ffi.typeof(lib.foo)
 assert len(ctype.args) == 2
 assert ctype.result == ffi.typeof(int)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: Fix test for approximate floating point return type

2014-11-21 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1579:9642bf799905
Date: 2014-11-21 14:40 +0300
http://bitbucket.org/cffi/cffi/changeset/9642bf799905/

Log:Fix test for approximate floating point return type

diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -91,13 +91,9 @@
 
 def test_rounding_1():
 ffi = FFI()
-ffi.cdef(float sin(double x);)
-lib = ffi.verify('''
-#include math.h
-static float my_sin(double x) { return (float)sin(x); }
-#define sin my_sin
-''', libraries=lib_m)
-res = lib.sin(1.23)
+ffi.cdef(double sinf(float x);)
+lib = ffi.verify('#include math.h', libraries=lib_m)
+res = lib.sinf(1.23)
 assert res != math.sin(1.23) # not exact, because of double-float
 assert abs(res - math.sin(1.23))  1E-5
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: Add and fix tests for enumerations

2014-11-21 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1578:0870cf7eeeca
Date: 2014-11-21 14:23 +0300
http://bitbucket.org/cffi/cffi/changeset/0870cf7eeeca/

Log:Add and fix tests for enumerations

diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -1085,7 +1085,7 @@
 )
 lib = ffi.verify(
 enum foo_e { AA, CC, BB };
-int foo_func(enum foo_e e) { return e; }
+int foo_func(enum foo_e e) { return (int)e; }
 )
 assert lib.foo_func(lib.BB) == 2
 py.test.raises(TypeError, lib.foo_func, BB)
@@ -1098,7 +1098,7 @@
 )
 lib = ffi.verify(
 enum foo_e { AA, CC, BB };
-enum foo_e foo_func(int x) { return x; }
+enum foo_e foo_func(int x) { return (enum foo_e)x; }
 )
 assert lib.foo_func(lib.BB) == lib.BB == 2
 
@@ -1133,6 +1133,19 @@
 assert lib.AA == 0
 assert lib.BB == 2
 
+def test_typedef_enum_as_argument():
+ffi = FFI()
+ffi.cdef(
+typedef enum { AA, BB, ... } foo_t;
+int foo_func(foo_t);
+)
+lib = ffi.verify(
+typedef enum { AA, CC, BB } foo_t;
+int foo_func(foo_t e) { return (int)e; }
+)
+assert lib.foo_func(lib.BB) == lib.BB == 2
+py.test.raises(TypeError, lib.foo_func, BB)
+
 def test_typedef_enum_as_function_result():
 ffi = FFI()
 ffi.cdef(
@@ -1141,7 +1154,7 @@
 )
 lib = ffi.verify(
 typedef enum { AA, CC, BB } foo_t;
-foo_t foo_func(int x) { return x; }
+foo_t foo_func(int x) { return (foo_t)x; }
 )
 assert lib.foo_func(lib.BB) == lib.BB == 2
 
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: CPython: Add explicit typecasts in _cffi_from_c_int()

2014-11-21 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1577:8106093be18d
Date: 2014-11-21 12:55 +0300
http://bitbucket.org/cffi/cffi/changeset/8106093be18d/

Log:CPython: Add explicit typecasts in _cffi_from_c_int()

diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -829,12 +829,15 @@
 PyLong_FromLongLong((long long)(x)))
 
 #define _cffi_from_c_int(x, type)\
-(((type)-1)  0 ?   /* unsigned */   \
-(sizeof(type)  sizeof(long) ? PyInt_FromLong(x) :   \
- sizeof(type) == sizeof(long) ? PyLong_FromUnsignedLong(x) : \
-PyLong_FromUnsignedLongLong(x))  \
-  : (sizeof(type) = sizeof(long) ? PyInt_FromLong(x) :  \
-PyLong_FromLongLong(x)))
+(((type)-1)  0 ? /* unsigned */ \
+(sizeof(type)  sizeof(long) ?   \
+PyInt_FromLong((long)x) :\
+ sizeof(type) == sizeof(long) ?  \
+PyLong_FromUnsignedLong((unsigned long)x) :  \
+PyLong_FromUnsignedLongLong((unsigned long long)x)) :\
+(sizeof(type) = sizeof(long) ?  \
+PyInt_FromLong((long)x) :\
+PyLong_FromLongLong((long long)x)))
 
 #define _cffi_to_c_int(o, type)  \
 (sizeof(type) == 1 ? (((type)-1)  0 ? (type)_cffi_to_c_u8(o)\
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: Enable GCC/Clang -Wconversion and add tests for expected failures

2014-11-21 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1580:d398e0a66539
Date: 2014-11-21 17:50 +0300
http://bitbucket.org/cffi/cffi/changeset/d398e0a66539/

Log:Enable GCC/Clang -Wconversion and add tests for expected failures

diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -14,12 +14,13 @@
 else:
 if (sys.platform == 'darwin' and
   [int(x) for x in os.uname()[2].split('.')] = [11, 0, 0]):
+# assume a standard clang or gcc
+extra_compile_args = ['-Werror', '-Wconversion']
 # special things for clang
-extra_compile_args = [
-'-Werror', '-Qunused-arguments', '-Wno-error=shorten-64-to-32']
+extra_compile_args.append('-Qunused-arguments')
 else:
 # assume a standard gcc
-extra_compile_args = ['-Werror']
+extra_compile_args = ['-Werror', '-Wconversion']
 
 class FFI(FFI):
 def verify(self, *args, **kwds):
@@ -89,6 +90,39 @@
 lib = ffi.verify('#include math.h', libraries=lib_m)
 assert lib.sin(1.23) == math.sin(1.23)
 
+def _Wconversion(cdef, source, **kargs):
+if sys.platform == 'win32':
+py.test.skip(needs GCC or Clang)
+ffi = FFI()
+ffi.cdef(cdef)
+py.test.raises(VerificationError, ffi.verify, source, **kargs)
+extra_compile_args_orig = extra_compile_args[:]
+extra_compile_args.remove('-Wconversion')
+try:
+ffi.verify(source, **kargs)
+finally:
+extra_compile_args[:] = extra_compile_args_orig 
+
+def test_Wconversion_unsigned():
+_Wconversion(unsigned foo(void);,
+ int foo(void) { return -1;})
+
+def test_Wconversion_integer():
+_Wconversion(short foo(void);,
+ long long foo(void) { return 1sizeof(short);})
+
+def test_Wconversion_floating():
+_Wconversion(float sin(double);,
+ #include math.h, libraries=lib_m)
+
+def test_Wconversion_float2int():
+_Wconversion(int sinf(float);,
+ #include math.h, libraries=lib_m)
+
+def test_Wconversion_double2int():
+_Wconversion(int sin(double);,
+ #include math.h, libraries=lib_m)
+
 def test_rounding_1():
 ffi = FFI()
 ffi.cdef(double sinf(float x);)
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: Add test for approximate return types

2014-11-21 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1576:4ebadac73fae
Date: 2014-04-30 14:18 +0300
http://bitbucket.org/cffi/cffi/changeset/4ebadac73fae/

Log:Add test for approximate return types

diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -109,17 +109,19 @@
 assert res != math.sin(1.23) # not exact, because of double-float
 assert abs(res - math.sin(1.23))  1E-5
 
-def test_strlen_exact():
+def test_return_exact():
 ffi = FFI()
 ffi.cdef(size_t strlen(const char *s);)
 lib = ffi.verify(#include string.h)
 assert lib.strlen(bhi there!) == 9
 
-def test_strlen_approximate():
-ffi = FFI()
-ffi.cdef(size_t strlen(char *s);)
-lib = ffi.verify(#include string.h)
-assert lib.strlen(bhi there!) == 9
+def test_return_approximate():
+for typename in ['short', 'int', 'long', 'long long']:
+ffi = FFI()
+ffi.cdef(%s foo(signed char x); % typename)
+lib = ffi.verify(signed char foo(signed char x) { return x;})
+assert lib.foo(-128) == -128
+assert lib.foo(+127) == +127
 
 def test_strlen_array_of_char():
 ffi = FFI()
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: CPython: Silent GCC -Wconversion warnings

2014-11-21 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1575:1f05f7f5f966
Date: 2014-04-26 20:44 +0300
http://bitbucket.org/cffi/cffi/changeset/1f05f7f5f966/

Log:CPython: Silent GCC -Wconversion warnings

diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -228,7 +228,8 @@
 converter = '_cffi_to_c_int'
 extraarg = ', %s' % tp.name
 else:
-converter = '_cffi_to_c_%s' % (tp.name.replace(' ', '_'),)
+converter = '(%s)_cffi_to_c_%s' % (tp.get_c_name(''),
+   tp.name.replace(' ', '_'))
 errvalue = '-1'
 #
 elif isinstance(tp, model.PointerType):
@@ -267,8 +268,8 @@
 self._prnt('  if (datasize != 0) {')
 self._prnt('if (datasize  0)')
 self._prnt('  %s;' % errcode)
-self._prnt('%s = alloca(datasize);' % (tovar,))
-self._prnt('memset((void *)%s, 0, datasize);' % (tovar,))
+self._prnt('%s = alloca((size_t)datasize);' % (tovar,))
+self._prnt('memset((void *)%s, 0, (size_t)datasize);' % (tovar,))
 self._prnt('if (_cffi_convert_array_from_object('
'(char *)%s, _cffi_type(%d), %s)  0)' % (
 tovar, self._gettypenum(tp), fromvar))
@@ -844,7 +845,7 @@
  : (type)_cffi_to_c_i32(o)) :\
  sizeof(type) == 8 ? (((type)-1)  0 ? (type)_cffi_to_c_u64(o)   \
  : (type)_cffi_to_c_i64(o)) :\
- (Py_FatalError(unsupported size for type  #type), 0))
+ (Py_FatalError(unsupported size for type  #type), (type)0))
 
 #define _cffi_to_c_i8\
  ((int(*)(PyObject *))_cffi_exports[1])
diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -92,7 +92,11 @@
 def test_rounding_1():
 ffi = FFI()
 ffi.cdef(float sin(double x);)
-lib = ffi.verify('#include math.h', libraries=lib_m)
+lib = ffi.verify('''
+#include math.h
+static float my_sin(double x) { return (float)sin(x); }
+#define sin my_sin
+''', libraries=lib_m)
 res = lib.sin(1.23)
 assert res != math.sin(1.23) # not exact, because of double-float
 assert abs(res - math.sin(1.23))  1E-5
@@ -113,13 +117,13 @@
 
 def test_strlen_approximate():
 ffi = FFI()
-ffi.cdef(int strlen(char *s);)
+ffi.cdef(size_t strlen(char *s);)
 lib = ffi.verify(#include string.h)
 assert lib.strlen(bhi there!) == 9
 
 def test_strlen_array_of_char():
 ffi = FFI()
-ffi.cdef(int strlen(char[]);)
+ffi.cdef(size_t strlen(char[]);)
 lib = ffi.verify(#include string.h)
 assert lib.strlen(bhello) == 5
 
@@ -208,8 +212,8 @@
 ffi = FFI()
 ffi.cdef('\n'.join([%s foo_%s(%s); % (tp, tp.replace(' ', '_'), tp)
for tp in typenames]))
-lib = ffi.verify('\n'.join([%s foo_%s(%s x) { return x+1; } %
-(tp, tp.replace(' ', '_'), tp)
+lib = ffi.verify('\n'.join([%s foo_%s(%s x) { return (%s)(x+1); } %
+(tp, tp.replace(' ', '_'), tp, tp)
 for tp in typenames]))
 for typename in typenames:
 foo = getattr(lib, 'foo_%s' % typename.replace(' ', '_'))
@@ -315,7 +319,7 @@
 def test_char_type():
 ffi = FFI()
 ffi.cdef(char foo(char);)
-lib = ffi.verify(char foo(char x) { return x+1; })
+lib = ffi.verify(char foo(char x) { return ++x; })
 assert lib.foo(bA) == bB
 py.test.raises(TypeError, lib.foo, bbar)
 py.test.raises(TypeError, lib.foo, bar)
@@ -896,7 +900,7 @@
 static int foo(token_t *tk) {
 if (!tk)
 return -42;
-*tk += 1.601;
+*tk += 1.601f;
 return (int)*tk;
 }
 #define TOKEN_SIZE sizeof(token_t)
@@ -991,7 +995,7 @@
 long a;
 };
 int foo(struct foo_s s) {
-return s.a - (int)s.b;
+return (int)s.a - (int)s.b;
 }
 )
 s = ffi.new(struct foo_s *, [100, 1])
@@ -1008,7 +1012,7 @@
 long a;
 };
 int foo1(struct foo_s s) {
-return s.a - (int)s.b;
+return (int)s.a - (int)s.b;
 }
 int (*foo)(struct foo_s s) = foo1;
 )
@@ -1067,7 +1071,7 @@
 def test_array_as_argument():
 ffi = FFI()
 ffi.cdef(
-int strlen(char string[]);
+size_t strlen(char string[]);
 )
 ffi.verify(#include string.h)
 
@@ -1676,7 +1680,7 @@
 static int c_callback(int how_many, ...) {
 va_list ap;
 /* collect the ... arguments into the values[] array */
-int i, *values = alloca(how_many * sizeof(int));
+int i, *values = alloca((size_t)how_many 

[pypy-commit] cffi default: CPython: Better C - Python conversion for integer constants

2014-04-26 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1506:5e1105060c45
Date: 2014-04-25 12:23 +0300
http://bitbucket.org/cffi/cffi/changeset/5e1105060c45/

Log:CPython: Better C - Python conversion for integer constants

 - Silent GCC -Wsign-compare

diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -600,13 +600,7 @@
   'variable type'),))
 assert delayed
 else:
-prnt('  if (LONG_MIN = (%s)  (%s) = LONG_MAX)' % (name, name))
-prnt('o = PyInt_FromLong((long)(%s));' % (name,))
-prnt('  else if ((%s) = 0)' % (name,))
-prnt('o = PyLong_FromLongLong((long long)(%s));' % (name,))
-prnt('  else')
-prnt('o = PyLong_FromUnsignedLongLong('
- '(unsigned long long)(%s));' % (name,))
+prnt('  o = _cffi_from_c_int_const(%s);' % name)
 prnt('  if (o == NULL)')
 prnt('return -1;')
 if size_too:
@@ -815,6 +809,15 @@
 #define _cffi_to_c_double PyFloat_AsDouble
 #define _cffi_to_c_float PyFloat_AsDouble
 
+#define _cffi_from_c_int_const(x)\
+(((x)  0) ? \
+((unsigned long long)(x) = (unsigned long long)LONG_MAX) ?  \
+PyInt_FromLong((long)(x)) :  \
+PyLong_FromUnsignedLongLong((unsigned long long)(x)) :   \
+((long long)(x) = (long long)LONG_MIN) ?\
+PyInt_FromLong((long)(x)) :  \
+PyLong_FromLongLong((long long)(x)))
+
 #define _cffi_from_c_int(x, type)\
 (((type)-1)  0 ?   /* unsigned */   \
 (sizeof(type)  sizeof(long) ? PyInt_FromLong(x) :   \
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: CPython: Add explicit typecasts in _cffi_to_c_int()

2014-04-24 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1505:6585558c79af
Date: 2014-04-23 18:58 +0300
http://bitbucket.org/cffi/cffi/changeset/6585558c79af/

Log:CPython: Add explicit typecasts in _cffi_to_c_int()

- Silent GCC -Wsign-compare warning: signed and unsigned type in
conditional expression

diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -824,14 +824,14 @@
 PyLong_FromLongLong(x)))
 
 #define _cffi_to_c_int(o, type)  \
-(sizeof(type) == 1 ? (((type)-1)  0 ? _cffi_to_c_u8(o)  \
- : _cffi_to_c_i8(o)) :   \
- sizeof(type) == 2 ? (((type)-1)  0 ? _cffi_to_c_u16(o) \
- : _cffi_to_c_i16(o)) :  \
- sizeof(type) == 4 ? (((type)-1)  0 ? _cffi_to_c_u32(o) \
- : _cffi_to_c_i32(o)) :  \
- sizeof(type) == 8 ? (((type)-1)  0 ? _cffi_to_c_u64(o) \
- : _cffi_to_c_i64(o)) :  \
+(sizeof(type) == 1 ? (((type)-1)  0 ? (type)_cffi_to_c_u8(o)\
+ : (type)_cffi_to_c_i8(o)) : \
+ sizeof(type) == 2 ? (((type)-1)  0 ? (type)_cffi_to_c_u16(o)   \
+ : (type)_cffi_to_c_i16(o)) :\
+ sizeof(type) == 4 ? (((type)-1)  0 ? (type)_cffi_to_c_u32(o)   \
+ : (type)_cffi_to_c_i32(o)) :\
+ sizeof(type) == 8 ? (((type)-1)  0 ? (type)_cffi_to_c_u64(o)   \
+ : (type)_cffi_to_c_i64(o)) :\
  (Py_FatalError(unsupported size for type  #type), 0))
 
 #define _cffi_to_c_i8\
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: Issue 153: Generate same C code for CPython 2 and 3

2014-04-23 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1503:bcedf12c6e01
Date: 2014-04-23 16:10 +0300
http://bitbucket.org/cffi/cffi/changeset/bcedf12c6e01/

Log:Issue 153: Generate same C code for CPython 2 and 3

- CPython 2 and 3: make ffi.ferify() generate identical C code
- CPython 3: fix refcount management in case of errors at import

diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -96,36 +96,47 @@
 #
 # standard init.
 modname = self.verifier.get_module_name()
-if sys.version_info = (3,):
-prnt('static struct PyModuleDef _cffi_module_def = {')
-prnt('  PyModuleDef_HEAD_INIT,')
-prnt('  %s,' % modname)
-prnt('  NULL,')
-prnt('  -1,')
-prnt('  _cffi_methods,')
-prnt('  NULL, NULL, NULL, NULL')
-prnt('};')
-prnt()
-initname = 'PyInit_%s' % modname
-createmod = 'PyModule_Create(_cffi_module_def)'
-errorcase = 'return NULL'
-finalreturn = 'return lib'
-else:
-initname = 'init%s' % modname
-createmod = 'Py_InitModule(%s, _cffi_methods)' % modname
-errorcase = 'return'
-finalreturn = 'return'
+constants = self._chained_list_constants[False]
+prnt('#if PY_MAJOR_VERSION = 3')
+prnt()
+prnt('static struct PyModuleDef _cffi_module_def = {')
+prnt('  PyModuleDef_HEAD_INIT,')
+prnt('  %s,' % modname)
+prnt('  NULL,')
+prnt('  -1,')
+prnt('  _cffi_methods,')
+prnt('  NULL, NULL, NULL, NULL')
+prnt('};')
+prnt()
 prnt('PyMODINIT_FUNC')
-prnt('%s(void)' % initname)
+prnt('PyInit_%s(void)' % modname)
 prnt('{')
 prnt('  PyObject *lib;')
-prnt('  lib = %s;' % createmod)
-prnt('  if (lib == NULL || %s  0)' % (
-self._chained_list_constants[False],))
-prnt('%s;' % errorcase)
-prnt('  _cffi_init();')
-prnt('  %s;' % finalreturn)
+prnt('  lib = PyModule_Create(_cffi_module_def);')
+prnt('  if (lib == NULL)')
+prnt('return NULL;')
+prnt('  if (%s  0 || _cffi_init()  0) {' % (constants,))
+prnt('Py_DECREF(lib);')
+prnt('return NULL;')
+prnt('  }')
+prnt('  return lib;')
 prnt('}')
+prnt()
+prnt('#else')
+prnt()
+prnt('PyMODINIT_FUNC')
+prnt('init%s(void)' % modname)
+prnt('{')
+prnt('  PyObject *lib;')
+prnt('  lib = Py_InitModule(%s, _cffi_methods);' % modname)
+prnt('  if (lib == NULL)')
+prnt('return;')
+prnt('  if (%s  0 || _cffi_init()  0)' % (constants,))
+prnt('return;')
+prnt('  return;')
+prnt('}')
+prnt()
+prnt('#endif')
 
 def load_library(self):
 # XXX review all usages of 'self' here!
@@ -894,25 +905,32 @@
 return PyBool_FromLong(was_alive);
 }
 
-static void _cffi_init(void)
+static int _cffi_init(void)
 {
-PyObject *module = PyImport_ImportModule(_cffi_backend);
-PyObject *c_api_object;
+PyObject *module, *c_api_object = NULL;
 
+module = PyImport_ImportModule(_cffi_backend);
 if (module == NULL)
-return;
+goto failure;
 
 c_api_object = PyObject_GetAttrString(module, _C_API);
 if (c_api_object == NULL)
-return;
+goto failure;
 if (!PyCapsule_CheckExact(c_api_object)) {
-Py_DECREF(c_api_object);
 PyErr_SetNone(PyExc_ImportError);
-return;
+goto failure;
 }
 memcpy(_cffi_exports, PyCapsule_GetPointer(c_api_object, cffi),
_CFFI_NUM_EXPORTS * sizeof(void *));
+
+Py_DECREF(module);
 Py_DECREF(c_api_object);
+return 0;
+
+  failure:
+Py_XDECREF(module);
+Py_XDECREF(c_api_object);
+return -1;
 }
 
 #define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num))
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit


[pypy-commit] cffi default: CPython: Initialize all slots of PyMethodDef table

2014-04-23 Thread dalcinl
Author: Lisandro Dalcin dalc...@gmail.com
Branch: 
Changeset: r1504:cc11d18fb59b
Date: 2014-04-23 17:35 +0300
http://bitbucket.org/cffi/cffi/changeset/cc11d18fb59b/

Log:CPython: Initialize all slots of PyMethodDef table

- Silent GCC -Wmissing-field-initializers

diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -89,8 +89,8 @@
 # by generate_cpy_function_method().
 prnt('static PyMethodDef _cffi_methods[] = {')
 self._generate(method)
-prnt('  {_cffi_setup, _cffi_setup, METH_VARARGS},')
-prnt('  {NULL, NULL}/* Sentinel */')
+prnt('  {_cffi_setup, _cffi_setup, METH_VARARGS, NULL},')
+prnt('  {NULL, NULL, 0, NULL}/* Sentinel */')
 prnt('};')
 prnt()
 #
@@ -405,7 +405,7 @@
 meth = 'METH_O'
 else:
 meth = 'METH_VARARGS'
-self._prnt('  {%s, _cffi_f_%s, %s},' % (name, name, meth))
+self._prnt('  {%s, _cffi_f_%s, %s, NULL},' % (name, name, meth))
 
 _loading_cpy_function = _loaded_noop
 
@@ -492,8 +492,8 @@
 if tp.fldnames is None:
 return # nothing to do with opaque structs
 layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name)
-self._prnt('  {%s, %s, METH_NOARGS},' % (layoutfuncname,
-   layoutfuncname))
+self._prnt('  {%s, %s, METH_NOARGS, NULL},' % (layoutfuncname,
+ layoutfuncname))
 
 def _loading_struct_or_union(self, tp, prefix, name, module):
 if tp.fldnames is None:
___
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit