DocType Server Script dont support iterations?

Has anyone tried using iterations e.g. For loop inside the Server Script Doctype? It failed to work with the error “NameError: name ‘getiter’ is not defined”.

Any solution to this?

Code inside Server Script:

itemList = frappe.db.get_list(
“Item”,
filters = {
“item_group”:(“=”, doc.name)
},
fields = “item_code”,
as_list = 1
)

for i in list(itemList):
frappe.msgprint(i)

The for loop gives an error

I got the same error.

the console error is like this

File “/home/xxxx/apps/frappe/frappe/utils/safe_exec.py”, line 28, in safe_exec
exec(compile_restricted(script), exec_globals, _locals) # pylint: disable=exec-used
File “”, line 1, in
NameError: name ‘getiter’ is not defined

Is this an error or is it that the feature is not supported yet? It is surprising given that iterations are a basic feature of any code!

didnt quite understand your query, are you saying that you are unable to use for loop at all? or specifically with get list ?

We have been able to use the FOR LOOP.

As in the example above, for i in list(itemList): - brings an error regardless of whatever variables you add.

it turned out that the current development version has fixed the bug

frappe.utils.safe_exec.py

def get_safe_globals():

allow iterators and list comprehension

out._getiter_ = iter
out._iter_unpack_sequence_ = RestrictedPython.Guards.guarded_iter_unpack_sequence
out.sorted = sorted
1 Like

Lovely!
So I just add the code above and I should be good to go in production?

Hi,

Did you get any solution, i am still facing this same error.

Yes.
Please follow the suggestion by @szufisher by either switching to develop branch or by editing frappe.utils.safe_exec.py temporarily and import iterations.

I have added that above code but it is giving me internal server error after bench restart.

Any solution?

Can you share your edited code please ?

Basically go to the develop branch of frappe and copy the entire code: frappe/safe_exec.py at develop · frappe/frappe · GitHub

And replace what you have.

Thank You!
It is working now.

1 Like