Skip to content

Unexpected different behaviors between Py_InitializeFromConfig and Py_InitializeFromInitConfig #146377

@matlo607

Description

@matlo607

Bug report

Bug description:

I am observing different initialization behaviors when I ported my code to the new API introduced in PEP 741 “Python Configuration C API”.
In the diff below, you can see that the configuration dump has a lot of differences, and the setting of use_environment failed (0 instead of 1).

Environment

Distributor ID: Ubuntu
Description:    Ubuntu 24.04.4 LTS
Release:        24.04
Codename:       noble
brew info python@3.14
==> python@3.14 ✔: stable 3.14.3 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
Installed
/home/linuxbrew/.linuxbrew/Cellar/python@3.14/3.14.3_1 (9,237 files, 323.3MB) *
  Built from source on 2026-02-23 at 14:37:03

Reproducer

#include <Python.h>
#include <stdio.h>

static void
dump_config (void)
{
  (void) PyRun_SimpleStringFlags(
    "import _testinternalcapi, json; "
    "print(json.dumps(_testinternalcapi.get_configs()))",
    0);
}

int init_python(void)
{
#if defined (WITH_PY314_CONFIG_API) && WITH_PY314_CONFIG_API == 1
  PyInitConfig *config = PyInitConfig_Create ();
  if (config == NULL) {
    printf ("PYTHON INIT ERROR: memory allocation failed\n");
    return -1;
  }

  if (PyInitConfig_SetInt (config, "write_bytecode", 1) < 0) {
    goto error;
  }

  if (PyInitConfig_SetInt (config, "use_environment", 1) < 0) {
    goto error;
  }

  // Initialize Python with the configuration
  if (Py_InitializeFromInitConfig (config) < 0) {
    goto error;
  }
  PyInitConfig_Free (config);
  return 0;

error:
  {
    // Display the error message.
    //
    // This uncommon braces style is used, because you cannot make
    // goto targets point to variable declarations.
    const char *err_msg;
    (void) PyInitConfig_GetError (config, &err_msg);
    printf ("PYTHON INIT ERROR: %s\n", err_msg);
    PyInitConfig_Free (config);
    return -1;
  }

#else

  PyStatus status;

  PyConfig config;
  PyConfig_InitPythonConfig(&config);

  config.write_bytecode = 1;
  config.use_environment = 1;

  status = PyConfig_Read (&config);
  if (PyStatus_Exception (status))
    goto exception;

  status = Py_InitializeFromConfig (&config);
  if (PyStatus_Exception (status)) {
    goto exception;
  }

  PyConfig_Clear (&config);
  return 0;

exception:
  PyConfig_Clear (&config);

  if (PyStatus_IsError (status)) {
    printf ("Python initialization failed: %s\n", status.err_msg);
  } else if (PyStatus_IsExit (status)) {
    printf ("Python initialization failed with exit status: %d\n", status.exitcode);
  }
  return -1;
#endif
}

int main ()
{
  if (init_python () != 0)
    return -1;

  dump_config();

  Py_Finalize();
  return 0;
}

Old API:

gcc -Wall -Werror -DWITH_PY314_CONFIG_API=0 -I/home/linuxbrew/.linuxbrew/Cellar/python@3.14/3.14.3_1/include/python3.14 -o test-pyinit test.c -L/home/linuxbrew/.linuxbrew/Cellar/python@
3.14/3.14.3_1/lib -lpython3.14

LD_LIBRARY_PATH=/home/linuxbrew/.linuxbrew/Cellar/python@3.14/3.14.3_1/lib ./test-pyinit | jq . > /tmp/test-pyinit-oldapi.out

New API:

gcc -Wall -Werror -DWITH_PY314_CONFIG_API=1 -I/home/linuxbrew/.linuxbrew/Cellar/python@3.14/3.14.3_1/include/python3.14 -o test-pyinit test.c -L/home/linuxbrew/.linuxbrew/Cellar/python@
3.14/3.14.3_1/lib -lpython3.14

LD_LIBRARY_PATH=/home/linuxbrew/.linuxbrew/Cellar/python@3.14/3.14.3_1/lib ./test-pyinit | jq . > /tmp/test-pyinit-314api.out

