Skip to content

Commit 2f3403a

Browse files
authored
Updated deprecated test approach + minor changes (#89)
* isort _io.py * type fix: FFMEG -> FFMPEG * removed unused variable i in for loops * added better, more descriptive variable names * fixed deprecated warns(None) syntax * method docstrings, removed unnecessary i in loops * isort all * black all * removed unused pytest.warns import * replaced variable p with process
1 parent 4edd3ef commit 2f3403a

File tree

11 files changed

+79
-52
lines changed

11 files changed

+79
-52
lines changed

imageio_ffmpeg/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# flake8: noqa
55

66
from ._definitions import __version__
7-
from ._utils import get_ffmpeg_exe, get_ffmpeg_version
87
from ._io import count_frames_and_secs, read_frames, write_frames
8+
from ._utils import get_ffmpeg_exe, get_ffmpeg_version

imageio_ffmpeg/_definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import platform
2-
import sys
32
import struct
3+
import sys
44

55
__version__ = "0.4.8"
66

imageio_ffmpeg/_io.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import sys
2-
import time
31
import pathlib
42
import subprocess
5-
from functools import lru_cache
3+
import sys
4+
import time
65
from collections import defaultdict
6+
from functools import lru_cache
77

8-
from ._utils import get_ffmpeg_exe, _popen_kwargs, logger
9-
from ._parsing import LogCatcher, parse_ffmpeg_header, cvsecs
10-
8+
from ._parsing import LogCatcher, cvsecs, parse_ffmpeg_header
9+
from ._utils import _popen_kwargs, get_ffmpeg_exe, logger
1110

1211
ISWIN = sys.platform.startswith("win")
1312

@@ -164,7 +163,9 @@ def count_frames_and_secs(path):
164163
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, **_popen_kwargs())
165164
except subprocess.CalledProcessError as err:
166165
out = err.output.decode(errors="ignore")
167-
raise RuntimeError("FFMEG call failed with {}:\n{}".format(err.returncode, out))
166+
raise RuntimeError(
167+
"FFMPEG call failed with {}:\n{}".format(err.returncode, out)
168+
)
168169

