Exemple #1
0
 def test_context_new_1(self):
     with self.assertRaisesRegex(TypeError, 'any arguments'):
         contextvars.Context(1)
     with self.assertRaisesRegex(TypeError, 'any arguments'):
         contextvars.Context(1, a=1)
     with self.assertRaisesRegex(TypeError, 'any arguments'):
         contextvars.Context(a=1)
     contextvars.Context(**{})
Exemple #2
0
 def test_context_new_1(self):
     with self.assertRaises(TypeError):
         contextvars.Context(1)
     with self.assertRaises(TypeError):
         contextvars.Context(1, a=1)
     with self.assertRaises(TypeError):
         contextvars.Context(a=1)
     contextvars.Context(**{})
Exemple #3
0
    def _ensure_updating(self) -> None:
        """Ensure that we are updating the job list from the remote resource.

        This will automatically stop if there are no outstanding requests.
        """

        async def updating():
            """Do the actual update, stop if not requests left."""
            await self._update_job_info()
            # Any outstanding requests?
            if self._update_requests_outstanding():
                self._update_handle = self._loop.call_later(
                    self._get_next_update_delay(),
                    asyncio.ensure_future,
                    updating(),
                    context=contextvars.Context(),  #  type: ignore[call-arg]
                )
            else:
                self._update_handle = None

        # Check if we're already updating
        if self._update_handle is None:
            self._update_handle = self._loop.call_later(
                self._get_next_update_delay(),
                asyncio.ensure_future,
                updating(),
                context=contextvars.Context(),  #  type: ignore[call-arg]
            )
Exemple #4
0
    def identify_indexes(self, di):
        di = list(self.expand_ellipsis(di))
        a2s = []
        contexts = []

        ta = self.transformation_axes
        if ta == None:
            ta = set(range(len(self.wrapped.axes)))

        for i in range(len(self.wrapped.axes)):
            if isinstance(di[i], Number):
                #wrap the scalar indexes, this is needed to make sure samplers work on the right axis
                di[i] = np.array([di[i]])
                a2s.append(i)

        for i in ta:
            if i < 0:
                i = len(self.wrapped.axes) - i
            ctx = contextvars.Context()
            contexts.append((i, ctx))
            di[i] = ctx.run(self.axis_identify_indexes, di[i], i)

        axes_to_squeeze.set(tuple(a2s))
        axis_contexts.set(contexts)
        return tuple(di)
Exemple #5
0
 def _get_context():
     state = _get_state()
     ctx = getattr(state, "context", None)
     if ctx is None:
         ctx = contextvars.Context()
         state.context = ctx
     return ctx
Exemple #6
0
    def test_context_run_2(self):
        ctx = contextvars.Context()

        def func(*args, **kwargs):
            kwargs['spam'] = 'foo'
            args += ('bar', )
            return args, kwargs

        for f in (func, functools.partial(func)):
            # partial doesn't support FASTCALL

            self.assertEqual(ctx.run(f), (('bar', ), {'spam': 'foo'}))
            self.assertEqual(ctx.run(f, 1), ((1, 'bar'), {'spam': 'foo'}))

            self.assertEqual(ctx.run(f, a=2), (('bar', ), {
                'a': 2,
                'spam': 'foo'
            }))

            self.assertEqual(ctx.run(f, 11, a=2), ((11, 'bar'), {
                'a': 2,
                'spam': 'foo'
            }))

            a = {}
            self.assertEqual(ctx.run(f, 11, **a), ((11, 'bar'), {
                'spam': 'foo'
            }))
            self.assertEqual(a, {})
Exemple #7
0
def ydumps(obj, sink=None, trait_help=None, classes=()) -> Optional[str]:
    """
    Dump any false objects as empty string, None as nothing, or as YAML.

    :param classes:
        The list of other classes to be YAMLed, to consider for redundancy.
        Will return `cls` even if it is not defined on `cls`
        if the defining class is not in `classes`.

        See also: :meth:`~Configurable._defining_class()`
    """

    if not obj:
        if sink:
            sink.write('')
            return  # type: ignore
        return ''

    def dump_with_contextvars():
        if trait_help is not None:
            _dump_trait_help.set(trait_help)
        _classes_yamling.set(classes)

        get_yamel().dump(obj, sink)

    dump_to_str = not bool(sink)
    if dump_to_str:
        sink = io.StringIO()

    cv.Context().run(dump_with_contextvars)

    if dump_to_str:
        return sink.getvalue().strip()
Exemple #8
0
def render_form(giveaway_view, prize_view, field_state):
    def build_state():
        fields.set(field_state)
        giveaway_id.set(f"{UUID(bytes=giveaway_view[0])}")
        title.set("\u00A0".join(giveaway_view[1].split()))

        prizes.set([
            prize_toggle(
                id="giveaway-prize:id",
                value=f"{UUID(bytes=id)}",
                title=title,
                theme=theme,
                image_src=image_src,
                featured="featured" if int(featured) == 1 else "",
            ) for title, image_src, theme, id, _giveaway_id, _quantity,
            featured in prize_view
        ])

    ctx = contextvars.Context()
    ctx.run(build_state)

    return ctx.run(lambda: html.tostring(document("Giveaway Registration: ",
                                                  *giveaway_form()),
                                         pretty_print=True,
                                         doctype="<!doctype html>"))
Exemple #9
0
    def test_context_run_7(self):
        ctx = contextvars.Context()

        def fun():
            with self.assertRaisesRegex(RuntimeError, 'is already entered'):
                ctx.run(fun)

        ctx.run(fun)
Exemple #10
0
    def test_context_getset_4(self):
        c = contextvars.ContextVar('c', default=42)
        ctx = contextvars.Context()

        tok = ctx.run(c.set, 1)

        with self.assertRaisesRegex(ValueError, 'different Context'):
            c.reset(tok)
Exemple #11
0
    def test_context_typerrors_1(self):
        ctx = contextvars.Context()

        with self.assertRaisesRegex(TypeError, 'ContextVar key was expected'):
            ctx[1]
        with self.assertRaisesRegex(TypeError, 'ContextVar key was expected'):
            1 in ctx
        with self.assertRaisesRegex(TypeError, 'ContextVar key was expected'):
            ctx.get(1)
Exemple #12
0
    def test_context_run_3(self):
        ctx = contextvars.Context()

        def func(*args, **kwargs):
            1 / 0

        with self.assertRaises(ZeroDivisionError):
            ctx.run(func)
        with self.assertRaises(ZeroDivisionError):
            ctx.run(func, 1, 2)
        with self.assertRaises(ZeroDivisionError):
            ctx.run(func, 1, 2, a=123)
Exemple #13
0
    def __start_span(self, span):
        if span.id in self.__span_by_id:
            LOGGER.debug(
                "Duplicate span with lifetime 'Start' "
                "with id '%s'", span.id)
            return
        kwargs = {
            "operation_name":
            span.symbolic_name,
            "start_time":
            span.timestamp.timestamp(),
            "ignore_active_span": (self.__ignore_active_span
                                   and self.__is_top(span)),
        }
        if not span.is_top:
            try:
                parent_span, parent_ot_span, is_stripped = self.__span_by_id[
                    span.parent_id]
            except KeyError:
                LOGGER.warning("Parent span with id '%s' is not found.",
                               span.parent_id)
                return
            kwargs["child_of"] = parent_ot_span
            LOGGER.debug(
                "%s is child of %s",
                span.symbolic_name,
                parent_span.symbolic_name,
            )
        else:
            LOGGER.debug("Top span: %s", span.symbolic_name)
        kwargs["tags"] = {
            "hostname": span.hostname,
            "process_id": span.process_id,
            "thread_id": span.thread_id,
            "file_name": span.file_name,
            "line_number": span.line_number,
            "is_coroutine": span.is_coroutine,
            "is_generator": span.is_generator,
        }
        context = self.__context_by_pid_tid.setdefault(
            (span.process_id, span.thread_id), contextvars.Context())
        should_strip = self.__should_strip(span)
        ot_span = None
        if not should_strip:
            ot_span = context.run(self.__tracer.start_span, **kwargs)

        self.__span_by_id[span.id] = (span, ot_span, should_strip)
        LOGGER.debug(
            "Started span with id '%s' and name '%s'%s.",
            span.id,
            span.symbolic_name,
            " as stripped" if should_strip else "",
        )
Exemple #14
0
 async def updating():
     """Do the actual update, stop if not requests left."""
     await self._update_job_info()
     # Any outstanding requests?
     if self._update_requests_outstanding():
         self._update_handle = self._loop.call_later(
             self._get_next_update_delay(),
             asyncio.ensure_future,
             updating(),
             context=contextvars.Context(),  #  type: ignore[call-arg]
         )
     else:
         self._update_handle = None
Exemple #15
0
    def test_context_run_6(self):
        ctx = contextvars.Context()
        c = contextvars.ContextVar('a', default=0)

        def fun():
            self.assertEqual(c.get(), 0)
            self.assertIsNone(ctx.get(c))

            c.set(42)
            self.assertEqual(c.get(), 42)
            self.assertEqual(ctx.get(c), 42)

        ctx.run(fun)
Exemple #16
0
    def test_context_run_5(self):
        ctx = contextvars.Context()
        var = contextvars.ContextVar('var')

        def func():
            self.assertIsNone(var.get(None))
            var.set('spam')
            1 / 0

        with self.assertRaises(ZeroDivisionError):
            ctx.run(func)

        self.assertIsNone(var.get(None))
Exemple #17
0
    def test_context_run_4(self):
        ctx1 = contextvars.Context()
        ctx2 = contextvars.Context()
        var = contextvars.ContextVar('var')

        def func2():
            self.assertIsNone(var.get(None))

        def func1():
            self.assertIsNone(var.get(None))
            var.set('spam')
            ctx2.run(func2)
            self.assertEqual(var.get(None), 'spam')

            cur = contextvars.copy_context()
            self.assertEqual(len(cur), 1)
            self.assertEqual(cur[var], 'spam')
            return cur

        returned_ctx = ctx1.run(func1)
        self.assertEqual(ctx1, returned_ctx)
        self.assertEqual(returned_ctx[var], 'spam')
        self.assertIn(var, returned_ctx)
Exemple #18
0
def render_status(status,
                  message,
                  giveaway_title,
                  registration_id=None,
                  **kwargs):
    def build_state():
        title.set("\u00A0".join(giveaway_title.split()))

    ctx = contextvars.Context()
    ctx.run(build_state)

    return ctx.run(lambda: html.tostring(document(
        "Registration Status: ",
        *status_section(status, message, registration_id)),
                                         pretty_print=True,
                                         doctype="<!doctype html>"))
Exemple #19
0
    def test_context_getset_3(self):
        c = contextvars.ContextVar('c', default=42)
        ctx = contextvars.Context()

        def fun():
            self.assertEqual(c.get(), 42)
            with self.assertRaises(KeyError):
                ctx[c]
            self.assertIsNone(ctx.get(c))
            self.assertEqual(ctx.get(c, 'spam'), 'spam')
            self.assertNotIn(c, ctx)
            self.assertEqual(list(ctx.keys()), [])

            t = c.set(1)
            self.assertEqual(list(ctx.keys()), [c])
            self.assertEqual(ctx[c], 1)

            c.reset(t)
            self.assertEqual(list(ctx.keys()), [])
            with self.assertRaises(KeyError):
                ctx[c]

        ctx.run(fun)
