bpo-36301: Add _Py_GetConfigsAsDict() function (GH-12540) · python/cpython@1075d16 (original) (raw)

`@@ -268,13 +268,19 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

`

268

268

` )

`

269

269

`# Mark config which should be get by get_default_config()

`

270

270

`GET_DEFAULT_CONFIG = object()

`

``

271

`+

DEFAULT_PRE_CONFIG = {

`

``

272

`+

'allocator': None,

`

``

273

`+

'coerce_c_locale': 0,

`

``

274

`+

'coerce_c_locale_warn': 0,

`

``

275

`+

'dev_mode': 0,

`

``

276

`+

'isolated': 0,

`

``

277

`+

'use_environment': 1,

`

``

278

`+

'utf8_mode': 0,

`

``

279

`+

}

`

271

280

`DEFAULT_CORE_CONFIG = {

`

272

281

`'install_signal_handlers': 1,

`

273

``

`-

'use_environment': 1,

`

274

282

`'use_hash_seed': 0,

`

275

283

`'hash_seed': 0,

`

276

``

`-

'allocator': None,

`

277

``

`-

'dev_mode': 0,

`

278

284

`'faulthandler': 0,

`

279

285

`'tracemalloc': 0,

`

280

286

`'import_time': 0,

`

`@@ -286,10 +292,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

`

286

292

`'filesystem_encoding': GET_DEFAULT_CONFIG,

`

287

293

`'filesystem_errors': GET_DEFAULT_CONFIG,

`

288

294

``

289

``

`-

'utf8_mode': 0,

`

290

``

`-

'coerce_c_locale': 0,

`

291

``

`-

'coerce_c_locale_warn': 0,

`

292

``

-

293

295

`'pycache_prefix': None,

`

294

296

`'program_name': './_testembed',

`

295

297

`'argv': [""],

`

`@@ -306,7 +308,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

`

306

308

`'exec_prefix': GET_DEFAULT_CONFIG,

`

307

309

`'base_exec_prefix': GET_DEFAULT_CONFIG,

`

308

310

``

309

``

`-

'isolated': 0,

`

310

311

`'site_import': 1,

`

311

312

`'bytes_warning': 0,

`

312

313

`'inspect': 0,

`

`@@ -332,8 +333,10 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

`

332

333

`'_frozen': 0,

`

333

334

` }

`

334

335

`if MS_WINDOWS:

`

335

``

`-

DEFAULT_CORE_CONFIG.update({

`

``

336

`+

DEFAULT_PRE_CONFIG.update({

`

336

337

`'legacy_windows_fs_encoding': 0,

`

``

338

`+

})

`

``

339

`+

DEFAULT_CORE_CONFIG.update({

`

337

340

`'legacy_windows_stdio': 0,

`

338

341

` })

`

339

342

``

`@@ -359,6 +362,11 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

`

359

362

`'Py_HashRandomizationFlag': 1,

`

360

363

`'_Py_HasFileSystemDefaultEncodeErrors': 0,

`

361

364

` }

`

``

365

`+

COPY_GLOBAL_PRE_CONFIG = [

`

``

366

`+

('Py_IgnoreEnvironmentFlag', 'use_environment', True),

`

``

367

`+

('Py_IsolatedFlag', 'isolated'),

`

``

368

`+

('Py_UTF8Mode', 'utf8_mode'),

`

``

369

`+

]

`

362

370

`COPY_GLOBAL_CONFIG = [

`

363

371

`# Copy core config to global config for expected values

`

364

372

`# True means that the core config value is inverted (0 => 1 and 1 => 0)

`

`@@ -368,21 +376,20 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

`

368

376

` ('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'),

`

369

377

` ('Py_FileSystemDefaultEncoding', 'filesystem_encoding'),

`

370

378

` ('Py_FrozenFlag', '_frozen'),

`

371

``

`-

('Py_IgnoreEnvironmentFlag', 'use_environment', True),

`

372

379

` ('Py_InspectFlag', 'inspect'),

`

373

380

` ('Py_InteractiveFlag', 'interactive'),

`

374

``

`-

('Py_IsolatedFlag', 'isolated'),

`

375

381

` ('Py_NoSiteFlag', 'site_import', True),

`

376

382

` ('Py_NoUserSiteDirectory', 'user_site_directory', True),

`

377

383

` ('Py_OptimizeFlag', 'optimization_level'),

`

378

384

` ('Py_QuietFlag', 'quiet'),

`

379

``

`-

('Py_UTF8Mode', 'utf8_mode'),

`

380

385

` ('Py_UnbufferedStdioFlag', 'buffered_stdio', True),

`

381

386

` ('Py_VerboseFlag', 'verbose'),

`

382

387

` ]

`

383

388

`if MS_WINDOWS:

`

384

``

`-

COPY_GLOBAL_CONFIG.extend((

`

``

389

`+

COPY_GLOBAL_PRE_CONFIG.extend((

`

385

390

` ('Py_LegacyWindowsFSEncodingFlag', 'legacy_windows_fs_encoding'),

`

``

391

`+

))

`

``

392

