The most beautiful thing the Catholic Church has - and the most misunderstood and, often times, intentionally misrepresented dogma of our faith by non-Catholics - is the grace by which God can bestow on certain people in our faith.
Just as God could have given sufficient grace for the Apostles, St. John the Baptist, and St. Joseph, to lead relatively or absolutely sinless lives, God also bestows that special grace on EVERY pope - whether that pope was bad or good as a human being - to prevent that particular people from ever fallibly teaching a matter of faith and morals. It is that special “charism” that follows from Christ’s promise to St. Peter that the gates of hell would never prevail against the church.
We had ONE pope (whose name escapes me right now

) who came very, very close to signing a document that would have, for the first and only time up to that point (and since) resulted in a teaching of a false doctrine concerning faith/morals. But even under duress (external or internal), all Holy Fathers “proved” that Christ had not lied when he said he would protect his Church.
P.S. - although I am very thankful for it, it does make me sad and feel ashamed at how I behave.
God grants ALL men sufficient grace to lead a sinless life. To not do so would make God out to be cruel, and that is simply not true. My shame arises - and arises IMMEDIATELY - the moment I sin, knowing that I had sufficient grace in me to have not acted as I just did, but nevertheless chose to turn away from God anyway, all because of that root of all sin: pride.