Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

""" 

per-test stdout/stderr capturing mechanism. 

 

""" 

from __future__ import absolute_import 

from __future__ import division 

from __future__ import print_function 

 

import collections 

import contextlib 

import io 

import os 

import sys 

from io import UnsupportedOperation 

from tempfile import TemporaryFile 

 

import six 

 

import pytest 

from _pytest.compat import CaptureIO 

 

patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"} 

 

 

def pytest_addoption(parser): 

group = parser.getgroup("general") 

group._addoption( 

"--capture", 

action="store", 

default="fd" if hasattr(os, "dup") else "sys", 

metavar="method", 

choices=["fd", "sys", "no"], 

help="per-test capturing method: one of fd|sys|no.", 

) 

group._addoption( 

"-s", 

action="store_const", 

const="no", 

dest="capture", 

help="shortcut for --capture=no.", 

) 

 

 

@pytest.hookimpl(hookwrapper=True) 

def pytest_load_initial_conftests(early_config, parser, args): 

ns = early_config.known_args_namespace 

if ns.capture == "fd": 

_py36_windowsconsoleio_workaround(sys.stdout) 

_colorama_workaround() 

_readline_workaround() 

pluginmanager = early_config.pluginmanager 

capman = CaptureManager(ns.capture) 

pluginmanager.register(capman, "capturemanager") 

 

# make sure that capturemanager is properly reset at final shutdown 

early_config.add_cleanup(capman.stop_global_capturing) 

 

# make sure logging does not raise exceptions at the end 

def silence_logging_at_shutdown(): 

if "logging" in sys.modules: 

sys.modules["logging"].raiseExceptions = False 

 

early_config.add_cleanup(silence_logging_at_shutdown) 

 

# finally trigger conftest loading but while capturing (issue93) 

capman.start_global_capturing() 

outcome = yield 

capman.suspend_global_capture() 

if outcome.excinfo is not None: 

out, err = capman.read_global_capture() 

sys.stdout.write(out) 

sys.stderr.write(err) 

 

 

class CaptureManager(object): 

""" 

Capture plugin, manages that the appropriate capture method is enabled/disabled during collection and each 

test phase (setup, call, teardown). After each of those points, the captured output is obtained and 

attached to the collection/runtest report. 

 

There are two levels of capture: 

* global: which is enabled by default and can be suppressed by the ``-s`` option. This is always enabled/disabled 

during collection and each test phase. 

* fixture: when a test function or one of its fixture depend on the ``capsys`` or ``capfd`` fixtures. In this 

case special handling is needed to ensure the fixtures take precedence over the global capture. 

""" 

 

def __init__(self, method): 

self._method = method 

self._global_capturing = None 

self._current_item = None 

 

def _getcapture(self, method): 

if method == "fd": 

return MultiCapture(out=True, err=True, Capture=FDCapture) 

elif method == "sys": 

return MultiCapture(out=True, err=True, Capture=SysCapture) 

elif method == "no": 

return MultiCapture(out=False, err=False, in_=False) 

else: 

raise ValueError("unknown capturing method: %r" % method) 

 

# Global capturing control 

 

def is_globally_capturing(self): 

return self._method != "no" 

 

def start_global_capturing(self): 

assert self._global_capturing is None 

self._global_capturing = self._getcapture(self._method) 

self._global_capturing.start_capturing() 

 

def stop_global_capturing(self): 

if self._global_capturing is not None: 

self._global_capturing.pop_outerr_to_orig() 

self._global_capturing.stop_capturing() 

self._global_capturing = None 

 

def resume_global_capture(self): 

# During teardown of the python process, and on rare occasions, capture 

# attributes can be `None` while trying to resume global capture. 

if self._global_capturing is not None: 

self._global_capturing.resume_capturing() 

 

def suspend_global_capture(self, in_=False): 

cap = getattr(self, "_global_capturing", None) 

if cap is not None: 

cap.suspend_capturing(in_=in_) 

 

def read_global_capture(self): 

return self._global_capturing.readouterr() 

 

# Fixture Control (it's just forwarding, think about removing this later) 

 

