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 inspect
import string
import re
from collections import namedtuple
from textwrap import dedent
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):
# extension will be based on the full path filename plus
# 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):
return_type_ann = ""