GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-152433: Windows: fix ``ctypes`` error in UWP build by thexai · Pull Request #154700 · python/cpython

Conversation

@thexai

@thexai thexai commented Jul 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

  • Issue: Windows: improve compatibility to build for UWP / Xbox #152433

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Jul 25, 2026
Windows: improve compatibility to build for UWP / Xbox #152433
Open
@bedevere-app bedevere-app Bot added the awaiting review label Jul 25, 2026
@thexai

thexai commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Ping @zooba for review

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thexai
thexai marked this pull request as draft August 4, 2026 11:15
@bedevere-app bedevere-app Bot removed the awaiting review label Aug 4, 2026
@thexai
thexai force-pushed the uwp-fix-ctypes branch from 277711d to 74cf359 Compare August 4, 2026 16:27
@thexai

thexai commented Aug 4, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thexai
thexai marked this pull request as ready for review August 4, 2026 16:38
@bedevere-app bedevere-app Bot added the awaiting review label Aug 4, 2026
@thexai
thexai force-pushed the uwp-fix-ctypes branch from 74cf359 to c0c7adf Compare August 4, 2026 18:07
chris-eibl
chris-eibl approved these changes Sep 5, 2026
View reviewed changes

@chris-eibl chris-eibl left a comment

Copy link
Copy Markdown
Member

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.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Sep 5, 2026
zooba
zooba reviewed Sep 7, 2026
View reviewed changes
Comment thread Modules/_ctypes/malloc_closure.c
item = (ITEM*)VirtualAllocFromApp(NULL,
count * sizeof(ITEM),
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);

@zooba zooba Sep 7, 2026

Copy link
Copy Markdown
Member

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.

All reactions

@thexai thexai Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

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:

cpython/Modules/_ctypes/_ctypes.c

Lines 6390 to 6397 in e5d4fa2

_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
All reactions
  • 👍 1 reaction

@zooba zooba Sep 14, 2026

Copy link
Copy Markdown
Member

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.

All reactions

@thexai thexai Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

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.

All reactions
@thexai
pythongh-152433: Windows: fix ctypes MemoryError in UWP build
3c4f753
UWP does not allow allocating memory with code execution permissions.
@thexai
thexai force-pushed the uwp-fix-ctypes branch from c0c7adf to 3c4f753 Compare September 17, 2026 13:56
zooba
zooba reviewed Sep 17, 2026
View reviewed changes
Comment thread Modules/_ctypes/malloc_closure.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@zooba
Apply suggestion from @zooba
7bd108c
@zooba zooba added the skip news label Sep 17, 2026
@zooba
zooba enabled auto-merge (squash) September 17, 2026 16:27
zooba
zooba reviewed Sep 17, 2026
View reviewed changes
Comment thread Modules/_ctypes/malloc_closure.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@zooba
Apply suggestion from @zooba
b8becdf
@zooba
zooba merged commit 0f00aef into python:main Sep 17, 2026
51 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Sep 17, 2026
@thexai
thexai deleted the uwp-fix-ctypes branch September 19, 2026 16:18
@thexai thexai mentioned this pull request Nov 7, 2026
gh-152433: Make ctypes callbacks work on Windows UWP builds #161539
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zooba zooba zooba left review comments
@chris-eibl chris-eibl chris-eibl approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

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.

3 participants

@thexai @zooba @chris-eibl
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.