Skip to content

Commit

Permalink
Merge pull request #179 from pre-commit/windows_store_sucks
Browse files Browse the repository at this point in the history
fix parse_shebang_from_file for windows store python
  • Loading branch information
asottile authored Mar 9, 2021
2 parents 587cbd3 + 038f00b commit dbca4e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions identify/identify.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import errno
import os.path
import re
import shlex
Expand Down Expand Up @@ -205,8 +206,14 @@ def parse_shebang_from_file(path: str) -> Tuple[str, ...]:
if not os.access(path, os.X_OK):
return ()

with open(path, 'rb') as f:
return parse_shebang(f)
try:
with open(path, 'rb') as f:
return parse_shebang(f)
except OSError as e:
if e.errno == errno.EINVAL:
return ()
else:
raise


COPYRIGHT_RE = re.compile(r'^\s*(Copyright|\(C\)) .*$', re.I | re.MULTILINE)
Expand Down
12 changes: 12 additions & 0 deletions tests/identify_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import builtins
import errno
import io
import os
import socket
import stat
from tempfile import TemporaryDirectory
from unittest import mock

import pytest

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


def test_parse_shebang_open_raises_einval(tmpdir):
x = tmpdir.join('f')
x.write('#!/usr/bin/env not-expected\n')
make_executable(x)
error = OSError(errno.EINVAL, f'Invalid argument {x}')
with mock.patch.object(builtins, 'open', side_effect=error):
assert identify.parse_shebang_from_file(x.strpath) == ()


def make_executable(filename):
original_mode = os.stat(filename).st_mode
os.chmod(
Expand Down

0 comments on commit dbca4e0

Please sign in to comment.