bpo-22831: Use "with" to avoid possible fd leaks in distutils. (GH-10… · python/cpython@c5d5dfd (original) (raw)

`@@ -378,35 +378,34 @@ def byte_compile (py_files,

`

378

378

`else:

`

379

379

`script = open(script_name, "w")

`

380

380

``

381

``

`-

script.write("""\

`

``

381

`+

with script:

`

``

382

`+

script.write("""\

`

382

383

`from distutils.util import byte_compile

`

383

384

`files = [

`

384

385

`""")

`

385

386

``

386

``

`-

XXX would be nice to write absolute filenames, just for

`

387

``

`-

safety's sake (script should be more robust in the face of

`

388

``

`-

chdir'ing before running it). But this requires abspath'ing

`

389

``

`-

'prefix' as well, and that breaks the hack in build_lib's

`

390

``

`-

'byte_compile()' method that carefully tacks on a trailing

`

391

``

`-

slash (os.sep really) to make sure the prefix here is "just

`

392

``

`-

right". This whole prefix business is rather delicate -- the

`

393

``

`-

problem is that it's really a directory, but I'm treating it

`

394

``

`-

as a dumb string, so trailing slashes and so forth matter.

`

395

``

-

396

``

`-

#py_files = map(os.path.abspath, py_files)

`

397

``

`-

#if prefix:

`

398

``

`-

prefix = os.path.abspath(prefix)

`

399

``

-

400

``

`-

script.write(",\n".join(map(repr, py_files)) + "]\n")

`

401

``

`-

script.write("""

`

``

387

`+

XXX would be nice to write absolute filenames, just for

`

``

388

`+

safety's sake (script should be more robust in the face of

`

``

389

`+

chdir'ing before running it). But this requires abspath'ing

`

``

390

`+

'prefix' as well, and that breaks the hack in build_lib's

`

``

391

`+

'byte_compile()' method that carefully tacks on a trailing

`

``

392

`+

slash (os.sep really) to make sure the prefix here is "just

`

``

393

`+

right". This whole prefix business is rather delicate -- the

`

``

394

`+

problem is that it's really a directory, but I'm treating it

`

``

395

`+

as a dumb string, so trailing slashes and so forth matter.

`

``

396

+

``

397

`+

#py_files = map(os.path.abspath, py_files)

`

``

398

`+

#if prefix:

`

``

399

`+

prefix = os.path.abspath(prefix)

`

``

400

+

``

401

`+

script.write(",\n".join(map(repr, py_files)) + "]\n")

`

``

402

`+

script.write("""

`

402

403

`byte_compile(files, optimize=%r, force=%r,

`

403

404

` prefix=%r, base_dir=%r,

`

404

405

` verbose=%r, dry_run=0,

`

405

406

` direct=1)

`

406

407

`""" % (optimize, force, prefix, base_dir, verbose))

`

407

408

``

408

``

`-

script.close()

`

409

``

-

410

409

`cmd = [sys.executable]

`

411

410

`cmd.extend(subprocess._optim_args_from_interpreter_flags())

`

412

411

`cmd.append(script_name)

`