Menu

#226 PyFoam crashed on import with Python 3 on Windows

acknowledged
None
normal
crash
Always
tweak
general
2023-09-14
2019-11-01
No

Using Windows 10 and Anaconda with Python 3. PyFoam installed with pip.
Python 3.6.8 |Anaconda custom (64-bit)| (default, Dec 30 2018, 18:50:55) [MSC v.1915 64 bit (AMD64)] on win32

import PyFoam chrashes with

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\timofey\AppData\Local\Continuum\anaconda3\lib\site-packages\PyFoam__init__.py", line 7, in <module>
from PyFoam.Infrastructure.Configuration import Configuration
File "C:\Users\timofey\AppData\Local\Continuum\anaconda3\lib\site-packages\PyFoam\Infrastructure\Configuration.py", line 85, in <module>
"descriptionPath": eval('["'+path.curdir+'","'+path.join(userDirectory(),"caseBuilderDescriptions")+'","'+path.join(globalDirectory(),"caseBuilderDescriptions")+'"]'),
File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape</string></module></module></module></stdin>

This is not a new bug, I'm pretty sure it was their at least ever since PyFoam became installable with pip.

Discussion

  • Bernhard Gschaider

    • status: new --> acknowledged
    • assigned_to: Bernhard Gschaider
    • Projection: none --> tweak
     
  • Bernhard Gschaider

    As a temporary workaround go to the Configuration.py-file described in the stacktrace and remove

        "CaseBuilder":{
            "descriptionPath": eval('["'+path.curdir+'","'+path.join(userDirectory(),"caseBuilderDescriptions")+'","'+path.join(globalDirectory(),"caseBuilderDescriptions")+'"]'),
        },
    

    I will have a look whether I have this problem too but probablyy will fix it by removing it. Because
    * the use of eval is evil
    * pyFoamCaseBuolder is not maintained anymore
    but I will look which of the hardcoded values (userDirectory() or globalDirectory() is causing it because these two probably won't work on Windows anyway

     
  • Erik Sällström

    Hi!
    Is eval really needed, or does it work to just remove the eval and replace with:

        "CaseBuilder":{
            "descriptionPath": [path.curdir, path.join(userDirectory(),"caseBuilderDescriptions"), path.join(globalDirectory(),"caseBuilderDescriptions")],
        },
    

    The issue is the use of backslash in Windows:

    In[43]: userDirectory()
    Out[43]: 'C:\\Users\\username\\.pyFoam'
    In[44]: globalDirectory()
    Out[44]: '/etc\\pyFoam'
    

    When you put a path with backslash in eval, it fails. E.g.:

    In[45]: eval('"'+userDirectory()+'"')
    Traceback (most recent call last):
      File "C:\Users\username\AppData\Local\Continuum\anaconda3\envs\env\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-45-586587067794>", line 1, in <module>
        eval('"'+userDirectory()+'"')
      File "<string>", line 1
    SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
    
     

    Last edit: Erik Sällström 2020-05-15
  • Gabriel Rodrigues Felix

    I in the configurations.py file I edited the userDirectory() and globalDirectory functions in CaseBuilder as to replace the backslashes with /.

    "CaseBuilder":{
            "descriptionPath": eval('["'+path.curdir+'","'+path.join(userDirectory().replace('\\','/'),"caseBuilderDescriptions")+'","'+path.join(globalDirectory().replace('\\','/'),"caseBuilderDescriptions")+'"]'),
        },
    

    Everything worked fine after this. The modules loaded perfectly.

     
  • Jake Yun

    Jake Yun - 2023-09-14

    The problem occurs in userDirectory() on Windows.
    And the root cause of the problem is "\Uxxxx", which is a unicode escape. Reference
    The solution might be this

    What about fixing it in userDirectory() like following?

    ------------------------------------------------------------------------------- 
    def userDirectory(): 
    """:return: the user directory""" 
    return path.expanduser(path.join("~","."+_pyFoamDirName)) 
    ------------------------------------------------------------------------------- 
    def userDirectory(): 
    """:return: the user directory""" 
    return path.expanduser(path.join("~","."+_pyFoamDirName)).encode('unicode_escape').decode() 
    ------------------------------------------------------------------------------- 
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.