Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
13833560
Commit
13833560
authored
Aug 07, 1995
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added sys.std files, read-only open, reload
parent
221df249
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
104 additions
and
3 deletions
+104
-3
Lib/rexec.py
Lib/rexec.py
+104
-3
No files found.
Lib/rexec.py
View file @
13833560
...
...
@@ -11,6 +11,11 @@ available; this module provides a reasonable default, but derived
classes can change the policies e.g. by overriding or extending class
variables like ok_builtin_modules or methods like make_sys().
XXX To do:
- r_open should allow writing tmp dir
- r_exec etc. with explicit globals/locals? (Use rexec("exec ... in ...")?)
- r_reload should reload from same location (that's one for ihooks?)
"""
...
...
@@ -21,6 +26,39 @@ import marshal
import
ihooks
class
FileBase
:
ok_file_methods
=
(
'fileno'
,
'flush'
,
'isatty'
,
'read'
,
'readline'
,
'readlines'
,
'seek'
,
'tell'
,
'write'
,
'writelines'
)
class
FileWrapper
(
FileBase
):
def
__init__
(
self
,
f
):
self
.
f
=
f
for
m
in
self
.
ok_file_methods
:
if
not
hasattr
(
self
,
m
):
setattr
(
self
,
m
,
getattr
(
f
,
m
))
def
close
(
f
):
self
.
flush
()
TEMPLATE
=
"""
def %s(self, *args):
return apply(getattr(self.mod, self.name).%s, args)
"""
class
FileDelegate
(
FileBase
):
def
__init__
(
self
,
mod
,
name
):
self
.
mod
=
mod
self
.
name
=
name
for
m
in
FileBase
.
ok_file_methods
+
(
'close'
,):
exec
TEMPLATE
%
(
m
,
m
)
class
RHooks
(
ihooks
.
Hooks
):
def
__init__
(
self
,
rexec
,
verbose
=
0
):
...
...
@@ -53,12 +91,19 @@ class RHooks(ihooks.Hooks):
class
RModuleLoader
(
ihooks
.
FancyModuleLoader
):
pass
def
load_module
(
self
,
name
,
stuff
):
file
,
filename
,
info
=
stuff
m
=
ihooks
.
FancyModuleLoader
.
load_module
(
self
,
name
,
stuff
)
m
.
__filename__
=
filename
return
m
class
RModuleImporter
(
ihooks
.
ModuleImporter
):
pass
def
reload
(
self
,
module
,
path
=
None
):
if
path
is
None
and
hasattr
(
module
,
'__filename__'
):
path
=
[
module
.
__filename__
]
return
ihooks
.
ModuleImporter
.
reload
(
self
,
module
,
path
)
class
RExec
(
ihooks
.
_Verbose
):
...
...
@@ -78,7 +123,7 @@ class RExec(ihooks._Verbose):
ok_sys_names
=
(
'ps1'
,
'ps2'
,
'copyright'
,
'version'
,
'platform'
,
'exit'
,
'maxint'
)
nok_builtin_names
=
(
'open'
,
'reload'
,
'__import__'
,
'raw_input'
,
'input'
)
nok_builtin_names
=
(
'open'
,
'reload'
,
'__import__'
)
def
__init__
(
self
,
hooks
=
None
,
verbose
=
0
):
ihooks
.
_Verbose
.
__init__
(
self
,
verbose
)
...
...
@@ -110,6 +155,8 @@ class RExec(ihooks._Verbose):
def
make_builtin
(
self
):
m
=
self
.
copy_except
(
__builtin__
,
self
.
nok_builtin_names
)
m
.
__import__
=
self
.
r_import
m
.
reload
=
self
.
r_reload
m
.
open
=
self
.
r_open
def
make_main
(
self
):
m
=
self
.
add_module
(
'__main__'
)
...
...
@@ -180,6 +227,60 @@ class RExec(ihooks._Verbose):
def
r_import
(
self
,
mname
,
globals
=
{},
locals
=
{},
fromlist
=
[]):
return
self
.
importer
.
import_module
(
mname
,
globals
,
locals
,
fromlist
)
def
r_reload
(
self
,
m
):
return
self
.
importer
.
reload
(
m
)
# The s_* methods are similar but also swap std{in,out,err}
def
set_files
(
self
):
s
=
self
.
modules
[
'sys'
]
s
.
stdin
=
FileWrapper
(
sys
.
stdin
)
s
.
stdout
=
FileWrapper
(
sys
.
stdout
)
s
.
stderr
=
FileWrapper
(
sys
.
stderr
)
sys
.
stdin
=
FileDelegate
(
s
,
'stdin'
)
sys
.
stdout
=
FileDelegate
(
s
,
'stdout'
)
sys
.
stderr
=
FileDelegate
(
s
,
'stderr'
)
def
save_files
(
self
):
self
.
save_stdin
=
sys
.
stdin
self
.
save_stdout
=
sys
.
stdout
self
.
save_stderr
=
sys
.
stderr
def
restore_files
(
files
):
sys
.
stdin
=
self
.
save_sydin
sys
.
stdout
=
self
.
save_stdout
sys
.
stderr
=
self
.
save_stderr
def
s_apply
(
self
,
func
,
*
args
,
**
kw
):
self
.
save_files
()
try
:
self
.
set_files
()
r
=
apply
(
func
,
args
,
kw
)
finally
:
self
.
restore_files
()
def
s_exec
(
self
,
*
args
):
self
.
s_apply
(
self
.
r_exec
,
args
)
def
s_eval
(
self
,
*
args
):
self
.
s_apply
(
self
.
r_eval
,
args
)
def
s_execfile
(
self
,
*
args
):
self
.
s_apply
(
self
.
r_execfile
,
args
)
def
s_import
(
self
,
*
args
):
self
.
s_apply
(
self
.
r_import
,
args
)
def
s_reload
(
self
,
*
args
):
self
.
s_apply
(
self
.
r_reload
,
args
)
# Restricted open(...)
def
r_open
(
self
,
file
,
mode
=
'r'
,
buf
=-
1
):
if
mode
not
in
(
'r'
,
'rb'
):
raise
IOError
,
"can't open files for writing in restricted mode"
return
open
(
file
,
'r'
,
buf
)
def
test
():
import
traceback
...
...
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