Simple unit python code works in 0.19, but fails in 0.20

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Post Reply
spanner888
Posts: 197
Joined: Tue May 28, 2019 10:51 am

Simple unit python code works in 0.19, but fails in 0.20

Post by spanner888 »

Following python code snippet :

Code: Select all

import FreeCAD

q = FreeCAD.Units.Quantity 

kFactor = q('1400.0 N/mm^2')
print("Kf : ", kFactor, kFactor.Unit)
Works fine in FC 0.19 and gives output:

Code: Select all

13:33:53  Kf :  1400000.0 kg/(mm*s^2) Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
But in Fc 0.20, gives error:

Code: Select all

# 13:48:46  Traceback (most recent call last):
#  File "/home/spanner888/.local/share/FreeCAD/Macro/units@001.FCMacro", line 5, in <module>
#    kFactor = q('1400.0 N/mm^2')
#<class 'ValueError'>: syntax error

Hoping someone has insight into what change or thing I am not understanding.

Edit: Works for other units, including MPa, mm, min etc, including the unit "maths". Also verified 0.19 OK on win 7 and 0.20 fails on win 7, with similar versions of FC.

FreeCAD infos:

Code: Select all

#OS: Debian GNU/Linux 11 (bullseye) (XFCE/xfce)
#Word size of OS: 64-bit
#Word size of FreeCAD: 64-bit
#Version: 0.19.24366 (Git)
#Build type: Release
#Branch: (HEAD detached at 0f9259c)
#Hash: 0f9259cda103ae1824ac16c68ac9b4a0d54b05fc
#Python version: 3.9.7
#Qt version: 5.12.9
#Coin version: 4.0.0
#OCC version: 7.5.3
#Locale: English/Australia (en_AU)

#OS: Debian GNU/Linux 11 (bullseye) (XFCE/xfce)
#Word size of FreeCAD: 64-bit
#Version: 0.20.27809 (Git)
#Build type: Release
#Branch: (HEAD detached at 409b922)
#Hash: 409b9225176ad1c772aede14f56f99c8ce225db8
#Python 3.9.10, Qt 5.12.9, Coin 4.0.0, OCC 7.5.3
#Locale: English/Australia (en_AU)
#Installed mods: 
  #* test_grouping_etc
  #* FeedsAndSpeeds 0.4.0
  #* PathOpsMine

OS: Debian GNU/Linux 11 (bullseye) (XFCE/xfce)
Word size of FreeCAD: 64-bit
Version: 0.20.28647 (Git)
Build type: Release
Branch: (HEAD detached at 1c1743b)
Hash: 1c1743b67d209f2b6501de4f75645a33b69d1c31
Python 3.9.12, Qt 5.12.9, Coin 4.0.0, OCC 7.5.3
Locale: English/Australia (en_AU)
Installed mods: 
  * test_grouping_etc
  * FeedsAndSpeeds 0.4.0
  * PathOpsMine
openBrain
Veteran
Posts: 7880
Joined: Fri Nov 09, 2018 5:38 pm

Re: Simple unit python code works in 0.19, but fails in 0.20

Post by openBrain »

Confirmed on below version.
You can workaround it by addin a space on either side of the division bar (eventually both).
From quick tests, the problematic character sequence is specifically 'N/mm'.
---

Code: Select all

OS: Ubuntu 21.04 (KDE/plasma)
Word size of FreeCAD: 64-bit
Version: 0.20.28657 (Git)
Build type: Debug
Branch: master
Hash: fb1f4433c6bdafc5ec58bea0e1ea799f302c0bf1
Python 3.9.5, Qt 5.15.2, Coin 4.0.0, OCC 7.5.2
Locale: French/France (fr_FR) [ OS: French/France (fr_FR) ]
spanner888
Posts: 197
Joined: Tue May 28, 2019 10:51 am

Re: Simple unit python code works in 0.19, but fails in 0.20

Post by spanner888 »

Thanks for the confirmation & workaround.

I had a look at the base/Quantity & Units lexer stuff and code, but my bumbling about code not work out any issue.

git issue 6735 raised
Syres
Veteran
Posts: 1548
Joined: Thu Aug 09, 2018 11:14 am

Re: Simple unit python code works in 0.19, but fails in 0.20

Post by Syres »

spanner888 wrote: Tue Apr 12, 2022 2:07 am I had a look at the base/Quantity & Units lexer stuff and code, but my bumbling about code not work out any issue.
I can certainly narrow down the commit range. the last build I have with it working as expected is (approx 13th August 2021) :

Code: Select all

OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.25452 (Git)
Build type: Release
Branch: master
Hash: b0b4b321cb7e90a15311c0a1deea35d618706733
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United Kingdom (en_GB)
and the first build with it failing (approx 21st August 2021):

Code: Select all

OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.25541 (Git)
Build type: Release
Branch: master
Hash: ca043dcb6d20d70b8e24d75e810683214eecc5d4
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United Kingdom (en_GB)
Syres
Veteran
Posts: 1548
Joined: Thu Aug 09, 2018 11:14 am

Re: Simple unit python code works in 0.19, but fails in 0.20

Post by Syres »

This would by my first port of call to investigate git commit 4ab37fc
openBrain
Veteran
Posts: 7880
Joined: Fri Nov 09, 2018 5:38 pm

Re: Simple unit python code works in 0.19, but fails in 0.20

Post by openBrain »

Syres wrote: Tue Apr 12, 2022 8:51 am This would by my first port of call to investigate git commit 4ab37fc
The follow-up is on the github issue. ;)
Post Reply