File tree Expand file tree Collapse file tree 2 files changed +12
-9
lines changed Expand file tree Collapse file tree 2 files changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -37,13 +37,19 @@ def export_od(
37
37
:raises ValueError:
38
38
When exporting to an unknown format.
39
39
"""
40
+ supported_doctypes = {"eds" , "dcf" }
41
+ if doc_type and doc_type not in supported_doctypes :
42
+ supported = ", " .join (supported_doctypes )
43
+ raise ValueError (
44
+ f"Cannot export to the { doc_type !r} format; "
45
+ f"supported formats: { supported } "
46
+ )
40
47
41
48
opened_here = False
42
49
try :
43
- doctypes = {"eds" , "dcf" }
44
50
if isinstance (dest , str ):
45
51
if doc_type is None :
46
- for t in doctypes :
52
+ for t in supported_doctypes :
47
53
if dest .endswith (f".{ t } " ):
48
54
doc_type = t
49
55
break
@@ -58,12 +64,6 @@ def export_od(
58
64
elif doc_type == "dcf" :
59
65
from canopen .objectdictionary import eds
60
66
return eds .export_dcf (od , dest )
61
- else :
62
- allowed = ", " .join (doctypes )
63
- raise ValueError (
64
- f"Cannot export to the { doc_type !r} format; "
65
- f"supported formats: { allowed } "
66
- )
67
67
finally :
68
68
# If dest is opened in this fn, it should be closed
69
69
if opened_here :
Original file line number Diff line number Diff line change @@ -222,11 +222,14 @@ def test_export_eds_unknown_doctype(self):
222
222
import io
223
223
filelike_object = io .StringIO ()
224
224
self .addCleanup (filelike_object .close )
225
- self .addCleanup (os .unlink , "filename" )
226
225
for dest in "filename" , None , filelike_object :
227
226
with self .subTest (dest = dest ):
228
227
with self .assertRaisesRegex (ValueError , "'unknown'" ):
229
228
canopen .export_od (self .od , dest , doc_type = "unknown" )
229
+ # Make sure no files are created is a filename is supplied.
230
+ if isinstance (dest , str ):
231
+ with self .assertRaises (FileNotFoundError ):
232
+ os .stat (dest )
230
233
231
234
def test_export_eds_to_filelike_object (self ):
232
235
import io
You can’t perform that action at this time.
0 commit comments