diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-12-18 11:04:10 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-12-18 11:18:13 +0300 |
commit | dcbf444872b81248ce958f05d47abad6e8a237a7 (patch) | |
tree | a93610b3dd80f19c5116e4e5885ea2783f6166d3 /contrib/python/ipython/py3/IPython/core | |
parent | e2c38d5aa55a58da33c1dc54792c131023bb7472 (diff) | |
download | ydb-dcbf444872b81248ce958f05d47abad6e8a237a7.tar.gz |
Intermediate changes
commit_hash:1f2ebe313aea1039145a9d68dcd511d5f22f383a
Diffstat (limited to 'contrib/python/ipython/py3/IPython/core')
11 files changed, 154 insertions, 77 deletions
diff --git a/contrib/python/ipython/py3/IPython/core/completer.py b/contrib/python/ipython/py3/IPython/core/completer.py index 8f843dd584..8260975765 100644 --- a/contrib/python/ipython/py3/IPython/core/completer.py +++ b/contrib/python/ipython/py3/IPython/core/completer.py @@ -1362,9 +1362,9 @@ def match_dict_keys( # All checks passed! return True - filtered_key_is_final: Dict[ - Union[str, bytes, int, float], _DictKeyState - ] = defaultdict(lambda: _DictKeyState.BASELINE) + filtered_key_is_final: Dict[Union[str, bytes, int, float], _DictKeyState] = ( + defaultdict(lambda: _DictKeyState.BASELINE) + ) for k in keys: # If at least one of the matches is not final, mark as undetermined. diff --git a/contrib/python/ipython/py3/IPython/core/crashhandler.py b/contrib/python/ipython/py3/IPython/core/crashhandler.py index f60a75bbc5..9c75c5d87c 100644 --- a/contrib/python/ipython/py3/IPython/core/crashhandler.py +++ b/contrib/python/ipython/py3/IPython/core/crashhandler.py @@ -24,14 +24,17 @@ import traceback from pprint import pformat from pathlib import Path +import builtins as builtin_mod + from IPython.core import ultratb +from IPython.core.application import Application from IPython.core.release import author_email from IPython.utils.sysinfo import sys_info -from IPython.utils.py3compat import input from IPython.core.release import __version__ as version -from typing import Optional +from typing import Optional, Dict +import types #----------------------------------------------------------------------------- # Code @@ -84,7 +87,7 @@ Extra-detailed tracebacks for bug-reporting purposes can be enabled via: """ -class CrashHandler(object): +class CrashHandler: """Customizable crash handlers for IPython applications. Instances of this class provide a :meth:`__call__` method which can be @@ -95,10 +98,11 @@ class CrashHandler(object): message_template = _default_message_template section_sep = '\n\n'+'*'*75+'\n\n' + info: Dict[str, Optional[str]] def __init__( self, - app, + app: Application, contact_name: Optional[str] = None, contact_email: Optional[str] = None, bug_tracker: Optional[str] = None, @@ -142,10 +146,14 @@ class CrashHandler(object): bug_tracker = bug_tracker, crash_report_fname = self.crash_report_fname) - - def __call__(self, etype, evalue, etb): + def __call__( + self, + etype: type[BaseException], + evalue: BaseException, + etb: types.TracebackType, + ) -> None: """Handle an exception, call for compatible with sys.excepthook""" - + # do not allow the crash handler to be called twice without reinstalling it # this prevents unlikely errors in the crash handling from entering an # infinite loop. @@ -155,21 +163,23 @@ class CrashHandler(object): color_scheme = 'NoColor' # Use this ONLY for developer debugging (keep commented out for release) - #color_scheme = 'Linux' # dbg - try: - rptdir = self.app.ipython_dir - except: + # color_scheme = 'Linux' # dbg + ipython_dir = getattr(self.app, "ipython_dir", None) + if ipython_dir is not None: + assert isinstance(ipython_dir, str) + rptdir = Path(ipython_dir) + else: rptdir = Path.cwd() - if rptdir is None or not Path.is_dir(rptdir): + if not rptdir.is_dir(): rptdir = Path.cwd() report_name = rptdir / self.crash_report_fname # write the report filename into the instance dict so it can get # properly expanded out in the user message template - self.crash_report_fname = report_name - self.info['crash_report_fname'] = report_name + self.crash_report_fname = str(report_name) + self.info["crash_report_fname"] = str(report_name) TBhandler = ultratb.VerboseTB( color_scheme=color_scheme, - long_header=1, + long_header=True, call_pdb=self.call_pdb, ) if self.call_pdb: @@ -195,11 +205,11 @@ class CrashHandler(object): print(self.message_template.format(**self.info), file=sys.stderr) # Construct report on disk - report.write(self.make_report(traceback)) + report.write(self.make_report(str(traceback))) - input("Hit <Enter> to quit (your terminal may close):") + builtin_mod.input("Hit <Enter> to quit (your terminal may close):") - def make_report(self,traceback): + def make_report(self, traceback: str) -> str: """Return a string containing a crash report.""" sec_sep = self.section_sep @@ -211,8 +221,8 @@ class CrashHandler(object): try: config = pformat(self.app.config) rpt_add(sec_sep) - rpt_add('Application name: %s\n\n' % self.app_name) - rpt_add('Current user configuration structure:\n\n') + rpt_add("Application name: %s\n\n" % self.app.name) + rpt_add("Current user configuration structure:\n\n") rpt_add(config) except: pass @@ -221,7 +231,9 @@ class CrashHandler(object): return ''.join(report) -def crash_handler_lite(etype, evalue, tb): +def crash_handler_lite( + etype: type[BaseException], evalue: BaseException, tb: types.TracebackType +) -> None: """a light excepthook, adding a small message to the usual traceback""" traceback.print_exception(etype, evalue, tb) diff --git a/contrib/python/ipython/py3/IPython/core/display.py b/contrib/python/ipython/py3/IPython/core/display.py index c3c44016f4..a9434473a8 100644 --- a/contrib/python/ipython/py3/IPython/core/display.py +++ b/contrib/python/ipython/py3/IPython/core/display.py @@ -16,7 +16,8 @@ from copy import deepcopy from os.path import splitext from pathlib import Path, PurePath -from IPython.utils.py3compat import cast_unicode +from typing import Optional + from IPython.testing.skipdoctest import skip_doctest from . import display_functions @@ -518,7 +519,7 @@ class SVG(DisplayObject): _read_flags = 'rb' # wrap data in a property, which extracts the <svg> tag, discarding # document headers - _data = None + _data: Optional[str] = None @property def data(self): @@ -540,8 +541,10 @@ class SVG(DisplayObject): # fallback on the input, trust the user # but this is probably an error. pass - svg = cast_unicode(svg) - self._data = svg + if isinstance(svg, bytes): + self._data = svg.decode(errors="replace") + else: + self._data = svg def _repr_svg_(self): return self._data_and_metadata() diff --git a/contrib/python/ipython/py3/IPython/core/formatters.py b/contrib/python/ipython/py3/IPython/core/formatters.py index 9e59e23de1..dcd82bdc5c 100644 --- a/contrib/python/ipython/py3/IPython/core/formatters.py +++ b/contrib/python/ipython/py3/IPython/core/formatters.py @@ -1,6 +1,61 @@ # -*- coding: utf-8 -*- """Display formatters. +This module defines the base instances in order to implement custom +formatters/mimetypes +got objects: + +As we want to see internal IPython working we are going to use the following +function to diaply objects instead of the normal print or display method: + + >>> ip = get_ipython() + >>> ip.display_formatter.format(...) + ({'text/plain': 'Ellipsis'}, {}) + +This return a tuple with the mimebumdle for the current object, and the +associated metadata. + + +We can now define our own formatter and register it: + + + >>> from IPython.core.formatters import BaseFormatter, FormatterABC + + + >>> class LLMFormatter(BaseFormatter): + ... + ... format_type = 'x-vendor/llm' + ... print_method = '_repr_llm_' + ... _return_type = (dict, str) + + >>> llm_formatter = LLMFormatter(parent=ip.display_formatter) + + >>> ip.display_formatter.formatters[LLMFormatter.format_type] = llm_formatter + +Now any class that define `_repr_llm_` will return a x-vendor/llm as part of +it's display data: + + >>> class A: + ... + ... def _repr_llm_(self, *kwargs): + ... return 'This a A' + ... + + >>> ip.display_formatter.format(A()) + ({'text/plain': '<IPython.core.formatters.A at ...>', 'x-vendor/llm': 'This a A'}, {}) + +As usual, you can register methods for third party types (see +:ref:`third_party_formatting`) + + >>> def llm_int(obj): + ... return 'This is the integer %s, in between %s and %s'%(obj, obj-1, obj+1) + + >>> llm_formatter.for_type(int, llm_int) + + >>> ip.display_formatter.format(42) + ({'text/plain': '42', 'x-vendor/llm': 'This is the integer 42, in between 41 and 43'}, {}) + + Inheritance diagram: .. inheritance-diagram:: IPython.core.formatters @@ -37,9 +92,10 @@ class DisplayFormatter(Configurable): active_types = List(Unicode(), help="""List of currently active mime-types to display. You can use this to set a white-list for formats to display. - + Most users will not need to change this value. - """).tag(config=True) + """, + ).tag(config=True) @default('active_types') def _active_types_default(self): @@ -144,7 +200,7 @@ class DisplayFormatter(Configurable): """ format_dict = {} md_dict = {} - + if self.ipython_display_formatter(obj): # object handled itself, don't proceed return {}, {} @@ -176,18 +232,18 @@ class DisplayFormatter(Configurable): continue if exclude and format_type in exclude: continue - + md = None try: data = formatter(obj) except: # FIXME: log the exception raise - + # formatters can return raw data or (data, metadata) if isinstance(data, tuple) and len(data) == 2: data, md = data - + if data is not None: format_dict[format_type] = data if md is not None: @@ -252,7 +308,7 @@ class FormatterABC(metaclass=abc.ABCMeta): # Is the formatter enabled... enabled = True - + @abc.abstractmethod def __call__(self, obj): """Return a JSON'able representation of the object. @@ -278,12 +334,15 @@ def _get_type(obj): return getattr(obj, '__class__', None) or type(obj) -_raise_key_error = Sentinel('_raise_key_error', __name__, -""" +_raise_key_error = Sentinel( + "_raise_key_error", + __name__, + """ Special value to raise a KeyError Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop` -""") +""", +) class BaseFormatter(Configurable): @@ -329,7 +388,7 @@ class BaseFormatter(Configurable): # The deferred-import type-specific printers. # Map (modulename, classname) pairs to the format functions. deferred_printers = Dict().tag(config=True) - + @catch_format_error def __call__(self, obj): """Compute the format for an object.""" @@ -348,7 +407,7 @@ class BaseFormatter(Configurable): return None else: return None - + def __contains__(self, typ): """map in to lookup_by_type""" try: @@ -357,7 +416,7 @@ class BaseFormatter(Configurable): return False else: return True - + def _check_return(self, r, obj): """Check that a return value is appropriate @@ -372,7 +431,7 @@ class BaseFormatter(Configurable): (self.format_type, type(r), self._return_type, _safe_repr(obj)), FormatterWarning ) - + def lookup(self, obj): """Look up the formatter for a given instance. @@ -395,7 +454,7 @@ class BaseFormatter(Configurable): return self.singleton_printers[obj_id] # then lookup by type return self.lookup_by_type(_get_type(obj)) - + def lookup_by_type(self, typ): """Look up the registered formatter for a type. @@ -426,7 +485,7 @@ class BaseFormatter(Configurable): for cls in pretty._get_mro(typ): if cls in self.type_printers or self._in_deferred_types(cls): return self.type_printers[cls] - + # If we have reached here, the lookup failed. raise KeyError("No registered printer for {0!r}".format(typ)) @@ -459,15 +518,15 @@ class BaseFormatter(Configurable): if isinstance(typ, str): type_module, type_name = typ.rsplit('.', 1) return self.for_type_by_name(type_module, type_name, func) - + try: oldfunc = self.lookup_by_type(typ) except KeyError: oldfunc = None - + if func is not None: self.type_printers[typ] = func - + return oldfunc def for_type_by_name(self, type_module, type_name, func=None): @@ -501,16 +560,16 @@ class BaseFormatter(Configurable): this will be the previous value (to enable restoring later). """ key = (type_module, type_name) - + try: oldfunc = self.lookup_by_type("%s.%s" % key) except KeyError: oldfunc = None - + if func is not None: self.deferred_printers[key] = func return oldfunc - + def pop(self, typ, default=_raise_key_error): """Pop a formatter for the given type. @@ -529,7 +588,7 @@ class BaseFormatter(Configurable): ------ KeyError if the type is not registered and default is not specified. """ - + if isinstance(typ, str): typ_key = tuple(typ.rsplit('.',1)) if typ_key not in self.deferred_printers: @@ -599,14 +658,14 @@ class PlainTextFormatter(BaseFormatter): # This subclass ignores this attribute as it always need to return # something. enabled = Bool(True).tag(config=False) - + max_seq_length = Integer(pretty.MAX_SEQ_LENGTH, help="""Truncate large collections (lists, dicts, tuples, sets) to this size. - + Set to 0 to disable truncation. - """ + """, ).tag(config=True) - + # Look for a _repr_pretty_ methods to use for pretty printing. print_method = ObjectName('_repr_pretty_') @@ -775,7 +834,7 @@ class PNGFormatter(BaseFormatter): format_type = Unicode('image/png') print_method = ObjectName('_repr_png_') - + _return_type = (bytes, str) @@ -829,7 +888,7 @@ class JSONFormatter(BaseFormatter): _return_type = (list, dict) print_method = ObjectName('_repr_json_') - + def _check_return(self, r, obj): """Check that a return value is appropriate @@ -887,19 +946,19 @@ class PDFFormatter(BaseFormatter): class IPythonDisplayFormatter(BaseFormatter): """An escape-hatch Formatter for objects that know how to display themselves. - + To define the callables that compute the representation of your objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` or :meth:`for_type_by_name` methods to register functions that handle this. Unlike mime-type displays, this method should not return anything, instead calling any appropriate display methods itself. - + This display formatter has highest priority. If it fires, no other display formatter will be called. Prior to IPython 6.1, `_ipython_display_` was the only way to display custom mime-types without registering a new Formatter. - + IPython 6.1 introduces `_repr_mimebundle_` for displaying custom mime-types, so `_ipython_display_` should only be used for objects that require unusual display patterns, such as multiple display calls. @@ -943,7 +1002,7 @@ class MimeBundleFormatter(BaseFormatter): """ print_method = ObjectName('_repr_mimebundle_') _return_type = dict - + def _check_return(self, r, obj): r = super(MimeBundleFormatter, self)._check_return(r, obj) # always return (data, metadata): diff --git a/contrib/python/ipython/py3/IPython/core/guarded_eval.py b/contrib/python/ipython/py3/IPython/core/guarded_eval.py index 39fe853f58..628fe2fbcc 100644 --- a/contrib/python/ipython/py3/IPython/core/guarded_eval.py +++ b/contrib/python/ipython/py3/IPython/core/guarded_eval.py @@ -44,20 +44,17 @@ else: @undoc class HasGetItem(Protocol): - def __getitem__(self, key) -> None: - ... + def __getitem__(self, key) -> None: ... @undoc class InstancesHaveGetItem(Protocol): - def __call__(self, *args, **kwargs) -> HasGetItem: - ... + def __call__(self, *args, **kwargs) -> HasGetItem: ... @undoc class HasGetAttr(Protocol): - def __getattr__(self, key) -> None: - ... + def __getattr__(self, key) -> None: ... @undoc @@ -329,9 +326,9 @@ class EvaluationContext(NamedTuple): #: Global namespace globals: dict #: Evaluation policy identifier - evaluation: Literal[ - "forbidden", "minimal", "limited", "unsafe", "dangerous" - ] = "forbidden" + evaluation: Literal["forbidden", "minimal", "limited", "unsafe", "dangerous"] = ( + "forbidden" + ) #: Whether the evaluation of code takes place inside of a subscript. #: Useful for evaluating ``:-1, 'col'`` in ``df[:-1, 'col']``. in_subscript: bool = False diff --git a/contrib/python/ipython/py3/IPython/core/inputsplitter.py b/contrib/python/ipython/py3/IPython/core/inputsplitter.py index 092f21408a..b20dfb16ca 100644 --- a/contrib/python/ipython/py3/IPython/core/inputsplitter.py +++ b/contrib/python/ipython/py3/IPython/core/inputsplitter.py @@ -15,6 +15,7 @@ and stores the results. For more details, see the class docstrings below. """ + from __future__ import annotations from warnings import warn diff --git a/contrib/python/ipython/py3/IPython/core/inputtransformer2.py b/contrib/python/ipython/py3/IPython/core/inputtransformer2.py index 949cf383e2..15d3f0630a 100644 --- a/contrib/python/ipython/py3/IPython/core/inputtransformer2.py +++ b/contrib/python/ipython/py3/IPython/core/inputtransformer2.py @@ -395,7 +395,10 @@ def _tr_quote2(content): def _tr_paren(content): "Translate lines escaped with a slash: /" - name, _, args = content.partition(' ') + name, _, args = content.partition(" ") + if name == "": + raise SyntaxError(f'"{ESC_SHELL}" must be followed by a callable name') + return '%s(%s)' % (name, ", ".join(args.split())) tr = { ESC_SHELL : 'get_ipython().system({!r})'.format, diff --git a/contrib/python/ipython/py3/IPython/core/magics/basic.py b/contrib/python/ipython/py3/IPython/core/magics/basic.py index 3e3eb2d3f8..11b569328a 100644 --- a/contrib/python/ipython/py3/IPython/core/magics/basic.py +++ b/contrib/python/ipython/py3/IPython/core/magics/basic.py @@ -461,7 +461,7 @@ Currently the magic system has the following functions:""", ptformatter.pprint = False disp_formatter.active_types = ['text/plain'] - shell.magic('xmode Plain') + shell.run_line_magic("xmode", "Plain") else: # turn off shell.separate_in = dstore.rc_separate_in @@ -472,7 +472,7 @@ Currently the magic system has the following functions:""", ptformatter.pprint = dstore.rc_pprint disp_formatter.active_types = dstore.rc_active_types - shell.magic('xmode ' + dstore.xmode) + shell.run_line_magic("xmode", dstore.xmode) # mode here is the state before we switch; switch_doctest_mode takes # the mode we're switching to. diff --git a/contrib/python/ipython/py3/IPython/core/magics/logging.py b/contrib/python/ipython/py3/IPython/core/magics/logging.py index b6b8d8a5af..81b167545c 100644 --- a/contrib/python/ipython/py3/IPython/core/magics/logging.py +++ b/contrib/python/ipython/py3/IPython/core/magics/logging.py @@ -83,14 +83,14 @@ class LoggingMagics(Magics): log 'raw' input. Normally, IPython's logs contain the processed input, so that user lines are logged in their final form, converted into valid Python. For example, %Exit is logged as - _ip.magic("Exit"). If the -r flag is given, all input is logged + _ip.run_line_magic("Exit"). If the -r flag is given, all input is logged exactly as typed, with no transformations applied. -t put timestamps before each input line logged (these are put in comments). - -q + -q suppress output of logstate message when logging is invoked """ diff --git a/contrib/python/ipython/py3/IPython/core/prompts.py b/contrib/python/ipython/py3/IPython/core/prompts.py index 7fd218d37a..105ff17f75 100644 --- a/contrib/python/ipython/py3/IPython/core/prompts.py +++ b/contrib/python/ipython/py3/IPython/core/prompts.py @@ -2,9 +2,11 @@ """Being removed """ + class LazyEvaluate(object): """This is used for formatting strings with values that need to be updated at that time, such as the current time or working directory.""" + def __init__(self, func, *args, **kwargs): self.func = func self.args = args @@ -16,6 +18,6 @@ class LazyEvaluate(object): def __str__(self): return str(self()) - + def __format__(self, format_spec): return format(self(), format_spec) diff --git a/contrib/python/ipython/py3/IPython/core/release.py b/contrib/python/ipython/py3/IPython/core/release.py index d9eb28aa95..702e037a24 100644 --- a/contrib/python/ipython/py3/IPython/core/release.py +++ b/contrib/python/ipython/py3/IPython/core/release.py @@ -16,7 +16,7 @@ # release. 'dev' as a _version_extra string means this is a development # version _version_major = 8 -_version_minor = 29 +_version_minor = 30 _version_patch = 0 _version_extra = ".dev" # _version_extra = "rc1" |