Exemple #20
0
    def test_context_copy_1(self):
        ctx1 = contextvars.Context()
        c = contextvars.ContextVar('c', default=42)

        def ctx1_fun():
            c.set(10)

            ctx2 = ctx1.copy()
            self.assertEqual(ctx2[c], 10)

            c.set(20)
            self.assertEqual(ctx1[c], 20)
            self.assertEqual(ctx2[c], 10)

            ctx2.run(ctx2_fun)
            self.assertEqual(ctx1[c], 20)
            self.assertEqual(ctx2[c], 30)

        def ctx2_fun():
            self.assertEqual(c.get(), 10)
            c.set(30)
            self.assertEqual(c.get(), 30)

        ctx1.run(ctx1_fun)
Exemple #21
0
 def test_context_run(self):
     contextvars.Context().run(self.assertLevel)
Exemple #22
0
 def wrapper(*args, **kwargs):
     ctx = contextvars.Context()
     return ctx.run(func, *args, **kwargs)
 def run_in_clean_context(*args, **kwargs):
     contextvars.Context().run(func, *args, **kwargs)
Exemple #24
0
            try:
                os.chdir(original_cwd)
            except os.error:
                pass


saved.starting_locals["help"] = arepl_overloads.help_overload
saved.starting_locals["input"] = arepl_overloads.input_overload
saved.starting_locals["howdoi"] = arepl_overloads.howdoi_wrapper

eval_locals = deepcopy(saved.starting_locals)

noGlobalVarsMsg = {"zz status": "AREPL is configured to not show global vars"}

try:
    run_context = contextvars.Context()
except NameError:
    run_context = None


def exec_input(exec_args: ExecArgs):
    """
    returns info about the executed code (local vars, errors, and timing)
    :rtype: returnInfo
    """
    global eval_locals
    global run_context

    argv[0] = exec_args.filePath
    # see https://docs.python.org/3/library/sys.html#sys.argv
    saved.starting_locals["__file__"] = exec_args.filePath
Exemple #25
0
def exec_input(exec_args: ExecArgs):
    """
    returns info about the executed code (local vars, errors, and timing)
    :rtype: returnInfo
    """
    global eval_locals
    global run_context

    argv[0] = exec_args.filePath
    # see https://docs.python.org/3/library/sys.html#sys.argv
    saved.starting_locals["__file__"] = exec_args.filePath
    if exec_args.filePath:
        saved.starting_locals["__loader__"].path = os.path.basename(
            exec_args.filePath)

    if not exec_args.usePreviousVariables:
        eval_locals = saved.get_eval_locals(exec_args.savedCode)

    # re-import imports. (pickling imports from saved code was unfortunately not possible)
    exec_args.evalCode = saved.copy_saved_imports_to_exec(
        exec_args.evalCode, exec_args.savedCode)

    # clear new modules from last run each run has same fresh start
    current_module_names = set([module_name for module_name in modules])
    new_modules = current_module_names - modules_to_keep
    for module_name in new_modules:
        del modules[module_name]

    # not sure why i need to do this when module was deleted
    # but if I don't next run will say event loop closed
    asyncio.set_event_loop(asyncio.new_event_loop())

    with script_path(os.path.dirname(exec_args.filePath)):
        if not exec_args.usePreviousVariables and run_context is not None:
            run_context = contextvars.Context()
        try:
            start = time()
            if run_context is not None:
                run_context.run(exec, exec_args.evalCode, eval_locals)
            else:
                # python 3.6 fallback
                exec(exec_args.evalCode, eval_locals)
            execTime = time() - start
        except BaseException:
            execTime = time() - start
            _, exc_obj, exc_tb = exc_info()
            if not get_settings().show_global_vars:
                raise UserError(exc_obj, exc_tb, noGlobalVarsMsg, execTime)
            else:
                raise UserError(exc_obj, exc_tb, eval_locals, execTime)

        finally:

            if sys.stdout.flush and callable(sys.stdout.flush):
                # a normal program will flush at the end of the run
                # arepl never stops so we have to do it manually
                sys.stdout.flush()

            saved.arepl_store = eval_locals.get("arepl_store")

            # clear mock stdin for next run
            arepl_overloads.arepl_input_iterator = None

    if get_settings().show_global_vars:
        userVariables = pickle_user_vars(
            eval_locals,
            get_settings().default_filter_vars,
            get_settings().default_filter_types,
        )
    else:
        userVariables = pickle_user_vars(
            noGlobalVarsMsg,
            get_settings().default_filter_vars,
            get_settings().default_filter_types,
        )

    return ReturnInfo("", userVariables, execTime, None)
