Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
abb91745
Commit
abb91745
authored
Feb 20, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor how script chunks are run.
parent
12b68767
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
23 deletions
+15
-23
common/lib/capa/capa/capa_problem.py
+15
-23
No files found.
common/lib/capa/capa/capa_problem.py
View file @
abb91745
...
...
@@ -18,7 +18,6 @@ import logging
import
math
import
numpy
import
os
import
random
import
re
import
struct
import
sys
...
...
@@ -437,49 +436,42 @@ class LoncapaProblem(object):
Problem XML goes to Python execution context. Runs everything in script tags.
'''
random
.
seed
(
self
.
seed
)
context
=
{}
context
[
'seed'
]
=
self
.
seed
context
[
'script_code'
]
=
''
self
.
_execute_scripts
(
tree
.
findall
(
'.//script'
),
context
)
return
context
all_code
=
''
def
_execute_scripts
(
self
,
scripts
,
context
):
'''
Executes scripts in the given context.
'''
original_path
=
sys
.
path
python_path
=
[]
for
script
in
scripts
:
sys
.
path
=
original_path
+
self
.
_extract_system_path
(
script
)
for
script
in
tree
.
findall
(
'.//script'
):
stype
=
script
.
get
(
'type'
)
if
stype
:
if
'javascript'
in
stype
:
continue
# skip javascript
if
'perl'
in
stype
:
continue
# skip perl
# TODO: evaluate only python
python_path
.
extend
(
self
.
_extract_system_path
(
script
))
code
=
script
.
text
XMLESC
=
{
"'"
:
"'"
,
"""
:
'"'
}
code
=
unescape
(
code
,
XMLESC
)
# store code source in context
context
[
'script_code'
]
+=
code
all_code
+=
code
if
all_code
:
try
:
# use "context" for global context; thus defs in code are global within code
locals_dict
=
{}
safe_exec
.
safe_exec
(
code
,
context
,
locals_dict
)
safe_exec
.
safe_exec
(
all_code
,
context
,
locals_dict
,
random_seed
=
self
.
seed
)
context
.
update
(
locals_dict
)
except
Exception
as
err
:
log
.
exception
(
"Error while execing script code: "
+
code
)
log
.
exception
(
"Error while execing script code: "
+
all_
code
)
msg
=
"Error while executing script code:
%
s"
%
str
(
err
)
.
replace
(
'<'
,
'<'
)
raise
responsetypes
.
LoncapaProblemError
(
msg
)
finally
:
sys
.
path
=
original_path
# store code source in context
context
[
'script_code'
]
=
all_code
return
context
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment