View Issue Details

IDProjectCategoryView StatusLast Update
0002749FreeCADBugpublic2016-10-26 19:12
ReporterBerner Assigned Towmayer  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionno change required 
PlatformLinux OSUbuntuOS VersionOS: Ubuntu 16.04
Product Version0.17 
Fixed in Version0.17 
Summary0002749: Language problem with german special characters ü
DescriptionWhen i create a Box with locale in Linux set to:
LANG=de_DE.UTF-8
LANGUAGE=de_DE
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER="de_DE.UTF-8"
LC_NAME="de_DE.UTF-8"
LC_ADDRESS="de_DE.UTF-8"
LC_TELEPHONE="de_DE.UTF-8"
LC_MEASUREMENT="de_DE.UTF-8"
LC_IDENTIFICATION="de_DE.UTF-8"
LC_ALL=

The Box will created with the Label "Würfel".

When i call the following Macro the Macro crashed:
# Macro Begin: /home/data/WINSTICK/CAD/ListObj.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
# -*- coding: utf-8 -*-
import FreeCAD,Draft,Part
# List all objects of the document
App.Console.PrintMessage("ListObj-Macro\n")
doc = FreeCAD.ActiveDocument
objs = FreeCAD.ActiveDocument.Objects
#App.Console.PrintMessage(str(objs) + "\n")
#App.Console.PrintMessage(str(len(FreeCAD.ActiveDocument.Objects)) + " Objects" + "\n")

for obj in objs:
    a = obj.Name # list the Name of the object (not modifiable)
    b = obj.Label # list the Label of the object (modifiable)
    try:
        c = obj.LabelText # list the LabeText of the text (modifiable)
        App.Console.PrintMessage(str(a) +" "+ str(b) +" "+ str(c) + "\n") # Displays the Name the Label and the text
    except:
        App.Console.PrintMessage(str(a) +" "+ str(b) + "\n") # Displays the Name and the Label of the object
#doc.removeObject("Box") # Clears the designated object
# Macro End: /home/data/WINSTICK/CAD/ListObj.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
 
Errormessage of the Macro:

    App.Console.PrintMessage(str(a) +" "+ str(b) + "\n") # Displays the Name and the Label of the object
<type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)

When i rename the Label to Wuerfel, the Macro is running correct.

The problem depends on the characters ü, ö, etc.




OS: Ubuntu 16.04.1 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.8764 (Git)
Build type: None
Branch: master
Hash: efff1b7773ec20d32efba878c3900076918232d4
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
Steps To ReproduceSee description above.
TagsNo tags attached.
FreeCAD Information

Activities

wmayer

2016-10-26 12:05

administrator   ~0007427

This is NOT a FreeCAD bug but standard Python (2.x) behaviour.
Just run the following code in a Python session:

a="Wuerfel"
b=u"Würfel"
c=str(a) + " " + str(b)
results into the error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)

This happens because when concatenating a string and a unicode string Python tries to create a string from the unicode string "b" but the code point for "ü" is outside the ASCII range. When you want to avoid the problem you must encode "b" e.g. as utf-8

str(a)+ " "+ str(b.encode("utf-8"))

Berner

2016-10-26 12:46

reporter   ~0007428

Thank you,
your solution solves the problem.

I got my sample from the Freecad Macro recipes.
Do you have an idea i can code a macro to run in any locales ?

wmayer

2016-10-26 19:12

administrator   ~0007429

> Do you have an idea i can code a macro to run in any locales ?
When using unicode strings everywhere then it should work with any locales. If you need a plain string for certain functions it's best to use encode("utf-8"). If you have a utf-8 encoded string and want to create a unicode string from it then use decide("utf-8")

Issue History

Date Modified Username Field Change
2016-10-25 16:33 Berner New Issue
2016-10-26 12:05 wmayer Note Added: 0007427
2016-10-26 12:06 wmayer Status new => feedback
2016-10-26 12:46 Berner Note Added: 0007428
2016-10-26 12:46 Berner Status feedback => new
2016-10-26 19:12 wmayer Note Added: 0007429
2016-10-26 19:12 wmayer Status new => closed
2016-10-26 19:12 wmayer Assigned To => wmayer
2016-10-26 19:12 wmayer Resolution open => no change required
2016-10-26 19:12 wmayer Fixed in Version => 0.17