[Greater-commits] r367 - in trunk/GREAT-ER-DB/generator: GreaterAPIGenerator WrapperGenerator

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Jul 7 12:01:48 CEST 2011


Author: bricks
Date: 2011-07-07 12:01:47 +0200 (Thu, 07 Jul 2011)
New Revision: 367

Modified:
   trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/greatertypes.py
   trunk/GREAT-ER-DB/generator/WrapperGenerator/typesystem.py
Log:
Use string helper code to check also for unicode


Modified: trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/greatertypes.py
===================================================================
--- trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/greatertypes.py	2011-07-07 09:59:39 UTC (rev 366)
+++ trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/greatertypes.py	2011-07-07 10:01:47 UTC (rev 367)
@@ -1,6 +1,7 @@
-# Copyright (c) 2002, 2003 by Intevation GmbH
+# Copyright (c) 2002, 2003, 2011 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
+# Bjoern Ricks <bjoern.ricks at intevation.de>
 #
 # This program is free software under the GPL (>=v2)
 # Read the file COPYING coming with the software for details.
@@ -14,6 +15,7 @@
 
 
 from WrapperGenerator.typesystem import Struct, Pointer, Char
+from GreaterAPIGenerator.helper import stringhelper
 
 
 class ListStruct(Struct):
@@ -152,23 +154,8 @@
             # temporarily so we don't make a copy here and we don't have
             # to free it later.
             if isinstance(t, Pointer) and t.basetype == Char:
-                if t.null_allowed:
-                    convert = "\n".join([
-                        "if (temp == Py_None)",
-                        "{",
-                        "    s->%s = NULL;" % name,
-                        "}",
-                        "else",
-                        "{",
-                        "    if (!PyString_Check(temp)) {",
-                        '        PyErr_Format(PyExc_TypeError, '
-                        '"s->%s must be a string or None");' % name,
-                        "        goto fail;",
-                        "    }",
-                        "    s->%s = PyString_AsString(temp);" % name,
-                        "}"])
-                else:
-                    convert = "s->%s = PyString_AsString(temp);" % name
+                tempcode = stringhelper("temp", "s->%s" % name, "fail", t.null_allowed)
+                convert = "\n".join(tempcode)
             else:
                 convert = t.py_to_c("s->" + name, "temp")
             convert = ["    " + line for line in convert.splitlines()]

Modified: trunk/GREAT-ER-DB/generator/WrapperGenerator/typesystem.py
===================================================================
--- trunk/GREAT-ER-DB/generator/WrapperGenerator/typesystem.py	2011-07-07 09:59:39 UTC (rev 366)
+++ trunk/GREAT-ER-DB/generator/WrapperGenerator/typesystem.py	2011-07-07 10:01:47 UTC (rev 367)
@@ -1,6 +1,7 @@
-# Copyright (c) 2002, 2003 by Intevation GmbH
+# Copyright (c) 2002, 2003, 2011 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
+# Bjoern Ricks <bjoern.ricks at intevation.de>
 #
 # This program is free software under the GPL (>=v2)
 # Read the file COPYING coming with the software for details.
@@ -14,6 +15,7 @@
 
 
 from types import StringType
+from GreaterAPIGenerator.helper import stringwithlength
 
 
 class BaseType:
@@ -250,26 +252,7 @@
         # terminated
         self.fix_references()
         if self.basetype == Char:
-            vars = locals().copy()
-            vars["length"] = self.length
-            return ("if (!PyString_Check(%(pyvar)s))\n"
-                    "{\n"
-                '    PyErr_Format(PyExc_TypeError, "%%s must be a string",'
-                        '"%(cvar)s");\n'
-                    "    goto fail;\n"
-                    "}\n"
-                "if (PyString_Size(%(pyvar)s) >= %(length)d)\n"
-                    "{\n"
-                '    PyErr_Format(PyExc_TypeError, "String too long for %%s",'
-                        '"%(cvar)s");\n'
-                    "    goto fail;\n"
-                    "}\n"
-                    # subtract one from the length to make sure the nul
-                    # bytes fits in as well.
-             "strncpy(%(cvar)s, PyString_AsString(%(pyvar)s), %(length)d-1);\n"
-                    # Add the nul
-                    "%(cvar)s[%(length)d - 1] = '\\000';\n"
-                    ) % vars
+            return "\n".join(stringwithlength(pyvar, cvar, self.length, "fail"))
         else:
             raise NotImplementedError
 



More information about the Greater-commits mailing list