Esempio n. 1
0
def c_string_array_initializer(components: List[bytes]) -> str:
    result = []
    result.append('{\n')
    for s in components:
        result.append('    ' + c_string_initializer(s) + ',\n')
    result.append('}')
    return ''.join(result)
Esempio n. 2
0
    def generate_literal_tables(self) -> None:
        """Generate tables containing descriptions of Python literals to construct.

        We will store the constructed literals in a single array that contains
        literals of all types. This way we can refer to an arbitrary literal by
        its index.
        """
        literals = self.context.literals
        # During module initialization we store all the constructed objects here
        self.declare_global('PyObject *[%d]' % literals.num_literals(),
                            'CPyStatics')
        # Descriptions of str literals
        init_str = c_string_initializer(literals.encoded_str_values())
        self.declare_global('const char []',
                            'CPyLit_Str',
                            initializer=init_str)
        # Descriptions of bytes literals
        init_bytes = c_string_initializer(literals.encoded_bytes_values())
        self.declare_global('const char []',
                            'CPyLit_Bytes',
                            initializer=init_bytes)
        # Descriptions of int literals
        init_int = c_string_initializer(literals.encoded_int_values())
        self.declare_global('const char []',
                            'CPyLit_Int',
                            initializer=init_int)
        # Descriptions of float literals
        init_floats = c_array_initializer(literals.encoded_float_values())
        self.declare_global('const double []',
                            'CPyLit_Float',
                            initializer=init_floats)
        # Descriptions of complex literals
        init_complex = c_array_initializer(literals.encoded_complex_values())
        self.declare_global('const double []',
                            'CPyLit_Complex',
                            initializer=init_complex)
        # Descriptions of tuple literals
        init_tuple = c_array_initializer(literals.encoded_tuple_values())
        self.declare_global('const int []',
                            'CPyLit_Tuple',
                            initializer=init_tuple)