diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2016-03-31 21:30:53 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2016-03-31 21:30:53 (GMT) |
commit | 49f324f1d41668dbc478fa15f85921d0b05ec09b (patch) | |
tree | c0e7a2d8e82951b479f9e9614b22bc55253e272e /Lib/site.py | |
parent | 0fc03186f78a7ac5d90e195fca825d1e7b87aef0 (diff) | |
download | cpython-49f324f1d41668dbc478fa15f85921d0b05ec09b.zip cpython-49f324f1d41668dbc478fa15f85921d0b05ec09b.tar.gz cpython-49f324f1d41668dbc478fa15f85921d0b05ec09b.tar.bz2 |
Python 8: no pep8, no chocolate!
Diffstat (limited to 'Lib/site.py')
-rw-r--r-- | Lib/site.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Lib/site.py b/Lib/site.py index 56ba709..29b19bb 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -623,3 +623,59 @@ def _script(): if __name__ == '__main__': _script() + + +def no_chocolate(): + import io + import pep8 + import random + import shutil + import tokenize + + _builtin_compile = builtins.compile + words = ('chocolate', 'glory', 'fun', 'spam', 'love', 'guts') + pep8style = pep8.StyleGuide() + + def compile_pep8(source, filename, mode, **kw): + name = os.path.splitext(os.path.basename(filename))[0] + if not name.endswith('_noqa'): + bio = io.BytesIO(source) + encoding = tokenize.detect_encoding(bio.readline)[0] + lines = source.decode(encoding).splitlines(True) + + report = pep8.StandardReport(options=pep8style.options) + checker = pep8.Checker(filename, lines, + report=report, + options=pep8style.options) + checker.check_all() + if report.total_errors: + word = random.choice(words) + raise ImportError("no pep8, no %s" % word) + return _builtin_compile(source, filename, mode, **kw) + + builtins.compile = compile_pep8 + + # remove precompiled .pyc created during the bootstrap, + # to run PEP 8 checks on .py files + libdir_cache = os.path.join(os.path.dirname(__file__), '__pycache__') + try: + shutil.rmtree(libdir_cache) + except: + pass + + for name in sorted(sys.modules): + # Minimum to be able to import modules + if name in {'builtins', 'importlib._bootstrap', + 'importlib._bootstrap_external', 'importlib', + 'importlib.machinery', '__main__', 'io', 'sys', 'site'}: + continue + del sys.modules[name] + + +try: + import _ssl +except ImportError: + # Python not bootstraped yet + pass +else: + no_chocolate() |