Example #1
0
File: Code.py Project: dagss/cython
    def generate_string_constants(self):
        c_consts = [(len(c.cname), c.cname, c) for c in self.string_const_index.values()]
        c_consts.sort()
        py_strings = []

        decls_writer = self.parts["decls"]
        for _, cname, c in c_consts:
            decls_writer.putln(
                'static char %s[] = "%s";' % (cname, StringEncoding.split_string_literal(c.escaped_value))
            )
            if c.py_strings is not None:
                for py_string in c.py_strings.values():
                    py_strings.append((c.cname, len(py_string.cname), py_string))

        if py_strings:
            import Nodes

            self.use_utility_code(Nodes.init_string_tab_utility_code)

            py_strings.sort()
            w = self.parts["pystring_table"]
            w.putln("")
            w.putln("static __Pyx_StringTabEntry %s[] = {" % Naming.stringtab_cname)
            for c_cname, _, py_string in py_strings:
                if (
                    not py_string.is_str
                    or not py_string.encoding
                    or py_string.encoding in ("ASCII", "USASCII", "US-ASCII", "UTF8", "UTF-8")
                ):
                    encoding = "0"
                else:
                    encoding = '"%s"' % py_string.encoding.lower()

                decls_writer.putln("static PyObject *%s;" % py_string.cname)
                w.putln(
                    "{&%s, %s, sizeof(%s), %s, %d, %d, %d},"
                    % (
                        py_string.cname,
                        c_cname,
                        c_cname,
                        encoding,
                        py_string.is_unicode,
                        py_string.is_str,
                        py_string.intern,
                    )
                )
            w.putln("{0, 0, 0, 0, 0, 0, 0}")
            w.putln("};")

            init_globals = self.parts["init_globals"]
            init_globals.putln(
                "if (__Pyx_InitStrings(%s) < 0) %s;"
                % (Naming.stringtab_cname, init_globals.error_goto(self.module_pos))
            )