[oss][torch.package] fix multiple error messages within PackageExporter (#124943)

Summary:
fixes two issues:
- when exporting with debug=True, the list of error-causing modules and a dependency path to them is not printed correctly, there's a missing newline after the path, meaning the name of the module for the next error is on the wrong line, which makes the output a confusing mess to read
- when a pickled object references more than one mocked module directly, the error message incorrectly repeats the same information, claiming the referenced attribute is present in several different libraries, because the if condition references the last seen module name while walking the pickle ops, not the module name from the enclosing block `for module_name in all_dependencies:`. this is confusing because one error will print as O(all_dependencies) errors, all with different module names but the same attribute name

Differential Revision: D56578035

Pull Request resolved: https://github.com/pytorch/pytorch/pull/124943
Approved by: https://github.com/JonAmazon, https://github.com/houseroad
This commit is contained in:
Bradley D
2024-04-29 18:11:28 +00:00
committed by PyTorch MergeBot
parent f7f018a0ed
commit 96cc73dc13

View File

@ -166,7 +166,7 @@ class PackagingError(Exception):
if debug:
module_path = dependency_graph.first_path(module_name)
message.write(
f" A path to {module_name}: {' -> '.join(module_path)}"
f" A path to {module_name}: {' -> '.join(module_path)}\n"
)
if not debug:
message.write("\n")
@ -705,9 +705,9 @@ class PackageExporter:
""" If an object happens to come from a mocked module, then we collect these errors and spit them
out with the other errors found by package exporter.
"""
if module in mocked_modules:
assert isinstance(module, str)
fields = mocked_modules[module]
if module_name in mocked_modules:
assert isinstance(module_name, str)
fields = mocked_modules[module_name]
self.dependency_graph.add_node(
module_name,
action=_ModuleProviderAction.MOCK,