3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

exec: accept newlines and other backslash escapes (in only "exec")

This way, more complex code can be entered using newlines: e.g. "for x in range(3):\n    irc.reply('hello world')"
This commit is contained in:
James Lu 2015-12-22 10:41:42 -08:00
parent 6330be8758
commit 375dbe8427

View File

@ -12,15 +12,20 @@ import world
def _exec(irc, source, args): def _exec(irc, source, args):
"""<code> """<code>
Admin-only. Executes <code> in the current PyLink instance. Admin-only. Executes <code> in the current PyLink instance. This command performs backslash escaping of characters, so things like \\n and \\ will work.
\x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02""" \x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02"""
utils.checkAuthenticated(irc, source, allowOper=False) utils.checkAuthenticated(irc, source, allowOper=False)
args = ' '.join(args)
# Allow using \n in the code, while escaping backslashes correctly otherwise.
args = bytes(' '.join(args), 'utf-8').decode("unicode_escape")
if not args.strip(): if not args.strip():
irc.reply('No code entered!') irc.reply('No code entered!')
return return
log.info('(%s) Executing %r for %s', irc.name, args, utils.getHostmask(irc, source))
log.info('(%s) Executing %r for %s', irc.name, args,
utils.getHostmask(irc, source))
exec(args, globals(), locals()) exec(args, globals(), locals())
utils.add_cmd(_exec, 'exec') utils.add_cmd(_exec, 'exec')
def _eval(irc, source, args): def _eval(irc, source, args):