`+

COPY_GLOBAL_CONFIG.extend((

`

386

393

` ('Py_LegacyWindowsStdioFlag', 'legacy_windows_stdio'),

`

387

394

` ))

`

388

395

``

`@@ -408,7 +415,7 @@ def check_main_config(self, config):

`

408

415

`expected['xoptions'] = self.main_xoptions(core_config['xoptions'])

`

409

416

`self.assertEqual(main_config, expected)

`

410

417

``

411

``

`-

def get_expected_config(self, expected, env):

`

``

418

`+

def get_expected_config(self, expected, expected_preconfig, env):

`

412

419

`expected = dict(self.DEFAULT_CORE_CONFIG, **expected)

`

413

420

``

414

421

`code = textwrap.dedent('''

`

`@@ -436,7 +443,7 @@ def get_expected_config(self, expected, env):

`

436

443

`# when test_embed is run from a venv (bpo-35313)

`

437

444

`args = (sys.executable, '-S', '-c', code)

`

438

445

`env = dict(env)

`

439

``

`-

if not expected['isolated']:

`

``

446

`+

if not expected_preconfig['isolated']:

`

440

447

`env['PYTHONCOERCECLOCALE'] = '0'

`

441

448

`env['PYTHONUTF8'] = '0'

`

442

449

`proc = subprocess.run(args, env=env,

`

`@@ -453,13 +460,19 @@ def get_expected_config(self, expected, env):

`

453

460

`expected[key] = config[key]

`

454

461

`return expected

`

455

462

``

``

463

`+

def check_pre_config(self, config, expected):

`

``

464

`+

pre_config = dict(config['pre_config'])

`

``

465

`+

core_config = dict(config['core_config'])

`

``

466

`+

self.assertEqual(pre_config, expected)

`

``

467

+

456

468

`def check_core_config(self, config, expected):

`

457

469

`core_config = dict(config['core_config'])

`

458

470

`for key in self.UNTESTED_CORE_CONFIG:

`

459

471

`core_config.pop(key, None)

`

460

472

`self.assertEqual(core_config, expected)

`

461

473

``

462

474

`def check_global_config(self, config):

`

``

475

`+

pre_config = config['pre_config']

`

463

476

`core_config = config['core_config']

`

464

477

``

465

478

`expected = dict(self.DEFAULT_GLOBAL_CONFIG)

`

`@@ -470,10 +483,17 @@ def check_global_config(self, config):

`

470

483

`else:

`

471

484

`global_key, core_key = item

`

472

485

`expected[global_key] = core_config[core_key]

`

``

486

`+

for item in self.COPY_GLOBAL_PRE_CONFIG:

`

``

487

`+

if len(item) == 3:

`

``

488

`+

global_key, core_key, opposite = item

`

``

489

`+

expected[global_key] = 0 if pre_config[core_key] else 1

`

``

490

`+

else:

`

``

491

`+

global_key, core_key = item

`

``

492

`+

expected[global_key] = pre_config[core_key]

`

473

493

``

474

494

`self.assertEqual(config['global_config'], expected)

`

475

495

``

476

``

`-

def check_config(self, testname, expected):

`

``

496

`+

def check_config(self, testname, expected_config, expected_preconfig):

`

477

497

`env = dict(os.environ)

`

478

498

`# Remove PYTHON* environment variables to get deterministic environment

`

479

499

`for key in list(env):

`

`@@ -488,15 +508,21 @@ def check_config(self, testname, expected):

`

488

508

`# Ignore err

`

489

509

`config = json.loads(out)

`

490

510

``

491

``

`-

expected = self.get_expected_config(expected, env)

`

492

``

`-

self.check_core_config(config, expected)

`

``

511

`+

expected_preconfig = dict(self.DEFAULT_PRE_CONFIG, **expected_preconfig)

`

``

512

`+

expected_config = self.get_expected_config(expected_config, expected_preconfig, env)

`

``

513

+

``

514

`+

self.check_core_config(config, expected_config)

`

``

515

`+

self.check_pre_config(config, expected_preconfig)

`

493

516

`self.check_main_config(config)

`

494

517

`self.check_global_config(config)

`

495

518

``

496

519

`def test_init_default_config(self):

`

497

``

`-

self.check_config("init_default_config", {})

`

``

520

`+

self.check_config("init_default_config", {}, {})

`

498

521

``

499

522

`def test_init_global_config(self):

`

``

523

`+

preconfig = {

`

``

524

`+

'utf8_mode': 1,

`

``

525

`+

}

`

500

526

`config = {

`

501

527

`'program_name': './globalvar',

`

502

528

`'site_import': 0,

`

`@@ -509,29 +535,30 @@ def test_init_global_config(self):

`

509

535

`'quiet': 1,

`

510

536

`'buffered_stdio': 0,

`

511

537

``

512

``

`-

'utf8_mode': 1,

`

513

538

`'stdio_encoding': 'utf-8',

`

514

539

`'stdio_errors': 'surrogateescape',

`

515

540

`'filesystem_encoding': 'utf-8',

`

516

541

`'filesystem_errors': self.UTF8_MODE_ERRORS,

`

517

542

`'user_site_directory': 0,

`

518

543

`'_frozen': 1,

`

519

544

` }

`

520

``

`-

self.check_config("init_global_config", config)

`

``

545

`+

self.check_config("init_global_config", config, preconfig)

`

521

546

``

522

547

`def test_init_from_config(self):

`

``

548

`+

preconfig = {

`

``

549

`+

'allocator': 'malloc',

`

``

550

`+

'utf8_mode': 1,

`

``

551

`+

}

`

523

552

`config = {

`

524

553

`'install_signal_handlers': 0,

`

525

554

`'use_hash_seed': 1,

`

526

555

`'hash_seed': 123,

`

527

``

`-

'allocator': 'malloc',

`

528

556

`'tracemalloc': 2,

`

529

557

`'import_time': 1,

`

530

558

`'show_ref_count': 1,

`

531

559

`'show_alloc_count': 1,

`

532

560

`'malloc_stats': 1,

`

533

561

``

534

``

`-

'utf8_mode': 1,

`

535

562

`'stdio_encoding': 'iso8859-1',

`

536

563

`'stdio_errors': 'replace',

`

537

564

`'filesystem_encoding': 'utf-8',

`

`@@ -559,16 +586,18 @@ def test_init_from_config(self):

`

559

586

`'_check_hash_pycs_mode': 'always',

`

560

587

`'_frozen': 1,

`

561

588

` }

`

562

``

`-

self.check_config("init_from_config", config)

`

``

589

`+

self.check_config("init_from_config", config, preconfig)

`

563

590

``

``

591

`+

INIT_ENV_PRECONFIG = {

`

``

592

`+

'allocator': 'malloc',

`

``

593

`+

'utf8_mode': 1,

`

``

594

`+

}

`

564

595

`INIT_ENV_CONFIG = {

`

565

596

`'use_hash_seed': 1,

`

566

597

`'hash_seed': 42,

`

567

``

`-

'allocator': 'malloc',

`

568

598

`'tracemalloc': 2,

`

569

599

`'import_time': 1,

`

570

600

`'malloc_stats': 1,

`

571

``

`-

'utf8_mode': 1,

`

572

601

`'filesystem_encoding': 'utf-8',

`

573

602

`'filesystem_errors': UTF8_MODE_ERRORS,

`

574

603

`'inspect': 1,

`

`@@ -584,35 +613,42 @@ def test_init_from_config(self):

`

584

613

` }

`

585

614

``

586

615

`def test_init_env(self):

`

587

``

`-

self.check_config("init_env", self.INIT_ENV_CONFIG)

`

``

616

`+

self.check_config("init_env", self.INIT_ENV_CONFIG, self.INIT_ENV_PRECONFIG)

`

588

617

``

589

618

`def test_init_env_dev_mode(self):

`

590

``

`-

config = dict(self.INIT_ENV_CONFIG,

`

``

619

`+

preconfig = dict(self.INIT_ENV_PRECONFIG,

`

591

620

`allocator='debug',

`

592

621

`dev_mode=1)

`

593

``

`-

self.check_config("init_env_dev_mode", config)

`

594

``

-

595

``

`-

def test_init_env_dev_mode(self):

`

596

622

`config = dict(self.INIT_ENV_CONFIG,

`

597

``

`-

allocator='malloc',

`

598

623

`dev_mode=1)

`

599

``

`-

self.check_config("init_env_dev_mode_alloc", config)

`

``

624

`+

self.check_config("init_env_dev_mode", config, preconfig)

`

``

625

+

``

626

`+

def test_init_env_dev_mode(self):

`

``

627

`+

preconfig = dict(self.INIT_ENV_PRECONFIG,

`

``

628

`+

allocator='malloc',

`

``

629

`+

dev_mode=1)

`

``

630

`+

config = dict(self.INIT_ENV_CONFIG)

`

``

631

`+

self.check_config("init_env_dev_mode_alloc", config, preconfig)

`

600

632

``

601

633

`def test_init_dev_mode(self):

`

602

``

`-

config = {

`

``

634

`+

preconfig = {

`

``

635

`+

'allocator': 'debug',

`

603

636

`'dev_mode': 1,

`

``

637

`+

}

`

``

638

`+

config = {

`

604

639

`'faulthandler': 1,

`

605

``

`-

'allocator': 'debug',

`

606

640

` }

`

607

``

`-

self.check_config("init_dev_mode", config)

`

``

641

`+

self.check_config("init_dev_mode", config, preconfig)

`

608

642

``

609

643

`def test_init_isolated(self):

`

610

``

`-

config = {

`

``

644

`+

preconfig = {

`

611

645

`'isolated': 1,

`

612

646

`'use_environment': 0,

`

``

647

`+

}

`

``

648

`+

config = {

`

613

649

`'user_site_directory': 0,

`

614

650

` }

`

615

``

`-

self.check_config("init_isolated", config)

`

``

651

`+

self.check_config("init_isolated", config, preconfig)

`

616

652

``

617

653

``

618

654

`if name == "main":

`