Skip to content

Commit e4509cc

Browse files
committed
Use backports package for subprocess instead.
1 parent 01c28e3 commit e4509cc

4 files changed

Lines changed: 77 additions & 38 deletions

File tree

‎language_tool/__init__.py‎

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import os
2424
import re
2525
import socket
26-
import subprocess
2726
import sys
2827
import urllib.parse
2928
import urllib.request
@@ -37,6 +36,7 @@
3736
except ImportError:
3837
from xml.etree import ElementTree
3938

39+
from .backports import subprocess
4040
from .country_codes import get_country_code
4141
from .which import which
4242

@@ -60,42 +60,6 @@
6060
cache = {}
6161

6262

63-
# For Python 3.2-
64-
try:
65-
subprocess.TimeoutExpired #@UndefinedVariable
66-
except AttributeError:
67-
import time
68-
69-
class TimeoutExpired(Exception):
70-
"""Raised when the timeout expires while waiting for a child process.
71-
"""
72-
73-
def Popen_wait(self, timeout=None):
74-
"""Wait for child process to terminate.
75-
"""
76-
if timeout is None:
77-
return _Popen_wait(self)
78-
deadline = time.time() + timeout
79-
while self.poll() is None:
80-
if time.time() > deadline:
81-
raise TimeoutExpired
82-
time.sleep(0.01)
83-
return self.returncode
84-
85-
def Popen_communicate(self, input=None, timeout=None): #@ReservedAssignment
86-
"""Interact with process.
87-
"""
88-
if timeout is not None:
89-
self.wait(timeout)
90-
return _Popen_communicate(self, input)
91-
92-
_Popen_wait = subprocess.Popen.wait
93-
_Popen_communicate = subprocess.Popen.communicate
94-
subprocess.Popen.wait = Popen_wait
95-
subprocess.Popen.communicate = Popen_communicate
96-
subprocess.TimeoutExpired = TimeoutExpired
97-
98-
9963
class Error(Exception):
10064
"""LanguageTool Error
10165
"""

‎language_tool/backports/__init__.py‎

Whitespace-only changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Some backported features for subprocess
2+
"""
3+
4+
from __future__ import absolute_import
5+
6+
from subprocess import *
7+
8+
9+
try:
10+
TimeoutExpired
11+
except NameError:
12+
import time
13+
14+
class SubprocessError(Exception):
15+
"""Exception classes used by this module.
16+
"""
17+
18+
class CalledProcessError(SubprocessError):
19+
"""Raised when a process run by check_call() or check_output()
20+
returns a non-zero exit status.
21+
"""
22+
def __init__(self, returncode, cmd, output=None):
23+
self.returncode = returncode
24+
self.cmd = cmd
25+
self.output = output
26+
27+
def __str__(self):
28+
return ("Command '%s' returned non-zero exit status %d" %
29+
(self.cmd, self.returncode))
30+
31+
class TimeoutExpired(SubprocessError):
32+
"""Raised when the timeout expires while waiting for a child process.
33+
"""
34+
def __init__(self, cmd, timeout, output=None):
35+
self.cmd = cmd
36+
self.timeout = timeout
37+
self.output = output
38+
39+
def __str__(self):
40+
return ("Command '%s' timed out after %s seconds" %
41+
(self.cmd, self.timeout))
42+
43+
def Popen_init(self, args, *args_, **kwargs):
44+
"""Create new Popen instance.
45+
"""
46+
_Popen_init(self, args, *args_, **kwargs)
47+
self.args = args
48+
49+
def Popen_wait(self, timeout=None):
50+
"""Wait for child process to terminate.
51+
"""
52+
if timeout is None:
53+
return _Popen_wait(self)
54+
deadline = time.time() + timeout
55+
while self.poll() is None:
56+
if time.time() > deadline:
57+
raise TimeoutExpired(self.args, timeout)
58+
time.sleep(0.01)
59+
return self.returncode
60+
61+
def Popen_communicate(self, input=None, timeout=None):
62+
"""Interact with process.
63+
"""
64+
if timeout is not None:
65+
self.wait(timeout)
66+
return _Popen_communicate(self, input)
67+
68+
_Popen_init = Popen.__init__
69+
_Popen_wait = Popen.wait
70+
_Popen_communicate = Popen.communicate
71+
Popen.__init__ = Popen_init
72+
Popen.wait = Popen_wait
73+
Popen.communicate = Popen_communicate

‎setup.cfg‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ classifier =
2020
Programming Language :: Python :: 3.2
2121

2222
[files]
23-
packages = language_tool
23+
packages =
24+
language_tool
25+
language_tool.backports
2426
extra_files =
2527
COPYING
2628
COPYING.LESSER

0 commit comments

Comments
 (0)