From 250790a35155138417a8e599abf4b3f8350635b4 Mon Sep 17 00:00:00 2001 From: Daniel Ohayon Date: Wed, 30 Apr 2025 07:50:12 -0700 Subject: [PATCH] fix __file__ value inside components (#1056) Summary: Pull Request resolved: https://github.com/pytorch/torchx/pull/1056 Currently when we exec a component, we pass it the caller file's namespace. As a result, `__file__` resolves to `.../torchx/specs/finder.py` inside the compoennt at runtime, which is confusing. This fixes that. Reviewed By: crassirostris Differential Revision: D73793210 --- torchx/specs/finder.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/torchx/specs/finder.py b/torchx/specs/finder.py index dabf744eb..4258ec7a7 100644 --- a/torchx/specs/finder.py +++ b/torchx/specs/finder.py @@ -7,6 +7,7 @@ # pyre-strict import abc +import copy import importlib import inspect import logging @@ -281,7 +282,9 @@ def find( ) file_source = read_conf_file(self._filepath) - namespace = globals() + namespace = copy.copy(globals()) + # so that __file__ used inside the component points to the correct file + namespace["__file__"] = os.path.abspath(self._filepath) exec(file_source, namespace) # noqa: P204 if self._function_name not in namespace: raise ComponentNotFoundException(