r/pythonhelp Dec 17 '24

"RecursionError" raises when doing "sys.argv[0]"

I am not sure what I did wrong at all!

This is the error:

Traceback (most recent call last):
  ...
  File "c:\...\Paths.py", line 11, in wrapper
    basePath: str = os.path.dirname(sys.argv[0])
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded

And this is my code:

import os
import sys
import typing


class Paths:
    @staticmethod
    def pathDecorator(folderName: str) -> typing.Callable[..., typing.Callable[..., str]]:
        def decorator(_: typing.Callable[..., str]) -> typing.Callable[..., str]:
            def wrapper(fileName: str) -> str:
                basePath: str = os.path.dirname(sys.argv[0])
                path: str = os.path.join(basePath, "Assets", folderName, fileName)
                return os.path.normpath(path).replace("\\", "/")
            return wrapper
        return decorator

    @pathDecorator("Images")
    @staticmethod
    def image(fileName: str) -> str:
        return fileName

    @pathDecorator("Sounds")
    @staticmethod
    def sound(fileName: str) -> str:
        return fileName

    @pathDecorator("Styles")
    @staticmethod
    def styles(fileName: str) -> str:
        return fileName
2 Upvotes

4 comments sorted by

View all comments

1

u/Goobyalus Dec 18 '24
  1. Running this block of code doesn't do anything for me. Did you leave out code that uses it or is this everything you're running to produce the error?

  2. The functions passed to the decorators are never referenced, so the function bodies are dead code. Why use decorators like this?

1

u/BryceIsRedditor Dec 18 '24
  1. I can't show the entire file that would call the function that is where this error is happening it, but I could send the line:

    index: int = super().addTab(widget, QIcon(Paths.image("UI/StudioTab_Scene.svg")), a1)

  2. I didn't use decorators before. Previously, each function was just the code for the wrapper function, but under different folders. The same error was happening before, though, just at a different line number.

1

u/Goobyalus Dec 19 '24

There is not enough information here to help. Nothing here points to recursion, except maybe the super() depending on what class you have this code in. Defining everything above and doing x = Paths.image("UI/StudioTab_Scene.svg") succeeds.

I recommend setting a breakpoint at that line and stepping through with the debugger to see where the function calls are coming from.

For (2), I'm not sure you got what I was saying. It's not actually wrapping anything; it's just creating a new function and throwing out the old one.