View Issue Details

IDProjectCategoryView StatusLast Update
0003862DraftBugpublic2019-09-06 02:04
ReporterAlexandr Parkhomenko Assigned To 
PrioritylowSeveritycrashReproducibilityalways
Status newResolutionopen 
Product Version0.19 
Target VersionFixed in Version 
Summary0003862: Export from freecad errors
DescriptionEXPORT FROM FREECAD ERRORS:

  File "Mod/Draft/importSVG.py", line 1255, in export
    svg.write('<?xml version="1.0"?>\n')
<class 'TypeError'>: a bytes-like object is required, not 'str'

-        svg = pythonopen(filename,'wb')
+        svg = pythonopen(filename,'w')  # b    IT WAS CHANGE! IT IS OK!

  File "Mod/Draft/importSVG.py", line 1287, in export
    .replace('<','<').replace('>','>'))
<class 'TypeError'>: a bytes-like object is required, not 'str'

-                svg.write('<title>%s</title>\n' % ob.Label.encode('utf8')\
-                        .replace('<','<').replace('>','>'))
+                #svg.write('<title>%s</title>\n' % ob.Label.encode('utf8')\
+                #        .replace('<','<').replace('>','>'))

  File "Mod/Draft/importOCA.py", line 265, in export
    oca.write("#oca file generated from FreeCAD\r\n")
<class 'TypeError'>: a bytes-like object is required, not 'str'

-    oca = pythonopen(filename,'wb')
+    oca = pythonopen(filename,'w') #b
Additional Informationfreecad git version 4 feb 2019
system Archlinux x64
Tags#pending, #post-to-forum, export, import
FreeCAD Information

Activities

Kunda1

2019-02-26 10:36

administrator   ~0012771

OP please follow the guidelines (large yellow banner at the top of the page)

Kunda1

2019-03-03 14:07

administrator   ~0012827

@wmayer care to weigh in ?

Alexandr Parkhomenko

2019-03-03 20:25

reporter   ~0012838

Last edited: 2019-03-03 20:43

View 2 revisions

OS: Arch Linux
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.10
Build type: Release
Python version: 3.7.2
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/United States (en_US)

Alexandr Parkhomenko

2019-03-03 20:36

reporter   ~0012839

https://docs.python.org/3/library/functions.html#open
'b' appended to the mode opens the file in binary mode.
in file
https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/Draft/importOCA.py#L264
we work with strings

Kunda1

2019-09-05 21:50

administrator   ~0013548

@yorik please weigh in

yorik

2019-09-06 02:03

administrator   ~0013550

Last edited: 2019-09-06 02:04

View 2 revisions

Yes, in python2, opening files in 'wb' mode worked for both text and binary files. In python3, this doesn't work anymore. 'w' is for text files, and 'wb' for binary files. But 'w' works for both py2 and 3 when we work with text.

The change that comments out <title> however doesn't seem good to me. What doesn't work in py3 is the .encode('utf8'). But that part is still needed for py2. So the proper way is to add something like this:

if sys.python_version.major < 3:
    label = label.encode('utf8')

Beware that the code has changed already since your patch.

Issue History

Date Modified Username Field Change
2019-02-25 18:42 Alexandr Parkhomenko New Issue
2019-02-25 18:42 Alexandr Parkhomenko Tag Attached: export
2019-02-25 18:42 Alexandr Parkhomenko Tag Attached: import
2019-02-26 10:36 Kunda1 Note Added: 0012771
2019-02-26 10:36 Kunda1 Status new => feedback
2019-02-26 10:37 Kunda1 Description Updated View Revisions
2019-03-03 14:07 Kunda1 Note Added: 0012827
2019-03-03 20:25 Alexandr Parkhomenko Note Added: 0012838
2019-03-03 20:25 Alexandr Parkhomenko Status feedback => new
2019-03-03 20:36 Alexandr Parkhomenko Note Added: 0012839
2019-03-03 20:43 Alexandr Parkhomenko Note Edited: 0012838 View Revisions
2019-09-05 21:50 Kunda1 Note Added: 0013548
2019-09-05 21:50 Kunda1 Tag Attached: #post-to-forum
2019-09-05 21:51 Kunda1 Tag Attached: #pending
2019-09-06 02:03 yorik Note Added: 0013550
2019-09-06 02:04 yorik Note Edited: 0013550 View Revisions