Ejemplo n.º 1
0
async def test_scope_copy(scope):
    scope2 = Scope()
    scope2.update(scope)

    assert scope.globals == scope2.globals, "Checking scope globals copied"
    assert scope.locals == scope2.locals, "Checking scope locals copied"

    insert_dict = {'e': 7}
    scope.update_locals(insert_dict)

    assert 'e' in scope.locals, "Checking scope locals updated"
    assert 'e' not in scope2.locals, "Checking scope clone locals not updated"

    scope.clear_intersection(insert_dict)

    assert 'e' not in scope.locals, "Checking locals intersection cleared"

    scope.update_globals(insert_dict)

    assert 'e' in scope.globals, "Checking scope globals updated"
    assert 'e' not in scope2.globals, "Checking scope clone globals not updated"

    scope.clear_intersection(insert_dict)

    assert 'e' not in scope.globals, "Checking globals intersection cleared"
Ejemplo n.º 2
0
async def test_scope_copy(scope):
    scope2 = Scope()
    scope2.update(scope)

    assert scope.globals == scope2.globals, "Checking scope globals copied"
    assert scope.locals == scope2.locals, "Checking scope locals copied"

    scope.update_locals({'e': 7})

    assert 'e' in scope.locals, "Checking scope locals updated"
    assert 'e' not in scope2.locals, "Checking scope clone locals not updated"
Ejemplo n.º 3
0
    async def jsk_retain(self, ctx: commands.Context, *, toggle: bool = None):
        """
        Turn variable retention for REPL on or off.

        Provide no argument for current status.
        """

        if toggle is None:
            if self.retain:
                return await ctx.send("Variable retention is set to ON.")

            return await ctx.send("Variable retention is set to OFF.")

        if toggle:
            if self.retain:
                return await ctx.send(
                    "Variable retention is already set to ON.")

            self.retain = True
            self._scope = Scope()
            return await ctx.send(
                "Variable retention is ON. Future REPL sessions will retain their scope."
            )

        if not self.retain:
            return await ctx.send("Variable retention is already set to OFF.")

        self.retain = False
        return await ctx.send(
            "Variable retention is OFF. Future REPL sessions will dispose their scope when done."
        )
Ejemplo n.º 4
0
 def __init__(self, bot: commands.Bot):
     self.bot = bot
     self._scope = Scope()
     self.retain = JISHAKU_RETAIN
     self.last_result = None
     self.start_time = datetime.datetime.now()
     self.tasks = collections.deque()
     self.task_count: int = 0
Ejemplo n.º 5
0
def scope():
    return Scope(
        {
            "add_numbers": add_numbers,
            "placement": 81
        },
        {
            "placement_local": 18
        }
    )
Ejemplo n.º 6
0
    def scope(self):
        """
        Gets a scope for use in REPL.

        If retention is on, this is the internal stored scope,
        otherwise it is always a new Scope.
        """

        if self.retain:
            return self._scope
        return Scope()
Ejemplo n.º 7
0
    def __init__(self, bot: commands.Bot):
        self.bot = bot
        self._scope = Scope()
        self.retain = JISHAKU_RETAIN
        self.last_result = None
        self.start_time = datetime.datetime.now()
        self.tasks = collections.deque()
        self.task_count: int = 0

        self.icon = "<:python:596577462335307777>"
        self.big_icon = "https://cdn.discordapp.com/attachments/427826132260487168/562303750589513743/PicsArt_04-01-05.54.17.png"
Ejemplo n.º 8
0
 def __init__(self, bot: commands.Bot, scope):
     self.bot = bot
     self._scope = Scope()
     self.retain = JISHAKU_RETAIN
     self.last_result = None
     self.start_time = datetime.datetime.utcnow()
     self.tasks = collections.deque()
     self.task_count: int = 0
     self.bot.old_help_command = bot.help_command
     self.queue = []
     self.SCOPE_PREFIX: str = scope
Ejemplo n.º 9
0
 def __init__(self, bot):
     self.token = os.getenv("DBL_TOKEN")
     self.bot = bot
     self.dbl = dbl.DBLClient(self.bot,
                              self.token,
                              autopost=True,
                              webhook_path="/dblwebhook",
                              webhook_port=os.getenv("DBL_PORT"),
                              webhook_auth=os.getenv("DBL_PASSWORD"))
     self.last_result = None
     self._scope = Scope()
     self.retain = JISHAKU_RETAIN
     self.tasks = collections.deque()
     self.task_count: int = 0
Ejemplo n.º 10
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self._scope = Scope()
     self.retain = JISHAKU_RETAIN
     self.last_result = None
Ejemplo n.º 11
0
    def scope(self):

        if self.retain:
            return self._scope
        return Scope()
