Skip to content

bpo-43312: Functions returning default and preferred sysconfig schemes - #24644

Merged
pfmoore merged 6 commits into
python:masterfrom
uranusjr:sysconfig-preferred-scheme
Apr 27, 2021
Merged

bpo-43312: Functions returning default and preferred sysconfig schemes#24644
pfmoore merged 6 commits into
python:masterfrom
uranusjr:sysconfig-preferred-scheme

Conversation

@uranusjr

@uranusjruranusjr commented Feb 25, 2021

Copy link
Copy Markdown
Contributor

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsBot added the stale Stale PR or inactive for long period of time. label Mar 28, 2021
@uranusjr
uranusjrforce-pushed the sysconfig-preferred-scheme branch from acadc40 to a0d613fCompareMarch 29, 2021 20:17
@uranusjr
uranusjr marked this pull request as ready for review March 29, 2021 21:36
@uranusjr

uranusjr commented Mar 29, 2021

Copy link
Copy Markdown
ContributorAuthor

Uh, how do I remove the stale label?

@github-actionsgithub-actionsBot removed the stale Stale PR or inactive for long period of time. label Mar 30, 2021

@pfmoorepfmoore left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't say whether the choices in _get_preferred_schemes are correct, but they seem reasonable. Unless there's anything we can check them against, I'd say we should go with them.

Comment threadLib/sysconfig.py


def get_paths(scheme=_get_default_scheme(), vars=None, expand=True):
def get_paths(scheme=get_default_scheme(), vars=None, expand=True):

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.

Do we need to call this function every time? Wouldn't be better to call it just once and save it into a private global variable, and use that as the default value for all these methods?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

That’s a separate issue already presented in the previous implementation. I’m not sure this is the best plac eto discuss the possible implications in e.g. multiprocessing settings (I don’t even have enough expertise to comment on it).

As it currently stands, the function is only called twice (get_paths and get_path) on import time, so the performance difference is pretty close to none anyway.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed, let's keep the change to the minimal required.

Comment threadLib/sysconfig.py Outdated
print('Platform: "%s"' % get_platform())
print('Python version: "%s"' % get_python_version())
print('Current installation scheme: "%s"' % _get_default_scheme())
print('Current installation scheme: "%s"' % get_default_scheme())

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.

Would be a good opportunity to migrate this to f-strings?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Probably, I’ll combine this to other needed changes, if there are any.

@hroncokhroncok left a comment

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.

I ❤️ it!

Several non-blocking questions attached.

However, I have a very strong suggestion: Please add tests.

Comment threadLib/sysconfig.py Outdated
Comment on lines +244 to +245
if scheme not in _INSTALL_SCHEMES:
raise KeyError(key)

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.

Will the above lookup not raise already? Should this be done before calling _get_preferred_schemes?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

The main intention is to guard against incorrect implementation, since _get_preferred_schemes() is expected to be modified by redistributors and implementers. Say an implementer makes a typo:

def_get_preferred_schemes():
return {
"prefix": "posix_prefix",
"home": "posix_home",
"user": "posix_uesr", # Oops!
}

And when the value is later used:

scheme=sysconfig.get_preferred_scheme("user")
... # Much later...paths=sysconfig.get_paths(scheme)

This check would catch that posix_uesr is not actually a valid scheme and error early, instead of later when the user actually tries to use the (invalid) scheme.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we raise a ValueError with a better message then? "Key '{}' returned scheme '{}' which is not valid on this platform" (or something more correct)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I couldn’t think of a better message and implemented yours for now, any ideas are very welcomed. Ideally I want to point users to the implementer/redistributor instead of reporting the error to CPython, but that’s probably not really feasible.

Comment threadLib/sysconfig.py Outdated
@uranusjr

Copy link
Copy Markdown
ContributorAuthor

I’ve changed get_default_scheme() to use get_preferred_scheme(), and a couple of simple tests for the new functions.

@frenzymadness

Copy link
Copy Markdown
Contributor

The implementation looks good to me.

@uranusjr

Copy link
Copy Markdown
ContributorAuthor

I’m wondering whether we should add documentation for the private _get_preferred_schemes() as well, to tell redistributors this is the function they’re advised to override/modify.

@encukou

Copy link
Copy Markdown
Member

I’m wondering whether we should add documentation for the private _get_preferred_schemes() as well, to tell redistributors this is the function they’re advised to override/modify.

At least a docstring would be great, yes.

@uranusjr

uranusjr commented Apr 17, 2021

Copy link
Copy Markdown
ContributorAuthor

Documentation entry added 👍


Edit: Plus converting %-formatting to f-strings.

@hroncok

Copy link
Copy Markdown
Contributor

Curiosity question: Should distutils use this?

@uranusjr

Copy link
Copy Markdown
ContributorAuthor

Ideally yes, but distutils’s implementation around this is structured very differently, and tangled with a lot of things inside distutils.command.install, I’m not sure if it’s really worthwhile (or even doable without risking behavioural changes) 😞

@zoobazooba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would like to see that error message improved, but the rest seems fine to me. I'm just taking on trust that it's sufficient for distros to override what they need, because I don't know that side of it well enough.

Comment threadLib/sysconfig.py Outdated
Comment on lines +244 to +245
if scheme not in _INSTALL_SCHEMES:
raise KeyError(key)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we raise a ValueError with a better message then? "Key '{}' returned scheme '{}' which is not valid on this platform" (or something more correct)

@uranusjr

Copy link
Copy Markdown
ContributorAuthor

Another nudge 🙂

@pfmoore

Copy link
Copy Markdown
Member

I believe all comments have been addressed, and I see no remaining concerns raised and general approval of the change, so I'm going to merge this based on my approval above.

@pfmoore
pfmoore merged commit d925133 into python:masterApr 27, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@uranusjr@frenzymadness@encukou@hroncok@pfmoore@gaborbernat@zooba@the-knights-who-say-ni@bedevere-bot