diff options
| author | 2020-08-05 13:27:23 -0700 | |
|---|---|---|
| committer | 2020-08-05 13:27:23 -0700 | |
| commit | 4d08df1afed319264afe78fbaae445018adf1d8d (patch) | |
| tree | d1c7b161d561b3fab76b641701ae155c19cb9a98 | |
| parent | Revert "HelpChannels: use more reliable check for claimed channel" (diff) | |
| parent | Source: remove redundant check for help commands (diff) | |
Merge pull request #1088 from python-discord/bug/util/1083/source-dynamic-obj
Source: raise BadArgument for dynamically-created objects
| -rw-r--r-- | bot/cogs/source.py | 22 | 
1 files changed, 15 insertions, 7 deletions
| diff --git a/bot/cogs/source.py b/bot/cogs/source.py index f1db745cd..205e0ba81 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -60,11 +60,12 @@ class BotSource(commands.Cog):          await ctx.send(embed=embed)      def get_source_link(self, source_item: SourceType) -> Tuple[str, str, Optional[int]]: -        """Build GitHub link of source item, return this link, file location and first line number.""" -        if isinstance(source_item, commands.HelpCommand): -            src = type(source_item) -            filename = inspect.getsourcefile(src) -        elif isinstance(source_item, commands.Command): +        """ +        Build GitHub link of source item, return this link, file location and first line number. + +        Raise BadArgument if `source_item` is a dynamically-created object (e.g. via internal eval). +        """ +        if isinstance(source_item, commands.Command):              if source_item.cog_name == "Alias":                  cmd_name = source_item.callback.__name__.replace("_alias", "")                  cmd = self.bot.get_command(cmd_name.replace("_", " ")) @@ -78,10 +79,17 @@ class BotSource(commands.Cog):              filename = tags_cog._cache[source_item]["location"]          else:              src = type(source_item) -            filename = inspect.getsourcefile(src) +            try: +                filename = inspect.getsourcefile(src) +            except TypeError: +                raise commands.BadArgument("Cannot get source for a dynamically-created object.")          if not isinstance(source_item, str): -            lines, first_line_no = inspect.getsourcelines(src) +            try: +                lines, first_line_no = inspect.getsourcelines(src) +            except OSError: +                raise commands.BadArgument("Cannot get source for a dynamically-created object.") +              lines_extension = f"#L{first_line_no}-L{first_line_no+len(lines)-1}"          else:              first_line_no = None | 