def activate_fixture(self, item): 

"""If the current item is using ``capsys`` or ``capfd``, activate them so they take precedence over 

the global capture. 

""" 

fixture = getattr(item, "_capture_fixture", None) 

if fixture is not None: 

fixture._start() 

 

def deactivate_fixture(self, item): 

"""Deactivates the ``capsys`` or ``capfd`` fixture of this item, if any.""" 

fixture = getattr(item, "_capture_fixture", None) 

if fixture is not None: 

fixture.close() 

 

def suspend_fixture(self, item): 

fixture = getattr(item, "_capture_fixture", None) 

if fixture is not None: 

fixture._suspend() 

 

def resume_fixture(self, item): 

fixture = getattr(item, "_capture_fixture", None) 

if fixture is not None: 

fixture._resume() 

 

# Helper context managers 

 

@contextlib.contextmanager 

def global_and_fixture_disabled(self): 

"""Context manager to temporarily disables global and current fixture capturing.""" 

# Need to undo local capsys-et-al if exists before disabling global capture 

self.suspend_fixture(self._current_item) 

self.suspend_global_capture(in_=False) 

try: 

yield 

finally: 

self.resume_global_capture() 

self.resume_fixture(self._current_item) 

 

@contextlib.contextmanager 

def item_capture(self, when, item): 

self.resume_global_capture() 

self.activate_fixture(item) 

try: 

yield 

finally: 

self.deactivate_fixture(item) 

self.suspend_global_capture(in_=False) 

 

out, err = self.read_global_capture() 

item.add_report_section(when, "stdout", out) 

item.add_report_section(when, "stderr", err) 

 

# Hooks 

 

@pytest.hookimpl(hookwrapper=True) 

def pytest_make_collect_report(self, collector): 

if isinstance(collector, pytest.File): 

self.resume_global_capture() 

outcome = yield 

self.suspend_global_capture() 

out, err = self.read_global_capture() 

rep = outcome.get_result() 

if out: 

rep.sections.append(("Captured stdout", out)) 

if err: 

rep.sections.append(("Captured stderr", err)) 

else: 

yield 

 

@pytest.hookimpl(hookwrapper=True) 

def pytest_runtest_protocol(self, item): 

self._current_item = item 

yield 

self._current_item = None 

 

@pytest.hookimpl(hookwrapper=True) 

def pytest_runtest_setup(self, item): 

with self.item_capture("setup", item): 

yield 

 

@pytest.hookimpl(hookwrapper=True) 

def pytest_runtest_call(self, item): 

with self.item_capture("call", item): 

yield 

 

@pytest.hookimpl(hookwrapper=True) 

def pytest_runtest_teardown(self, item): 

with self.item_capture("teardown", item): 

yield 

 

@pytest.hookimpl(tryfirst=True) 

def pytest_keyboard_interrupt(self, excinfo): 

self.stop_global_capturing() 

 

@pytest.hookimpl(tryfirst=True) 

def pytest_internalerror(self, excinfo): 

self.stop_global_capturing() 

 

 

capture_fixtures = {"capfd", "capfdbinary", "capsys", "capsysbinary"} 

 

 

def _ensure_only_one_capture_fixture(request, name): 

fixtures = set(request.fixturenames) & capture_fixtures - {name} 

if fixtures: 

fixtures = sorted(fixtures) 

fixtures = fixtures[0] if len(fixtures) == 1 else fixtures 

raise request.raiseerror( 

"cannot use {} and {} at the same time".format(fixtures, name) 

) 

 

 

@pytest.fixture 

def capsys(request): 

"""Enable capturing of writes to ``sys.stdout`` and ``sys.stderr`` and make 

captured output available via ``capsys.readouterr()`` method calls 

which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` 

objects. 

""" 

_ensure_only_one_capture_fixture(request, "capsys") 

with _install_capture_fixture_on_item(request, SysCapture) as fixture: 

yield fixture 

 

 

@pytest.fixture 

def capsysbinary(request): 

