1515# along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
1717import keyword
18- from tokenize import DEDENT , ENCODING , INDENT , NAME , NEWLINE , NL , NUMBER , OP , STRING
18+ from tokenize import (
19+ DEDENT ,
20+ ENCODING ,
21+ FSTRING_END ,
22+ FSTRING_START ,
23+ INDENT ,
24+ NAME ,
25+ NEWLINE ,
26+ NL ,
27+ NUMBER ,
28+ OP ,
29+ STRING ,
30+ )
1931
2032from pof .utils .generator import BasicGenerator
2133from pof .utils .tokens import merge_implicit_strings
@@ -207,12 +219,13 @@ def __init__(self, generator=None) -> None:
207219 def generate_new_name (self ):
208220 return next (self .generator )
209221
210- def obfuscate_tokens (self , tokens ): # noqa: C901
222+ def obfuscate_tokens (self , tokens ): # noqa: C901, PLR0912
211223 tokens = merge_implicit_strings (tokens )
212224 result = []
213225 new_line_buffer = []
214226 line_buffer = []
215227 parenthesis_depth = 0
228+ fstring_depth = 0
216229 prev_toknum = None
217230 in_decorator = False
218231
@@ -224,6 +237,11 @@ def obfuscate_tokens(self, tokens): # noqa: C901
224237 elif toknum == OP and tokval == ")" :
225238 parenthesis_depth -= 1
226239
240+ if toknum == FSTRING_START :
241+ fstring_depth += 1
242+ elif toknum == FSTRING_END :
243+ fstring_depth -= 1
244+
227245 # track decorator context, suppress flushing between @ and def/class
228246 if toknum == OP and tokval == "@" :
229247 in_decorator = True
@@ -244,8 +262,10 @@ def obfuscate_tokens(self, tokens): # noqa: C901
244262 on_continuation_line = first_name_in_line in self .CONTINUATION_KEYWORDS
245263
246264 if (
247- (toknum == STRING and not is_docstring ) or toknum == NUMBER
248- ) and not on_continuation_line :
265+ ((toknum == STRING and not is_docstring ) or toknum == NUMBER )
266+ and not on_continuation_line
267+ and fstring_depth == 0
268+ ):
249269 random_name = self .generate_new_name ()
250270 new_line_buffer .extend (
251271 [
0 commit comments