diff options
author | robot-contrib <robot-contrib@yandex-team.ru> | 2022-05-18 00:43:36 +0300 |
---|---|---|
committer | robot-contrib <robot-contrib@yandex-team.ru> | 2022-05-18 00:43:36 +0300 |
commit | 9e5f436a8b2a27bcc7802e443ea3ef3e41a82a75 (patch) | |
tree | 78b522cab9f76336e62064d4d8ff7c897659b20e /contrib/python/ipython/py3/IPython/extensions | |
parent | 8113a823ffca6451bb5ff8f0334560885a939a24 (diff) | |
download | ydb-9e5f436a8b2a27bcc7802e443ea3ef3e41a82a75.tar.gz |
Update contrib/python/ipython/py3 to 8.3.0
ref:e84342d4d30476f9148137f37fd0c6405fd36f55
Diffstat (limited to 'contrib/python/ipython/py3/IPython/extensions')
5 files changed, 151 insertions, 136 deletions
diff --git a/contrib/python/ipython/py3/IPython/extensions/autoreload.py b/contrib/python/ipython/py3/IPython/extensions/autoreload.py index ada680fcf0..816d2f35ea 100644 --- a/contrib/python/ipython/py3/IPython/extensions/autoreload.py +++ b/contrib/python/ipython/py3/IPython/extensions/autoreload.py @@ -48,6 +48,11 @@ The following magic commands are provided: Reload all modules (except those excluded by ``%aimport``) every time before executing the Python code typed. +``%autoreload 3`` + + Reload all modules AND autoload newly added objects + every time before executing the Python code typed. + ``%aimport`` List modules which are to be automatically imported or not to be imported. @@ -92,23 +97,23 @@ Some of the known remaining caveats are: - C extension modules cannot be reloaded, and so cannot be autoreloaded. """ -skip_doctest = True +__skip_doctest__ = True -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Copyright (C) 2000 Thomas Heller # Copyright (C) 2008 Pauli Virtanen <pav@iki.fi> # Copyright (C) 2012 The IPython Development Team # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # # This IPython module is written by Pauli Virtanen, based on the autoreload # code by Thomas Heller. -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Imports -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- import os import sys @@ -116,22 +121,25 @@ import traceback import types import weakref import gc -from importlib import import_module +from importlib import import_module, reload from importlib.util import source_from_cache -from imp import reload -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ # Autoreload functionality -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ -class ModuleReloader(object): + +class ModuleReloader: enabled = False """Whether this reloader is enabled""" check_all = True """Autoreload all modules, not just those listed in 'modules'""" - def __init__(self): + autoload_obj = False + """Autoreload all modules AND autoload all new objects""" + + def __init__(self, shell=None): # Modules that failed to reload: {module: mtime-on-failed-reload, ...} self.failed = {} # Modules specially marked as autoreloadable. @@ -142,6 +150,7 @@ class ModuleReloader(object): self.old_objects = {} # Module modification timestamps self.modules_mtimes = {} + self.shell = shell # Cache module modification times self.check(check_all=True, do_reload=False) @@ -176,22 +185,22 @@ class ModuleReloader(object): self.mark_module_reloadable(module_name) import_module(module_name) - top_name = module_name.split('.')[0] + top_name = module_name.split(".")[0] top_module = sys.modules[top_name] return top_module, top_name def filename_and_mtime(self, module): - if not hasattr(module, '__file__') or module.__file__ is None: + if not hasattr(module, "__file__") or module.__file__ is None: return None, None - if getattr(module, '__name__', None) in [None, '__mp_main__', '__main__']: + if getattr(module, "__name__", None) in [None, "__mp_main__", "__main__"]: # we cannot reload(__main__) or reload(__mp_main__) return None, None filename = module.__file__ path, ext = os.path.splitext(filename) - if ext.lower() == '.py': + if ext.lower() == ".py": py_filename = filename else: try: @@ -242,21 +251,35 @@ class ModuleReloader(object): # If we've reached this point, we should try to reload the module if do_reload: try: - superreload(m, reload, self.old_objects) + if self.autoload_obj: + superreload(m, reload, self.old_objects, self.shell) + else: + superreload(m, reload, self.old_objects) if py_filename in self.failed: del self.failed[py_filename] except: - print("[autoreload of %s failed: %s]" % ( - modname, traceback.format_exc(10)), file=sys.stderr) + print( + "[autoreload of {} failed: {}]".format( + modname, traceback.format_exc(10) + ), + file=sys.stderr, + ) self.failed[py_filename] = pymtime -#------------------------------------------------------------------------------ + +# ------------------------------------------------------------------------------ # superreload -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ -func_attrs = ['__code__', '__defaults__', '__doc__', - '__closure__', '__globals__', '__dict__'] +func_attrs = [ + "__code__", + "__defaults__", + "__doc__", + "__closure__", + "__globals__", + "__dict__", +] def update_function(old, new): @@ -272,7 +295,7 @@ def update_instances(old, new): """Use garbage collector to find all instances that refer to the old class definition and update their __class__ to point to the new class definition""" - + refs = gc.get_referrers(old) for ref in refs: @@ -298,20 +321,25 @@ def update_class(old, new): except (AttributeError, TypeError): pass continue + except ValueError: + # can't compare nested structures containing + # numpy arrays using `==` + pass - if update_generic(old_obj, new_obj): continue + if update_generic(old_obj, new_obj): + continue try: setattr(old, key, getattr(new, key)) except (AttributeError, TypeError): - pass # skip non-writable attributes + pass # skip non-writable attributes for key in list(new.__dict__.keys()): if key not in list(old.__dict__.keys()): try: setattr(old, key, getattr(new, key)) except (AttributeError, TypeError): - pass # skip non-writable attributes + pass # skip non-writable attributes # update all instances of class update_instances(old, new) @@ -329,16 +357,18 @@ def isinstance2(a, b, typ): UPDATE_RULES = [ - (lambda a, b: isinstance2(a, b, type), - update_class), - (lambda a, b: isinstance2(a, b, types.FunctionType), - update_function), - (lambda a, b: isinstance2(a, b, property), - update_property), + (lambda a, b: isinstance2(a, b, type), update_class), + (lambda a, b: isinstance2(a, b, types.FunctionType), update_function), + (lambda a, b: isinstance2(a, b, property), update_property), ] -UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType), - lambda a, b: update_function(a.__func__, b.__func__)), -]) +UPDATE_RULES.extend( + [ + ( + lambda a, b: isinstance2(a, b, types.MethodType), + lambda a, b: update_function(a.__func__, b.__func__), + ), + ] +) def update_generic(a, b): @@ -349,14 +379,45 @@ def update_generic(a, b): return False -class StrongRef(object): +class StrongRef: def __init__(self, obj): self.obj = obj + def __call__(self): return self.obj -def superreload(module, reload=reload, old_objects=None): +mod_attrs = [ + "__name__", + "__doc__", + "__package__", + "__loader__", + "__spec__", + "__file__", + "__cached__", + "__builtins__", +] + + +def append_obj(module, d, name, obj, autoload=False): + in_module = hasattr(obj, "__module__") and obj.__module__ == module.__name__ + if autoload: + # check needed for module global built-ins + if not in_module and name in mod_attrs: + return False + else: + if not in_module: + return False + + key = (module.__name__, name) + try: + d.setdefault(key, []).append(weakref.ref(obj)) + except TypeError: + pass + return True + + +def superreload(module, reload=reload, old_objects=None, shell=None): """Enhanced version of the builtin reload function. superreload remembers objects previously in the module, and @@ -371,7 +432,7 @@ def superreload(module, reload=reload, old_objects=None): # collect old objects in the module for name, obj in list(module.__dict__.items()): - if not hasattr(obj, '__module__') or obj.__module__ != module.__name__: + if not append_obj(module, old_objects, name, obj): continue key = (module.__name__, name) try: @@ -385,8 +446,8 @@ def superreload(module, reload=reload, old_objects=None): old_dict = module.__dict__.copy() old_name = module.__name__ module.__dict__.clear() - module.__dict__['__name__'] = old_name - module.__dict__['__loader__'] = old_dict['__loader__'] + module.__dict__["__name__"] = old_name + module.__dict__["__loader__"] = old_dict["__loader__"] except (TypeError, AttributeError, KeyError): pass @@ -400,12 +461,21 @@ def superreload(module, reload=reload, old_objects=None): # iterate over all objects and update functions & classes for name, new_obj in list(module.__dict__.items()): key = (module.__name__, name) - if key not in old_objects: continue + if key not in old_objects: + # here 'shell' acts both as a flag and as an output var + if ( + shell is None + or name == "Enum" + or not append_obj(module, old_objects, name, new_obj, True) + ): + continue + shell.user_ns[name] = new_obj new_refs = [] for old_ref in old_objects[key]: old_obj = old_ref() - if old_obj is None: continue + if old_obj is None: + continue new_refs.append(old_ref) update_generic(old_obj, new_obj) @@ -416,22 +486,25 @@ def superreload(module, reload=reload, old_objects=None): return module -#------------------------------------------------------------------------------ + +# ------------------------------------------------------------------------------ # IPython connectivity -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ from IPython.core.magic import Magics, magics_class, line_magic + @magics_class class AutoreloadMagics(Magics): def __init__(self, *a, **kw): - super(AutoreloadMagics, self).__init__(*a, **kw) - self._reloader = ModuleReloader() + super().__init__(*a, **kw) + self._reloader = ModuleReloader(self.shell) self._reloader.check_all = False + self._reloader.autoload_obj = False self.loaded_modules = set(sys.modules) @line_magic - def autoreload(self, parameter_s=''): + def autoreload(self, parameter_s=""): r"""%autoreload => Reload modules automatically %autoreload @@ -475,19 +548,24 @@ class AutoreloadMagics(Magics): autoreloaded. """ - if parameter_s == '': + if parameter_s == "": self._reloader.check(True) - elif parameter_s == '0': + elif parameter_s == "0": self._reloader.enabled = False - elif parameter_s == '1': + elif parameter_s == "1": self._reloader.check_all = False self._reloader.enabled = True - elif parameter_s == '2': + elif parameter_s == "2": + self._reloader.check_all = True + self._reloader.enabled = True + self._reloader.enabled = True + elif parameter_s == "3": self._reloader.check_all = True self._reloader.enabled = True + self._reloader.autoload_obj = True @line_magic - def aimport(self, parameter_s='', stream=None): + def aimport(self, parameter_s="", stream=None): """%aimport => Import modules for automatic reloading. %aimport @@ -511,13 +589,13 @@ class AutoreloadMagics(Magics): if self._reloader.check_all: stream.write("Modules to reload:\nall-except-skipped\n") else: - stream.write("Modules to reload:\n%s\n" % ' '.join(to_reload)) - stream.write("\nModules to skip:\n%s\n" % ' '.join(to_skip)) - elif modname.startswith('-'): + stream.write("Modules to reload:\n%s\n" % " ".join(to_reload)) + stream.write("\nModules to skip:\n%s\n" % " ".join(to_skip)) + elif modname.startswith("-"): modname = modname[1:] self._reloader.mark_module_skipped(modname) else: - for _module in ([_.strip() for _ in modname.split(',')]): + for _module in [_.strip() for _ in modname.split(",")]: top_module, top_name = self._reloader.aimport_module(_module) # Inject module to user namespace @@ -531,8 +609,7 @@ class AutoreloadMagics(Magics): pass def post_execute_hook(self): - """Cache the modification times of any modules imported in this execution - """ + """Cache the modification times of any modules imported in this execution""" newly_loaded_modules = set(sys.modules) - self.loaded_modules for modname in newly_loaded_modules: _, pymtime = self._reloader.filename_and_mtime(sys.modules[modname]) @@ -546,5 +623,5 @@ def load_ipython_extension(ip): """Load the extension in IPython.""" auto_reload = AutoreloadMagics(ip) ip.register_magics(auto_reload) - ip.events.register('pre_run_cell', auto_reload.pre_run_cell) - ip.events.register('post_execute', auto_reload.post_execute_hook) + ip.events.register("pre_run_cell", auto_reload.pre_run_cell) + ip.events.register("post_execute", auto_reload.post_execute_hook) diff --git a/contrib/python/ipython/py3/IPython/extensions/cythonmagic.py b/contrib/python/ipython/py3/IPython/extensions/cythonmagic.py deleted file mode 100644 index 3c88e7c2a1..0000000000 --- a/contrib/python/ipython/py3/IPython/extensions/cythonmagic.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -""" -**DEPRECATED** - -The cython magic has been integrated into Cython itself, -which is now released in version 0.21. - -cf github `Cython` organisation, `Cython` repo, under the -file `Cython/Build/IpythonMagic.py` -""" -#----------------------------------------------------------------------------- -# Copyright (C) 2010-2011, IPython Development Team. -#----------------------------------------------------------------------------- - -import warnings - -## still load the magic in IPython 3.x, remove completely in future versions. -def load_ipython_extension(ip): - """Load the extension in IPython.""" - - warnings.warn("""The Cython magic has been moved to the Cython package""") diff --git a/contrib/python/ipython/py3/IPython/extensions/rmagic.py b/contrib/python/ipython/py3/IPython/extensions/rmagic.py deleted file mode 100644 index ec5763972e..0000000000 --- a/contrib/python/ipython/py3/IPython/extensions/rmagic.py +++ /dev/null @@ -1,12 +0,0 @@ -# -*- coding: utf-8 -*- - -#----------------------------------------------------------------------------- -# Copyright (C) 2012 The IPython Development Team -#----------------------------------------------------------------------------- - -import warnings - -def load_ipython_extension(ip): - """Load the extension in IPython.""" - warnings.warn("The rmagic extension in IPython has moved to " - "`rpy2.ipython`, please see `rpy2` documentation.") diff --git a/contrib/python/ipython/py3/IPython/extensions/storemagic.py b/contrib/python/ipython/py3/IPython/extensions/storemagic.py index 51b79ad314..d9d00f14b9 100644 --- a/contrib/python/ipython/py3/IPython/extensions/storemagic.py +++ b/contrib/python/ipython/py3/IPython/extensions/storemagic.py @@ -17,6 +17,7 @@ import inspect, os, sys, textwrap from IPython.core.error import UsageError from IPython.core.magic import Magics, magics_class, line_magic +from IPython.testing.skipdoctest import skip_doctest from traitlets import Bool @@ -74,6 +75,7 @@ class StoreMagics(Magics): if self.autorestore: restore_data(self.shell) + @skip_doctest @line_magic def store(self, parameter_s=''): """Lightweight persistence for python variables. @@ -82,6 +84,7 @@ class StoreMagics(Magics): In [1]: l = ['hello',10,'world'] In [2]: %store l + Stored 'l' (list) In [3]: exit (IPython session is closed and started again...) @@ -126,13 +129,13 @@ class StoreMagics(Magics): if 'd' in opts: try: todel = args[0] - except IndexError: - raise UsageError('You must provide the variable to forget') + except IndexError as e: + raise UsageError('You must provide the variable to forget') from e else: try: del db['autorestore/' + todel] - except: - raise UsageError("Can't delete variable '%s'" % todel) + except BaseException as e: + raise UsageError("Can't delete variable '%s'" % todel) from e # reset elif 'z' in opts: for k in db.keys('autorestore/*'): @@ -173,12 +176,12 @@ class StoreMagics(Magics): # default action - store the variable else: # %store foo >file.txt or >>file.txt - if len(args) > 1 and args[1].startswith('>'): - fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) - if args[1].startswith('>>'): - fil = open(fnam, 'a') + if len(args) > 1 and args[1].startswith(">"): + fnam = os.path.expanduser(args[1].lstrip(">").lstrip()) + if args[1].startswith(">>"): + fil = open(fnam, "a", encoding="utf-8") else: - fil = open(fnam, 'w') + fil = open(fnam, "w", encoding="utf-8") with fil: obj = ip.ev(args[0]) print("Writing '%s' (%s) to file '%s'." % (args[0], @@ -203,8 +206,8 @@ class StoreMagics(Magics): name = arg try: cmd = ip.alias_manager.retrieve_alias(name) - except ValueError: - raise UsageError("Unknown variable '%s'" % name) + except ValueError as e: + raise UsageError("Unknown variable '%s'" % name) from e staliases = db.get('stored_aliases',{}) staliases[name] = cmd diff --git a/contrib/python/ipython/py3/IPython/extensions/sympyprinting.py b/contrib/python/ipython/py3/IPython/extensions/sympyprinting.py deleted file mode 100644 index e6a83cd34b..0000000000 --- a/contrib/python/ipython/py3/IPython/extensions/sympyprinting.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -**DEPRECATED** - -A print function that pretty prints sympy Basic objects. - -:moduleauthor: Brian Granger - -Usage -===== - -Once the extension is loaded, Sympy Basic objects are automatically -pretty-printed. - -As of SymPy 0.7.2, maintenance of this extension has moved to SymPy under -sympy.interactive.ipythonprinting, any modifications to account for changes to -SymPy should be submitted to SymPy rather than changed here. This module is -maintained here for backwards compatibility with old SymPy versions. - -""" -#----------------------------------------------------------------------------- -# Copyright (C) 2008 The IPython Development Team -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- - -import warnings - -def load_ipython_extension(ip): - warnings.warn("The sympyprinting extension has moved to `sympy`, " - "use `from sympy import init_printing; init_printing()`") |