"""Enable capturing of writes to ``sys.stdout`` and ``sys.stderr`` and make 

captured output available via ``capsys.readouterr()`` method calls 

which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``bytes`` 

objects. 

""" 

_ensure_only_one_capture_fixture(request, "capsysbinary") 

# Currently, the implementation uses the python3 specific `.buffer` 

# property of CaptureIO. 

if sys.version_info < (3,): 

raise request.raiseerror("capsysbinary is only supported on python 3") 

with _install_capture_fixture_on_item(request, SysCaptureBinary) as fixture: 

yield fixture 

 

 

@pytest.fixture 

def capfd(request): 

"""Enable capturing of writes to file descriptors ``1`` and ``2`` and make 

captured output available via ``capfd.readouterr()`` method calls 

which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``text`` 

objects. 

""" 

_ensure_only_one_capture_fixture(request, "capfd") 

if not hasattr(os, "dup"): 

pytest.skip( 

"capfd fixture needs os.dup function which is not available in this system" 

) 

with _install_capture_fixture_on_item(request, FDCapture) as fixture: 

yield fixture 

 

 

@pytest.fixture 

def capfdbinary(request): 

"""Enable capturing of write to file descriptors 1 and 2 and make 

captured output available via ``capfdbinary.readouterr`` method calls 

which return a ``(out, err)`` tuple. ``out`` and ``err`` will be 

``bytes`` objects. 

""" 

_ensure_only_one_capture_fixture(request, "capfdbinary") 

if not hasattr(os, "dup"): 

pytest.skip( 

"capfdbinary fixture needs os.dup function which is not available in this system" 

) 

with _install_capture_fixture_on_item(request, FDCaptureBinary) as fixture: 

yield fixture 

 

 

@contextlib.contextmanager 

def _install_capture_fixture_on_item(request, capture_class): 

""" 

Context manager which creates a ``CaptureFixture`` instance and "installs" it on 

the item/node of the given request. Used by ``capsys`` and ``capfd``. 

 

The CaptureFixture is added as attribute of the item because it needs to accessed 

by ``CaptureManager`` during its ``pytest_runtest_*`` hooks. 

""" 

request.node._capture_fixture = fixture = CaptureFixture(capture_class, request) 

capmanager = request.config.pluginmanager.getplugin("capturemanager") 

# need to active this fixture right away in case it is being used by another fixture (setup phase) 

# if this fixture is being used only by a test function (call phase), then we wouldn't need this 

# activation, but it doesn't hurt 

capmanager.activate_fixture(request.node) 

yield fixture 

fixture.close() 

del request.node._capture_fixture 

 

 

class CaptureFixture(object): 

""" 

Object returned by :py:func:`capsys`, :py:func:`capsysbinary`, :py:func:`capfd` and :py:func:`capfdbinary` 

fixtures. 

""" 

 

def __init__(self, captureclass, request): 

self.captureclass = captureclass 

self.request = request 

self._capture = None 

self._captured_out = self.captureclass.EMPTY_BUFFER 

self._captured_err = self.captureclass.EMPTY_BUFFER 

 

def _start(self): 

# Start if not started yet 

if getattr(self, "_capture", None) is None: 

self._capture = MultiCapture( 

out=True, err=True, in_=False, Capture=self.captureclass 

) 

self._capture.start_capturing() 

 

def close(self): 

if self._capture is not None: 

out, err = self._capture.pop_outerr_to_orig() 

self._captured_out += out 

self._captured_err += err 

self._capture.stop_capturing() 

self._capture = None 

 

def readouterr(self): 

"""Read and return the captured output so far, resetting the internal buffer. 

 

:return: captured content as a namedtuple with ``out`` and ``err`` string attributes 

""" 

captured_out, captured_err = self._captured_out, self._captured_err 

if self._capture is not None: 

out, err = self._capture.readouterr() 

captured_out += out 

captured_err += err 

self._captured_out = self.captureclass.EMPTY_BUFFER 

self._captured_err = self.captureclass.EMPTY_BUFFER 

return CaptureResult(captured_out, captured_err) 

 

def _suspend(self): 

