bpo-34725: Adds _Py_SetProgramFullPath so embedders may override sys.… · python/cpython@177a41a (original) (raw)

`@@ -49,10 +49,9 @@ _PyPathConfig_Clear(_PyPathConfig *config)

`

49

49

``

50

50

`CLEAR(config->prefix);

`

51

51

`CLEAR(config->program_full_path);

`

``

52

`+

CLEAR(config->exec_prefix);

`

52

53

`#ifdef MS_WINDOWS

`

53

54

`CLEAR(config->dll_path);

`

54

``

`-

#else

`

55

``

`-

CLEAR(config->exec_prefix);

`

56

55

`#endif

`

57

56

`CLEAR(config->module_search_path);

`

58

57

`CLEAR(config->home);

`

`@@ -74,8 +73,8 @@ _PyPathConfig_Calculate(_PyPathConfig *path_config,

`

74

73

`PyMemAllocatorEx old_alloc;

`

75

74

`_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

76

75

``

77

``

`-

/* Calculate program_full_path, prefix, exec_prefix (Unix)

`

78

``

`-

or dll_path (Windows), and module_search_path */

`

``

76

`+

/* Calculate program_full_path, prefix, exec_prefix,

`

``

77

`+

dll_path (Windows), and module_search_path */

`

79

78

`err = _PyPathConfig_Calculate_impl(&new_config, core_config);

`

80

79

`if (_Py_INIT_FAILED(err)) {

`

81

80

` goto err;

`

`@@ -126,10 +125,9 @@ _PyPathConfig_SetGlobal(const _PyPathConfig *config)

`

126

125

``

127

126

`COPY_ATTR(program_full_path);

`

128

127

`COPY_ATTR(prefix);

`

``

128

`+

COPY_ATTR(exec_prefix);

`

129

129

`#ifdef MS_WINDOWS

`

130

130

`COPY_ATTR(dll_path);

`

131

``

`-

#else

`

132

``

`-

COPY_ATTR(exec_prefix);

`

133

131

`#endif

`

134

132

`COPY_ATTR(module_search_path);

`

135

133

`COPY_ATTR(program_name);

`

`@@ -208,12 +206,11 @@ _PyCoreConfig_SetPathConfig(const _PyCoreConfig *core_config)

`

208

206

`if (copy_wstr(&path_config.prefix, core_config->prefix) < 0) {

`

209

207

` goto no_memory;

`

210

208

` }

`

211

``

`-

#ifdef MS_WINDOWS

`

212

``

`-

if (copy_wstr(&path_config.dll_path, core_config->dll_path) < 0) {

`

``

209

`+

if (copy_wstr(&path_config.exec_prefix, core_config->exec_prefix) < 0) {

`

213

210

` goto no_memory;

`

214

211

` }

`

215

``

`-

#else

`

216

``

`-

if (copy_wstr(&path_config.exec_prefix, core_config->exec_prefix) < 0) {

`

``

212

`+

#ifdef MS_WINDOWS

`

``

213

`+

if (copy_wstr(&path_config.dll_path, core_config->dll_path) < 0) {

`

217

214

` goto no_memory;

`

218

215

` }

`

219

216

`#endif

`

`@@ -317,12 +314,8 @@ _PyCoreConfig_CalculatePathConfig(_PyCoreConfig *config)

`

317

314

` }

`

318

315

``

319

316

`if (config->exec_prefix == NULL) {

`

320

``

`-

#ifdef MS_WINDOWS

`

321

``

`-

wchar_t *exec_prefix = path_config.prefix;

`

322

``

`-

#else

`

323

``

`-

wchar_t *exec_prefix = path_config.exec_prefix;

`

324

``

`-

#endif

`

325

``

`-

if (copy_wstr(&config->exec_prefix, exec_prefix) < 0) {

`

``

317

`+

if (copy_wstr(&config->exec_prefix,

`

``

318

`+

path_config.exec_prefix) < 0) {

`

326

319

` goto no_memory;

`

327

320

` }

`

328

321

` }

`

`@@ -379,7 +372,8 @@ _PyCoreConfig_InitPathConfig(_PyCoreConfig *config)

`

379

372

` }

`

380

373

``

381

374

`if (config->base_exec_prefix == NULL) {

`

382

``

`-

if (copy_wstr(&config->base_exec_prefix, config->exec_prefix) < 0) {

`

``

375

`+

if (copy_wstr(&config->base_exec_prefix,

`

``

376

`+

config->exec_prefix) < 0) {

`

383

377

`return _Py_INIT_NO_MEMORY();

`

384

378

` }

`

385

379

` }

`

`@@ -435,12 +429,11 @@ Py_SetPath(const wchar_t *path)

`

435

429

`int alloc_error = (new_config.program_full_path == NULL);

`

436

430

`new_config.prefix = _PyMem_RawWcsdup(L"");

`

437

431

`alloc_error |= (new_config.prefix == NULL);

`

``

432

`+

new_config.exec_prefix = _PyMem_RawWcsdup(L"");

`

``

433

`+

alloc_error |= (new_config.exec_prefix == NULL);

`

438

434

`#ifdef MS_WINDOWS

`

439

435

`new_config.dll_path = _PyMem_RawWcsdup(L"");

`

440

436

`alloc_error |= (new_config.dll_path == NULL);

`

441

``

`-

#else

`

442

``

`-

new_config.exec_prefix = _PyMem_RawWcsdup(L"");

`

443

``

`-

alloc_error |= (new_config.exec_prefix == NULL);

`

444

437

`#endif

`

445

438

`new_config.module_search_path = _PyMem_RawWcsdup(path);

`

446

439

`alloc_error |= (new_config.module_search_path == NULL);

`

`@@ -503,6 +496,26 @@ Py_SetProgramName(const wchar_t *program_name)

`

503

496

` }

`

504

497

`}

`

505

498

``

``

499

`+

void

`

``

500

`+

_Py_SetProgramFullPath(const wchar_t *program_full_path)

`

``

501

`+

{

`

``

502

`+

if (program_full_path == NULL || program_full_path[0] == L'\0') {

`

``

503

`+

return;

`

``

504

`+

}

`

``

505

+

``

506

`+

PyMemAllocatorEx old_alloc;

`

``

507

`+

_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

508

+

``

509

`+

PyMem_RawFree(_Py_path_config.program_full_path);

`

``

510

`+

_Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);

`

``

511

+

``

512

`+

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

513

+

``

514

`+

if (_Py_path_config.program_full_path == NULL) {

`

``

515

`+

Py_FatalError("_Py_SetProgramFullPath() failed: out of memory");

`

``

516

`+

}

`

``

517

`+

}

`

``

518

+

506

519

``

507

520

`wchar_t *

`

508

521

`Py_GetPath(void)

`

`@@ -523,12 +536,8 @@ Py_GetPrefix(void)

`

523

536

`wchar_t *

`

524

537

`Py_GetExecPrefix(void)

`

525

538

`{

`

526

``

`-

#ifdef MS_WINDOWS

`

527

``

`-

return Py_GetPrefix();

`

528

``

`-

#else

`

529

539

`pathconfig_global_init();

`

530

540

`return _Py_path_config.exec_prefix;

`

531

``

`-

#endif

`

532

541

`}

`

533

542

``

534

543

``