Getting Unknown Exception thrown but no traceback

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Getting Unknown Exception thrown but no traceback

Post by cduran » Mon Feb 27, 2017 8:54 pm

I'm using the minimal build of mp running on an ATMEL ATSAME70Q21. At the initialization of mp (and my firmware in general) I load several (around 70+) py scripts using the following code:

Code: Select all

void add_script(const char *src)
{
	debugf(src);
	
	mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
	if (lex == NULL) {
		debugf("MPY ** MemoryError: lexer could not allocate **");
		return;
	}
	
	nlr_buf_t nlr;
	if (nlr_push(&nlr) == 0)
	{
		qstr source_name = lex->source_name;
		mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
		mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
		mp_call_function_0(module_fun);
		nlr_pop();
	} else {
		// uncaught exception
		debugf("MPY ** Uncaught Exception **");
		mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
	}
}
About halfway through the scripts being loaded I start getting Uncaught Exception errors but no lexer errors. I also don't get a traceback. At first I thought it could be because of my heap size being too small, but doubling the heap size didn't help.

Is there anywhere else I should be looking? I know the scripts are sound because they were each tested individually before trying to load all of them.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Getting Unknown Exception thrown but no traceback

Post by dhylands » Mon Feb 27, 2017 10:07 pm

With that many scripts you're probably running out of RAM.

I'd recommend that tou use the cross compiler and create frozen bytecode. This way the scripts are all compiled on the host and the bytescode all gets loaded into flash, so the bytecode itself doesn't take up any flash.

Examining the size of the bytecode files .mpy will also give you a rough estimate of how much memory would be needed.

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Getting Unknown Exception thrown but no traceback

Post by cduran » Mon Feb 27, 2017 10:54 pm

How do I load that byte code? I don't have a file system and the only way feasible was through byte strings, if I create byte arrays of the compiled byte code, what function does mp have for me to load it?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Getting Unknown Exception thrown but no traceback

Post by dhylands » Mon Feb 27, 2017 11:07 pm

In stmhal, you just put your python files into the directory pointed to by FROZEN_MPY_DIR and they'll get cross compiled, and the byte code gets converted into a C file called frozen_mpy.c in the build directory which is linked into the firmware.

Then you just import the modules normally.

Post Reply