Exemple #26
0
    def request_transport(
            self, authinfo: AuthInfo) -> Iterator[Awaitable[Transport]]:
        """
        Request a transport from an authinfo.  Because the client is not allowed to
        request a transport immediately they will instead be given back a future
        that can be awaited to get the transport::

            async def transport_task(transport_queue, authinfo):
                with transport_queue.request_transport(authinfo) as request:
                    transport = await request
                    # Do some work with the transport

        :param authinfo: The authinfo to be used to get transport
        :return: A future that can be yielded to give the transport
        """
        open_callback_handle = None
        transport_request = self._transport_requests.get(authinfo.id, None)

        if transport_request is None:
            # There is no existing request for this transport (i.e. on this authinfo)
            transport_request = TransportRequest()
            self._transport_requests[authinfo.id] = transport_request

            transport = authinfo.get_transport()
            safe_open_interval = transport.get_safe_open_interval()

            def do_open():
                """ Actually open the transport """
                if transport_request and transport_request.count > 0:
                    # The user still wants the transport so open it
                    _LOGGER.debug('Transport request opening transport for %s',
                                  authinfo)
                    try:
                        transport.open()
                    except Exception as exception:  # pylint: disable=broad-except
                        _LOGGER.error(
                            'exception occurred while trying to open transport:\n %s',
                            exception)
                        transport_request.future.set_exception(exception)

                        # Cleanup of the stale TransportRequest with the excepted transport future
                        self._transport_requests.pop(authinfo.id, None)
                    else:
                        transport_request.future.set_result(transport)

            # Save the handle so that we can cancel the callback if the user no longer wants it
            # Note: Don't pass the Process context, since (a) it is not needed by `do_open` and (b) the transport is
            # passed around to many places, including outside aiida-core (e.g. paramiko). Anyone keeping a reference
            # to this handle would otherwise keep the Process context (and thus the process itself) in memory.
            # See https://github.com/aiidateam/aiida-core/issues/4698
            open_callback_handle = self._loop.call_later(
                safe_open_interval, do_open,
                context=contextvars.Context())  #  type: ignore[call-arg]

        try:
            transport_request.count += 1
            yield transport_request.future
        except asyncio.CancelledError:  # pylint: disable=try-except-raise
            # note this is only required in python<=3.7,
            # where asyncio.CancelledError inherits from Exception
            _LOGGER.debug('Transport task cancelled')
            raise
        except Exception:
            _LOGGER.error('Exception whilst using transport:\n%s',
                          traceback.format_exc())
            raise
        finally:
            transport_request.count -= 1
            assert transport_request.count >= 0, 'Transport request count dropped below 0!'
            # Check if there are no longer any users that want the transport
            if transport_request.count == 0:
                if transport_request.future.done():
                    _LOGGER.debug('Transport request closing transport for %s',
                                  authinfo)
                    transport_request.future.result().close()
                elif open_callback_handle is not None:
                    open_callback_handle.cancel()

                self._transport_requests.pop(authinfo.id, None)
Exemple #27
0
    def test_context_run_1(self):
        ctx = contextvars.Context()

        with self.assertRaisesRegex(TypeError, 'missing 1 required'):
            ctx.run()
Exemple #28
0
 def __getitem__(self, indexes):
     ctx = contextvars.Context()
     indexes = ctx.run(self.identify_indexes, indexes)
     values = self.wrapped[indexes]
     values = ctx.run(self.calculate_values, values)
     return values