Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Begin testing on Python 3.12 #1247

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
strategy:
matrix:
os: ['ubuntu-latest', 'windows-latest', 'macos-latest']
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
architecture: ['x64', 'x86']
install: ['pip']
check: ['test']
Expand All @@ -54,6 +54,8 @@ jobs:
architecture: x86
- os: macos-latest
architecture: x86
- python-version: '3.12'
architecture: x86

env:
DEPENDS: ${{ matrix.depends }}
Expand All @@ -72,6 +74,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture }}
allow-prereleases: true
- name: Display Python version
run: python -c "import sys; print(sys.version)"
- name: Create virtual environment
Expand Down
6 changes: 6 additions & 0 deletions nibabel/openers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def __init__(
mtime=mtime,
)

def seek(self, pos: int, whence: int = 0, /) -> int:
# Work around bug (gh-180111) in Python 3.12rc1, where seeking without
# flushing can cause write of excess null bytes
self.flush()
return super().seek(pos, whence)


def _gzip_open(
filename: str,
Expand Down
8 changes: 5 additions & 3 deletions nibabel/streamlines/tests/test_streamlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def setup():
)


def test_is_supported_detect_format():
def test_is_supported_detect_format(tmp_path):
# Test is_supported and detect_format functions
# Empty file/string
f = BytesIO()
Expand All @@ -103,15 +103,17 @@ def test_is_supported_detect_format():

# Wrong extension but right magic number
for tfile_cls in FORMATS.values():
with tempfile.TemporaryFile(mode='w+b', suffix='.txt') as f:
fpath = tmp_path / 'test.txt'
with open(fpath, 'w+b') as f:
f.write(asbytes(tfile_cls.MAGIC_NUMBER))
f.seek(0, os.SEEK_SET)
assert nib.streamlines.is_supported(f)
assert nib.streamlines.detect_format(f) is tfile_cls

# Good extension but wrong magic number
for ext, tfile_cls in FORMATS.items():
with tempfile.TemporaryFile(mode='w+b', suffix=ext) as f:
fpath = tmp_path / f'test{ext}'
with open(fpath, 'w+b') as f:
f.write(b'pass')
f.seek(0, os.SEEK_SET)
assert not nib.streamlines.is_supported(f)
Expand Down
5 changes: 5 additions & 0 deletions nibabel/tests/test_image_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io
import pathlib
import sys
import warnings
from functools import partial
from itertools import product
Expand Down Expand Up @@ -579,6 +580,10 @@ def validate_from_url(self, imaker, params):
del img
del rt_img

@pytest.mark.xfail(
sys.version_info >= (3, 12),
reason='Response type for file: urls is not a stream in Python 3.12',
)
def validate_from_file_url(self, imaker, params):
tmp_path = self.tmp_path

Expand Down
59 changes: 30 additions & 29 deletions nibabel/tests/test_openers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,35 +127,36 @@ def patch_indexed_gzip(state):
yield


def test_Opener_gzip_type():
# Test that BufferedGzipFile or IndexedGzipFile are used as appropriate

data = 'this is some test data'
fname = 'test.gz'

with InTemporaryDirectory():

# make some test data
with GzipFile(fname, mode='wb') as f:
f.write(data.encode())

# Each test is specified by a tuple containing:
# (indexed_gzip present, Opener kwargs, expected file type)
tests = [
(False, {'mode': 'rb', 'keep_open': True}, GzipFile),
(False, {'mode': 'rb', 'keep_open': False}, GzipFile),
(False, {'mode': 'wb', 'keep_open': True}, GzipFile),
(False, {'mode': 'wb', 'keep_open': False}, GzipFile),
(True, {'mode': 'rb', 'keep_open': True}, MockIndexedGzipFile),
(True, {'mode': 'rb', 'keep_open': False}, MockIndexedGzipFile),
(True, {'mode': 'wb', 'keep_open': True}, GzipFile),
(True, {'mode': 'wb', 'keep_open': False}, GzipFile),
]

for test in tests:
igzip_present, kwargs, expected = test
with patch_indexed_gzip(igzip_present):
assert isinstance(Opener(fname, **kwargs).fobj, expected)
def test_Opener_gzip_type(tmp_path):
# Test that GzipFile or IndexedGzipFile are used as appropriate

data = b'this is some test data'
fname = tmp_path / 'test.gz'

# make some test data
with GzipFile(fname, mode='wb') as f:
f.write(data)

# Each test is specified by a tuple containing:
# (indexed_gzip present, Opener kwargs, expected file type)
tests = [
(False, {'mode': 'rb', 'keep_open': True}, GzipFile),
(False, {'mode': 'rb', 'keep_open': False}, GzipFile),
(False, {'mode': 'wb', 'keep_open': True}, GzipFile),
(False, {'mode': 'wb', 'keep_open': False}, GzipFile),
(True, {'mode': 'rb', 'keep_open': True}, MockIndexedGzipFile),
(True, {'mode': 'rb', 'keep_open': False}, MockIndexedGzipFile),
(True, {'mode': 'wb', 'keep_open': True}, GzipFile),
(True, {'mode': 'wb', 'keep_open': False}, GzipFile),
]

for test in tests:
igzip_present, kwargs, expected = test
with patch_indexed_gzip(igzip_present):
opener = Opener(fname, **kwargs)
assert isinstance(opener.fobj, expected)
# Explicit close to appease Windows
del opener


class TestImageOpener(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tools/ci/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ if [ -n "$EXTRA_PIP_FLAGS" ]; then
fi

if [ -n "$DEPENDS" ]; then
pip install ${EXTRA_PIP_FLAGS} --prefer-binary ${!DEPENDS}
pip install ${EXTRA_PIP_FLAGS} --only-binary :all: ${!DEPENDS}
if [ -n "$OPTIONAL_DEPENDS" ]; then
for DEP in ${!OPTIONAL_DEPENDS}; do
pip install ${EXTRA_PIP_FLAGS} --prefer-binary $DEP || true
pip install ${EXTRA_PIP_FLAGS} --only-binary :all: $DEP || true
done
fi
fi
Expand Down
Loading