#🔒 Why is this error raising? What does even unterminated f-string literal mean?

43 messages · Page 1 of 1 (latest)

foggy pendant
#

There is a code that gives this error:

Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ui\modal.py", line 188, in _scheduled_task
    await self.on_submit(interaction)
  File "c:\Users\PC\Documents\Python Projects\BotExperience\main.py", line 66, in on_submit
    eval(self.log_message_edit())
  File "<string>", line 1
    await self.log_info_message.edit(content = f"{interaction.user.mention} başvuruda bulundu:
                                               ^
SyntaxError: unterminated f-string literal (detected at line 1)

And this is the code:

async def on_submit(self, interaction: discord.Interaction):
        self.edit_counter += 1
        await interaction.response.send_message(content = f"Metin şu şekilde güncellendi:\n\n{self.edit.value}")
        edited_message = await interaction.original_response()
        await self.private_info_message.edit(view = None)
        await interaction.edit_original_response(view = Edit_button(edited_message, self.log_info_message, self.edit.value, self.edit_counter))
        
        eval(self.log_message_edit())
        edit_log_channel = bot.get_channel(1252613310755639460)
        await edit_log_channel.send(content= f"{interaction.user.mention} mesajını güncellediği için önceki mesajı kaydedildi:\n\n{self.content}")

    def log_message_edit(self):
        if "osmanyd tarafından kabul edildi." in self.log_info_message.content:
            leader_acceptance_info = "osmanyd tarafından kabul edildi."
        elif "klontar tarafından kabul edildi." in self.log_info_message.content:
            leader_acceptance_info = "klontar tarafından kabul edildi."
        else:
            leader_acceptance_info = 0


        if leader_acceptance_info:
            the_returning = """await self.log_info_message.edit(content = f"{interaction.user.mention} başvuruda bulundu:\n\n{self.edit.value}\n\n({self.edit_counter} kere düzenlendi)\n\n{leader_acceptance_info}")"""
        else:
            the_returning = """await self.log_info_message.edit(content = f"{interaction.user.mention} başvuruda bulundu:\n\n{self.edit.value}\n\n({self.edit_counter} kere düzenlendi)")"""
        return the_returning

(eval(self.log_message_edit())

sonic sequoiaBOT
#

@foggy pendant

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sterile crescent
#

huh, i see some eval shenanigans here, don't seem very safe (also have high chances to break when executing asynchronous code)
here it seems like the issue is that you have line breaks in your string that gets interpreted when evaluating the code, you should either escape those or have a triple double quote f-string inside your string

#

as for what the error message means, it's quite self explanatory, it's the same as an "unterminated string literal" but when using f-strings in 3.12+ (before 3.12 it is not specified as an f-string)
it's when your string isn't "ended" on the same line when using a single-line string
here's an example :

f"{1}
"
  File "<input>", line 1
    f"{1}
    ^
SyntaxError: unterminated string literal (detected at line 1)
molten summit
#

it means that the "f-string literal" was not "terminated". to give you an idea of what that means, this is an f-string: f"hello", while this is an unterminated f-string: f"hello (notice lack of closing quotes). this basically means nothing ever closes the f-string.

when it reads the first quotes, it knows to read until it sees endquotes, and if there are none it just keeps reading until eventually it stops because it reaches the end of the file or the end of the line.

#

you can't write strings (f-string or not) like this:

"abc
def"
#

they are contained in a single line unless they are a docstring (eg triple quotes: """hello""")

warped talon
#

Docstrings are just a string literal at the start of a function body.

Triple quoted strings may span lines.

foggy pendant
#

Cuz I control what's being evaluated

#

It's not free

molten summit
#

no its really not safe

sterile crescent
#

well if you do then yes but i'd advice against using evals altogether any way

warped talon
#

In theory, if you've been very careful.

molten summit
#

not even

sterile crescent
#

(also ive no way of knowing what's really safe or no in what you sent)

molten summit
#

even pydis' eval sandbox (!e) has had and still has a few outstanding vulnerabilities and it is extensively protected

sterile crescent
#

theres no inherent vulnerability in the way the function work is what i mean, its how you use it

warped talon
#

Yes, but sandboxes are for evaluating code from outside.

The OP's evaluating things they think they've provided. But it's terrible risky, as it's very easy to embed a value with syntactic problems.

foggy pendant
#

Okay then I won't make it like that

warped talon
#

Yah - stay away from eval if you possibly can. We all do 🙂

foggy pendant
#

I actually realized that I already found out a structure of code that can be written directly inside the on_submit definition

#

Instead of using eval

molten summit
sterile crescent
#

all that eval does is parse an expression from a string and use your current interpreter to execute the code as is

#

if there was an inherent vulnerability with that, then there would be the same with python as a whole

molten summit
#

i dont really know where that value comes from but if its even tangentially related to input it is almost certainly possible to break

sterile crescent
#

yes i agree with that

spiral thorn
foggy pendant
#

And nevermind

#

I don't even need a def func for that

spiral thorn
# foggy pendant Async
    async def log_message_edit(self):
        if "osmanyd tarafından kabul edildi." in self.log_info_message.content:
            leader_acceptance_info = "osmanyd tarafından kabul edildi."
        elif "klontar tarafından kabul edildi." in self.log_info_message.content:
            leader_acceptance_info = "klontar tarafından kabul edildi."
        else:
            leader_acceptance_info = 0


        if leader_acceptance_info:
            await self.log_info_message.edit(content = f"{interaction.user.mention} başvuruda bulundu:\n\n{self.edit.value}\n\n({self.edit_counter} kere düzenlendi)\n\n{leader_acceptance_info}")
        else:
            await self.log_info_message.edit(content = f"{interaction.user.mention} başvuruda bulundu:\n\n{self.edit.value}\n\n({self.edit_counter} kere düzenlendi)")
foggy pendant
#

Yea you are right

spiral thorn
#

The reason you're getting an error is that you're inserting \n (the line break) literally into the string

foggy pendant
#

yea I got it

spiral thorn
#

as a rule of thumb, never use eval or exec

#

They exist for some very niche use cases that you're very unlikely to encounter

sterile crescent
#

yes, even if you wanted to parse python (to make your own python interpreter for instance, that's a fun project)
there are better things out there dedicated to that (like the ast and parser modules)

sonic sequoiaBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.