GeezerVison is showing github.com — gh-152433: Windows: fix ``ctypes`` error in UWP build by thexai · Pull Request #154700 · python/cpython
Conversation
thexai
commented
Jul 25, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fix ctypes error in UWP build:
In UWP is possible call GetLastError() Win32 API but not like import from ctypes with LibraryLoader:
GetLastError = windll.kernel32.GetLastError
The error produced is like this (in this case using Cryptodome):
EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <class 'AttributeError'>
Error Contents: kernel32
Traceback (most recent call last):
File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\site-packages\Cryptodome\Util\_raw_api.py", line 83, in <module>
raise ImportError("CFFI is not compatible with Python 3.12 on Windows")
ImportError: CFFI is not compatible with Python 3.12 on Windows
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 461, in __getattr__
dll = self._dlltype(name)
File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 390, in __init__
self._handle = _dlopen(self._name, mode)
~~~~~~~^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'kernel32' (or one of its dependencies). Try using the full path with constructor syntax.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Joel\AppData\Local\Packages\XBMCFoundation.Kodi_4n2hpmxwrvr6p\LocalState\addons\script.kodi.loguploader\default.py", line 1, in <module>
from Cryptodome.Cipher import AES
File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\site-packages\Cryptodome\Cipher\AES.py", line 26, in <module>
from Cryptodome.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
c_size_t, c_uint8_ptr)
File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\site-packages\Cryptodome\Util\_raw_api.py", line 170, in <module>
import ctypes
File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 492, in <module>
GetLastError = windll.kernel32.GetLastError
^^^^^^^^^^^^^^^
File "V:\kodi-build-UWP\Debug\AppX\system\python\Lib\ctypes\__init__.py", line 463, in __getattr__
raise AttributeError(name)
AttributeError: kernel32
-->End of Python script error report<--
Since GetLastError is available from _winapi, the workaround is import from _winapi and call directly.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
thexai
commented
Aug 4, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
I've finally resolved all the issues related to ctype. The first error was due to loading kernel32 with LoadPackagedLibrary. Being able to use LoadLibrary resolved it automatically. This has been changed in #152792.
The second error was a MemoryError exception. This is because VirtualAlloc doesn't have code execution permissions in UWP. Since don't think execution permission is needed, changed also for Windows Desktop.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. This is sitting for a while now. If nobody objects, I'm gonna merge end of next week at the latest.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
item = (ITEM*)VirtualAllocFromApp(NULL,
|
|
|
count * sizeof(ITEM),
|
|
|
MEM_COMMIT | MEM_RESERVE,
|
|
|
PAGE_READWRITE);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this works without EXECUTE, then do we need EXECUTE in the alternative VirtualAlloc call?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More work is needed for ctypes to have full functionality in UWP.
The EXECUTE flag is required, but in UWP, it is not allowed alongside READWRITE. This PR addresses some of the issues that recently arose after adding this code:
|
_ctypes_mod_exec(PyObject *mod) |
|
{ |
|
// See https://github.com/python/cpython/issues/128485 |
|
// This allocates some memory and then frees it to ensure that the |
|
// the dlmalloc allocator initializes itself to avoid data races |
|
// in free-threading. |
|
void *codeloc = NULL; |
|
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc); |
This causes errors immediately upon ctypes initialization (because reserves memory with execute permission).
But executing code in memory remains necessary for full ctypes functionality.
Let's say this PR only enables "limited" ctypes functionality on UWP but causes no harm on Windows Desktop.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
zooba reacted with thumbs up emoji
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we say that in a comment against this code, for next time one of us has to remember why we did it this way?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a code comment with more context and what remains to be implemented.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
UWP does not allow allocating memory with code execution permissions.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
zooba
zooba left review comments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|