|
5 | 5 | import warnings |
6 | 6 | import tempfile |
7 | 7 | import os |
| 8 | +from time import sleep |
8 | 9 |
|
9 | 10 | import numpy as np |
10 | 11 | from scipy.sparse import coo_matrix, csr_matrix |
|
62 | 63 | from sklearn.model_selection.tests.test_split import MockClassifier |
63 | 64 |
|
64 | 65 |
|
| 66 | +try: |
| 67 | + WindowsError |
| 68 | +except NameError: |
| 69 | + WindowsError = None |
| 70 | + |
| 71 | + |
65 | 72 | class MockImprovingEstimator(BaseEstimator): |
66 | 73 | """Dummy classifier to test the learning curve""" |
67 | 74 | def __init__(self, n_max_train_sizes): |
@@ -781,12 +788,20 @@ def test_score_memmap(): |
781 | 788 | tf = tempfile.NamedTemporaryFile(mode='wb', delete=False) |
782 | 789 | tf.write(b'Hello world!!!!!') |
783 | 790 | tf.close() |
784 | | - scores = np.memmap(tf.name, dtype=float) |
785 | | - score = np.memmap(tf.name, shape=(), mode='w+', dtype=float) |
| 791 | + scores = np.memmap(tf.name, dtype=np.float64) |
| 792 | + score = np.memmap(tf.name, shape=(), mode='r', dtype=np.float64) |
786 | 793 | try: |
787 | 794 | cross_val_score(clf, X, y, scoring=lambda est, X, y: score) |
788 | 795 | # non-scalar should still fail |
789 | 796 | assert_raises(ValueError, cross_val_score, clf, X, y, |
790 | 797 | scoring=lambda est, X, y: scores) |
791 | 798 | finally: |
792 | | - os.unlink(tf.name) |
| 799 | + # Best effort to release the mmap file handles before deleting the |
| 800 | + # backing file under Windows |
| 801 | + scores, score = None, None |
| 802 | + for _ in range(3): |
| 803 | + try: |
| 804 | + os.unlink(tf.name) |
| 805 | + break |
| 806 | + except WindowsError: |
| 807 | + sleep(1.) |
0 commit comments