prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Unexpected behavior when distutils is invoked before Setuptools
  TL;DR We are forced to use distutils because setuptools has broken symlink processing and this causes `distutils.errors.DistutilsClassError: command class <class '__main__.SDistCommand'> must subclass Command`.
  
  It works with `setuptools<48` and the changelog doesn't document any breaking behaviors for this version.
  
  Repro:
  ```console
  $ git clone https://github.com/ansible/ansible.git
  $ cd ansible
  $ pip install -U 'setuptools>=48'
  $ python setup.py sdist
  ```
  (tried under Python 3.8)
  
  Ref: https://github.com/ansible/ansible/issues/70456
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `setuptools/distutils_patch.py`
  Content:
  ```
  1 """
  2 Ensure that the local copy of distutils is preferred over stdlib.
  3 
  4 See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
  5 for more motivation.
  6 """
  7 
  8 import sys
  9 import re
  10 import os
  11 import importlib
  12 import warnings
  13 
  14 
  15 def clear_distutils():
  16     if 'distutils' not in sys.modules:
  17         return
  18     warnings.warn("Setuptools is replacing distutils")
  19     mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
  20     for name in mods:
  21         del sys.modules[name]
  22 
  23 
  24 def enabled():
  25     """
  26     Allow selection of distutils by environment variable.
  27     """
  28     which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
  29     return which == 'local'
  30 
  31 
  32 def ensure_local_distutils():
  33     clear_distutils()
  34     distutils = importlib.import_module('setuptools._distutils')
  35     distutils.__name__ = 'distutils'
  36     sys.modules['distutils'] = distutils
  37 
  38     # sanity check that submodules load as expected
  39     core = importlib.import_module('distutils.core')
  40     assert '_distutils' in core.__file__, core.__file__
  41 
  42 
  43 if enabled():
  44     ensure_local_distutils()
  45
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py
  --- a/setuptools/distutils_patch.py
  +++ b/setuptools/distutils_patch.py
  @@ -12,10 +12,26 @@
   import warnings
   
   
  +is_pypy = '__pypy__' in sys.builtin_module_names
  +
  +
  +def warn_distutils_present():
  +    if 'distutils' not in sys.modules:
  +        return
  +    if is_pypy and sys.version_info < (3, 7):
  +        # PyPy for 3.6 unconditionally imports distutils, so bypass the warning
  +        # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
  +        return
  +    warnings.warn(
  +        "Distutils was imported before Setuptools. This usage is discouraged "
  +        "and may exhibit undesirable behaviors or errors. Please use "
  +        "Setuptools' objects directly or at least import Setuptools first.")
  +
  +
   def clear_distutils():
       if 'distutils' not in sys.modules:
           return
  -    warnings.warn("Setuptools is replacing distutils")
  +    warnings.warn("Setuptools is replacing distutils.")
       mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
       for name in mods:
           del sys.modules[name]
  @@ -40,5 +56,6 @@
       assert '_distutils' in core.__file__, core.__file__
   
   
  +warn_distutils_present()
   if enabled():
       ensure_local_distutils()
  
verification_info: {"golden_diff": "diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py\n--- a/setuptools/distutils_patch.py\n+++ b/setuptools/distutils_patch.py\n@@ -12,10 +12,26 @@\n import warnings\n \n \n+is_pypy = '__pypy__' in sys.builtin_module_names\n+\n+\n+def warn_distutils_present():\n+    if 'distutils' not in sys.modules:\n+        return\n+    if is_pypy and sys.version_info < (3, 7):\n+        # PyPy for 3.6 unconditionally imports distutils, so bypass the warning\n+        # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250\n+        return\n+    warnings.warn(\n+        \"Distutils was imported before Setuptools. This usage is discouraged \"\n+        \"and may exhibit undesirable behaviors or errors. Please use \"\n+        \"Setuptools' objects directly or at least import Setuptools first.\")\n+\n+\n def clear_distutils():\n     if 'distutils' not in sys.modules:\n         return\n-    warnings.warn(\"Setuptools is replacing distutils\")\n+    warnings.warn(\"Setuptools is replacing distutils.\")\n     mods = [name for name in sys.modules if re.match(r'distutils\\b', name)]\n     for name in mods:\n         del sys.modules[name]\n@@ -40,5 +56,6 @@\n     assert '_distutils' in core.__file__, core.__file__\n \n \n+warn_distutils_present()\n if enabled():\n     ensure_local_distutils()\n", "issue": "Unexpected behavior when distutils is invoked before Setuptools\nTL;DR We are forced to use distutils because setuptools has broken symlink processing and this causes `distutils.errors.DistutilsClassError: command class <class '__main__.SDistCommand'> must subclass Command`.\r\n\r\nIt works with `setuptools<48` and the changelog doesn't document any breaking behaviors for this version.\r\n\r\nRepro:\r\n```console\r\n$ git clone https://github.com/ansible/ansible.git\r\n$ cd ansible\r\n$ pip install -U 'setuptools>=48'\r\n$ python setup.py sdist\r\n```\r\n(tried under Python 3.8)\r\n\r\nRef: https://github.com/ansible/ansible/issues/70456\n", "before_files": [{"content": "\"\"\"\nEnsure that the local copy of distutils is preferred over stdlib.\n\nSee https://github.com/pypa/setuptools/issues/417#issuecomment-392298401\nfor more motivation.\n\"\"\"\n\nimport sys\nimport re\nimport os\nimport importlib\nimport warnings\n\n\ndef clear_distutils():\n    if 'distutils' not in sys.modules:\n        return\n    warnings.warn(\"Setuptools is replacing distutils\")\n    mods = [name for name in sys.modules if re.match(r'distutils\\b', name)]\n    for name in mods:\n        del sys.modules[name]\n\n\ndef enabled():\n    \"\"\"\n    Allow selection of distutils by environment variable.\n    \"\"\"\n    which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')\n    return which == 'local'\n\n\ndef ensure_local_distutils():\n    clear_distutils()\n    distutils = importlib.import_module('setuptools._distutils')\n    distutils.__name__ = 'distutils'\n    sys.modules['distutils'] = distutils\n\n    # sanity check that submodules load as expected\n    core = importlib.import_module('distutils.core')\n    assert '_distutils' in core.__file__, core.__file__\n\n\nif enabled():\n    ensure_local_distutils()\n", "path": "setuptools/distutils_patch.py"}], "after_files": [{"content": "\"\"\"\nEnsure that the local copy of distutils is preferred over stdlib.\n\nSee https://github.com/pypa/setuptools/issues/417#issuecomment-392298401\nfor more motivation.\n\"\"\"\n\nimport sys\nimport re\nimport os\nimport importlib\nimport warnings\n\n\nis_pypy = '__pypy__' in sys.builtin_module_names\n\n\ndef warn_distutils_present():\n    if 'distutils' not in sys.modules:\n        return\n    if is_pypy and sys.version_info < (3, 7):\n        # PyPy for 3.6 unconditionally imports distutils, so bypass the warning\n        # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250\n        return\n    warnings.warn(\n        \"Distutils was imported before Setuptools. This usage is discouraged \"\n        \"and may exhibit undesirable behaviors or errors. Please use \"\n        \"Setuptools' objects directly or at least import Setuptools first.\")\n\n\ndef clear_distutils():\n    if 'distutils' not in sys.modules:\n        return\n    warnings.warn(\"Setuptools is replacing distutils.\")\n    mods = [name for name in sys.modules if re.match(r'distutils\\b', name)]\n    for name in mods:\n        del sys.modules[name]\n\n\ndef enabled():\n    \"\"\"\n    Allow selection of distutils by environment variable.\n    \"\"\"\n    which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')\n    return which == 'local'\n\n\ndef ensure_local_distutils():\n    clear_distutils()\n    distutils = importlib.import_module('setuptools._distutils')\n    distutils.__name__ = 'distutils'\n    sys.modules['distutils'] = distutils\n\n    # sanity check that submodules load as expected\n    core = importlib.import_module('distutils.core')\n    assert '_distutils' in core.__file__, core.__file__\n\n\nwarn_distutils_present()\nif enabled():\n    ensure_local_distutils()\n", "path": "setuptools/distutils_patch.py"}]}