-
Notifications
You must be signed in to change notification settings - Fork 2
New Wrapping PR 5: Update all sources to new wrapping system #369
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
Merged
bitterpanda63
merged 25 commits into
improved-wrapping-main
from
update-wrapping-to-wrapt-for-sources
May 21, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
12f0668
Create new process worker script and run it on context creation
bitterpanda63 3021f26
Cleanup of process_worker logic
bitterpanda63 808ee67
Add extra tests for psycopg2 and fix issue for immutables
bitterpanda63 4a7526c
Remove package tracking for gunicorn and uwsgi (should be standard)
bitterpanda63 2479d00
Update django wrapper to use wrapt
bitterpanda63 793a1fe
Django cleanup and expand comments
bitterpanda63 0ff8140
Create a before_async decorator for patching
bitterpanda63 011963c
Add "django" as a module name
bitterpanda63 bb37d04
Cleanup starlette's __init__.py code
bitterpanda63 929b4f2
Refactor starlette.applications wrapping
bitterpanda63 10cb78a
Fix previous mistakes with module not being ref'd in starlette_app's.py
bitterpanda63 af7a892
Port the starlette_routing wrapper
bitterpanda63 635c7a9
Also mention starlette package in starlette_routing
bitterpanda63 c82f1bf
Create new @before_modifies_return
bitterpanda63 44f6d53
Port flask.py to the new wrapt way
bitterpanda63 1eb0e08
Cleanup flask.py imports
bitterpanda63 444a529
linting
bitterpanda63 b958f4c
remove try-finally for return, which was swallowing error
bitterpanda63 f671c5f
Flask patch, get correct "environ" variable
bitterpanda63 9e0c8b8
Fix starlette bugs encountered after testing
bitterpanda63 32cdce1
Update quart patches to use new system
bitterpanda63 a0a17df
Remove checks for who wrote response (not necessary)
bitterpanda63 b2f833e
lxml.py use new wrapping system
bitterpanda63 3c427fb
Update xml.py to use new wrapping system
bitterpanda63 8a14842
Zen update xml.py to also patch fromstring/fromstringlist
bitterpanda63 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,36 @@ | ||
"""`Django` source module""" | ||
|
||
import copy | ||
import aikido_zen.importhook as importhook | ||
from aikido_zen.helpers.logging import logger | ||
from aikido_zen.background_process.packages import is_package_compatible, ANY_VERSION | ||
from ..functions.request_handler import request_handler | ||
from .run_init_stage import run_init_stage | ||
from .pre_response_middleware import pre_response_middleware | ||
from ...helpers.get_argument import get_argument | ||
from ...sinks import on_import, patch_function, before, after | ||
|
||
|
||
@importhook.on_import("django.core.handlers.base") | ||
def on_django_gunicorn_import(django): | ||
""" | ||
Hook 'n wrap on `django.core.handlers.base` | ||
Our goal is to wrap the `_get_response` function | ||
# https://github.com/django/django/blob/5865ff5adcf64da03d306dc32b36e87ae6927c85/django/core/handlers/base.py#L174 | ||
Returns : Modified django.core.handlers.base object | ||
""" | ||
if not is_package_compatible("django", required_version=ANY_VERSION): | ||
return django | ||
modified_django = importhook.copy_module(django) | ||
@before | ||
def _get_response_before(func, instance, args, kwargs): | ||
request = get_argument(args, kwargs, 0, "request") | ||
|
||
former__get_response = copy.deepcopy(django.BaseHandler._get_response) | ||
run_init_stage(request) | ||
|
||
def aikido__get_response(self, request): # Synchronous (WSGI) | ||
run_init_stage(request) # We do some initial request handling | ||
if pre_response_middleware not in instance._view_middleware: | ||
# The rate limiting middleware needs to be last in the chain. | ||
instance._view_middleware += [pre_response_middleware] | ||
|
||
if pre_response_middleware not in self._view_middleware: | ||
# The rate limiting middleware needs to be last in the chain. | ||
self._view_middleware += [pre_response_middleware] | ||
|
||
res = former__get_response(self, request) | ||
if hasattr(res, "status_code"): | ||
request_handler(stage="post_response", status_code=res.status_code) | ||
return res | ||
@after | ||
def _get_response_after(func, instance, args, kwargs, return_value): | ||
if hasattr(return_value, "status_code"): | ||
request_handler(stage="post_response", status_code=return_value.status_code) | ||
|
||
# pylint: disable=no-member | ||
setattr(modified_django.BaseHandler, "_get_response", aikido__get_response) | ||
setattr(django.BaseHandler, "_get_response", aikido__get_response) | ||
|
||
return modified_django | ||
@on_import("django.core.handlers.base", "django") | ||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def patch(m): | ||
""" | ||
Patch for _get_response (Synchronous/WSGI) | ||
- before: Parse body, create context & add middleware to run before a response | ||
- after: Check respone code to see if route should be analyzed | ||
# https://github.com/django/django/blob/5865ff5adcf64da03d306dc32b36e87ae6927c85/django/core/handlers/base.py#L174 | ||
""" | ||
patch_function(m, "BaseHandler._get_response", _get_response_before) | ||
patch_function(m, "BaseHandler._get_response", _get_response_after) |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.