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

gh-116622: Redirect stdout and stderr to system log when embedded in an Android app #118063

Merged
merged 19 commits into from Apr 30, 2024

Conversation

mhsmith
Copy link
Member

@mhsmith mhsmith commented Apr 18, 2024

When embedded in an app on current versions of Android, there's no easy way to monitor the process's stdout and stderr. So, as specified in PEP 738, this PR redirects them to the system log, which can be viewed using the developer tools.

@mhsmith
Copy link
Member Author

mhsmith commented Apr 22, 2024

@serhiy-storchaka: This PR is connected to the io module, which I see you've been working on recently. Are you able to review it?

Lib/_android_support.py Show resolved Hide resolved
Lib/_android_support.py Outdated Show resolved Hide resolved
Lib/_android_support.py Outdated Show resolved Hide resolved
Lib/_android_support.py Outdated Show resolved Hide resolved
Lib/_android_support.py Show resolved Hide resolved
Lib/_android_support.py Outdated Show resolved Hide resolved
Python/pylifecycle.c Outdated Show resolved Hide resolved
Python/pylifecycle.c Show resolved Hide resolved
Python/pylifecycle.c Outdated Show resolved Hide resolved
Python/pylifecycle.c Outdated Show resolved Hide resolved
Python/pylifecycle.c Show resolved Hide resolved
Lib/_android_support.py Show resolved Hide resolved
configure.ac Outdated Show resolved Hide resolved
Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Thank you!
This looks good to me, modulo a few small nitpicks. I'll let @serhiy-storchaka resolve the conversations he started.

Lib/_android_support.py Show resolved Hide resolved

def tearDown(self):
self.logcat_process.terminate()
self.logcat_process.wait(0.1)
Copy link
Member

Choose a reason for hiding this comment

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

Cunsider using test.support.LOOPBACK_TIMEOUT here; there's machinery to scale the _TIMEOUT constants for slower machines.

(@vstinner, if you can remember from 2019: is LOOPBACK_TIMEOUT appropriate to wait for a process?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.


# Long lines are split into blocks of 1000 *characters*, but
# TextIOWrapper should then join them back together as much as
# possible without exceeding 4000 UTF-8 *bytes*.
Copy link
Member

Choose a reason for hiding this comment

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

Let's leave a breadcrumb here:

Suggested change
# possible without exceeding 4000 UTF-8 *bytes*.
# possible without exceeding MAX_BYTES_PER_WRITE (4000) UTF-8 *bytes*.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Lib/_android_support.py Show resolved Hide resolved
Lib/_android_support.py Outdated Show resolved Hide resolved
Lib/_android_support.py Outdated Show resolved Hide resolved
Lib/_android_support.py Show resolved Hide resolved
Lib/_android_support.py Outdated Show resolved Hide resolved
Python/pylifecycle.c Show resolved Hide resolved
Lib/test/test_android.py Show resolved Hide resolved
Lib/test/test_android.py Show resolved Hide resolved
Lib/test/test_android.py Show resolved Hide resolved
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for addressing my comments. LGTM in general. I left several minor suggestions, but they are not mandatory, you can implement or reject them on case by case base.

In any case I'm going to merge this PR after your answer.

Lib/_android_support.py Outdated Show resolved Hide resolved
Lib/test/test_android.py Outdated Show resolved Hide resolved
Lib/test/test_android.py Outdated Show resolved Hide resolved
Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Using the tooling and instructions in #117878, I've confirmed that this works for the test suite:

image

@erlend-aasland erlend-aasland removed their request for review April 30, 2024 07:34


static PyMethodDef android_log_write_method = {
"android_log_write", android_log_write_impl, METH_VARARGS
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to use METH_FASTCALL?

Copy link
Member

Choose a reason for hiding this comment

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

You can replace PyArg_ParseTuple() with _PyArg_ParseStack().

Copy link
Member

Choose a reason for hiding this comment

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

I do not think that it will make significant difference in this case. It is better to use the stable public API.

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 agree: this isn't a performance-critical area.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, let's go with VARARGS.

configure.ac Outdated Show resolved Hide resolved
mhsmith and others added 2 commits April 30, 2024 12:54
Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner vstinner merged commit 3b268f4 into python:main Apr 30, 2024
36 checks passed
@vstinner
Copy link
Member

Merged. Let's see how it goes. It can be adjusted later if needed.

Nice change @mhsmith.

SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
Sign up for free to 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.

None yet

5 participants