dd-trace-py
dd-trace-py copied to clipboard
chore(internal): better support for Python legacy versions in ModuleWatchdog
Description
This change improves support for Python legacy versions, like 2.7 and 3.5 by using a common loader wrapper that makes more attributes available upon request, e.g. is_package. Furthermore, this change also provides support for dict copy via the dict constructor, which is required by some pytest fixtures, like testdir.
More technical details
In Python<=3.5, calling the dict constructor on a dictionary makes a copy of it by first checking if it has a keys attribute, and then performing a PyDict_Check. If these checks succeed, the dictionary is copied using the C API. Since ModuleWatchdog is a subclass of dict, this means that the wrapping logic is bypassed, and dict ends up copying the dictionary backing ModuleWatchdog rather than the wrapped sys.modules. We exploit the keys attribute access to then copy the state of sys.modules over to the backing dict, so that calling dict on a ModuleWatchdog instance actually creates a copy of the wrapped dictionary instead of the backing one.
Testing
This change is a pre-requisite for #4070 to make the test suite pass.
Checklist
- [ ] Title must conform to conventional commit.
- [x] Add additional sections for
featandfixpull requests. - [ ] Ensure tests are passing for affected code.
- [x] Library documentation and/or Datadog's documentation site is updated. Link to doc PR in description.
Reviewer Checklist
- [ ] Title is accurate.
- [ ] Description motivates each change.
- [ ] No unnecessary changes were introduced in this PR.
- [ ] PR cannot be broken up into smaller PRs.
- [ ] Avoid breaking API changes unless absolutely necessary.
- [ ] Tests provided or description of manual testing performed is included in the code or PR.
- [x] Release note has been added for fixes and features, or else
changelog/no-changeloglabel added. - [ ] All relevant GitHub issues are correctly linked.
- [ ] Backports are identified and tagged with Mergifyio.
- [ ] Add to milestone.