Skip to content

Commit d2fb4c5

Browse files
Patch sysconfig data at install time (#9857)
## Summary This PR reimplements [`sysconfigpatcher`](https://github.com/bluss/sysconfigpatcher) in Rust and applies it to our Python installations at install-time, ensuring that the `sysconfig` data is more likely to be correct. For now, we only rewrite prefixes (i.e., any path that starts with `/install` gets rewritten to the correct absolute path for the current machine). Unlike `sysconfigpatcher`, this PR does not yet do any of the following: - Patch `pkginfo` files. - Change `clang` references to `cc`. A few things that we should do as follow-ups, in my opinion: 1. Rewrite [`AR`](https://github.com/bluss/sysconfigpatcher/blob/c1ebf8ab9274dcde255484d93ce0f1fd1f76a248/src/sysconfigpatcher.py#L61). 2. Remove `-isysroot`, which we already do for newer builds.
1 parent 5903ce5 commit d2fb4c5

File tree

13 files changed

+817
-19
lines changed

13 files changed

+817
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uv-python/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ windows-result = { workspace = true }
7272
anyhow = { version = "1.0.89" }
7373
assert_fs = { version = "1.1.2" }
7474
indoc = { workspace = true }
75+
insta = { version = "1.40.0" }
7576
itertools = { version = "0.13.0" }
7677
temp-env = { version = "0.3.6" }
7778
tempfile = { workspace = true }

crates/uv-python/python/get_interpreter_info.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,10 @@ def main() -> None:
573573
"sys_executable": sys.executable,
574574
"sys_path": sys.path,
575575
"stdlib": sysconfig.get_path("stdlib"),
576-
"sysconfig_prefix": sysconfig.get_config_var("prefix"),
576+
# Prior to the introduction of `sysconfig` patching, python-build-standalone installations would always use
577+
# "/install" as the prefix. With `sysconfig` patching, we rewrite the prefix to match the actual installation
578+
# location. So in newer versions, we also write a dedicated flag to indicate standalone builds.
579+
"standalone": sysconfig.get_config_var("prefix") == "/install" or bool(sysconfig.get_config_var("PYTHON_BUILD_STANDALONE")),
577580
"scheme": get_scheme(),
578581
"virtualenv": get_virtualenv(),
579582
"platform": os_and_arch,

crates/uv-python/src/discovery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,7 @@ fn disjunction(items: &[&str]) -> String {
25182518
fn try_into_u8_slice(release: &[u64]) -> Result<Vec<u8>, std::num::TryFromIntError> {
25192519
release
25202520
.iter()
2521-
.map(|x| match (*x).try_into() {
2521+
.map(|x| match u8::try_from(*x) {
25222522
Ok(x) => Ok(x),
25232523
Err(e) => Err(e),
25242524
})
@@ -2527,7 +2527,7 @@ fn try_into_u8_slice(release: &[u64]) -> Result<Vec<u8>, std::num::TryFromIntErr
25272527

25282528
/// Convert a wheel tag formatted version (e.g., `38`) to multiple components (e.g., `3.8`).
25292529
///
2530-
/// The major version is always assumed to be a single digit 0-9. The minor version is all of
2530+
/// The major version is always assumed to be a single digit 0-9. The minor version is all
25312531
/// the following content.
25322532
///
25332533
/// If not a wheel tag formatted version, the input is returned unchanged.

crates/uv-python/src/installation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ impl PythonInstallation {
163163

164164
let installed = ManagedPythonInstallation::new(path)?;
165165
installed.ensure_externally_managed()?;
166+
installed.ensure_sysconfig_patched()?;
166167
installed.ensure_canonical_executables()?;
167168

168169
Ok(Self {

crates/uv-python/src/interpreter.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct Interpreter {
4646
sys_executable: PathBuf,
4747
sys_path: Vec<PathBuf>,
4848
stdlib: PathBuf,
49-
sysconfig_prefix: Option<PathBuf>,
49+
standalone: bool,
5050
tags: OnceLock<Tags>,
5151
target: Option<Target>,
5252
prefix: Option<Prefix>,
@@ -80,7 +80,7 @@ impl Interpreter {
8080
sys_executable: info.sys_executable,
8181
sys_path: info.sys_path,
8282
stdlib: info.stdlib,
83-
sysconfig_prefix: info.sysconfig_prefix,
83+
standalone: info.standalone,
8484
tags: OnceLock::new(),
8585
target: None,
8686
prefix: None,
@@ -368,11 +368,6 @@ impl Interpreter {
368368
&self.stdlib
369369
}
370370

371-
/// Return the `prefix` path for this Python interpreter, as returned by `sysconfig.get_config_var("prefix")`.
372-
pub fn sysconfig_prefix(&self) -> Option<&Path> {
373-
self.sysconfig_prefix.as_deref()
374-
}
375-
376371
/// Return the `purelib` path for this Python interpreter, as returned by `sysconfig.get_paths()`.
377372
pub fn purelib(&self) -> &Path {
378373
&self.scheme.purelib
@@ -441,8 +436,7 @@ impl Interpreter {
441436
///
442437
/// See: <https://github.com/indygreg/python-build-standalone/issues/382>
443438
pub fn is_standalone(&self) -> bool {
444-
self.sysconfig_prefix()
445-
.is_some_and(|prefix| prefix == Path::new("/install"))
439+
self.standalone
446440
}
447441

448442
/// Return the [`Layout`] environment used to install wheels into this interpreter.
@@ -626,7 +620,7 @@ struct InterpreterInfo {
626620
sys_executable: PathBuf,
627621
sys_path: Vec<PathBuf>,
628622
stdlib: PathBuf,
629-
sysconfig_prefix: Option<PathBuf>,
623+
standalone: bool,
630624
pointer_size: PointerSize,
631625
gil_disabled: bool,
632626
}
@@ -854,6 +848,7 @@ mod tests {
854848
"arch": "x86_64"
855849
},
856850
"manylinux_compatible": false,
851+
"standalone": false,
857852
"markers": {
858853
"implementation_name": "cpython",
859854
"implementation_version": "3.12.0",

crates/uv-python/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use crate::discovery::{
88
find_python_installations, EnvironmentPreference, Error as DiscoveryError, PythonDownloads,
99
PythonNotFound, PythonPreference, PythonRequest, PythonSource, PythonVariant, VersionRequest,
1010
};
11-
pub use crate::environment::{InvalidEnvironment, InvalidEnvironmentKind, PythonEnvironment};
11+
pub use crate::environment::{InvalidEnvironmentKind, PythonEnvironment};
1212
pub use crate::implementation::ImplementationName;
1313
pub use crate::installation::{PythonInstallation, PythonInstallationKey};
1414
pub use crate::interpreter::{Error as InterpreterError, Interpreter};
@@ -39,6 +39,7 @@ mod prefix;
3939
#[cfg(windows)]
4040
mod py_launcher;
4141
mod python_version;
42+
mod sysconfig;
4243
mod target;
4344
mod version_files;
4445
mod virtualenv;
@@ -220,6 +221,7 @@ mod tests {
220221
"arch": "x86_64"
221222
},
222223
"manylinux_compatible": true,
224+
"standalone": true,
223225
"markers": {
224226
"implementation_name": "{IMPLEMENTATION}",
225227
"implementation_version": "{FULL_VERSION}",

crates/uv-python/src/managed.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::libc::LibcDetectionError;
2525
use crate::platform::Error as PlatformError;
2626
use crate::platform::{Arch, Libc, Os};
2727
use crate::python_version::PythonVersion;
28-
use crate::{PythonRequest, PythonVariant};
28+
use crate::{sysconfig, PythonRequest, PythonVariant};
2929
#[derive(Error, Debug)]
3030
pub enum Error {
3131
#[error(transparent)]
@@ -40,6 +40,8 @@ pub enum Error {
4040
InvalidPythonVersion(String),
4141
#[error(transparent)]
4242
ExtractError(#[from] uv_extract::Error),
43+
#[error(transparent)]
44+
SysconfigError(#[from] sysconfig::Error),
4345
#[error("Failed to copy to: {0}", to.user_display())]
4446
CopyError {
4547
to: PathBuf,
@@ -491,6 +493,21 @@ impl ManagedPythonInstallation {
491493
Ok(())
492494
}
493495

496+
/// Ensure that the `sysconfig` data is patched to match the installation path.
497+
pub fn ensure_sysconfig_patched(&self) -> Result<(), Error> {
498+
if cfg!(unix) {
499+
if *self.implementation() == ImplementationName::CPython {
500+
sysconfig::update_sysconfig(
501+
self.path(),
502+
self.key.major,
503+
self.key.minor,
504+
self.key.variant.suffix(),
505+
)?;
506+
}
507+
}
508+
Ok(())
509+
}
510+
494511
/// Create a link to the managed Python executable.
495512
///
496513
/// If the file already exists at the target path, an error will be returned.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#![allow(dead_code)]
2+
3+
use std::str::Chars;
4+
5+
pub(super) const EOF_CHAR: char = '\0';
6+
7+
/// A cursor represents a pointer in the source code.
8+
///
9+
/// Based on [`rustc`'s `Cursor`](https://github.com/rust-lang/rust/blob/d1b7355d3d7b4ead564dbecb1d240fcc74fff21b/compiler/rustc_lexer/src/cursor.rs)
10+
#[derive(Clone, Debug)]
11+
pub(super) struct Cursor<'src> {
12+
/// An iterator over the [`char`]'s of the source code.
13+
chars: Chars<'src>,
14+
15+
/// Stores the previous character for debug assertions.
16+
#[cfg(debug_assertions)]
17+
prev_char: char,
18+
}
19+
20+
impl<'src> Cursor<'src> {
21+
pub(super) fn new(source: &'src str) -> Self {
22+
Self {
23+
chars: source.chars(),
24+
#[cfg(debug_assertions)]
25+
prev_char: EOF_CHAR,
26+
}
27+
}
28+
29+
/// Returns the previous character. Useful for debug assertions.
30+
#[cfg(debug_assertions)]
31+
pub(super) const fn previous(&self) -> char {
32+
self.prev_char
33+
}
34+
35+
/// Peeks the next character from the input stream without consuming it.
36+
/// Returns [`EOF_CHAR`] if the position is past the end of the file.
37+
pub(super) fn first(&self) -> char {
38+
self.chars.clone().next().unwrap_or(EOF_CHAR)
39+
}
40+
41+
/// Peeks the second character from the input stream without consuming it.
42+
/// Returns [`EOF_CHAR`] if the position is past the end of the file.
43+
pub(super) fn second(&self) -> char {
44+
let mut chars = self.chars.clone();
45+
chars.next();
46+
chars.next().unwrap_or(EOF_CHAR)
47+
}
48+
49+
/// Returns the remaining text to lex.
50+
///
51+
/// Use [`Cursor::text_len`] to get the length of the remaining text.
52+
pub(super) fn rest(&self) -> &'src str {
53+
self.chars.as_str()
54+
}
55+
56+
/// Returns `true` if the cursor is at the end of file.
57+
pub(super) fn is_eof(&self) -> bool {
58+
self.chars.as_str().is_empty()
59+
}
60+
61+
/// Moves the cursor to the next character, returning the previous character.
62+
/// Returns [`None`] if there is no next character.
63+
pub(super) fn bump(&mut self) -> Option<char> {
64+
let prev = self.chars.next()?;
65+
66+
#[cfg(debug_assertions)]
67+
{
68+
self.prev_char = prev;
69+
}
70+
71+
Some(prev)
72+
}
73+
74+
pub(super) fn eat_char(&mut self, c: char) -> bool {
75+
if self.first() == c {
76+
self.bump();
77+
true
78+
} else {
79+
false
80+
}
81+
}
82+
83+
pub(super) fn eat_char2(&mut self, c1: char, c2: char) -> bool {
84+
let mut chars = self.chars.clone();
85+
if chars.next() == Some(c1) && chars.next() == Some(c2) {
86+
self.bump();
87+
self.bump();
88+
true
89+
} else {
90+
false
91+
}
92+
}
93+
94+
pub(super) fn eat_char3(&mut self, c1: char, c2: char, c3: char) -> bool {
95+
let mut chars = self.chars.clone();
96+
if chars.next() == Some(c1) && chars.next() == Some(c2) && chars.next() == Some(c3) {
97+
self.bump();
98+
self.bump();
99+
self.bump();
100+
true
101+
} else {
102+
false
103+
}
104+
}
105+
106+
pub(super) fn eat_if<F>(&mut self, mut predicate: F) -> Option<char>
107+
where
108+
F: FnMut(char) -> bool,
109+
{
110+
if predicate(self.first()) && !self.is_eof() {
111+
self.bump()
112+
} else {
113+
None
114+
}
115+
}
116+
117+
/// Eats symbols while predicate returns true or until the end of file is reached.
118+
#[inline]
119+
pub(super) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
120+
// It was tried making optimized version of this for eg. line comments, but
121+
// LLVM can inline all of this and compile it down to fast iteration over bytes.
122+
while predicate(self.first()) && !self.is_eof() {
123+
self.bump();
124+
}
125+
}
126+
127+
/// Skips the next `count` bytes.
128+
///
129+
/// ## Panics
130+
/// - If `count` is larger than the remaining bytes in the input stream.
131+
/// - If `count` indexes into a multi-byte character.
132+
pub(super) fn skip_bytes(&mut self, count: usize) {
133+
#[cfg(debug_assertions)]
134+
{
135+
self.prev_char = self.chars.as_str()[..count]
136+
.chars()
137+
.next_back()
138+
.unwrap_or('\0');
139+
}
140+
141+
self.chars = self.chars.as_str()[count..].chars();
142+
}
143+
144+
/// Skips to the end of the input stream.
145+
pub(super) fn skip_to_end(&mut self) {
146+
self.chars = "".chars();
147+
}
148+
}

0 commit comments

Comments
 (0)