Skip to content

Commit f363a33

Browse files
Ailing Zhangfacebook-github-bot
authored andcommitted
Set __file__ for torch.ops (#21888)
Summary: Fixes #19351 meta-pytorch/lockdown#93 Pull Request resolved: #21888 Differential Revision: D15871142 Pulled By: ailzhang fbshipit-source-id: 339e9d493e2e13f09e118814bdd1d7a5942804b8
1 parent 31e1e63 commit f363a33

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

test/test_jit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,13 @@ def test_pytorch_jit_env_off(self):
28292829
except subprocess.CalledProcessError as e:
28302830
raise RuntimeError("Could not 'import torch' with PYTORCH_JIT=0")
28312831

2832+
def test_print_op_module(self):
2833+
# Issue #19351: python2 and python3 go through different paths.
2834+
# python2 returns '<module 'torch.ops' (built-in)>'
2835+
# python3 uses __file__ and return
2836+
# '<module 'torch.ops' from '/scratch/ailzhang/pytorch/torch/_ops.py'>'
2837+
s = str(torch.ops)
2838+
self.assertRegex(s, r'ops')
28322839

28332840
def execWrapper(code, glob, loc):
28342841
if PY2:

torch/_ops.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import contextlib
44
import ctypes
5+
import os
56
import sys
67
import types
78

@@ -66,6 +67,8 @@ def __getattr__(self, op_name):
6667

6768

6869
class _Ops(types.ModuleType):
70+
__file__ = os.path.join(os.path.dirname(__file__), '_ops.py')
71+
6972
def __init__(self):
7073
super(_Ops, self).__init__('torch.ops')
7174
self.loaded_libraries = set()

0 commit comments

Comments
 (0)