[torch.package, 3.13] fixes to torch.package for 3.13 (#141409)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/141409
Approved by: https://github.com/PaliC, https://github.com/atalman
This commit is contained in:
William Wen
2024-12-04 00:02:44 -08:00
committed by PyTorch MergeBot
parent e6e75ebd0a
commit 22ae34af88
5 changed files with 21 additions and 4 deletions

View File

@ -0,0 +1,10 @@
import os # noqa: F401
import os.path # noqa: F401
import typing # noqa: F401
import torch
class Module(torch.nn.Module):
def forward(self):
return os.path.abspath("test")

View File

@ -178,6 +178,7 @@ class DirectoryReaderTest(PackageTestCase):
self.assertIsNone(importer.get_resource_reader("nonexistent_package"))
@skipIf(version_info < (3, 7), "ResourceReader API introduced in Python 3.7")
@skipIf(version_info >= (3, 13), "https://github.com/python/cpython/issues/127012")
def test_package_resource_access(self):
"""Packaged modules should be able to use the importlib.resources API to access
resources saved in the package.

View File

@ -338,10 +338,13 @@ class TestMisc(PackageTestCase):
causes modules who do this hackery to fail on import. See
https://github.com/pytorch/pytorch/issues/57490 for more information.
"""
import package_a.std_sys_module_hacks
if sys.version_info < (3, 13):
import package_a.std_sys_module_hacks as std_sys_module_hacks
else:
import package_a.std_sys_module_hacks_3_13 as std_sys_module_hacks
buffer = BytesIO()
mod = package_a.std_sys_module_hacks.Module()
mod = std_sys_module_hacks.Module()
with PackageExporter(buffer) as pe:
pe.intern("**")

View File

@ -77,6 +77,7 @@ class TestResources(PackageTestCase):
self.assertIsNone(importer.get_resource_reader("nonexistent_package"))
@skipIf(version_info >= (3, 13), "https://github.com/python/cpython/issues/127012")
def test_package_resource_access(self):
"""Packaged modules should be able to use the importlib.resources API to access
resources saved in the package.

View File

@ -6,6 +6,7 @@ import inspect
import io
import linecache
import os
import sys
import types
from contextlib import contextmanager
from typing import (
@ -526,6 +527,7 @@ class PackageImporter(Importer):
if name == "os":
self.modules["os.path"] = cast(Any, module).path
elif name == "typing":
if sys.version_info < (3, 13):
self.modules["typing.io"] = cast(Any, module).io
self.modules["typing.re"] = cast(Any, module).re