"""Suspends this fixture's own capturing temporarily.""" 

self._capture.suspend_capturing() 

 

def _resume(self): 

"""Resumes this fixture's own capturing temporarily.""" 

self._capture.resume_capturing() 

 

@contextlib.contextmanager 

def disabled(self): 

"""Temporarily disables capture while inside the 'with' block.""" 

capmanager = self.request.config.pluginmanager.getplugin("capturemanager") 

with capmanager.global_and_fixture_disabled(): 

yield 

 

 

def safe_text_dupfile(f, mode, default_encoding="UTF8"): 

""" return an open text file object that's a duplicate of f on the 

FD-level if possible. 

""" 

encoding = getattr(f, "encoding", None) 

try: 

fd = f.fileno() 

except Exception: 

if "b" not in getattr(f, "mode", "") and hasattr(f, "encoding"): 

# we seem to have a text stream, let's just use it 

return f 

else: 

newfd = os.dup(fd) 

if "b" not in mode: 

mode += "b" 

f = os.fdopen(newfd, mode, 0) # no buffering 

return EncodedFile(f, encoding or default_encoding) 

 

 

class EncodedFile(object): 

errors = "strict" # possibly needed by py3 code (issue555) 

 

def __init__(self, buffer, encoding): 

self.buffer = buffer 

self.encoding = encoding 

 

def write(self, obj): 

if isinstance(obj, six.text_type): 

obj = obj.encode(self.encoding, "replace") 

self.buffer.write(obj) 

 

def writelines(self, linelist): 

data = "".join(linelist) 

self.write(data) 

 

@property 

def name(self): 

"""Ensure that file.name is a string.""" 

return repr(self.buffer) 

 

def __getattr__(self, name): 

return getattr(object.__getattribute__(self, "buffer"), name) 

 

 

CaptureResult = collections.namedtuple("CaptureResult", ["out", "err"]) 

 

 

class MultiCapture(object): 

out = err = in_ = None 

 

def __init__(self, out=True, err=True, in_=True, Capture=None): 

if in_: 

self.in_ = Capture(0) 

if out: 

self.out = Capture(1) 

if err: 

self.err = Capture(2) 

 

def start_capturing(self): 

if self.in_: 

self.in_.start() 

if self.out: 

self.out.start() 

if self.err: 

self.err.start() 

 

def pop_outerr_to_orig(self): 

""" pop current snapshot out/err capture and flush to orig streams. """ 

out, err = self.readouterr() 

if out: 

self.out.writeorg(out) 

if err: 

self.err.writeorg(err) 

return out, err 

 

def suspend_capturing(self, in_=False): 

if self.out: 

self.out.suspend() 

if self.err: 

self.err.suspend() 

if in_ and self.in_: 

self.in_.suspend() 

self._in_suspended = True 

 

def resume_capturing(self): 

if self.out: 

self.out.resume() 

if self.err: 

self.err.resume() 

if hasattr(self, "_in_suspended"): 

self.in_.resume() 

del self._in_suspended 

 

def stop_capturing(self): 

""" stop capturing and reset capturing streams """ 

if hasattr(self, "_reset"): 

raise ValueError("was already stopped") 

self._reset = True 

if self.out: 

self.out.done() 

if self.err: 

self.err.done() 

if self.in_: 

self.in_.done() 

 

def readouterr(self): 

""" return snapshot unicode value of stdout/stderr capturings. """ 

return CaptureResult( 

self.out.snap() if self.out is not None else "", 

self.err.snap() if self.err is not None else "", 

) 

 

 

class NoCapture(object): 

EMPTY_BUFFER = None 

__init__ = start = done = suspend = resume = lambda *args: None 

 

 

class FDCaptureBinary(object): 

"""Capture IO to/from a given os-level filedescriptor. 

 

snap() produces `bytes` 

""" 

 

EMPTY_BUFFER = b"" 

 

def __init__(self, targetfd, tmpfile=None): 

self.targetfd = targetfd 

try: 

self.targetfd_save = os.dup(self.targetfd) 

except OSError: 

