# Buggy code
def _intersect(self, other):
    from sympy.solvers.diophantine import diophantine

    if self.base_set is S.Integers:
        if isinstance(other, ImageSet) and other.base_set is S.Integers:
            f, g = self.lamda.expr, other.lamda.expr
            n, m = self.lamda.variables[0], other.lamda.variables[0]

            # Diophantine sorts the solutions according to the alphabetic
            # order of the variable names, since the result should not depend
            # on the variable name, they are replaced by the dummy variables
            # below
            a, b = Dummy("a"), Dummy("b")
            f, g = f.subs(n, a), g.subs(m, b)
            solns_set = diophantine(f - g)
            if solns_set == set():
                return EmptySet()
            solns = list(diophantine(f - g))
            if len(solns) == 1:
                t = list(solns[0][0].free_symbols)[0]
            else:
                return None

            # since 'a' < 'b'
            return imageset(Lambda(t, f.subs(a, solns[0][0])), S.Integers)

    if other == S.Reals:
        from sympy.solvers.solveset import solveset_real
        from sympy.core.function import expand_complex

        if len(self.lamda.variables) > 1:
            return None

        f = self.lamda.expr
        n = self.lamda.variables[0]

        n_ = Dummy(n.name, real=True)
        f_ = f.subs(n, n_)

        re, im = f_.as_real_imag()
        im = expand_complex(im)

        return imageset(Lambda(n_, re), self.base_set.intersect(solveset_real(im, n_)))


# Traceback (TypeError)
Following function call generate an error in Python3 but work well in Python2.

factor_list(x*(x+y))

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/thilinarmtb/GSoC/sympy/sympy/polys/polytools.py", line 5675, in factor_list
return _generic_factor_list(f, gens, args, method='factor')
File "/home/thilinarmtb/GSoC/sympy/sympy/polys/polytools.py", line 5423, in _generic_factor_list
fp = _sorted_factors(fp, method)
File "/home/thilinarmtb/GSoC/sympy/sympy/polys/polytools.py", line 5322, in _sorted_factors
return sorted(factors, key=key)
TypeError: unorderable types: list() < int()


Similarly following calls generate the same error:
[1] factor_list(y*(x+y)),  factor_list(x*y + y**2)
[2] factor_list(x*(x+y+z)), factor_list(x**2+x*y+x*z)
[3] factor_list((x + 1)*(x + y))

But the following don't:
[4] factor_list(x*(x**2 + y**2)), factor_list(x**3 + x*y**2)
[5] factor_list(x**3 - x),  factor_list(x*(x**2 - 1))
[6] factor_list(x**2 + x*y + x + y)
[7] factor_list((x + z)*(x + y)), factor_list(x**2 + x*z + y*x + y*z)

Here [6] is mathematically same as [3]. But only [3] gives the error.

# Fixed code
def _intersect(self, other):
    from sympy.solvers.diophantine import diophantine

    if self.base_set is S.Integers:
        g = None
        if isinstance(other, ImageSet) and other.base_set is S.Integers:
            g = other.lamda.expr
            m = other.lamda.variables[0]
        elif other is S.Integers:
            m = g = Dummy("x")
        if g is not None:
            f = self.lamda.expr
            n = self.lamda.variables[0]
            # Diophantine sorts the solutions according to the alphabetic
            # order of the variable names, since the result should not depend
            # on the variable name, they are replaced by the dummy variables
            # below
            a, b = Dummy("a"), Dummy("b")
            f, g = f.subs(n, a), g.subs(m, b)
            solns_set = diophantine(f - g)
            if solns_set == set():
                return EmptySet()
            solns = list(diophantine(f - g))

            if len(solns) != 1:
                return

            # since 'a' < 'b', select soln for n
            nsol = solns[0][0]
            t = nsol.free_symbols.pop()
            return imageset(Lambda(n, f.subs(a, nsol.subs(t, n))), S.Integers)

    if other == S.Reals:
        from sympy.solvers.solveset import solveset_real
        from sympy.core.function import expand_complex

        if len(self.lamda.variables) > 1:
            return None

        f = self.lamda.expr
        n = self.lamda.variables[0]

        n_ = Dummy(n.name, real=True)
        f_ = f.subs(n, n_)

        re, im = f_.as_real_imag()
        im = expand_complex(im)

        return imageset(Lambda(n_, re), self.base_set.intersect(solveset_real(im, n_)))