169170
# Note that other than with the subprocess calls below, ffmpeg wont hang here.
170171
# Worst case Python will stop/crash and ffmpeg will continue running until done.
@@ -264,15 +265,15 @@ def read_frames(
264265
cmd += input_params + ["-i", path]
265266
cmd += pre_output_params + output_params + ["-"]
266267

267-
p = subprocess.Popen(
268+
process = subprocess.Popen(
268269
cmd,
269270
stdin=subprocess.PIPE,
270271
stdout=subprocess.PIPE,
271272
stderr=subprocess.PIPE,
272273
**_popen_kwargs(prevent_sigint=True)
273274
)
274275

275-
log_catcher = LogCatcher(p.stderr)
276+
log_catcher = LogCatcher(process.stderr)
276277

277278
# Init policy by which to terminate ffmpeg. May be set to "kill" later.
278279
stop_policy = "timeout" # not wait; ffmpeg should be able to quit quickly
@@ -302,8 +303,8 @@ def read_frames(
302303

303304
# ----- Read frames
304305

305-
w, h = meta["size"]
306-
framesize_bits = w * h * bits_per_pixel
306+
width, height = meta["size"]
307+
framesize_bits = width * height * bits_per_pixel
307308
framesize_bytes = framesize_bits / 8
308309
assert (
309310
framesize_bytes.is_integer()
@@ -316,7 +317,7 @@ def read_frames(
316317
try:
317318
bb = bytes()
318319
while len(bb) < framesize_bytes:
319-
extra_bytes = p.stdout.read(framesize_bytes - len(bb))
320+
extra_bytes = process.stdout.read(framesize_bytes - len(bb))
320321
if not extra_bytes:
321322
if len(bb) == 0:
322323
return
@@ -350,7 +351,7 @@ def read_frames(
350351
log_catcher.stop_me()
351352

352353
# Make sure that ffmpeg is terminated.
353-
if p.poll() is None:
354+
if process.poll() is None:
354355
# Ask ffmpeg to quit
355356
try:
356357
# I read somewhere that modern ffmpeg on Linux prefers a
@@ -364,8 +365,8 @@ def read_frames(
364365
# Found that writing to stdin can cause "Invalid argument" on
365366
# Windows # and "Broken Pipe" on Unix.
366367
# p.stdin.write(b"q") # commented out in v0.4.1
367-
p.stdout.close()
368-
p.stdin.close()
368+
process.stdout.close()
369+
process.stdin.close()
369370
# p.stderr.close() -> not here, the log_catcher closes it
370371
except Exception as err: # pragma: no cover
371372
logger.warning("Error while attempting stop ffmpeg (r): " + str(err))
@@ -374,16 +375,16 @@ def read_frames(
374375
# Wait until timeout, produce a warning and kill if it still exists
375376
try:
376377
etime = time.time() + 1.5
377-
while time.time() < etime and p.poll() is None:
378+
while time.time() < etime and process.poll() is None:
378379
time.sleep(0.01)
379380
finally:
380-
if p.poll() is None: # pragma: no cover
381+
if process.poll() is None: # pragma: no cover
381382
logger.warning("We had to kill ffmpeg to stop it.")
382-
p.kill()
383+
process.kill()
383384

384385
else: # stop_policy == "kill"
385386
# Just kill it
386-
p.kill()
387+
process.kill()
387388

388389

389390
def write_frames(

imageio_ffmpeg/_parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
2-
import time
32
import threading
3+
import time
44

55
from ._utils import logger
66

imageio_ffmpeg/_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import os
2-
import sys
31
import logging
2+
import os
43
import subprocess
4+
import sys
55
from functools import lru_cache
6+
67
from pkg_resources import resource_filename
78

8-
from ._definitions import get_platform, FNAME_PER_PLATFORM
9+
from ._definitions import FNAME_PER_PLATFORM, get_platform
910

1011
logger = logging.getLogger("imageio_ffmpeg")
1112

tasks.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
""" Invoke tasks for imageio-ffmpeg
22
"""
33

4+
import importlib
45
import os
5-
import sys
66
import shutil
7-
import importlib
87
import subprocess
8+
import sys
99
from urllib.request import urlopen
1010

1111
from invoke import task
@@ -138,7 +138,7 @@ def build(ctx):
138138
# Get version and more
139139
sys.path.insert(0, os.path.join(ROOT_DIR, "imageio_ffmpeg"))
140140
try:
141-
from _definitions import __version__, FNAME_PER_PLATFORM, WHEEL_BUILDS
141+
from _definitions import FNAME_PER_PLATFORM, WHEEL_BUILDS, __version__
142142
finally:
143143
sys.path.pop(0)
144144

@@ -236,6 +236,7 @@ def update_readme(ctx):
236236
text = text.split("\n## API\n")[0] + "\n## API\n\n"
237237

238238
import inspect
239+
239240
import imageio_ffmpeg
240241

241242
for func in (

tests/snippets.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
# is now to wait for ffmpeg.
1212

1313
import os
14+
1415
import numpy as np
16+
1517
import imageio_ffmpeg
1618

1719
ims = [
@@ -30,6 +32,7 @@
3032
# %% Behavior of KeyboardInterrupt / Ctrl+C
3133

3234
import os
35+
3336
import imageio_ffmpeg
3437

3538
filename = os.path.expanduser("~/.imageio/images/cockatoo.mp4")

tests/test_io.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22
The main tests for the public API.
33
"""
44

5+
import tempfile
56
import time
67
import types
7-
import tempfile
8+
import warnings
9+
10+
from pytest import raises, skip
11+
from testutils import (
12+
ensure_test_files,
13+
no_warnings_allowed,
14+
test_dir,
15+
test_file1,
16+
test_file2,
17+
test_file3,
18+
)
819

920
import imageio_ffmpeg
10-
from imageio_ffmpeg._io import ffmpeg_test_encoder, get_compiled_h264_encoders
11-
from imageio_ffmpeg._io import get_first_available_h264_encoder
12-
13-
from pytest import skip, raises, warns
14-
from testutils import no_warnings_allowed
15-
from testutils import ensure_test_files, test_dir, test_file1, test_file2, test_file3
21+
from imageio_ffmpeg._io import (
22+
ffmpeg_test_encoder,
23+
get_compiled_h264_encoders,
24+
get_first_available_h264_encoder,
25+
)
1626

1727

1828
def setup_module():
@@ -40,12 +50,13 @@ def test_read_frames_resource_warning():
4050
todo: use pytest.does_not_warn() as soon as it becomes available
4151
(see https://github.com/pytest-dev/pytest/issues/9404)
4252
"""
43-
with warns(None) as warnings:
53+
54+
with warnings.catch_warnings(record=True) as warnings_:
4455
gen = imageio_ffmpeg.read_frames(test_file1)
4556
next(gen)
4657
gen.close()
4758
# there should not be any warnings, but show warning messages if there are
48-
assert not [w.message for w in warnings]
59+
assert not [w.message for w in warnings_]
4960

5061

5162
@no_warnings_allowed

tests/test_special.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import queue
66
import threading
77

8-
import imageio_ffmpeg
9-
108
from testutils import ensure_test_files, test_file1
119

10+
import imageio_ffmpeg
11+
1212

1313
def setup_module():
1414
ensure_test_files()

tests/test_terminate.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
"""
66

77
import gc
8-
import sys
98
import subprocess
9+
import sys
1010

11-
import imageio_ffmpeg
12-
13-
from testutils import no_warnings_allowed, get_ffmpeg_pids
14-
from testutils import ensure_test_files, test_file1, test_file2
11+
from testutils import (
12+
ensure_test_files,
13+
get_ffmpeg_pids,
14+
no_warnings_allowed,
15+
test_file1,
16+
test_file2,
17+
)
1518

19+
import imageio_ffmpeg
1620

1721
N = 2 # number of times to perform each test
1822

@@ -30,7 +34,8 @@ def test_ffmpeg_version():
3034

3135
@no_warnings_allowed
3236
def test_reader_done():
33-
for i in range(N):
37+
"""Test that the reader is done after reading all frames"""
38+
for _ in range(N):
3439
pids0 = get_ffmpeg_pids()
3540
r = imageio_ffmpeg.read_frames(test_file1)
3641
pids1 = get_ffmpeg_pids().difference(pids0) # generator has not started
@@ -47,7 +52,8 @@ def test_reader_done():
4752

4853
@no_warnings_allowed
4954
def test_reader_close():
50-
for i in range(N):
55+
"""Test that the reader is done after closing it"""
56+
for _ in range(N):
5157
pids0 = get_ffmpeg_pids()
5258
r = imageio_ffmpeg.read_frames(test_file1)
5359
pids1 = get_ffmpeg_pids().difference(pids0) # generator has not started
@@ -63,7 +69,8 @@ def test_reader_close():
6369

6470
@no_warnings_allowed
6571
def test_reader_del():
66-
for i in range(N):
72+
"""Test that the reader is done after deleting it"""
73+
for _ in range(N):
6774
pids0 = get_ffmpeg_pids()
6875
r = imageio_ffmpeg.read_frames(test_file1)
6976
pids1 = get_ffmpeg_pids().difference(pids0) # generator has not started
@@ -80,7 +87,8 @@ def test_reader_del():
8087

8188
@no_warnings_allowed
8289
def test_write_close():
83-
for i in range(N):
90+
"""Test that the writer is done after closing it"""
91+
for _ in range(N):
8492
pids0 = get_ffmpeg_pids()
8593
w = imageio_ffmpeg.write_frames(test_file2, (64, 64))
8694
pids1 = get_ffmpeg_pids().difference(pids0) # generator has not started
@@ -97,7 +105,7 @@ def test_write_close():
97105

98106
@no_warnings_allowed
99107
def test_write_del():
100-
for i in range(N):
108+
for _ in range(N):
101109
pids0 = get_ffmpeg_pids()
102110
w = imageio_ffmpeg.write_frames(test_file2, (64, 64))
103111
pids1 = get_ffmpeg_pids().difference(pids0) # generator has not started
@@ -115,7 +123,10 @@ def test_write_del():
115123

116124
def test_partial_read():
117125
# Case: https://github.com/imageio/imageio-ffmpeg/issues/69
118-
template = "import sys; import imageio_ffmpeg; f=sys.argv[1]; print(f); r=imageio_ffmpeg.read_frames(f);"
126+
template = (
127+
"import sys; import imageio_ffmpeg; f=sys.argv[1]; print(f); "
128+
"r=imageio_ffmpeg.read_frames(f);"
129+
)
119130
for i in range(4):
120131
code = template + " r.__next__();" * i
121132
cmd = [sys.executable, "-c", code, test_file1]

tests/testutils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
import logging.handlers
12
import os
23
import tempfile
3-
import logging.handlers
44
from urllib.request import urlopen
55

66
import psutil
77

88
import imageio_ffmpeg
99

10-
1110
test_dir = tempfile.gettempdir()
1211
test_url1 = "https://raw.githubusercontent.com/imageio/imageio-binaries/master/images/cockatoo.mp4"
1312
test_url2 = "https://raw.githubusercontent.com/imageio/imageio-binaries/master/images/realshort.mp4"

0 commit comments

Comments
 (0)