self.start = lambda: None 

self.done = lambda: None 

else: 

if targetfd == 0: 

assert not tmpfile, "cannot set tmpfile with stdin" 

tmpfile = open(os.devnull, "r") 

self.syscapture = SysCapture(targetfd) 

else: 

if tmpfile is None: 

f = TemporaryFile() 

with f: 

tmpfile = safe_text_dupfile(f, mode="wb+") 

if targetfd in patchsysdict: 

self.syscapture = SysCapture(targetfd, tmpfile) 

else: 

self.syscapture = NoCapture() 

self.tmpfile = tmpfile 

self.tmpfile_fd = tmpfile.fileno() 

 

def __repr__(self): 

return "<FDCapture %s oldfd=%s>" % (self.targetfd, self.targetfd_save) 

 

def start(self): 

""" Start capturing on targetfd using memorized tmpfile. """ 

try: 

os.fstat(self.targetfd_save) 

except (AttributeError, OSError): 

raise ValueError("saved filedescriptor not valid anymore") 

os.dup2(self.tmpfile_fd, self.targetfd) 

self.syscapture.start() 

 

def snap(self): 

self.tmpfile.seek(0) 

res = self.tmpfile.read() 

self.tmpfile.seek(0) 

self.tmpfile.truncate() 

return res 

 

def done(self): 

""" stop capturing, restore streams, return original capture file, 

seeked to position zero. """ 

targetfd_save = self.__dict__.pop("targetfd_save") 

os.dup2(targetfd_save, self.targetfd) 

os.close(targetfd_save) 

self.syscapture.done() 

_attempt_to_close_capture_file(self.tmpfile) 

 

def suspend(self): 

self.syscapture.suspend() 

os.dup2(self.targetfd_save, self.targetfd) 

 

def resume(self): 

self.syscapture.resume() 

os.dup2(self.tmpfile_fd, self.targetfd) 

 

def writeorg(self, data): 

""" write to original file descriptor. """ 

if isinstance(data, six.text_type): 

data = data.encode("utf8") # XXX use encoding of original stream 

os.write(self.targetfd_save, data) 

 

 

class FDCapture(FDCaptureBinary): 

"""Capture IO to/from a given os-level filedescriptor. 

 

snap() produces text 

""" 

 

EMPTY_BUFFER = str() 

 

def snap(self): 

res = FDCaptureBinary.snap(self) 

enc = getattr(self.tmpfile, "encoding", None) 

if enc and isinstance(res, bytes): 

res = six.text_type(res, enc, "replace") 

return res 

 

 

class SysCapture(object): 

 

EMPTY_BUFFER = str() 

 

def __init__(self, fd, tmpfile=None): 

name = patchsysdict[fd] 

self._old = getattr(sys, name) 

self.name = name 

if tmpfile is None: 

if name == "stdin": 

tmpfile = DontReadFromInput() 

else: 

tmpfile = CaptureIO() 

self.tmpfile = tmpfile 

 

def start(self): 

setattr(sys, self.name, self.tmpfile) 

 

def snap(self): 

res = self.tmpfile.getvalue() 

self.tmpfile.seek(0) 

self.tmpfile.truncate() 

return res 

 

def done(self): 

setattr(sys, self.name, self._old) 

del self._old 

_attempt_to_close_capture_file(self.tmpfile) 

 

def suspend(self): 

setattr(sys, self.name, self._old) 

 

def resume(self): 

setattr(sys, self.name, self.tmpfile) 

 

def writeorg(self, data): 

self._old.write(data) 

self._old.flush() 

 

 

class SysCaptureBinary(SysCapture): 

EMPTY_BUFFER = b"" 

 

def snap(self): 

res = self.tmpfile.buffer.getvalue() 

self.tmpfile.seek(0) 

self.tmpfile.truncate() 

return res 

 

 

class DontReadFromInput(six.Iterator): 

"""Temporary stub class. Ideally when stdin is accessed, the 

capturing should be turned off, with possibly all data captured 

so far sent to the screen. This should be configurable, though, 

because in automated test runs it is better to crash than 

hang indefinitely. 

""" 

 

