Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add feature ctypes to force enable _ctypes module
  • Loading branch information
EtherealPsyche authored and EtherealPsyche committed Mar 12, 2026
commit 2b06f507fa8825581e40e1b0e6fa93b9739da904
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license.workspace = true
[features]
default = ["threading", "stdlib", "stdio", "importlib", "ssl-rustls", "host_env"]
host_env = ["rustpython-vm/host_env", "rustpython-stdlib?/host_env"]
ctypes = ["rustpython-vm/ctypes"]
importlib = ["rustpython-vm/importlib"]
encodings = ["rustpython-vm/encodings"]
stdio = ["rustpython-vm/stdio"]
Expand Down
1 change: 1 addition & 0 deletions crates/stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license.workspace = true

[features]
default = ["compiler", "host_env"]
ctypes = ["rustpython-vm/ctypes"]
host_env = ["rustpython-vm/host_env"]
compiler = ["rustpython-vm/compiler"]
threading = ["rustpython-common/threading", "rustpython-vm/threading"]
Expand Down
54 changes: 29 additions & 25 deletions crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license.workspace = true
[features]
default = ["compiler", "wasmbind", "gc", "host_env", "stdio"]
host_env = []
ctypes = ["dep:libffi", "dep:libloading"]
stdio = []
importlib = []
encodings = ["importlib"]
Expand Down Expand Up @@ -83,6 +84,9 @@ optional = { workspace = true }
result-like = "0.5.0"
timsort = "0.1.2"

libffi = { workspace = true, features = ["system"], optional = true }
libloading = { version = "0.9", optional = true }

## unicode stuff
# TODO: use unic for this; needed for title case:
# https://github.com/RustPython/RustPython/pull/832#discussion_r275428939
Expand Down Expand Up @@ -117,31 +121,31 @@ junction = { workspace = true }
[target.'cfg(windows)'.dependencies.windows-sys]
workspace = true
features = [
"Win32_Foundation",
"Win32_Globalization",
"Win32_Media_Audio",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_Security_Authorization",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_Diagnostics_Debug",
"Win32_System_Environment",
"Win32_System_IO",
"Win32_System_Ioctl",
"Win32_System_Kernel",
"Win32_System_LibraryLoader",
"Win32_System_Memory",
"Win32_System_Performance",
"Win32_System_Pipes",
"Win32_System_Registry",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_Time",
"Win32_System_WindowsProgramming",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
"Win32_Foundation",
"Win32_Globalization",
"Win32_Media_Audio",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_Security_Authorization",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_Diagnostics_Debug",
"Win32_System_Environment",
"Win32_System_IO",
"Win32_System_Ioctl",
"Win32_System_Kernel",
"Win32_System_LibraryLoader",
"Win32_System_Memory",
"Win32_System_Performance",
"Win32_System_Pipes",
"Win32_System_Registry",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_Time",
"Win32_System_WindowsProgramming",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
]

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
Expand Down
22 changes: 14 additions & 8 deletions crates/vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ pub mod posix;
#[path = "posix_compat.rs"]
pub mod posix;

#[cfg(all(
feature = "host_env",
any(target_os = "linux", target_os = "macos", target_os = "windows"),
not(any(target_env = "musl", target_env = "sgx"))
#[cfg(any(
feature = "ctypes",
all(
feature = "host_env",
any(target_os = "linux", target_os = "macos", target_os = "windows"),
not(any(target_env = "musl", target_env = "sgx"))
)
))]
mod _ctypes;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if _ctypes module or its submodules have any platform guards that would protect against musl/sgx
echo "=== Checking _ctypes module for platform guards ==="
rg -n "cfg.*musl|cfg.*sgx" crates/vm/src/stdlib/_ctypes/

echo ""
echo "=== Checking top-level imports in _ctypes files ==="
rg -n "^use (libffi|libloading)" crates/vm/src/stdlib/_ctypes/

Repository: RustPython/RustPython

Length of output: 454


🏁 Script executed:

#!/bin/bash
# Read the mod.rs file to check the cfg conditions
echo "=== Reading crates/vm/src/stdlib/mod.rs around lines 41-49 ==="
sed -n '35,55p' crates/vm/src/stdlib/mod.rs

echo ""
echo "=== Checking for second cfg block around lines 91-98 ==="
sed -n '85,105p' crates/vm/src/stdlib/mod.rs

echo ""
echo "=== Full view of cfg conditions for _ctypes ==="
rg -B2 -A8 'mod _ctypes' crates/vm/src/stdlib/mod.rs

Repository: RustPython/RustPython

Length of output: 1668


🏁 Script executed:

#!/bin/bash
# Check if _ctypes.rs has any top-level cfg guards
echo "=== Checking _ctypes.rs for top-level cfg ==="
head -20 crates/vm/src/stdlib/_ctypes.rs

echo ""
echo "=== Check if libffi and libloading are available on musl/sgx ==="
# Check if there are any feature flags or conditional compilation for these deps
rg -n "libffi|libloading" Cargo.toml | head -20

Repository: RustPython/RustPython

Length of output: 609


🏁 Script executed:

