Compare commits

...

2 Commits

Author SHA1 Message Date
1d9b64d126 [1.7.1] torch: Stop using _nt_quote_args from distutils (#48618) (#48768)
Summary:
They removed the specific function in Python 3.9 so we should just
remake the function here and use our own instead of relying on hidden
functions from the stdlib

Signed-off-by: Eli Uriegas <eliuriegas@fb.com>

Fixes https://github.com/pytorch/pytorch/issues/48617

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

Reviewed By: samestep

Differential Revision: D25230281

Pulled By: seemethere

fbshipit-source-id: 57216af40a4ae4dc8bafcf40d2eb3ba793b9b6e2
(cherry picked from commit 780f2b9a9bea86f92909d2fe02662ed6cb451861)
Signed-off-by: Eli Uriegas <eliuriegas@fb.com>
2020-12-02 20:41:55 -08:00
62d6dfce58 [Release/1.7] Fix index parsing on Python-3.9 (#48744)
Summary:
In 3.9, `ast.Index` and `ast.ExtSlice` are deprecated, so:
-  `ast.parse('img[3]', model='eval')` evaluates to
`Expression(body=Subscript(value=Name(id='img'), slice=Constant(value=3)))` by 3.9,
but was previously evaluated to `Expression(body=Subscript(value=Name(id='img'), slice=Index(value=Num(n=3))))`
- and `ast.parse('img[..., 10:20]', mode='eval')` is evaluated to
`
Subscript(value=Name(id='img'),slice=Tuple(elts=[Constant(value=Ellipsis),Slice(lower=Constant(value=10), upper=Constant(value=20))]))
`
, but was evaluated to
`
Subscript(value=Name(id='img'), slice=ExtSlice(dims=[Index(value=Ellipsis()), Slice(lower=Num(n=10), upper=Num(n=20), step=None)]))
`

Fixes https://github.com/pytorch/pytorch/issues/48674

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

Reviewed By: seemethere, gmagogsfm

Differential Revision: D25261323

Pulled By: malfet

fbshipit-source-id: cc818ecc596a062ed5f1a1d11d3fdf0f22bf7f4a
2020-12-02 13:20:47 -08:00
2 changed files with 25 additions and 5 deletions

View File

@ -686,9 +686,7 @@ class ExprBuilder(Builder):
if isinstance(expr.slice.value, ast.Tuple):
# N-dimensional indexing using Tuple: x[(i, j, k)] is equivalent to x[i, j, k]
# XXX: Indexing using a list is **different**! It triggers advanced indexing.
indices = []
for index_expr in expr.slice.value.elts:
indices.append(build_expr(ctx, index_expr))
indices = [build_expr(ctx, index_expr) for index_expr in expr.slice.value.elts]
return Subscript(base, indices)
else:
return Subscript(base, [build_expr(ctx, expr.slice.value)])
@ -696,6 +694,17 @@ class ExprBuilder(Builder):
return Subscript(base, [build_SliceExpr(ctx, base, expr.slice)])
elif sub_type is ast.ExtSlice:
return Subscript(base, build_ExtSlice(ctx, base, expr.slice))
elif sys.version_info >= (3, 9): # In Python3.9 array indicies are not wrapped in ast.Index
if sub_type is ast.Tuple:
# N-dimensional indexing using Tuple: x[(i, j, k)] is equivalent to x[i, j, k]
indices = []
for index_expr in expr.slice.elts:
if isinstance(index_expr, ast.Slice):
indices.append(build_SliceExpr(ctx, base, index_expr))
else:
indices.append(build_expr(ctx, index_expr))
return Subscript(base, indices)
return Subscript(base, [build_expr(ctx, expr.slice)])
else: # Ellipsis (can only happen in Python 2)
raise NotSupportedError(base.range(), "ellipsis is not supported")

View File

@ -24,6 +24,19 @@ from setuptools.command.build_ext import build_ext
IS_WINDOWS = sys.platform == 'win32'
# Taken directly from python stdlib < 3.9
# See https://github.com/pytorch/pytorch/issues/48617
def _nt_quote_args(args: Optional[List[str]]) -> List[str]:
"""Quote command-line arguments for DOS/Windows conventions.
Just wraps every argument which contains blanks in double quotes, and
returns a new argument list.
"""
# Cover None-type
if not args:
return []
return [f'"{arg}"' if ' ' in arg else arg for arg in args]
def _find_cuda_home() -> Optional[str]:
r'''Finds the CUDA install path.'''
# Guess #1
@ -621,7 +634,6 @@ class BuildExtension(build_ext, object):
cuda_post_cflags = list(extra_postargs)
cuda_post_cflags = win_cuda_flags(cuda_post_cflags)
from distutils.spawn import _nt_quote_args # type: ignore
cflags = _nt_quote_args(cflags)
post_cflags = _nt_quote_args(post_cflags)
if with_cuda:
@ -1607,7 +1619,6 @@ def _write_ninja_file_to_build_library(path,
if IS_WINDOWS:
cflags = common_cflags + COMMON_MSVC_FLAGS + extra_cflags
from distutils.spawn import _nt_quote_args # type: ignore
cflags = _nt_quote_args(cflags)
else:
cflags = common_cflags + ['-fPIC', '-std=c++14'] + extra_cflags