Ejemplo n.º 12
0
    async def internal_test(self):
        scope = Scope()

        return_data = []
        async for result in AsyncCodeExecutor('3 + 4', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking eval produces single result")
        self.assertEqual(return_data[0],
                         7,
                         msg="Checking eval result is consistent")

        return_data = []
        async for result in AsyncCodeExecutor('return 3 + 9', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking manual return produces single result")
        self.assertEqual(return_data[0],
                         12,
                         msg="Checking return result is consistent")

        return_data = []
        async for result in AsyncCodeExecutor('b = 12 + 82', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking that assignment returns")
        self.assertIsNone(return_data[0],
                          msg="Checking assignment returns None")

        return_data = []
        async for result in AsyncCodeExecutor('b', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking variable eval returns")
        self.assertEqual(return_data[0],
                         94,
                         msg="Checking retained variable consistent")

        scope.update_globals({'d': 41})

        return_data = []
        async for result in AsyncCodeExecutor('c = d + 7; c', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking multi-expression implicitly returns")
        self.assertEqual(return_data[0],
                         48,
                         msg="Checking last expression return is consistent")

        return_data = []
        async for result in AsyncCodeExecutor('yield 30; yield 40', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         2,
                         msg="Checking two yields returns two results")
        self.assertEqual(return_data[0],
                         30,
                         msg="Checking first yield consistency")
        self.assertEqual(return_data[1],
                         40,
                         msg="Checking second yield consistency")

        return_data = []
        async for result in AsyncCodeExecutor('yield 60; 70', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         2,
                         msg="Checking multi-statement implicitly yields")
        self.assertEqual(return_data[0],
                         60,
                         msg="Checking explicit yield consistent")
        self.assertEqual(return_data[1],
                         70,
                         msg="Checking implicit yield consistent")

        return_data = []
        async for result in AsyncCodeExecutor('90; 100', scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking multi-statement implicitly returns")
        self.assertEqual(return_data[0],
                         100,
                         msg="Checking implicit return consistent")

        arg_dict = {'_cool_data': 45, '_not_so_cool': 400}
        return_data = []
        async for result in AsyncCodeExecutor('_cool_data + _not_so_cool',
                                              scope,
                                              arg_dict=arg_dict):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking arg dictionary expression returned")
        self.assertEqual(return_data[0],
                         445,
                         msg="Checking arg dictionary expression consistent")

        scope.clean()

        hit_exception = False
        try:
            async for result in AsyncCodeExecutor('_cool_data', scope):
                pass
        except NameError:
            hit_exception = True

        self.assertTrue(hit_exception, msg="Checking private locals removed")

        scope2 = Scope()
        scope2.update(scope)

        self.assertEqual(scope.globals,
                         scope2.globals,
                         msg="Checking scope globals copied")
        self.assertEqual(scope.locals,
                         scope2.locals,
                         msg="Checking scope locals copied")

        scope.update_locals({'e': 7})

        self.assertIn('e', scope.locals, msg="Checking scope locals updated")
        self.assertNotIn('e',
                         scope2.locals,
                         msg="Checking scope clone locals not updated")

        scope.clean()

        codeblock = inspect.cleandoc("""
        def ensure_builtins():
            return ValueError
        """)

        async for result in AsyncCodeExecutor(codeblock, scope):
            pass

        scope.clean()

        self.assertIn('ensure_builtins',
                      scope.globals,
                      msg="Checking function remains defined")
        self.assertTrue(callable(scope.globals['ensure_builtins']),
                        msg="Checking defined is callable")
        self.assertEqual(scope.globals['ensure_builtins'](),
                         ValueError,
                         msg="Checking defined retuurn consistent")

        if sys.version_info >= (3, 7):
            codeblock = inspect.cleandoc("""
            eval('''
            3 + 4
            ''')
            """)

            return_data = []
            async for result in AsyncCodeExecutor(codeblock, scope):
                return_data.append(result)

            self.assertEqual(len(return_data),
                             1,
                             msg="Checking multi-line docstring eval returns")
            self.assertEqual(return_data[0],
                             7,
                             msg="Checking eval return consistent")

            scope.clean()

        scope.update_globals({'add_numbers': self.add_numbers})

        return_data = []
        async for result in AsyncCodeExecutor("await add_numbers(10, 12)",
                                              scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking await returns result")
        self.assertEqual(return_data[0],
                         22,
                         msg="Checking await result consistent")

        return_data = []
        async for result in AsyncCodeExecutor("", scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking empty eval returns")
        self.assertIsNone(return_data[0],
                          msg="Checking empty eval returns None")

        return_data = []
        async for result in AsyncCodeExecutor("# this is a comment", scope):
            return_data.append(result)

        self.assertEqual(len(return_data),
                         1,
                         msg="Checking eval of only comment returns")
        self.assertIsNone(return_data[0],
                          msg="Checking eval of only comment returns None")