Changeset 1ddb794 in sasmodels


Ignore:
Timestamp:
Dec 18, 2017 8:48:16 AM (6 years ago)
Author:
Paul Kienzle <pkienzle@…>
Children:
8224d24
Parents:
98a4f14
Message:

python 2/3 compatibility

Location:
sasmodels
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/autoc.py

    r2badeca r1ddb794  
    7272        function_name, function = translate.pop(0) 
    7373        filename = function.__code__.co_filename 
     74        escaped_filename = filename.replace('\\', '\\\\') 
    7475        offset = function.__code__.co_firstlineno 
    7576        refs = function.__code__.co_names 
     
    9394            elif isinstance(obj, float): 
    9495                constants[name] = obj 
     96                snippets.append('#line 1 "%s"' % escaped_filename) 
    9597                snippets.append("const double %s = %.15g;"%(name, obj)) 
    9698            elif isinstance(obj, int): 
    9799                constants[name] = obj 
     100                snippets.append('#line 1 "%s"' % escaped_filename) 
    98101                snippets.append("const int %s = %d;"%(name, obj)) 
    99102            elif isinstance(obj, (list, tuple, np.ndarray)): 
     
    106109                    obj = list(obj) + [0.]*(4-len(obj)) 
    107110                vals = ", ".join("%.15g"%v for v in obj) 
     111                snippets.append('#line 1 "%s"' % escaped_filename) 
    108112                snippets.append("const double %s[] = {%s};" %(name, vals)) 
    109113            elif isinstance(obj, special.Gauss): 
     
    129133 
    130134    # translate source 
    131     functions = py2c.translate( 
    132         [code[name] for name in ordered_dag(depends) if name in code], 
    133         constants) 
    134     snippets.clear() 
    135     snippets.append(functions) 
    136     #print("source", info.source) 
    137     print("\n".join(snippets)) 
    138     try: 
    139         c_text = "\n".join(snippets) 
    140         translated = open ("_autoc.c", "a+") 
    141         translated.write (c_text) 
    142         translated.close() 
    143     except Exception as excp: 
    144         strErr = "Error:\n" + str(excp.args) 
    145         print(strErr) 
    146     #return 
    147 #    raise RuntimeError("not yet converted...") 
     135    ordered_code = [code[name] for name in ordered_dag(depends) if name in code] 
     136    functions = py2c.translate(ordered_code, constants) 
     137    snippets.extend(functions) 
    148138 
    149139    # update model info 
  • sasmodels/kerneldll.py

    r2d81cfe r1ddb794  
    185185        subprocess.check_output(command, shell=shell, stderr=subprocess.STDOUT) 
    186186    except subprocess.CalledProcessError as exc: 
    187         raise RuntimeError("compile failed.\n%s\n%s"%(command_str, exc.output)) 
     187        raise RuntimeError("compile failed.\n%s\n%s" 
     188                           % (command_str, exc.output.decode())) 
    188189    if not os.path.exists(output): 
    189190        raise RuntimeError("compile failed.  File is in %r"%source) 
  • sasmodels/py2c.py

    r98a4f14 r1ddb794  
    243243# for C 
    244244        for arg in node.args: 
    245             self.arguments.append(arg.arg) 
     245            # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 
     246            try: 
     247                arg_name = arg.arg 
     248            except AttributeError: 
     249                arg_name = arg.id 
     250            self.arguments.append(arg_name) 
    246251 
    247252        padding = [None] *(len(node.args) - len(node.defaults)) 
    248253        for arg, default in zip(node.args, padding + node.defaults): 
    249254            if default is not None: 
    250                 self.warnings.append("Default Parameter unknown to C") 
    251                 w_str = "Default Parameters are unknown to C: '" + arg.arg + \ 
    252                         " = " + str(default.n) + "'" 
     255                # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 
     256                # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 
     257                try: 
     258                    arg_name = arg.arg 
     259                except AttributeError: 
     260                    arg_name = arg.id 
     261                w_str = ("Default Parameters are unknown to C: '%s = %s" 
     262                         % arg_name, str(default.n)) 
    253263                self.warnings.append(w_str) 
    254264#                self.write_python('=') 
     
    305315        if(len(self.Tuples) > 0): 
    306316            tplTargets = list(self.Tuples) 
    307             self.Tuples.clear() 
     317            del self.Tuples[:] 
    308318        self.write_c(' = ') 
    309319        self.is_sequence = False 
     
    410420                self.c_proc.insert(start_var, c_dcl + "\n") 
    411421                start_var += 1 
    412         self.C_Vars.clear() 
    413         self.C_IntVars.clear() 
    414         self.C_Vectors.clear() 
    415         self.C_Pointers.clear() 
     422        del self.C_Vars[:] 
     423        del self.C_IntVars[:] 
     424        del self.C_Vectors[:] 
     425        del self.C_Pointers[:] 
    416426        self.C_DclPointers 
    417427        if(fLine == True): 
     
    427437            self.c_proc.insert(start_var + 1, "\n") 
    428438 
    429     def writeInclude(self): 
    430         if(self.MathIncludeed == False): 
    431             self.add_c_line("#include <math.h>\n") 
    432             self.add_c_line("static double pi = 3.14159265359;\n") 
    433             self.MathIncludeed = True 
    434  
    435439    def ListToString(self, strings): 
    436440        s = '' 
     
    475479 
    476480        self.visit(node.args) 
    477 # for C 
    478 #        self.writeInclude() 
    479481        self.getMethodSignature() 
    480482# for C 
     
    802804        if((name not in self.C_Functions) and (name not in self.C_Vars) and \ 
    803805            (name not in self.C_IntVars) and (name not in self.arguments) and \ 
    804             (name.isnumeric() == False)): 
     806            (name.isdigit() == False)): 
    805807            if(self.InSubscript): 
    806808                self.C_IntVars.append(node.id) 
     
    11011103 
    11021104def translate(functions, constants=0): 
    1103     sniplets = [] 
    1104     sniplets.append("#include <math.h>") 
    1105     sniplets.append("static double pi = 3.14159265359;") 
    1106     for source,fname,line_no in functions: 
    1107         line_directive = '#line %d "%s"' %(line_no,fname) 
    1108         line_directive = line_directive.replace('\\','\\\\') 
    1109 #        sniplets.append(line_directive) 
     1105    snippets = [] 
     1106    for source, fname, line_no in functions: 
     1107        line_directive = '#line %d "%s"'%(line_no, fname.replace('\\','\\\\')) 
     1108        snippets.append(line_directive) 
    11101109        tree = ast.parse(source) 
    1111         sniplet = to_source(tree, functions) # in the future add filename, offset, constants 
    1112         sniplets.append(sniplet) 
    1113     c_code = "\n".join(sniplets) 
    1114     f_out = open ("xlate.c", "w+") 
    1115     f_out.write (c_code) 
    1116     f_out.close() 
    1117     return("\n".join(sniplets)) 
     1110        # in the future add filename, offset, constants 
     1111        c_code = to_source(tree, functions) 
     1112        snippets.append(c_code) 
     1113    return snippets 
    11181114 
    11191115def get_file_names(): 
Note: See TracChangeset for help on using the changeset viewer.