Commit 0338b2d2 authored by Iliya Manolov's avatar Iliya Manolov

Fixed imports that look like import modulea.moduleb, added tests and fixed environment.showSetup().

parent e79dcdaf
......@@ -459,8 +459,8 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
user_context['_volatile_variable_list'] += variable
if environment_collector.showEnvironmentSetup():
inject_variable_dict.write("%s\n" % str(notebook_context['setup']))
inject_variable_dict['_print'].write("%s\n" % str(notebook_context['setup']))
# Execute the nodes with 'exec' mode
for node in to_run_exec:
mod = ast.Module([node])
......@@ -888,12 +888,19 @@ class ImportFixer(ast.NodeTransformer):
# is immediately raised and doesn't block next Jupyter cell execution
exec(test_import_string)
empty_function = self.newEmptyFunction("%s_setup" %result_name)
dotless_result_name = ""
for character in result_name:
if character == '.':
dotless_result_name = dotless_result_name + '_dot_'
else:
dotless_result_name = dotless_result_name + character
empty_function = self.newEmptyFunction("%s_setup" %dotless_result_name)
return_dict = self.newReturnDict(final_module_names)
empty_function.body = [node, return_dict]
environment_set = self.newEnvironmentSetCall("%s_setup" %result_name)
self.newImportWarningCall(root_module_name, result_name)
environment_set = self.newEnvironmentSetCall("%s_setup" %dotless_result_name)
self.newImportWarningCall(root_module_name, dotless_result_name)
return [empty_function, environment_set]
else:
return node
......@@ -914,7 +921,11 @@ class ImportFixer(ast.NodeTransformer):
"""
return_dict = "return {"
for name in module_names:
return_dict = return_dict + "'%s': %s, " % (name, name)
if name.find('.') != -1:
base_name = name[:name.find('.')]
else:
base_name = name
return_dict = return_dict + "'%s': %s, " % (base_name, base_name)
return_dict = return_dict + '}'
return ast.parse(return_dict).body[0]
......
......@@ -960,4 +960,34 @@ print np.array([1, 2, 3])
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
self.assertEquals(result['code_result'].strip(), u'[1 2 3]')
\ No newline at end of file
self.assertEquals(result['code_result'].strip(), u'[1 2 3]')
def testDotImport(self):
'''
This test guarantees that "import modulea.moduleb" works in Jupyter.
'''
self.login('dev_user')
import_code = '''
import os.path
'''
reference = 'Test.Notebook.EnvironmentObject.Errors.DotImport'
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=import_code
)
self.tic()
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
jupyter_code = '''
print os.path
'''
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=jupyter_code
)
self.tic()
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment