Andrew Miller is UK editor of ESPNcricinfo @miller_cricket
Pakistan 177 and 294 (Shafiq 88, Babar 72, Masood 61) lead South Africa 431 (du Plessis 103, Bavuma 75, de Kock 59) by 40 runs
At the fourth time of asking, Pakistan's batsmen found the grit, resolve and flair required to compete with a pumped-up South African attack in their own conditions. But, despite a fine century stand between Asad Shafiq and Shan Masood, and a doughty 72 from Babar Azam, their opponents could not be denied forever, as South Africa's pace quartet closed ranks in the evening session to put a crushing victory beyond doubt.
By the close, Pakistan had at least staved off the prospect of an innings defeat, with Babar's hard-fought half-century providing the lower-order with backbone as Dale Steyn in particular rolled back the years in a venomous short-pitched onslaught on any tailender who strayed into his sights. But, with a target of 41 when play resumes on day four, a swift kill and a 2-0 series win is surely a formality.
In fact, the match might yet have been wrapped up this evening, but for some farcical scenes in the closing overs of the day. With a lead of 25, and the prospect of an extra half-hour for South Africa to gallop to the finish, Vernon Philander looked to have finished the innings when Mohammad Abbas slogged to mid-off. But with the players already sprinting from the field - not least Quinton de Kock, who had clearly been primed to give it some humpty as a pinch-hitter - replays showed that Philander had overstepped.
Back came South Africa's fielders - de Kock still pulling his gloves back on with a look of misery on his face - whereupon Shaheen Afridi served up another over of mayhem, mowing 12 vital runs off Steyn, including a top-edged four, a straight six, a dropped catch and a thump on the helmet. By the time his fun was eventually ended to give Kagiso Rabada his fourth wicket of the innings, the moment had passed, and de Kock decided that a 20-minute timeframe in which to slap his side to victory was too tight an ask, and the umpires said that was that.
The scenes in those closing overs epitomised an engrossing day's play, and one in which Pakistan actually made much of the running. This came principally through Shafiq and Masood's enterprising third-wicket stand of 132 - their team's first three-figure partnership of the series - and so well did they play that, for as long as it lasted, a Headingley or Kolkata-style miracle could not be entirely ruled out. This is Pakistan after all, the team that is never more dangerous than when they have been completely written off.
And that was pretty much the situation when Shafiq and Masood came together at 27 for 2 in the 11th over of Pakistan's second innings, following the dismissal of Azhar Ali to a shooter from Rabada that pinned him plumb on the front pad for 6. In the first hour of the morning, South Africa's tail had biffed a quick 49 runs to take their lead to a daunting 254, and that surely seemed enough to see off a side that had not passed 200 in any of its previous three innings of the series.
But Masood and Shafiq had other ideas. Masood had been promoted to open in place of the misfiring Fakhar Zaman after making an attractive 44 in the first innings, and responded with another unruffled performance that was at odds with the skittish efforts around him. But it was Shafiq's injection of intent after the lunch break that turned Pakistan's innings from one of dour survival to something approaching a genuine counterattack.
Shafiq had to ride his luck early on, with both Philander and Duanne Olivier finding awkward lift from a good length to threaten his gloves and ribs. But, having laced a brace of enterprising cuts for four through backward point, his innings went into overdrive after taking on an Olivier bouncer, and top-edging a six over fine leg.
Suddenly, all short balls were being treated as an opportunity for runs, and with South Africa maintaining their attacking fields with a big deficit still to be written off, there were plenty of gaps to be exploited. At the close of the second day, Mickey Arthur had made some damning comments about the state of the pitches for this series, but Shafiq's shift in mindset seemed more than enough to transcend the apparent demons, as he rattled through to a 56-ball half-century.
At the other end, Masood grew in stature as the partnership progressed. He had the closest shave of the innings when, on 24, he successfully overturned an lbw decision that was shown to be missing leg, but aside from his defensive purity, it was the quality of his strokeplay that stood out, in particular a laced drive through long-off that was little more than a push.
His fifty came up from 98 balls with a flick for four off the pads against Steyn, and he had just passed 100 runs for the match when Steyn extracted his revenge, finding a perfect length outside off to draw a fatal nibble.
Enter Babar, who lived dangerously in the opening moments of his innings, edging low through the slips to get off the mark before wearing a nasty lifter from Rabada on the body two balls later. But he drilled the final two balls of the session for four to sign off in style, and send a signal to South Africa that there'd be plenty more hard yakka to come in the evening session.
And so it proved, although perhaps not in the manner anticipated. Shafiq continued to take on the short ball with aplomb to move to within touching distance of a richly deserved century, when the persevering Philander induced a decisive nick in the channel outside off, to send him on his way for 88 and leave Pakistan's innings in the balance once again at 194 for 4.
Fakhar, making his belated appearance at No. 6, was in no mood to restore that balance. His grim tour continued when he made an utter hash of a cross-batted mow at Rabada, who ran back in his follow through to complete a steepling catch, and when the skipper Sarfraz Ahmed was done in by Olivier - his lbw adjudged to be clipping the bails by roughly the same margin that Temba Bavuma's had been missing in the first innings - Pakistan were 220 for 6 and freefalling.
South Africa saw their opening, and went for it, with Mohammad Amir subjected to a fearful peppering - first from Olivier, who slammed him on the gloves two balls in a row, and then Steyn, who ripped a bouncer through his defences for de Kock to wrap up a savage five-ball duck. The same approach soon did for Yasir Shah too, the substitute Zubayr Hamza making a tough chance look easy at an old-school long-stop position on the boundary, but not before Babar had taken the attack back to his opponents, motoring to his half-century with four fours in an over off Olivier.
He did not hold back after that, either. Another top-edge for four took Pakistan into the lead, and he managed four more boundaries - all full-blooded, not all middled - before chancing his arm once too often to give Rabada another breakthrough. From then on it was merely a race against time for South Africa to earn themselves an extra day on the golf course. Afridi's defiance ensured one last moral victory for Pakistan but a series defeat was all but sealed.
Faf du Plessis suspended for one Test due to second over-rate offence
The South Africa captain will be forced to skip the third and final Test against Pakistan, beginning in Johannesburg from January 11
Pakistan bowling 'not up to the mark', laments Sarfraz Ahmed
Pakistan captain critical of his seamers' lack of pace after his team were beaten by nine wickets in Cape Town
Amla injured but Elgar seals South Africa's fourth-morning victory
Hashim Amla was forced to retire hurt as South Africa ticked off the 41 runs required for victory
Counterattacking approach brings runs but little joy for Asad Shafiq
The Pakistan batsman went after the short ball and scored a high-quality 88 on a bittersweet day when his side slid to the brink of defeat