Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 038f00b

Browse files
committed
fix parse_shebang_from_file for windows store python
1 parent 587cbd3 commit 038f00b

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

identify/identify.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import errno
12
import os.path
23
import re
34
import shlex
@@ -205,8 +206,14 @@ def parse_shebang_from_file(path: str) -> Tuple[str, ...]:
205206
if not os.access(path, os.X_OK):
206207
return ()
207208

208-
with open(path, 'rb') as f:
209-
return parse_shebang(f)
209+
try:
210+
with open(path, 'rb') as f:
211+
return parse_shebang(f)
212+
except OSError as e:
213+
if e.errno == errno.EINVAL:
214+
return ()
215+
else:
216+
raise
210217

211218

212219
COPYRIGHT_RE = re.compile(r'^\s*(Copyright|\(C\)) .*$', re.I | re.MULTILINE)

tests/identify_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import builtins
2+
import errno
13
import io
24
import os
35
import socket
46
import stat
57
from tempfile import TemporaryDirectory
8+
from unittest import mock
69

710
import pytest
811

@@ -330,6 +333,15 @@ def test_parse_shebang_from_file_simple(tmpdir):
330333
assert identify.parse_shebang_from_file(x.strpath) == ('python',)
331334

332335

336+
def test_parse_shebang_open_raises_einval(tmpdir):
337+
x = tmpdir.join('f')
338+
x.write('#!/usr/bin/env not-expected\n')
339+
make_executable(x)
340+
error = OSError(errno.EINVAL, f'Invalid argument {x}')
341+
with mock.patch.object(builtins, 'open', side_effect=error):
342+
assert identify.parse_shebang_from_file(x.strpath) == ()
343+
344+
333345
def make_executable(filename):
334346
original_mode = os.stat(filename).st_mode
335347
os.chmod(

0 commit comments

Comments
 (0)