Bug report #19742

accentued caracters make problem to load python expression script

Added by Jérôme Seigneuret about 6 years ago. Updated about 6 years ago.

Status:Reopened
Priority:Normal
Assignee:-
Category:Expressions
Affected QGIS version:3.2.2 Regression?:No
Operating System:Windows 7 64x Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:27567

Description

Hi,

Error

2018-08-31T15:05:56 WARNING L'expression de l'utilisateur base_water_function n'est pas valide
Traceback (most recent call last):
File "C:/OSGEO4~1/apps/qgis/./python\qgis\user.py", line 48, in load_user_expressions
import("expressions.{0}".format(name), locals(), globals())
File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 674, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
File "C:/Users/user1/AppData/Roaming/QGIS/QGIS3\profiles\default/python\expressions\base_water_function.py", line 21
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xe9 in position 4: invalid continuation byte

Context:

I make a function in expresion script panel and it is used to set label.

The expression script is:


# -*- coding: utf-8 -*-
from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def get_pipe_material_code_fr(v,feature, parent):
    """ 
    return french material name  to it corresponding code
    """ 
    if v == "Fonte":
        return "F" 
    elif v == "Pvc":
        return "PVC" 
    elif v == "inconnu":
        return "?" 
    elif v == "Polyéthylène": 
        return "PE" 
    else:
        return "X" 

In fact i see
C:\Users\user1\AppData\Roaming\QGIS\QGIS3\profiles\default\QGIS\QGIS3.ini
encoding=UTF-8

there is no information with python but if I have understand, QGIS 3 work with python 3 and default encoding is UTF-8 so I don't understrand

my label call get_pipe_material_code_fr("Material") || (' Ø' ) || "Diameter"

If I go in function panel and click on load, all work fine.

Software information

Version de QGIS
3.2.2-Bonn
Révision du code
26842169e9
Compilé avec Qt
5.9.2
Utilisant Qt
5.9.2
Compilé avec GDAL/OGR
2.2.4
Utilisé avec GDAL/OGR
2.2.4
Compilé avec GEOS
3.6.1-CAPI-1.10.1
Utilisé avec GEOS
3.6.1-CAPI-1.10.1 r0
Version du client PostgreSQL
9.2.4
Version de SpatiaLite
4.3.0
Version de QWT
6.1.3
Version de QScintilla2
2.10.1
Version de PROJ.4 :
493

History

#1 Updated by Giovanni Manghi about 6 years ago

  • Category changed from Unknown to Expressions

#2 Updated by Jérôme Seigneuret about 6 years ago

  • Status changed from Open to Closed

This error are caused by encoding file...

I have create expression in an old version and I don't understand why but file is in ANSI and not utf-8. I convert encoding with Notepad++ and all work fine.

It's possible to add analyse on encoding file if it's not identical to header declaration or/and default encoding?

#3 Updated by Giovanni Manghi about 6 years ago

  • Resolution set to invalid

#4 Updated by Harrissou Santanna about 6 years ago

2018-08-31T15:05:56 WARNING L'expression de l'utilisateur base_water_function n'est pas valide

L'utilisateur s'appelle base_water_function ou c'est une mauvaise traduction et c'est plutôt le nom de la fonction?

#5 Updated by Jérôme Seigneuret about 6 years ago

Harrissou Santanna wrote:

2018-08-31T15:05:56 WARNING L'expression de l'utilisateur base_water_function n'est pas valide

L'utilisateur s'appelle base_water_function ou c'est une mauvaise traduction et c'est plutôt le nom de la fonction?

Sorry for french resume but I think this is an error of translation same as @Harrissou

Il y a peut être une erreur de traduction. En fait je dirais :

"Le fichier d'expression utilisateur base_water_function n'est pas valide"

File have same name as user expression file listed in fonction (normaly) but there is really a problem because ther is no analyse to check if name are valid or not with file system. I think this message need to be a new issue.

\\ is a valide expression file name! file stored ... I don't no where because there is no file in folder
// is a valide expression file name! file stored is .py

#6 Updated by Jérôme Seigneuret about 6 years ago

  • Status changed from Closed to Reopened

I understand why my fucntion don't work

When I load function file it is save in local encoding and not in utf-8

so when I reopen it on notepad++ this looks like to that


# -*- coding: utf-8 -*-
from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def get_pipe_material_code_fr(v,feature, parent):
    """ 
    read and replace value by code
    """ 
    if v == "Fonte":
        return "F" 
    elif v == "Pvc":
        return "PVC" 
    elif v == "inconnu":
        return "?" 
    elif v == "Poly굨yl鯥":
        return "PE" 
    else:
        return "X" 

This comportment is on all scripts... But not all my script contains non ascii caracters. This is the why I don't detect that before

Also available in: Atom PDF