Title: `ctypes.util.find_msvcrt()` returns "msvcrt.dll" on clang-cl Windows builds, breaking `ctypes.get_errno()` · Issue #154199 · python/cpython · GitHub
Open Graph Title: `ctypes.util.find_msvcrt()` returns "msvcrt.dll" on clang-cl Windows builds, breaking `ctypes.get_errno()` · Issue #154199 · python/cpython
X Title: `ctypes.util.find_msvcrt()` returns "msvcrt.dll" on clang-cl Windows builds, breaking `ctypes.get_errno()` · Issue #154199 · python/cpython
Description: Bug description: On a Windows CPython built with clang-cl (build.bat "/p:PlatformToolset=ClangCL", documented in PCbuild/readme.txt; gh-77532), ctypes.util.find_msvcrt() returns 'msvcrt.dll' instead of None. The cause is sys.version trun...
Open Graph Description: Bug description: On a Windows CPython built with clang-cl (build.bat "/p:PlatformToolset=ClangCL", documented in PCbuild/readme.txt; gh-77532), ctypes.util.find_msvcrt() returns 'msvcrt.dll' instea...
X Description: Bug description: On a Windows CPython built with clang-cl (build.bat "/p:PlatformToolset=ClangCL", documented in PCbuild/readme.txt; gh-77532), ctypes.util.find_msvcrt() returns 'msvc...
Opengraph URL: https://github.com/python/cpython/issues/154199
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`ctypes.util.find_msvcrt()` returns \"msvcrt.dll\" on clang-cl Windows builds, breaking `ctypes.get_errno()`","articleBody":"### Bug description:\n\nOn a Windows CPython built with clang-cl (`build.bat \"/p:PlatformToolset=ClangCL\"`, documented in PCbuild/readme.txt; gh-77532), `ctypes.util.find_msvcrt()` returns `'msvcrt.dll'` instead of `None`. The cause is `sys.version` truncation: `Python/getversion.c` caps the compiler segment at 80 chars (`%.80s`), which cuts off the `\"MSC v.\"` marker that `ctypes.util._get_build_version()` sniffs for, so it falls back to \"assume MSVC 6\". The practical impact: `CDLL(find_library(\"c\"), use_errno=True)` loads the OS-private legacy `msvcrt.dll`, whose `errno` is a different variable from the ucrt `errno` that `_ctypes` saves/restores around foreign calls — `ctypes.get_errno()` returns stale values (typically `0`) with no exception raised.\n\nObservable symptom: `test.test_ctypes.test_errno.Test.test_open` **fails** on clang-cl builds, while on MSVC builds `find_msvcrt()` returns `None` and the same test silently **skips** (`skipTest(\"Unable to find C library\")`), which is why this has gone unnoticed.\n\n```python\nimport sys, errno\nfrom ctypes import CDLL, c_char_p, c_int, get_errno\nfrom ctypes.util import find_library, find_msvcrt\n\nprint(sys.version)\nprint(find_msvcrt()) # 'msvcrt.dll' — expected None, as on MSVC builds\nlibc = CDLL(find_library(\"c\"), use_errno=True)\nlibc._open.argtypes = c_char_p, c_int\nprint(libc._open(b\"\", 0)) # -1 (call fails, as intended)\nprint(get_errno()) # 0 — expected errno.ENOENT == 2\n```\n\nObserved on a clang-cl build of main:\n\n```\n3.16.0a0 free-threading build (heads/ft-opt-dirty:2f032556f42, Jul 16 2026, 11:20:12) [Clang 22.1.8 (https://github.com/llvm/llvm-project ca7933e47d3a3451d81e72ac174d\nmsvcrt.dll\n-1\n0\n```\n\n```\n$ python -m unittest -v test.test_ctypes.test_errno\n...\nFAIL: test_open (test.test_ctypes.test_errno.Test.test_open)\n----------------------------------------------------------------------\nTraceback (most recent call last):\n File \"...\\Lib\\test\\test_ctypes\\test_errno.py\", line 25, in test_open\n self.assertEqual(get_errno(), errno.ENOENT)\n ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^\nAssertionError: 0 != 2\n```\n\nThe same build additionally shows both CRTs loaded after the call (`ctypes.util.dllist()` lists `ucrtbase.dll` *and* `msvcrt.dll`), which is the divergence in one line.\n\n**Root cause chain** (permalinks sha-pinned to main @ 16d0c89b875, 2026-07-18; `_get_build_version`/`find_msvcrt`/`find_library` are textually unchanged on today's main, merely shifted a few lines by later unrelated ctypes commits):\n\n1. [`PC/pyconfig.h#L161`](https://github.com/python/cpython/blob/16d0c89b875c520cf5af9da5ba4db133358f742f/PC/pyconfig.h#L161) builds the clang banner as `\"[Clang \" __clang_version__ \"] 64 bit (AMD64) with MSC v.\" _Py_STRINGIZE(_MSC_VER) \" CRT]\"` (added in GH-101352) — so the `MSC v.` marker *is intended* to be present.\n2. But [`Python/getversion.c#L18-L20`](https://github.com/python/cpython/blob/16d0c89b875c520cf5af9da5ba4db133358f742f/Python/getversion.c#L18-L20) composes `sys.version` with `\"%.80s (%.80s) %.80s\"` (free-threaded builds use the `\"%.80s free-threading build (%.80s) %.80s\"` variant — same `%.80s` cap), truncating the compiler segment at 80 chars. Official LLVM Windows binaries have a long `__clang_version__` (`22.1.8 (https://github.com/llvm/llvm-project \u003c40-hex-sha>)` — 86 chars by itself, 93 with the `[Clang ` prefix), so the tail carrying both markers is always truncated away. The full `Py_GetCompiler()` string on my build is 130 chars:\n ```\n [Clang 22.1.8 (https://github.com/llvm/llvm-project ca7933e47d3a3451d81e72ac174dcb5aa28b59d1)] 64 bit (AMD64) with MSC v.1951 CRT]\n ```\n while the `sys.version` compiler segment ends at `...ca7933e47d3a3451d81e72ac174d` — 28 characters into the commit hash, without `MSC v.`, `(AMD64)`, or the closing bracket. (An MSVC banner is ~27 chars and is never truncated, which is why this is clang-specific. The same truncation already broke `sysconfig.get_platform()` on clang-cl builds — gh-145410, fixed in March 2026 by moving the check into C: GH-146145.)\n3. [`Lib/ctypes/util.py#L10-L33`](https://github.com/python/cpython/blob/16d0c89b875c520cf5af9da5ba4db133358f742f/Lib/ctypes/util.py#L10-L33): `_get_build_version()` sniffs `sys.version` for `\"MSC v.\"` and, when absent, **returns 6** (\"assume the compiler is MSVC 6\", a distutils-era assumption).\n4. [`Lib/ctypes/util.py#L35-L54`](https://github.com/python/cpython/blob/16d0c89b875c520cf5af9da5ba4db133358f742f/Lib/ctypes/util.py#L35-L54): `find_msvcrt()` maps `version \u003c= 6` to `'msvcrt.dll'`, and [`find_library(\"c\")`](https://github.com/python/cpython/blob/16d0c89b875c520cf5af9da5ba4db133358f742f/Lib/ctypes/util.py#L56-L58) returns that.\n5. `msvcrt.dll` maintains its own CRT state; the interpreter and `_ctypes` use the ucrt. The `errno` set by `msvcrt.dll`'s `_open` is never seen by the `use_errno` machinery → `get_errno() == 0`.\n\nNote that the `'msvcrt.dll'` return is reachable *only* through this banner-sniff failure: when the marker survives (e.g. MSVC's `[MSC v.1943 64 bit (AMD64)]`), `_get_build_version()` yields 14.x and `find_msvcrt()` returns `None` — the code comment there cites issue23606, \"CRT is no longer directly loadable\" (gh-67794/bpo-23606). A clang-cl build links the same ucrt as the MSVC build, so `'msvcrt.dll'` is simply the wrong answer for it. The MSVC-6 fallback would still be meaningful for a toolchain that genuinely links `msvcrt.dll` (classic MinGW), which is worth weighing when choosing between the options below.\n\n**Suggested fix directions** (deferring to the maintainers):\n\n- Minimal: make `_get_build_version()` return `None` instead of `6` when `\"MSC v.\"` is absent, so `find_msvcrt()`/`find_library(\"c\")` behave exactly as on modern MSVC builds and `test_errno` skips consistently. This trades the VC6-era fallback away; if the MinGW case above is considered worth preserving, the next option handles both.\n- Alternative: make `find_msvcrt()` report the CRT the interpreter is actually linked against (`ucrtbase.dll` / the `api-ms-win-crt-*` stable ABI) independent of the compiler banner — correct for every toolchain, but it would reverse the bpo-23606 decision that modern MSVC returns `None`.\n- Longer-term: stop deriving build facts from `sys.version` string-sniffing altogether; the truncation shows the banner is not a reliable data channel (gh-145410 already moved `sysconfig` off it).\n\nI'm happy to submit a PR for whichever direction a maintainer prefers, with a regression test that exercises `_get_build_version()` against a truncated clang-style banner.\n\nSide observations I can file separately if useful: the `%.80s` cap leaves `sys.version` ending mid-commit-hash without its closing bracket on official-LLVM clang builds, and the clang `_Py_COMPILER` string literal has unbalanced brackets (one `[`, two `]`).\n\n**Environment / build:** CPython main @ f62050d6574 (2026-07-16), built per PCbuild with `build.bat -p x64 --disable-gil --pgo --tail-call-interp \"/p:PlatformToolset=ClangCL\"`, LLVM 22.1.8 official Windows release, Windows 11 x64. Also reproduced identically on an earlier (June 2026) clang-cl build, and I confirmed the contrasting skip behaviour on an MSVC build of the same source. My working branch carries only build-system tuning (PCbuild props/vcxproj plus a zlib-ng header) and build-regenerated files (hence the `-dirty` banner); the files involved here — `Lib/ctypes/util.py`, `Lib/test/test_ctypes/test_errno.py`, `Python/getversion.c`, `PC/pyconfig.h` — are unmodified from upstream. The failure is not related to free-threading — the mechanism only involves the compiler banner segment.\n\nI'm aware clang-cl is `PY_SUPPORT_TIER 0`; filing since PCbuild documents and actively maintains the toolset (gh-77532, and it is still being kept working — e.g. gh-146210 in 2026, gh-131278/gh-130090/gh-131035 in 2025), the defect is in a stdlib banner-sniff rather than the toolchain, and the same latent defect affects any build whose banner lacks the marker.\n\nRelated: gh-77532 / GH-101352 (clang banner introduction), gh-67794 (bpo-23606, `find_msvcrt` → `None` for modern MSVC), gh-70914 (user report that `find_msvcrt()` returns `None` on post-VC6 builds; closed as a duplicate of bpo-23606), gh-145916 (soft-deprecation of `ctypes.util.find_library` — its linked PR touches only the docs, so it removes neither this defect nor the test failure), gh-145410 / GH-146145 (same truncation, `sysconfig` side). The same truncated banner also breaks the vendored `get_platform()` in `Lib/test/test_importlib/test_windows.py`; filed separately as gh-154200.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nWindows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154553\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/SynaptSea","@type":"Person","name":"SynaptSea"},"datePublished":"2026-07-19T20:52:07Z","url":"https://github.com/python/cpython/issues/154199"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:389f397a-436a-5d96-b194-960e6afe4607 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D414:34CAB:35DE09B:44E6247:6B01F3E1 |
| html-safe-nonce | 247bf792c2152638eb6041fa555a0e15cdc3625e639a919cef674e5af6944cdc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENDE0OjM0Q0FCOjM1REUwOUI6NDRFNjI0Nzo2QjAxRjNFMSIsInZpc2l0b3JfaWQiOiI1MzIwNzY1NjYwMjk1NDU5ODA5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 78d5ab1c8fbf30adb9b215e6f3376beead66d1519b6335cd0cc1f8ec1f796094 |
| hovercard-subject-tag | issue:4924345791 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/154199/issue_layout |
| twitter:image | https://opengraph.githubassets.com/abac1bfded96b582e6038ca4f63d5bfd7da8316669d940c1bfbe766b1047bc50/python/cpython/issues/154199 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/abac1bfded96b582e6038ca4f63d5bfd7da8316669d940c1bfbe766b1047bc50/python/cpython/issues/154199 |
| og:image:alt | Bug description: On a Windows CPython built with clang-cl (build.bat "/p:PlatformToolset=ClangCL", documented in PCbuild/readme.txt; gh-77532), ctypes.util.find_msvcrt() returns 'msvcrt.dll' instea... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SynaptSea |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width