|
| 1 | +/****************************************************************************** |
| 2 | + Copyright 2017-2022 typed_python Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +******************************************************************************/ |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +inline bool isCanonicalName(std::string name) { |
| 20 | + // this is the list of standard library modules in python 3.8 |
| 21 | + static std::set<std::string> canonicalPythonModuleNames({ |
| 22 | + "abc", "aifc", "antigravity", "argparse", "ast", "asynchat", "asyncio", "asyncore", |
| 23 | + "base64", "bdb", "binhex", "bisect", "_bootlocale", "bz2", "calendar", "cgi", "cgitb", |
| 24 | + "chunk", "cmd", "codecs", "codeop", "code", "collections", "_collections_abc", |
| 25 | + "colorsys", "_compat_pickle", "compileall", "_compression", "concurrent", |
| 26 | + "configparser", "contextlib", "contextvars", "copy", "copyreg", "cProfile", "crypt", |
| 27 | + "csv", "ctypes", "curses", "dataclasses", "datetime", "dbm", "decimal", "difflib", |
| 28 | + "dis", "distutils", "doctest", "dummy_threading", "_dummy_thread", "email", |
| 29 | + "encodings", "ensurepip", "enum", "filecmp", "fileinput", "fnmatch", "formatter", |
| 30 | + "fractions", "ftplib", "functools", "__future__", "genericpath", "getopt", "getpass", |
| 31 | + "gettext", "glob", "gzip", "hashlib", "heapq", "hmac", "html", "http", "idlelib", |
| 32 | + "imaplib", "imghdr", "importlib", "imp", "inspect", "io", "ipaddress", "json", |
| 33 | + "keyword", "lib2to3", "linecache", "locale", "logging", "lzma", "mailbox", "mailcap", |
| 34 | + "marshal", |
| 35 | + "_markupbase", "mimetypes", "modulefinder", "msilib", "multiprocessing", "netrc", |
| 36 | + "nntplib", "ntpath", "nturl2path", "numbers", "opcode", "operator", "optparse", "os", |
| 37 | + "_osx_support", "pathlib", "pdb", "pickle", "pickletools", "pipes", "pkgutil", |
| 38 | + "platform", "plistlib", "poplib", "posixpath", "pprint", "profile", "pstats", "pty", |
| 39 | + "_py_abc", "pyclbr", "py_compile", "_pydecimal", "pydoc_data", "pydoc", "_pyio", |
| 40 | + "queue", "quopri", "random", "reprlib", "re", "rlcompleter", "runpy", "sched", |
| 41 | + "secrets", "selectors", "shelve", "shlex", "shutil", "signal", "_sitebuiltins", |
| 42 | + "site-packages", "site", "smtpd", "smtplib", "sndhdr", "socket", "socketserver", |
| 43 | + "sqlite3", "sre_compile", "sre_constants", "sre_parse", "ssl", "statistics", "stat", |
| 44 | + "stringprep", "string", "_strptime", "struct", "subprocess", "sunau", "symbol", |
| 45 | + "symtable", "sysconfig", "tabnanny", "tarfile", "telnetlib", "tempfile", "test", |
| 46 | + "textwrap", "this", "_threading_local", "threading", "timeit", "tkinter", "tokenize", |
| 47 | + "token", "traceback", "tracemalloc", "trace", "tty", "turtledemo", "turtle", "types", |
| 48 | + "typing", "unittest", "urllib", "uuid", "uu", "venv", "warnings", "wave", "weakref", |
| 49 | + "_weakrefset", "webbrowser", "wsgiref", "xdrlib", "xml", "xmlrpc", "zipapp", |
| 50 | + "zipfile", "zipimport", "pytz", "psutil", |
| 51 | + |
| 52 | + // and some standard ones we might commonly install |
| 53 | + "numpy", "pandas", "scipy", "pytest", "_pytest", "typed_python", "object_database", "llvmlite", |
| 54 | + "requests", "redis", "websockets", "boto3", "py", "xdist", "pytest_jsonreport", |
| 55 | + "pytest_metadata", "flask", "flaky", "coverage", "pyasn1", "cryptography", "paramiko", |
| 56 | + "six", "torch" |
| 57 | + }); |
| 58 | + |
| 59 | + std::string moduleNameRoot; |
| 60 | + |
| 61 | + int posOfDot = name.find("."); |
| 62 | + if (posOfDot != std::string::npos) { |
| 63 | + moduleNameRoot = name.substr(0, posOfDot); |
| 64 | + } else { |
| 65 | + moduleNameRoot = name; |
| 66 | + } |
| 67 | + |
| 68 | + return canonicalPythonModuleNames.find(moduleNameRoot) != canonicalPythonModuleNames.end(); |
| 69 | +} |
| 70 | + |
| 71 | +// is this a special name in a dict, module, or class that we shouldn't hash? |
| 72 | +// we do want to hash methods like __init__ |
| 73 | +inline bool isSpecialIgnorableName(const std::string& name) { |
| 74 | + static std::set<std::string> canonicalMagicMethods({ |
| 75 | + "__abs__", "__add__", "__and__", "__bool__", |
| 76 | + "__bytes__", "__call__", "__contains__", "__del__", |
| 77 | + "__delattr__", "__eq__", "__float__", "__floordiv__", |
| 78 | + "__format__", "__ge__", "__getitem__", "__gt__", |
| 79 | + "__hash__", "__iadd__", "__iand__", "__ieq__", |
| 80 | + "__ifloordiv__", "__ige__", "__igt__", "__ile__", |
| 81 | + "__ilshift__", "__ilt__", "__imatmul__", "__imod__", |
| 82 | + "__imul__", "__index__", "__ine__", "__init__", |
| 83 | + "__int__", "__invert__", "__ior__", "__ipow__", |
| 84 | + "__irshift__", "__isub__", "__itruediv__", "__ixor__", |
| 85 | + "__le__", "__len__", "__lshift__", "__lt__", |
| 86 | + "__matmul__", "__mod__", "__mul__", "__ne__", |
| 87 | + "__neg__", "__not__", "__or__", "__pos__", |
| 88 | + "__pow__", "__radd__", "__rand__", "__repr__", |
| 89 | + "__rfloordiv__", "__rlshift__", "__rmatmul__", "__rmod__", |
| 90 | + "__rmul__", "__ror__", "__round__", "__round__", |
| 91 | + "__rpow__", "__rrshift__", "__rshift__", "__rsub__", |
| 92 | + "__rtruediv__", "__rxor__", "__setattr__", "__setitem__", |
| 93 | + "__str__", "__sub__", "__truediv__", "__xor__", |
| 94 | + }); |
| 95 | + |
| 96 | + return ( |
| 97 | + name.substr(0, 2) == "__" |
| 98 | + && name.substr(name.size() - 2) == "__" |
| 99 | + && canonicalMagicMethods.find(name) == canonicalMagicMethods.end() |
| 100 | + ); |
| 101 | +} |
0 commit comments