#!/bin/bash
# Check for documentation about ctypes feature
echo "=== Searching for ctypes in README or docs ==="
find . -name "README*" -o -name "Cargo.toml" -o -name "FEATURES*" | head -10 | xargs grep -l "ctypes" 2>/dev/null || echo "No direct ctypes documentation found"

echo ""
echo "=== Check Cargo.toml for ctypes feature definition ==="
rg -A5 '\[features\]' Cargo.toml | grep -A100 "ctypes"

Repository: RustPython/RustPython

Length of output: 294


🏁 Script executed:

#!/bin/bash
# Check if there are any comments in mod.rs explaining this design
echo "=== Searching for comments around the ctypes cfg condition ==="
sed -n '35,55p' crates/vm/src/stdlib/mod.rs | cat -n

echo ""
echo "=== Check if libloading/libffi have musl support ==="
rg -B3 -A3 "libffi|libloading" Cargo.toml

Repository: RustPython/RustPython

Length of output: 1108


🏁 Script executed:

#!/bin/bash
# Check if there are any issues or PRs mentioning musl/sgx and ctypes
echo "=== Searching for any musl-related comments in stdlib ==="
rg -n "musl" crates/vm/src/stdlib/ | head -20

echo ""
echo "=== Check if libffi/libloading have documented musl issues ==="
# Check the Cargo.lock or dependency versions for any indicators
rg -B5 -A5 "libffi.*5\|libloading" Cargo.lock 2>/dev/null | head -30 || echo "Cargo.lock check skipped"

Repository: RustPython/RustPython

Length of output: 1938


🏁 Script executed:

#!/bin/bash
# Check if there's any issue or discussion about ctypes and musl in git history or issues
echo "=== Check if there's context in _ctypes.rs about musl/sgx ==="
grep -n "musl\|sgx\|platform" crates/vm/src/stdlib/_ctypes.rs || echo "No platform mentions in _ctypes.rs"

echo ""
echo "=== Verify the exact behavior: what happens when ctypes feature is enabled on musl ==="
# Check if there's a CI configuration that tests this
find . -name "*.yml" -o -name "*.yaml" | xargs grep -l "ctypes\|musl" 2>/dev/null | head -5

Repository: RustPython/RustPython

Length of output: 396


🏁 Script executed:

#!/bin/bash
# Check CI configuration for ctypes testing
echo "=== Checking CI for ctypes feature tests ==="
grep -n "ctypes" .github/workflows/ci.yaml

echo ""
echo "=== Checking if musl builds are tested ==="
grep -n "musl" .github/workflows/ci.yaml | head -20

echo ""
echo "=== Check if ctypes and musl are tested together ==="
grep -B5 -A5 "ctypes" .github/workflows/ci.yaml | grep -A10 -B10 "musl"

Repository: RustPython/RustPython

Length of output: 282


🏁 Script executed:

#!/bin/bash
# Check if there's any documentation or indication of libffi/libloading support on musl
echo "=== Check Cargo.toml dependencies for any musl-related features ==="
grep -B2 -A2 "libffi\|libloading" Cargo.toml

echo ""
echo "=== Look for any conditional dependencies based on target_env ==="
sed -n '/^\[target\./,/^\[/p' Cargo.toml | grep -A5 "musl"

echo ""
echo "=== Check if the ctypes feature is explicitly documented anywhere ==="
find . -name "*.md" -type f | xargs grep -i "ctypes" 2>/dev/null | head -10

Repository: RustPython/RustPython

Length of output: 716


Document the platform constraints for the ctypes feature.

The feature = "ctypes" flag bypasses platform checks and allows enabling ctypes on musl and SGX targets. Since _ctypes and its submodules unconditionally depend on libffi and libloading (which are not available on these platforms), users who enable --features ctypes on musl or SGX will encounter compile-time errors. This appears intentional—to allow users to opt-in despite platform restrictions—but should be documented in Cargo.toml or the project README so users understand the platform requirements before attempting to enable this feature.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/stdlib/mod.rs` around lines 41 - 49, Document that enabling the
"ctypes" feature bypasses the platform guards around the mod _ctypes and
therefore may try to build dependencies (libffi, libloading) that are
unsupported on musl and SGX targets; update Cargo.toml (feature description for
"ctypes") and/or the README to explicitly state the platform constraints and the
potential for compile errors on musl/SGX when users pass --features ctypes, and
mention that the conditional cfg(any(feature = "ctypes", ...)) in mod _ctypes is
the reason this opt-in can override the target checks.

#[cfg(all(feature = "host_env", windows))]
Expand Down Expand Up @@ -85,10 +88,13 @@ pub fn builtin_module_defs(ctx: &Context) -> Vec<&'static PyModuleDef> {
atexit::module_def(ctx),
_codecs::module_def(ctx),
_collections::module_def(ctx),
#[cfg(all(
feature = "host_env",
any(target_os = "linux", target_os = "macos", target_os = "windows"),
not(any(target_env = "musl", target_env = "sgx"))
#[cfg(any(
feature = "ctypes",
all(
feature = "host_env",
any(target_os = "linux", target_os = "macos", target_os = "windows"),
not(any(target_env = "musl", target_env = "sgx"))
)
))]
_ctypes::module_def(ctx),
errno::module_def(ctx),
Expand Down