encoding = None 

 

def read(self, *args): 

raise IOError("reading from stdin while output is captured") 

 

readline = read 

readlines = read 

__next__ = read 

 

def __iter__(self): 

return self 

 

def fileno(self): 

raise UnsupportedOperation("redirected stdin is pseudofile, has no fileno()") 

 

def isatty(self): 

return False 

 

def close(self): 

pass 

 

@property 

def buffer(self): 

if sys.version_info >= (3, 0): 

return self 

else: 

raise AttributeError("redirected stdin has no attribute buffer") 

 

 

def _colorama_workaround(): 

""" 

Ensure colorama is imported so that it attaches to the correct stdio 

handles on Windows. 

 

colorama uses the terminal on import time. So if something does the 

first import of colorama while I/O capture is active, colorama will 

fail in various ways. 

""" 

 

if not sys.platform.startswith("win32"): 

return 

try: 

import colorama # noqa 

except ImportError: 

pass 

 

 

def _readline_workaround(): 

""" 

Ensure readline is imported so that it attaches to the correct stdio 

handles on Windows. 

 

Pdb uses readline support where available--when not running from the Python 

prompt, the readline module is not imported until running the pdb REPL. If 

running pytest with the --pdb option this means the readline module is not 

imported until after I/O capture has been started. 

 

This is a problem for pyreadline, which is often used to implement readline 

support on Windows, as it does not attach to the correct handles for stdout 

and/or stdin if they have been redirected by the FDCapture mechanism. This 

workaround ensures that readline is imported before I/O capture is setup so 

that it can attach to the actual stdin/out for the console. 

 

See https://github.com/pytest-dev/pytest/pull/1281 

""" 

 

if not sys.platform.startswith("win32"): 

return 

try: 

import readline # noqa 

except ImportError: 

pass 

 

 

def _py36_windowsconsoleio_workaround(stream): 

""" 

Python 3.6 implemented unicode console handling for Windows. This works 

by reading/writing to the raw console handle using 

``{Read,Write}ConsoleW``. 

 

The problem is that we are going to ``dup2`` over the stdio file 

descriptors when doing ``FDCapture`` and this will ``CloseHandle`` the 

handles used by Python to write to the console. Though there is still some 

weirdness and the console handle seems to only be closed randomly and not 

on the first call to ``CloseHandle``, or maybe it gets reopened with the 

same handle value when we suspend capturing. 

 

The workaround in this case will reopen stdio with a different fd which 

also means a different handle by replicating the logic in 

"Py_lifecycle.c:initstdio/create_stdio". 

 

:param stream: in practice ``sys.stdout`` or ``sys.stderr``, but given 

here as parameter for unittesting purposes. 

 

See https://github.com/pytest-dev/py/issues/103 

""" 

if not sys.platform.startswith("win32") or sys.version_info[:2] < (3, 6): 

return 

 

# bail out if ``stream`` doesn't seem like a proper ``io`` stream (#2666) 

if not hasattr(stream, "buffer"): 

return 

 

buffered = hasattr(stream.buffer, "raw") 

raw_stdout = stream.buffer.raw if buffered else stream.buffer 

 

if not isinstance(raw_stdout, io._WindowsConsoleIO): 

return 

 

def _reopen_stdio(f, mode): 

if not buffered and mode[0] == "w": 

buffering = 0 

else: 

buffering = -1 

 

return io.TextIOWrapper( 

open(os.dup(f.fileno()), mode, buffering), 

f.encoding, 

f.errors, 

f.newlines, 

f.line_buffering, 

) 

 

sys.stdin = _reopen_stdio(sys.stdin, "rb") 

sys.stdout = _reopen_stdio(sys.stdout, "wb") 

sys.stderr = _reopen_stdio(sys.stderr, "wb") 

 

 

def _attempt_to_close_capture_file(f): 

"""Suppress IOError when closing the temporary file used for capturing streams in py27 (#2370)""" 

if six.PY2: 

try: 

f.close() 

except IOError: 

pass 

else: 

f.close()