Skip to content

Commit 3aed6da

Browse files
committed
Do not set winerror or filenames if they are None.
1 parent ac82d41 commit 3aed6da

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ repos:
2222
- id: trailing-whitespace
2323
- id: end-of-file-fixer
2424
- id: mixed-line-ending
25-
args: [--fix=lf]
25+
args: [--fix]
2626
- id: debug-statements

src/tblib/pickling_support.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def pickle_exception(
6565
'args': obj.args,
6666
}
6767
if isinstance(obj, OSError):
68-
attrs.update(
69-
errno=obj.errno,
70-
strerror=obj.strerror,
71-
winerror=getattr(obj, 'winerror', None),
72-
filename=obj.filename,
73-
filename2=obj.filename2,
74-
)
68+
attrs.update(errno=obj.errno, strerror=obj.strerror)
69+
if (winerror := getattr(obj, 'winerror', None)) is not None:
70+
attrs['winerror'] = winerror
71+
if obj.filename is not None:
72+
attrs['filename'] = obj.filename
73+
if obj.filename2 is not None:
74+
attrs['filename2'] = obj.filename2
7575

7676
return (
7777
unpickle_exception_with_attrs,

tests/test_pickle_exception.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import traceback
12
from traceback import format_exception
23

34
try:
@@ -230,6 +231,35 @@ def test_oserror():
230231
assert exc.__traceback__ is not None
231232

232233

234+
class OpenError(Exception):
235+
pass
236+
237+
238+
def bad_open():
239+
try:
240+
raise PermissionError(13, 'Booboo', 'filename', None, None)
241+
except Exception as e:
242+
raise OpenError(e) from e
243+
244+
245+
def test_permissionerror():
246+
try:
247+
bad_open()
248+
except Exception as e:
249+
exc = e
250+
251+
tblib.pickling_support.install(exc)
252+
exc = pickle.loads(pickle.dumps(exc))
253+
print(''.join(traceback.format_exception(exc)))
254+
assert isinstance(exc, OpenError)
255+
assert exc.__traceback__ is not None
256+
assert repr(exc) == "OpenError(PermissionError(13, 'Booboo'))"
257+
assert str(exc) == "[Errno 13] Booboo: 'filename'"
258+
assert exc.errno == 13
259+
assert exc.strerror == 'Permission denied'
260+
assert exc.filename == 'filename'
261+
262+
233263
class BadError(Exception):
234264
def __init__(self):
235265
super().__init__('Bad Bad Bad!')

0 commit comments

Comments
 (0)