Changeset 4654 for trunk/GSASIIobj.py
- Timestamp:
- Nov 10, 2020 11:24:39 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIobj.py
r4643 r4654 1243 1243 import platform 1244 1244 import re 1245 import imp1246 1245 import random as ran 1247 1246 import sys … … 2884 2883 df = f.split('.') 2885 2884 pkgdict = {} 2886 # no listed package, try in current namespace2885 # no listed module name, try in current namespace 2887 2886 if len(df) == 1: 2888 2887 try: … … 2891 2890 except (AttributeError, NameError): 2892 2891 return None,None 2893 else: 2894 try: 2895 fxnobj = eval(f) 2896 pkgdict[df[0]] = eval(df[0]) 2897 return pkgdict,fxnobj 2898 except (AttributeError, NameError): 2899 pass 2900 # includes a package, lets try to load the packages 2901 pkgname = '' 2902 path = sys.path+['./',] 2903 for pkg in f.split('.')[:-1]: # if needed, descend down the tree 2904 if pkgname: 2905 pkgname += '.' + pkg 2906 else: 2907 pkgname = pkg 2908 fp = None 2909 try: 2910 fp, fppath,desc = imp.find_module(pkg,path) 2911 pkgobj = imp.load_module(pkg,fp,fppath,desc) 2912 pkgdict[pkgname] = pkgobj 2913 path = [fppath] 2914 except Exception as msg: 2915 print('load of '+pkgname+' failed with error='+str(msg)) 2916 return {},None 2917 finally: 2918 if fp: fp.close() 2919 try: 2920 #print 'before',pkgdict.keys() 2921 fxnobj = eval(f,globals(),pkgdict) 2922 #print 'after 1',pkgdict.keys() 2923 #fxnobj = eval(f,pkgdict) 2924 #print 'after 2',pkgdict.keys() 2925 return pkgdict,fxnobj 2926 except: 2927 continue 2928 return None # not found 2892 2893 # includes a package, see if package is already imported 2894 pkgnam = '.'.join(df[:-1]) 2895 try: 2896 fxnobj = eval(f) 2897 pkgdict[pkgnam] = eval(pkgnam) 2898 return pkgdict,fxnobj 2899 except (AttributeError, NameError): 2900 pass 2901 # package not yet imported, so let's try 2902 if '.' not in sys.path: sys.path.append('.') 2903 pkgnam = '.'.join(df[:-1]) 2904 #for pkg in f.split('.')[:-1]: # if needed, descend down the tree 2905 # if pkgname: 2906 # pkgname += '.' + pkg 2907 # else: 2908 # pkgname = pkg 2909 try: 2910 exec('import '+pkgnam) 2911 pkgdict[pkgnam] = eval(pkgnam) 2912 fxnobj = eval(f) 2913 except Exception as msg: 2914 print('load of '+pkgnam+' failed with error='+str(msg)) 2915 return {},None 2916 # can we access the function? I am not exactly sure what 2917 # I intended this to test originally (BHT) 2918 try: 2919 fxnobj = eval(f,globals(),pkgdict) 2920 return pkgdict,fxnobj 2921 except Exception as msg: 2922 print('call to',f,' failed with error=',str(msg)) 2923 return None,None # not found 2929 2924 def ASTtransverse(node,fxn=False): 2930 2925 '''Transverse a AST-parsed expresson, compiling a list of variables
Note: See TracChangeset
for help on using the changeset viewer.