Image copyright AFP/Getty Images Image caption Alan Turing's work at Bletchley Park during World War Two enabled German codes to be cracked

A bill that would have wiped clean the criminal records of thousands of gay men has fallen at its first parliamentary hurdle.

The private member's bill would have pardoned all men living with UK convictions for same-sex offences committed before the law was changed.

There were emotional scenes with one MP fighting back tears during his speech.

The government, which has its own plans for posthumous pardons, "talked out" the bill, which will not now go ahead.

Minister Sam Gyimah spoke for 25 minutes, reaching the time limit allotted for the debate.

There were shouts of "shame" and "shameful" from angry MPs as the seconds ticked down and proceedings came to an end.

Media playback is unsupported on your device Media caption Justice Minister Sam Gyimah heckled by MPs during Turing Bill debate

SNP MP John Nicolson said he had secured government support for his bill, which was withdrawn when the Ministry of Justice published its own commitment to a "Turing's law" on Thursday.

During Friday's debate Mr Nicolson accused the government of trying to "hijack" his plans.

The government's rival measure, an amendment to the Policing and Crimes Bill announced on Thursday, would grant pardons for those convicted who have since died. Ministers say those who are still alive can go through a "disregard process" to clear their names.

Media playback is unsupported on your device Media caption An emotional Chris Bryant speaks about gay MPs' roles in the anti-Nazi movement

The government said it would not support Mr Nicolson's Sexual Offences (Pardons) Bill - which proposes a blanket pardon for the living - because it could lead to some people being cleared of offences that are still crimes.

"I understand and support the intentions behind Mr Nicolson's Bill, however I worry that he has not fully thought through the consequences," said justice minister Sam Gyimah. "Our way forward will be both faster and fairer."

Speaking during the debate, Mr Nicolson said: "I have to ask the House, should we not prioritise the living over the dead?"

His bill would have "set aside" nearly 50,000 convictions, about 15,000 of which apply to men who are still alive today.

Mr Nicolson says he was motivated by his work as a BBC journalist in the 1990s: "I made a documentary in the 1990s looking at the discriminatory laws which criminalised gay men.

"There were some shocking injustices. Men were arrested aged 21 for having 'under-age sex' with their 20-year-old boyfriends," he said.

How would the Turing Bill work?

The bill is intended to set aside only convictions made under:

Section 12 of the Sexual Offences Act 1956 (buggery)

Section 13 of that act (gross indecency between men)

Section 32 of that act (solicitation by men), or

Section 61 of the Offences against the Person Act 1861 or section 11 of the Criminal Law Amendment Act 1885.

And it would do so only under the conditions that:

The other person involved at the time the act was committed was a consenting partner aged 16 or above

The act would not constitute an offence under section 71 of the Sexual Offences Act 2003 (sexual activity in a public lavatory)

The act in question would not still be illegal for any other reason.

The debate saw MPs from both government and opposition parties speaking in favour of moving the bill through Parliament.

Labour Rhondda MP Chris Bryant made an emotional speech in favour of the bill, recalling gay and bisexual MPs who demanded the right to fight in World War Two, insisting they and others should receive "something that feels like an apology".

Image caption The SNP's John Nicolson wanted to bring "closure" to gay men still living with criminal convictions for sex.

Conservative MP Iain Stewart said that while he would support the government amendment, he believed it wouldn't go far enough.

Mr Stewart said: "We can move forward in a much more symbolic way... a way which will really make a difference to many people in this country."

A 2015 petition calling for pardons for the estimated 49,000 men affected by convictions for consensual gay sex was signed by over half a million people, including the actor Benedict Cumberbatch, who played Turing in the 2014 film The Imitation Game.

Another signatory was Turing's great-niece Rachel Barnes. Speaking to BBC News in 2015, Ms Barnes said: "We've always considered that it is totally unjust that only Alan was given a pardon.

"There were 50,000 other homosexuals who were convicted and not given a pardon. We would really like this to be put right now."