Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress socket imports that might not exist on some systems #2916

Merged
merged 6 commits into from
Jan 21, 2024

Conversation

CoolCat467
Copy link
Member

This pull request adds import exception suppression to a few imports from the socket library that apparently might not exist on all systems. Fixes #2915.

Copy link

codecov bot commented Jan 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6547313) 99.64% compared to head (6d035ef) 99.64%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2916   +/-   ##
=======================================
  Coverage   99.64%   99.64%           
=======================================
  Files         116      116           
  Lines       17477    17479    +2     
  Branches     3133     3134    +1     
=======================================
+ Hits        17415    17417    +2     
  Misses         43       43           
  Partials       19       19           
Files Coverage Δ
src/trio/socket.py 100.00% <100.00%> (ø)

@Cheaterman
Copy link

Cheaterman commented Jan 3, 2024

LGTM 👍 thanks!

EDIT: OTOH I can't help but notice the code uses as to re-export using the same name, this can probably be omitted for clarity?

@jakkdl
Copy link
Member

jakkdl commented Jan 3, 2024

EDIT: OTOH I can't help but notice the code uses as to re-export using the same name, this can probably be omitted for clarity?

this is for pyright --verifytypes reasons. See #2625
(there's a comment to that effect further up in the file, but its not immediately visible in the diff view)

@jakkdl
Copy link
Member

jakkdl commented Jan 3, 2024

This is a partial revert of #2887 which broke these out.
The logic behind the change seems to be that support for these were added to windows in 3.8, and with dropping <3.8 it was now only pypy that didn't support them.

If we want to be extra thorough it'd perhaps be nice if we could add a conditional that is only true for the failing android API. @Cheaterman do you know of any sys.something variables that would be useful? The documentation says it's unavailable on Emscriptem and WASI: https://docs.python.org/3/library/socket.html#socket.if_indextoname - so maybe that's relevant? Otherwise I'm quite curious why they are missing.

I suppose building on android could maybe be considered for #2885

@Cheaterman have you tried building against this branch, or with local changes fixing this import, to verify these are the only problematic imports on your system? Does the test suite run correctly otherwise?

API 24 was released in august 2016 - which is very old by python standards: python3.8 was released in 2019. So I'm a bit wary that even if we fixed this import there'd be a lot of additional problems hiding.

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

This makes sense to me and while I would prefer to gate this rather than suppress import errors (so we have documentation for exactly what platforms fail in form of code), this works fine too.

That other approach might be too much work for something so small!

@Cheaterman
Copy link

@jakkdl Thanks for all the comments! I'm not aware of a sys module variable or anything else that would indicate the minimum supported Android API ; in fact that's why I was surprised to see this error happen in the first place, it should largely not be visible from Python.

I didn't get to test against this branch just yet - bumping minimum API is acceptable for my current application and other users I interacted with, in fact we might also bump the default in P4A to be thorough.

The reason it does matter to support such an old API version is simply because this isn't the target API but the minimum supported one, which means (AIUI at least) devices older than that will not be able to run the app. In practice I agree the ever-shrinking pool of 7+ year old devices isn't a huge concern, but it matters to some users ; and given we're actively recommending people switch to trio these days so they avoid all the threading footguns, we'd like to make their experience as smooth as possible :-).

@jakkdl
Copy link
Member

jakkdl commented Jan 10, 2024

@Cheaterman
Ah, P4A seems relevant. Is this possibly a bug on their side then? https://github.com/kivy/python-for-android
Glancing through the docs I don't see any reason they wouldn't support these socket imports - though they state the minimum version is 3.7 on PyPI and the github README, but 3.8 in the docs, so maybe there's something there.
Does it change if explicitly setting the python version in the build options?

@A5rocks
Copy link
Contributor

A5rocks commented Jan 10, 2024

-- snip --

OK, I found where all 3 attributes here are defined and commit date (to add if_nameindex) seems to point to January 2016. Checking Wikipedia's list of Android API versions seems to say that the next-released API version was 24, in August 2016.

Makes sense to me.

@Cheaterman
Copy link

Hi @A5rocks and thanks for your comments, I think you are entirely correct (only if_nameindex was introduced in 24) :-)

Hi @jakkdl and thanks for your work, P4A is indeed very relevant but the reason we were caught offguard by this is that the name will be unavailable on even Python 3.11 if building against minAPI < 24. Either way, I completely forgot to mention that yes, this is indeed being addressed on the P4A side, simply by bumping up the default minAPI to 24 ; but as you might guess, this isn't an ideal fix for people wanting to use recent trio versions while targeting older Android devices, that's why IMHO both are necessary.

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

Just a nitpick. I'm fine whether or not you update the comment.

if_nametoindex as if_nametoindex,
)

# In https://github.com/kivy/python-for-android, if_nameindex
Copy link
Contributor

@A5rocks A5rocks Jan 11, 2024

Choose a reason for hiding this comment

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

For what it's worth: as far as I can tell, this is just a CPython thing. Though that's a guess based on how CPython is pulling it in (see https://bugs.python.org/msg279495). I kinda assume this is just building CPython for Android, no specific changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think 9dda073 should be sufficient

@CoolCat467 CoolCat467 requested a review from jakkdl January 19, 2024 18:40
@jakkdl jakkdl merged commit d3bb19b into python-trio:master Jan 21, 2024
30 of 31 checks passed
@CoolCat467 CoolCat467 deleted the suppress-socket-import-errors branch January 21, 2024 20:54
@nabnaj
Copy link

nabnaj commented Nov 2, 2024

image

I have same error, should I change the API version ?
requirements = python3,Cython==0.29.34,kivy,trio==0.22.2
python 3.12.7

@CoolCat467
Copy link
Member Author

Would have been better to make a new issue, but yes, you should upgrade Trio to 0.27.0, which is the latest release as of writing.

@python-trio python-trio locked as resolved and limited conversation to collaborators Nov 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failing to import if_indextoname on Android
5 participants