Handle real paths that have other invalid Python identifiers

For me, this manifested as the test failing when my folder
had a hyphen in it.  But this should fix it once and for
all.

Signed-off-by: Edward Z. Yang <ezyangfb.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75564

Approved by: https://github.com/suo
This commit is contained in:
Edward Z. Yang 2022-04-10 23:39:57 -04:00 committed by PyTorch MergeBot
parent e02584cd95
commit 0b9eb19d04

View File

@ -3,6 +3,7 @@ import sys
import ast import ast
import inspect import inspect
import string import string
import re
from collections import namedtuple from collections import namedtuple
from textwrap import dedent from textwrap import dedent
from typing import List, Tuple # noqa: F401 from typing import List, Tuple # noqa: F401
@ -390,7 +391,8 @@ def build_ignore_context_manager(ctx, stmt):
def create_unique_name_ext(ctx, stmt): def create_unique_name_ext(ctx, stmt):
# extension will be based on the full path filename plus # extension will be based on the full path filename plus
# the line number of original context manager # the line number of original context manager
return ctx.filename.replace(".", "_").replace("/", "_") + "_" + str(stmt.lineno) fn = re.sub(r'[^a-zA-Z0-9_]', '_', ctx.filename)
return f"{fn}_{stmt.lineno}"
def build_return_ann_stmt(outputs): def build_return_ann_stmt(outputs):
return_type_ann = "" return_type_ann = ""