# Buggy code
def generate_type_ready_code(self, env, entry, code):
    # Generate a call to PyType_Ready for an extension
    # type defined in this module.
    type = entry.type
    typeobj_cname = type.typeobj_cname
    scope = type.scope
    if scope:  # could be None if there was an error
        if entry.visibility != "extern":
            for slot in TypeSlots.slot_table:
                slot.generate_dynamic_init_code(scope, code)
            code.putln(
                "if (PyType_Ready(&%s) < 0) %s"
                % (typeobj_cname, code.error_goto(entry.pos))
            )
            # Don't inherit tp_print from builtin types, restoring the
            # behavior of using tp_repr or tp_str instead.
            code.putln("%s.tp_print = 0;" % typeobj_cname)
            # Fix special method docstrings. This is a bit of a hack, but
            # unless we let PyType_Ready create the slot wrappers we have
            # a significant performance hit. (See trac #561.)
            for func in entry.type.scope.pyfunc_entries:
                is_buffer = func.name in ("__getbuffer__", "__releasebuffer__")
                if (
                    func.is_special
                    and Options.docstrings
                    and func.wrapperbase_cname
                    and not is_buffer
                ):
                    slot = TypeSlots.method_name_to_slot[func.name]
                    preprocessor_guard = slot.preprocessor_guard_code()
                    if preprocessor_guard:
                        code.putln(preprocessor_guard)
                    code.putln("#if CYTHON_COMPILING_IN_CPYTHON")
                    code.putln("{")
                    code.putln(
                        'PyObject *wrapper = PyObject_GetAttrString((PyObject *)&%s, "%s"); %s'
                        % (
                            typeobj_cname,
                            func.name,
                            code.error_goto_if_null("wrapper", entry.pos),
                        )
                    )
                    code.putln("if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {")
                    code.putln(
                        "%s = *((PyWrapperDescrObject *)wrapper)->d_base;"
                        % (func.wrapperbase_cname)
                    )
                    code.putln(
                        "%s.doc = %s;" % (func.wrapperbase_cname, func.doc_cname)
                    )
                    code.putln(
                        "((PyWrapperDescrObject *)wrapper)->d_base = &%s;"
                        % (func.wrapperbase_cname)
                    )
                    code.putln("}")
                    code.putln("}")
                    code.putln("#endif")
                    if preprocessor_guard:
                        code.putln("#endif")
            if type.vtable_cname:
                code.putln(
                    "if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s"
                    % (typeobj_cname, type.vtabptr_cname, code.error_goto(entry.pos))
                )
                code.globalstate.use_utility_code(
                    UtilityCode.load_cached("SetVTable", "ImportExport.c")
                )
            if not type.scope.is_internal and not type.scope.directives["internal"]:
                # scope.is_internal is set for types defined by
                # Cython (such as closures), the 'internal'
                # directive is set by users
                code.putln(
                    'if (PyObject_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s'
                    % (
                        Naming.module_cname,
                        scope.class_name,
                        typeobj_cname,
                        code.error_goto(entry.pos),
                    )
                )
            weakref_entry = (
                scope.lookup_here("__weakref__")
                if not scope.is_closure_class_scope
                else None
            )
            if weakref_entry:
                if weakref_entry.type is py_object_type:
                    tp_weaklistoffset = "%s.tp_weaklistoffset" % typeobj_cname
                    if type.typedef_flag:
                        objstruct = type.objstruct_cname
                    else:
                        objstruct = "struct %s" % type.objstruct_cname
                    code.putln(
                        "if (%s == 0) %s = offsetof(%s, %s);"
                        % (
                            tp_weaklistoffset,
                            tp_weaklistoffset,
                            objstruct,
                            weakref_entry.cname,
                        )
                    )
                else:
                    error(
                        weakref_entry.pos, "__weakref__ slot must be of type 'object'"
                    )
            if (
                scope.lookup_here("__reduce_cython__")
                if not scope.is_closure_class_scope
                else None
            ):
                # Unfortunately, we cannot reliably detect whether a
                # superclass defined __reduce__ at compile time, so we must
                # do so at runtime.
                code.globalstate.use_utility_code(
                    UtilityCode.load_cached("SetupReduce", "ExtensionTypes.c")
                )
                code.putln(
                    "if (__Pyx_setup_reduce((PyObject*)&%s) < 0) %s"
                    % (typeobj_cname, code.error_goto(entry.pos))
                )


# Traceback (KeyError)
[sagelib-8.1.rc2] [1/2] Cythonizing sage/libs/pynac/constant.pyx
[sagelib-8.1.rc2] [2/2] Cythonizing sage/libs/pynac/pynac.pyx
[sagelib-8.1.rc2] Traceback (most recent call last):
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 1179, in cythonize_one_helper
[sagelib-8.1.rc2]     return cythonize_one(*m)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 1144, in cythonize_one
[sagelib-8.1.rc2]     result = compile([pyx_file], options)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/Main.py", line 695, in compile
[sagelib-8.1.rc2]     return compile_multiple(source, options)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/Main.py", line 673, in compile_multiple
[sagelib-8.1.rc2]     result = run_pipeline(source, options, context=context)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/Main.py", line 494, in run_pipeline
[sagelib-8.1.rc2]     err, enddata = Pipeline.run_pipeline(pipeline, source)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/Pipeline.py", line 354, in run_pipeline
[sagelib-8.1.rc2]     data = run(phase, data)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/Pipeline.py", line 334, in run
[sagelib-8.1.rc2]     return phase(data)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/Pipeline.py", line 52, in generate_pyx_code_stage
[sagelib-8.1.rc2]     module_node.process_implementation(options, result)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.py", line 141, in process_implementation
[sagelib-8.1.rc2]     self.generate_c_code(env, options, result)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.py", line 380, in generate_c_code
[sagelib-8.1.rc2]     self.generate_module_init_func(modules[:-1], env, globalstate['init_module'])
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.py", line 2377, in generate_module_init_func
[sagelib-8.1.rc2]     self.generate_type_init_code(env, code)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.py", line 2864, in generate_type_init_code
[sagelib-8.1.rc2]     self.generate_type_ready_code(env, entry, code)
[sagelib-8.1.rc2]   File "/home/ralf/sage/local/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.py", line 2963, in generate_type_ready_code
[sagelib-8.1.rc2]     slot = TypeSlots.method_name_to_slot[func.name]
[sagelib-8.1.rc2] KeyError: u'__eq__'

