Fix Python instrumentation to handle from __future__ imports#3170
Open
Fix Python instrumentation to handle from __future__ imports#3170
from __future__ imports#3170Conversation
…_ imports Co-authored-by: plengauer <100447901+plengauer@users.noreply.github.com>
from __future__ imports
plengauer
approved these changes
Mar 13, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Python auto-instrumentation wrapper to avoid breaking scripts that use from __future__ imports by inserting the injected __file__ assignment after the last future import, and adds a regression test for this scenario.
Changes:
- Update Python file execution wrapper to detect
from __future__imports and insert__file__after them. - Add an auto test case covering execution of a Python file containing
from __future__imports.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/usr/share/opentelemetry_shell/agent.instrumentation.python.sh |
Adjusts how Python file content is wrapped/exec’d so __file__ isn’t prepended before from __future__ imports. |
tests/auto/test_auto_injection_python.sh |
Adds a regression test intended to catch the from __future__ ordering SyntaxError in instrumented execution. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Python instrumentation was prepending
__file__assignment at the start of executed files, violating Python's requirement thatfrom __future__imports must be first. This causedgoogle-github-actions/setup-gcloudto fail withSyntaxError: from __future__ imports must occur at the beginning of the file.Changes
agent.instrumentation.python.sh: Scan file content forfrom __future__imports and insert__file__assignment after the last one (if any exist), otherwise use original prepend behaviortest_auto_injection_python.shfor files withfrom __future__importsImplementation
The fix scans all lines to find the last
from __future__import statement and inserts__file__immediately after:This maintains backward compatibility with files that don't use
from __future__imports while fixing the gcloud.py case.Original prompt