18
18
#include < libasr/utils.h>
19
19
#include < libasr/pass/global_stmts_program.h>
20
20
#include < libasr/pass/instantiate_template.h>
21
+ #include < libasr/modfile.h>
21
22
22
23
#include < lpython/python_ast.h>
23
24
#include < lpython/semantics/python_ast_to_asr.h>
@@ -150,6 +151,23 @@ namespace CastingUtil {
150
151
}
151
152
}
152
153
154
+ int save_pyc_files (const LFortran::ASR::TranslationUnit_t &u,
155
+ std::string infile) {
156
+ LFORTRAN_ASSERT (LFortran::asr_verify (u));
157
+ std::string modfile_binary = LFortran::save_pycfile (u);
158
+
159
+ while ( infile.back () != ' .' ) {
160
+ infile.pop_back ();
161
+ }
162
+ std::string modfile = infile + " pyc" ;
163
+ {
164
+ std::ofstream out;
165
+ out.open (modfile, std::ofstream::out | std::ofstream::binary);
166
+ out << modfile_binary;
167
+ }
168
+ return 0 ;
169
+ }
170
+
153
171
// Does a CPython style lookup for a module:
154
172
// * First the current directory (this is incorrect, we need to do it relative to the current file)
155
173
// * Then the LPython runtime directory
@@ -191,6 +209,19 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
191
209
}
192
210
}
193
211
212
+ bool set_module_path (std::string infile0, std::vector<std::string> &rl_path,
213
+ std::string& infile, std::string& path_used, bool & ltypes) {
214
+ for (auto path: rl_path) {
215
+ Result<std::string> rinfile = get_full_path (infile0, path, ltypes);
216
+ if (rinfile.ok ) {
217
+ infile = rinfile.result ;
218
+ path_used = path;
219
+ return true ;
220
+ }
221
+ }
222
+ return false ;
223
+ }
224
+
194
225
ASR::Module_t* load_module (Allocator &al, SymbolTable *symtab,
195
226
const std::string &module_name,
196
227
const Location &loc, bool intrinsic,
@@ -214,17 +245,17 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
214
245
215
246
// Parse the module `module_name`.py to AST
216
247
std::string infile0 = module_name + " .py" ;
217
- bool found = false ;
248
+ std::string infile0c = infile0 + " c " ;
218
249
std::string path_used = " " , infile;
219
- for (auto path: rl_path) {
220
- Result<std::string> rinfile = get_full_path (infile0, path, ltypes);
221
- if (rinfile.ok ) {
222
- found = true ;
223
- infile = rinfile.result ;
224
- path_used = path;
225
- break ;
226
- }
250
+ bool found = set_module_path (infile0c, rl_path, infile,
251
+ path_used, ltypes);
252
+ if ( !found ) {
253
+ found = set_module_path (infile0, rl_path, infile,
254
+ path_used, ltypes);
255
+ } else {
256
+ load_pycfile ();
227
257
}
258
+
228
259
if (!found) {
229
260
err (" Could not find the module '" + infile0 + " '" , loc);
230
261
}
0 commit comments