pep508: un-export fields for MarkerEnvironment

We now use the getters and setters everywhere.

There were some places where we wanted to build a `MarkerEnvironment`
out of whole cloth, usually in tests. To facilitate those use cases, we
add a `MarkerEnvironmentBuilder` that provides a convenient constructor.
It's basically like a `MarkerEnvironment::new`, but with named
parameters. That's useful here because there are so many fields (and
they many have the same type).
This commit is contained in:
Andrew Gallant 2024-05-08 19:01:44 -04:00 committed by Andrew Gallant
parent be12cfb2b8
commit 7d67b7bb49
11 changed files with 198 additions and 169 deletions

View file

@ -202,31 +202,31 @@ impl Interpreter {
/// Returns the Python version.
#[inline]
pub const fn python_version(&self) -> &Version {
&self.markers.python_full_version.version
pub fn python_version(&self) -> &Version {
&self.markers.python_full_version().version
}
/// Returns the `python_full_version` marker corresponding to this Python version.
#[inline]
pub const fn python_full_version(&self) -> &StringVersion {
&self.markers.python_full_version
pub fn python_full_version(&self) -> &StringVersion {
self.markers.python_full_version()
}
/// Return the major version of this Python version.
pub fn python_major(&self) -> u8 {
let major = self.markers.python_full_version.version.release()[0];
let major = self.markers.python_full_version().version.release()[0];
u8::try_from(major).expect("invalid major version")
}
/// Return the minor version of this Python version.
pub fn python_minor(&self) -> u8 {
let minor = self.markers.python_full_version.version.release()[1];
let minor = self.markers.python_full_version().version.release()[1];
u8::try_from(minor).expect("invalid minor version")
}
/// Return the patch version of this Python version.
pub fn python_patch(&self) -> u8 {
let minor = self.markers.python_full_version.version.release()[2];
let minor = self.markers.python_full_version().version.release()[2];
u8::try_from(minor).expect("invalid patch version")
}
@ -237,13 +237,13 @@ impl Interpreter {
/// Return the major version of the implementation (e.g., `CPython` or `PyPy`).
pub fn implementation_major(&self) -> u8 {
let major = self.markers.implementation_version.version.release()[0];
let major = self.markers.implementation_version().version.release()[0];
u8::try_from(major).expect("invalid major version")
}
/// Return the minor version of the implementation (e.g., `CPython` or `PyPy`).
pub fn implementation_minor(&self) -> u8 {
let minor = self.markers.implementation_version.version.release()[1];
let minor = self.markers.implementation_version().version.release()[1];
u8::try_from(minor).expect("invalid minor version")
}
@ -254,7 +254,7 @@ impl Interpreter {
/// Returns the implementation name (e.g., `CPython` or `PyPy`).
pub fn implementation_name(&self) -> &str {
&self.markers.implementation_name
self.markers.implementation_name()
}
/// Return the `sys.base_exec_prefix` path for this Python interpreter.
@ -337,7 +337,7 @@ impl Interpreter {
Layout {
python_version: self.python_tuple(),
sys_executable: self.sys_executable().to_path_buf(),
os_name: self.markers.os_name.clone(),
os_name: self.markers.os_name().to_string(),
scheme: if let Some(target) = self.target.as_ref() {
target.scheme()
} else {
@ -540,7 +540,7 @@ impl InterpreterInfo {
if cached.timestamp == modified {
debug!(
"Cached interpreter info for Python {}, skipping probing: {}",
cached.data.markers.python_full_version,
cached.data.markers.python_full_version(),
executable.user_display()
);
return Ok(cached.data);
@ -567,7 +567,7 @@ impl InterpreterInfo {
let info = Self::query(executable, cache)?;
debug!(
"Found Python {} for: {}",
info.markers.python_full_version,
info.markers.python_full_version(),
executable.display()
);
@ -670,7 +670,7 @@ mod tests {
.unwrap();
let interpreter = Interpreter::query(&mocked_interpreter, &cache).unwrap();
assert_eq!(
interpreter.markers.python_version.version,
interpreter.markers.python_version().version,
Version::from_str("3.12").unwrap()
);
fs::write(
@ -683,7 +683,7 @@ mod tests {
.unwrap();
let interpreter = Interpreter::query(&mocked_interpreter, &cache).unwrap();
assert_eq!(
interpreter.markers.python_version.version,
interpreter.markers.python_version().version,
Version::from_str("3.13").unwrap()
);
}

View file

@ -88,29 +88,29 @@ impl PythonVersion {
let mut markers = base.clone();
// Ex) `implementation_version == "3.12.0"`
if markers.implementation_name == "cpython" {
if markers.implementation_name() == "cpython" {
let python_full_version = self.python_full_version();
markers.implementation_version = StringVersion {
markers = markers.with_implementation_version(StringVersion {
// Retain the verbatim representation, provided by the user.
string: self.0.to_string(),
version: python_full_version,
};
});
}
// Ex) `python_full_version == "3.12.0"`
let python_full_version = self.python_full_version();
markers.python_full_version = StringVersion {
markers = markers.with_python_full_version(StringVersion {
// Retain the verbatim representation, provided by the user.
string: self.0.to_string(),
version: python_full_version,
};
});
// Ex) `python_version == "3.12"`
let python_version = self.python_version();
markers.python_version = StringVersion {
markers = markers.with_python_version(StringVersion {
string: python_version.to_string(),
version: python_version,
};
});
markers
}