[pypy-commit] cffi define-integer-constant: Refactor copy&paste of raise dup constant into method

mozbugbox noreply at buildbot.pypy.org
Fri Apr 4 16:52:46 CEST 2014


Author: mozbugbox <mozbugbox at yahoo.com.au>
Branch: define-integer-constant
Changeset: r1493:3626870c2ce8
Date: 2014-04-03 19:10 +0800
http://bitbucket.org/cffi/cffi/changeset/3626870c2ce8/

Log:	Refactor copy&paste of raise dup constant into method

diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -207,6 +207,12 @@
                 e.args = (e.args[0] + "\n    *** Err: %s" % msg,)
             raise
 
+    def _add_constants(self, key, val):
+        if key in self._int_constants:
+            raise api.FFIError(
+                "multiple declarations of constant: %s" % (key,))
+        self._int_constants[key] = val
+
     def _process_macros(self, macros):
         for key, value in macros.items():
             value = value.strip()
@@ -221,11 +227,7 @@
                     int_str = "0o" + int_str[1:]
 
                 pyvalue = int(int_str, 0)
-                if key not in self._int_constants:
-                    self._int_constants[key] = pyvalue
-                else:
-                    raise api.FFIError(
-                        "multiple declarations of constant %s" % (key,))
+                self._add_constants(key, pyvalue)
             elif value == '...':
                 self._declare('macro ' + key, value)
             else:
@@ -564,11 +566,7 @@
                 if enum.value is not None:
                     nextenumvalue = self._parse_constant(enum.value)
                 enumvalues.append(nextenumvalue)
-                if enum.name in self._int_constants:
-                    raise api.FFIError(
-                        "multiple declarations of constant %s" % (enum.name,))
-
-                self._int_constants[enum.name] = nextenumvalue
+                self._add_constants(enum.name, nextenumvalue)
                 nextenumvalue += 1
             enumvalues = tuple(enumvalues)
             tp = model.EnumType(explicit_name, enumerators, enumvalues)
@@ -583,8 +581,4 @@
             if kind in ('typedef', 'struct', 'union', 'enum'):
                 self._declare(name, tp)
         for k, v in other._int_constants.items():
-            if k not in self._int_constants:
-                self._int_constants[k] = v
-            else:
-                raise api.FFIError(
-                    "multiple declarations of constant %s" % (k,))
+            self._add_constants(k, v)


More information about the pypy-commit mailing list