Many changes from the new toolchain · rec/safer@df6ec1d

Original file line numberDiff line numberDiff line change

@@ -147,6 +147,7 @@

147147

# Either the whole file is written, or nothing

148148
149149

"""

150+
150151

from __future__ import annotations

151152
152153

import contextlib

@@ -253,6 +254,7 @@ def writer(

253254

raise NotImplementedError(BUG_MESSAGE)

254255
255256

def write(v):

257+

assert isinstance(stream, t.ContextManager), (stream, type(stream))

256258

with stream:

257259

stream.write(v)

258260

@@ -399,7 +401,7 @@ def open(

399401

parent = os.path.dirname(os.path.abspath(name))

400402

if not os.path.exists(parent):

401403

if not make_parents:

402-

raise IOError('Directory does not exist')

404+

raise OSError('Directory does not exist')

403405

os.makedirs(parent)

404406
405407

def simple_open():

@@ -552,7 +554,7 @@ def printer(

552554

Same as for `safer.open()`

553555

"""

554556

if 'r' in mode and '+' not in mode:

555-

raise IOError('File not open for writing')

557+

raise OSError('File not open for writing')

556558
557559

if 'b' in mode:

558560

raise ValueError('Cannot print to a file open in binary mode')

@@ -598,7 +600,7 @@ def wrapped(*args, **kwargs):

598600
599601
600602

# Wrap an existing IO class so that it calls safer at the end

601-

@functools.lru_cache()

603+

@functools.lru_cache

602604

def _wrap_class(stream_cls):

603605

@functools.wraps(stream_cls.__exit__)

604606

def exit(self, *args):