# Fixed code
def generate_type_ready_code(self, env, entry, code):
    # Generate a call to PyType_Ready for an extension
    # type defined in this module.
    type = entry.type
    typeobj_cname = type.typeobj_cname
    scope = type.scope
    if scope:  # could be None if there was an error
        if entry.visibility != "extern":
            for slot in TypeSlots.slot_table:
                slot.generate_dynamic_init_code(scope, code)
            code.putln(
                "if (PyType_Ready(&%s) < 0) %s"
                % (typeobj_cname, code.error_goto(entry.pos))
            )
            # Don't inherit tp_print from builtin types, restoring the
            # behavior of using tp_repr or tp_str instead.
            code.putln("%s.tp_print = 0;" % typeobj_cname)
            # Fix special method docstrings. This is a bit of a hack, but
            # unless we let PyType_Ready create the slot wrappers we have
            # a significant performance hit. (See trac #561.)
            for func in entry.type.scope.pyfunc_entries:
                is_buffer = func.name in ("__getbuffer__", "__releasebuffer__")
                if (
                    func.is_special
                    and Options.docstrings
                    and func.wrapperbase_cname
                    and not is_buffer
                ):
                    slot = TypeSlots.method_name_to_slot.get(func.name)
                    preprocessor_guard = (
                        slot.preprocessor_guard_code() if slot else None
                    )
                    if preprocessor_guard:
                        code.putln(preprocessor_guard)
                    code.putln("#if CYTHON_COMPILING_IN_CPYTHON")
                    code.putln("{")
                    code.putln(
                        'PyObject *wrapper = PyObject_GetAttrString((PyObject *)&%s, "%s"); %s'
                        % (
                            typeobj_cname,
                            func.name,
                            code.error_goto_if_null("wrapper", entry.pos),
                        )
                    )
                    code.putln("if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {")
                    code.putln(
                        "%s = *((PyWrapperDescrObject *)wrapper)->d_base;"
                        % (func.wrapperbase_cname)
                    )
                    code.putln(
                        "%s.doc = %s;" % (func.wrapperbase_cname, func.doc_cname)
                    )
                    code.putln(
                        "((PyWrapperDescrObject *)wrapper)->d_base = &%s;"
                        % (func.wrapperbase_cname)
                    )
                    code.putln("}")
                    code.putln("}")
                    code.putln("#endif")
                    if preprocessor_guard:
                        code.putln("#endif")
            if type.vtable_cname:
                code.putln(
                    "if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s"
                    % (typeobj_cname, type.vtabptr_cname, code.error_goto(entry.pos))
                )
                code.globalstate.use_utility_code(
                    UtilityCode.load_cached("SetVTable", "ImportExport.c")
                )
            if not type.scope.is_internal and not type.scope.directives["internal"]:
                # scope.is_internal is set for types defined by
                # Cython (such as closures), the 'internal'
                # directive is set by users
                code.putln(
                    'if (PyObject_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s'
                    % (
                        Naming.module_cname,
                        scope.class_name,
                        typeobj_cname,
                        code.error_goto(entry.pos),
                    )
                )
            weakref_entry = (
                scope.lookup_here("__weakref__")
                if not scope.is_closure_class_scope
                else None
            )
            if weakref_entry:
                if weakref_entry.type is py_object_type:
                    tp_weaklistoffset = "%s.tp_weaklistoffset" % typeobj_cname
                    if type.typedef_flag:
                        objstruct = type.objstruct_cname
                    else:
                        objstruct = "struct %s" % type.objstruct_cname
                    code.putln(
                        "if (%s == 0) %s = offsetof(%s, %s);"
                        % (
                            tp_weaklistoffset,
                            tp_weaklistoffset,
                            objstruct,
                            weakref_entry.cname,
                        )
                    )
                else:
                    error(
                        weakref_entry.pos, "__weakref__ slot must be of type 'object'"
                    )
            if (
                scope.lookup_here("__reduce_cython__")
                if not scope.is_closure_class_scope
                else None
            ):
                # Unfortunately, we cannot reliably detect whether a
                # superclass defined __reduce__ at compile time, so we must
                # do so at runtime.
                code.globalstate.use_utility_code(
                    UtilityCode.load_cached("SetupReduce", "ExtensionTypes.c")
                )
                code.putln(
                    "if (__Pyx_setup_reduce((PyObject*)&%s) < 0) %s"
                    % (typeobj_cname, code.error_goto(entry.pos))
                )