[Greater-commits] r364 - trunk/GREAT-ER-DB/generator/GreaterAPIGenerator
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Jul 7 11:56:04 CEST 2011
Author: bricks
Date: 2011-07-07 11:56:03 +0200 (Thu, 07 Jul 2011)
New Revision: 364
Added:
trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/helper.py
Log:
Add helper module to generate save python to c string converstion code
The new stringhelper functions also checks for unicode and converts it accordingly
Added: trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/helper.py
===================================================================
--- trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/helper.py 2011-07-07 08:29:01 UTC (rev 363)
+++ trunk/GREAT-ER-DB/generator/GreaterAPIGenerator/helper.py 2011-07-07 09:56:03 UTC (rev 364)
@@ -0,0 +1,74 @@
+# Copyright (c) 2011 by Intevation GmbH
+# vim: set fileencoding=utf-8 et sw=4 ts=4 tw=80:
+#
+# Authors:
+# Björn 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.
+
+
+def stringhelper(cpyvar, cvar, goto="fail", none_allowed=False, indent=0):
+ errorstring = "%s must be a string" % cvar
+ codeindent = indent
+ nonecode = []
+ if none_allowed:
+ errorstring += " or None"
+ codeindent += 1
+ nonecode = ["if (%s == Py_None)" % cpyvar,
+ "{",
+ " %s = NULL;" % cvar,
+ "}",
+ "else",
+ "{",
+ ]
+ code = ["if (!PyString_Check(%s)) {" % cpyvar,
+ " if (PyUnicode_Check(%s)) {" % cpyvar,
+ " %s = _PyUnicode_AsDefaultEncodedString(%s, NULL);" % (cpyvar, cpyvar),
+ " if (%s == NULL) {" % cpyvar,
+ ' PyErr_Format(PyExc_TypeError, "%s");' % errorstring,
+ " goto %s;" % goto,
+ " }",
+ " }",
+ " else",
+ " {",
+ ' PyErr_Format(PyExc_TypeError, "%s");' % errorstring,
+ " goto %s;" % goto,
+ " }",
+ "}",
+ "%s = PyString_AsString(%s);" % (cvar, cpyvar),
+ ]
+ result = [(" " * indent) + line for line in nonecode]
+ result.extend([(" " * codeindent) + line for line in code])
+ if none_allowed:
+ result.append((" " * indent) + "}")
+ return result
+
+def stringwithlength(cpyvar, cvar, length, goto="fail", indent=0):
+ code = ["if (!PyString_Check(%s))" % cpyvar,
+ "{",
+ " if (PyUnicode_Check(%s)) {" % cpyvar,
+ " %s = _PyUnicode_AsDefaultEncodedString(%s, NULL);" % (cpyvar, cpyvar),
+ " if (%s == NULL) {" % cpyvar,
+ ' PyErr_Format(PyExc_TypeError, "%s must be a string");' % cvar,
+ " goto %s;" % goto,
+ " }",
+ " }",
+ " else",
+ " {",
+ ' PyErr_Format(PyExc_TypeError, "%s must be a string");' % cvar,
+ " goto %s;" % goto,
+ " }",
+ "}",
+ "if (PyString_Size(%s) >= %d)" % (cpyvar, length),
+ "{",
+ ' PyErr_Format(PyExc_TypeError, "String too long for %s");' % cvar,
+ " goto %s;" % goto,
+ "}",
+ # subtract one from the length to make sure the nul
+ # bytes fits in as well.
+ "strncpy(%s, PyString_AsString(%s), %d);" % (cvar, cpyvar, length-1),
+ # Add the null
+ "%s[%d] = '\\000';" % (cvar, length-1),
+ ]
+ return [" " * indent + line for line in code]
More information about the Greater-commits
mailing list