Diff

diff -u /tmp/test-pyinit-oldapi.out /tmp/test-pyinit-314api.out

--- /tmp/test-pyinit-oldapi.out 2026-03-24 12:53:23.926875801 +0000
+++ /tmp/test-pyinit-314api.out 2026-03-24 12:54:06.982977631 +0000
@@ -1,6 +1,6 @@
 {
   "global_config": {
-    "Py_FileSystemDefaultEncoding": "utf-8",
+    "Py_FileSystemDefaultEncoding": "ascii",
     "Py_HasFileSystemDefaultEncoding": 0,
     "Py_FileSystemDefaultEncodeErrors": "surrogateescape",
     "_Py_HasFileSystemDefaultEncodeErrors": 0,
@@ -13,20 +13,20 @@
     "Py_OptimizeFlag": 0,
     "Py_NoSiteFlag": 0,
     "Py_BytesWarningFlag": 0,
-    "Py_FrozenFlag": 0,
-    "Py_IgnoreEnvironmentFlag": 0,
+    "Py_FrozenFlag": 1,
+    "Py_IgnoreEnvironmentFlag": 1,
     "Py_DontWriteBytecodeFlag": 0,
-    "Py_NoUserSiteDirectory": 0,
+    "Py_NoUserSiteDirectory": 1,
     "Py_UnbufferedStdioFlag": 0,
     "Py_HashRandomizationFlag": 1,
-    "Py_IsolatedFlag": 0
+    "Py_IsolatedFlag": 1
   },
   "pre_config": {
-    "_config_init": 2,
-    "parse_argv": 1,
-    "isolated": 0,
-    "use_environment": 1,
-    "configure_locale": 1,
+    "_config_init": 3,
+    "parse_argv": 0,
+    "isolated": 1,
+    "use_environment": 0,
+    "configure_locale": 0,
     "coerce_c_locale": 0,
     "coerce_c_locale_warn": 0,
     "utf8_mode": 0,
@@ -59,7 +59,7 @@
     "pycache_prefix": null,
     "quiet": false,
     "stdlib_dir": "/home/linuxbrew/.linuxbrew/Cellar/python@3.14/3.14.3_1/lib/python3.14",
-    "use_environment": true,
+    "use_environment": false,
     "verbose": 0,
     "warnoptions": [],
     "write_bytecode": true,
@@ -67,42 +67,42 @@
     "buffered_stdio": true,
     "check_hash_pycs_mode": "default",
     "code_debug_ranges": true,
-    "configure_c_stdio": true,
+    "configure_c_stdio": false,
     "dev_mode": false,
     "dump_refs": false,
     "dump_refs_file": null,
     "faulthandler": false,
-    "filesystem_encoding": "utf-8",
+    "filesystem_encoding": "ascii",
     "filesystem_errors": "surrogateescape",
     "hash_seed": 0,
     "home": null,
     "thread_inherit_context": 0,
     "context_aware_warnings": 0,
     "import_time": 0,
-    "install_signal_handlers": true,
-    "isolated": false,
+    "install_signal_handlers": false,
+    "isolated": true,
     "malloc_stats": false,
     "orig_argv": [],
-    "parse_argv": true,
-    "pathconfig_warnings": true,
+    "parse_argv": false,
+    "pathconfig_warnings": false,
     "perf_profiling": 0,
     "remote_debug": true,
     "program_name": "python3",
     "run_command": null,
     "run_filename": null,
     "run_module": null,
-    "safe_path": false,
+    "safe_path": true,
     "show_ref_count": false,
     "site_import": true,
     "skip_source_first_line": false,
-    "stdio_encoding": "utf-8",
-    "stdio_errors": "strict",
+    "stdio_encoding": "ascii",
+    "stdio_errors": "surrogateescape",
     "tracemalloc": 0,
     "use_frozen_modules": true,
     "use_hash_seed": false,
-    "user_site_directory": true,
+    "user_site_directory": false,
     "warn_default_encoding": false,
-    "_config_init": 2,
+    "_config_init": 3,
     "_init_main": true,
     "_install_importlib": true,
     "_is_python_build": false,

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions