Skip to content

requests.package.chardet* references not assigned correctly? #6223

@babyhoo976

Description

@babyhoo976

I found that packages.py is assigning sys.modules['requests.package.chardet'] over and over with different modules, is this intentional or a bug?

I looked at the code and this target variable confuses me, it is assigned again in loop and placed with itself(so completely?), looks like a name confliction to me. Code is referenced from installed version 2.28.1.

target = chardet.__name__
for mod in list(sys.modules):
    if mod == target or mod.startswith(f"{target}."):
        target = target.replace(target, "chardet")
        sys.modules[f"requests.packages.{target}"] = sys.modules[mod]

Expected Result

every chardet.* package maps to requests.packages.chardet.* respectively

Actual Result

only requests.package.chardet is assigned at last.

Reproduction Steps

import requests
import sys
print([m for m in sys.modules if name.startswith('requests.packages.chardet')])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions