Python inspect.py() Examples
The following are 6
code examples of inspect.py().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
inspect
, or try the search function
.
Example #1
Source File: scalyr_logging.py From scalyr-agent-2 with Apache License 2.0 | 6 votes |
def set_log_level(level): """Sets the log level that should be used by all AgentLogger instances. This method is thread-safe. @param level: The level, in the logging units by the logging package, such as logging.INFO, logging.DEBUG, etc. You can also use one of the Scalyr debug levels, such as DEBUG_LEVEL_0, DEBUG_LEVEL_1, etc. @type level: int """ __log_manager__.set_log_level(level) # # _srcfile is used when walking the stack to check when we've got the first # caller stack frame. This is copied from the logging/__init__.py #
Example #2
Source File: PyforaInspect.py From ufora with Apache License 2.0 | 5 votes |
def getsourcefile(pyObject): """Return the filename that can be used to locate an object's source. Return None if no way can be identified to get the source. """ filename = getfile(pyObject) if filename == "<stdin>": return filename if string.lower(filename[-4:]) in ('.pyc', '.pyo'): filename = filename[:-4] + '.py' for suffix, mode, _ in imp.get_suffixes(): if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix: # Looks like a binary file. We want to only return a text file. return None if filename not in pathExistsOnDiskCache_: pathExistsOnDiskCache_[filename] = os.path.exists(filename) if pathExistsOnDiskCache_[filename]: return filename # only return a non-existent filename if the module has a PEP 302 loader if hasattr(getmodule(pyObject, filename), '__loader__'): return filename # or it is in the linecache if filename in linecache.cache: return filename
Example #3
Source File: scalyr_logging.py From scalyr-agent-2 with Apache License 2.0 | 5 votes |
def alternateCurrentFrame(): # noinspection PyProtectedMember return sys._getframe(3) # We set this variable to True after close_handlers() has been called (this happens when termination # handler function is called when shutting down the agent. # This way we can avoid "IOError: [Errno 0] Error" errors which may appear in stdout on agent # shutdown when using agent_main.py stop command which sends SIGTERM signal multiple times. # Those logs appeared if we try to log a message inside SIGTERM handler after all the log # handlers have already been closed.
Example #4
Source File: inspector.py From locality-sensitive-hashing with MIT License | 4 votes |
def tbsource(tb, context=6): """Get source from a traceback object. A tuple of two things is returned: a list of lines of context from the source code, and the index of the current line within that list. The optional second argument specifies the number of lines of context to return, which are centered around the current line. .. Note :: This is adapted from inspect.py in the python 2.4 standard library, since a bug in the 2.3 version of inspect prevents it from correctly locating source lines in a traceback frame. """ lineno = tb.tb_lineno frame = tb.tb_frame if context > 0: start = lineno - 1 - context//2 log.debug("lineno: %s start: %s", lineno, start) try: lines, dummy = inspect.findsource(frame) except IOError: lines, index = [''], 0 else: all_lines = lines start = max(start, 1) start = max(0, min(start, len(lines) - context)) lines = lines[start:start+context] index = lineno - 1 - start # python 2.5 compat: if previous line ends in a continuation, # decrement start by 1 to match 2.4 behavior if sys.version_info >= (2, 5) and index > 0: while lines[index-1].strip().endswith('\\'): start -= 1 lines = all_lines[start:start+context] else: lines, index = [''], 0 log.debug("tbsource lines '''%s''' around index %s", lines, index) return (lines, index)
Example #5
Source File: inspector.py From Computable with MIT License | 4 votes |
def tbsource(tb, context=6): """Get source from a traceback object. A tuple of two things is returned: a list of lines of context from the source code, and the index of the current line within that list. The optional second argument specifies the number of lines of context to return, which are centered around the current line. .. Note :: This is adapted from inspect.py in the python 2.4 standard library, since a bug in the 2.3 version of inspect prevents it from correctly locating source lines in a traceback frame. """ lineno = tb.tb_lineno frame = tb.tb_frame if context > 0: start = lineno - 1 - context//2 log.debug("lineno: %s start: %s", lineno, start) try: lines, dummy = inspect.findsource(frame) except IOError: lines, index = [''], 0 else: all_lines = lines start = max(start, 1) start = max(0, min(start, len(lines) - context)) lines = lines[start:start+context] index = lineno - 1 - start # python 2.5 compat: if previous line ends in a continuation, # decrement start by 1 to match 2.4 behavior if sys.version_info >= (2, 5) and index > 0: while lines[index-1].strip().endswith('\\'): start -= 1 lines = all_lines[start:start+context] else: lines, index = [''], 0 log.debug("tbsource lines '''%s''' around index %s", lines, index) return (lines, index)
Example #6
Source File: infer.py From torchlayers with MIT License | 4 votes |
def create_getattr(module) -> typing.Callable: """ Create __getattr__ of uninstantiated module. Will return values from `uninstantiated` network if exist, if not will check it's `instantiated` network (if it exists), otherwise return `NoAttributeError`. Can be considered proxy passing user calls `module` after instantiation. Parameters ---------- module : str Name of variable where instantiated module will be saved. Usually equal to global variable `MODULE` Returns ------- Callable __getattr__ function """ def __getattr__(self, name) -> str: if name == module: return super(type(self), self).__getattr__(name) return getattr(getattr(self, module), name) return __getattr__ # FIXED FOR PyTorch 1.4.0, 1.2.0 should work fine as well although it may throw warnings # For warning regarding inability to find source code # https://github.com/pytorch/pytorch/blob/master/torch/_utils_internal.py#L44 # getsourcefile # https://github.com/python/cpython/blob/master/Lib/inspect.py#L692 # getfile # https://github.com/python/cpython/blob/master/Lib/inspect.py#L654 # Simulate self.__module__.__file__ variable appropriately # getsourcelines # https://github.com/python/cpython/blob/master/Lib/inspect.py#L958