Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
pygolang
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Carlos Ramos Carreño
pygolang
Commits
e205dbf6
Commit
e205dbf6
authored
Apr 06, 2021
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gpython: Implement -i (interactive inspect after program run) + promised interactive-mode tests
/reviewed-by
@jerome
/reviewed-on
!15
parent
2351dd27
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
6 deletions
+56
-6
gpython/__init__.py
gpython/__init__.py
+23
-5
gpython/gpython_test.py
gpython/gpython_test.py
+27
-1
gpython/testdata/hello.py
gpython/testdata/hello.py
+3
-0
gpython/testdata/world.py
gpython/testdata/world.py
+3
-0
No files found.
gpython/__init__.py
View file @
e205dbf6
...
...
@@ -41,7 +41,7 @@ $GPYTHON_RUNTIME=threads.
from
__future__
import
print_function
,
absolute_import
_pyopt
=
"c:m:OVW:X:"
_pyopt
=
"c:
i
m:OVW:X:"
_pyopt_long
=
(
'version'
,)
# pymain mimics `python ...`
...
...
@@ -93,6 +93,7 @@ def pymain(argv, init=None):
warnoptions
=
[]
# collected `-W arg`
reexec_with
=
[]
# reexecute underlying python with those options (e.g. -O, -S, ...)
reexec_argv
=
[]
# if reexecuting, reexecute with this application-level argv
inspect
=
False
# inspect interactively at the end
igetopt
=
_IGetOpt
(
argv
,
_pyopt
,
_pyopt_long
)
for
(
opt
,
arg
)
in
igetopt
:
...
...
@@ -147,6 +148,10 @@ def pymain(argv, init=None):
elif
opt
==
'-W'
:
warnoptions
.
append
(
arg
)
# -i inspect interactively
elif
opt
==
'-i'
:
inspect
=
True
else
:
print
(
"unknown option: '%s'"
%
opt
,
file
=
sys
.
stderr
)
sys
.
exit
(
2
)
...
...
@@ -173,7 +178,8 @@ def pymain(argv, init=None):
sys
.
argv
=
[
''
]
if
len
(
argv
)
==
0
else
argv
# e.g. ['-']
sys
.
path
.
insert
(
0
,
''
)
# cwd
if
sys
.
stdin
.
isatty
():
if
sys
.
stdin
.
isatty
()
or
inspect
:
inspect
=
False
# no console after console
def
run
(
mmain
):
mmain
.
__file__
=
'<stdin>'
_interact
(
mmain
)
...
...
@@ -259,11 +265,23 @@ def pymain(argv, init=None):
sys
.
modules
[
'__main__'
]
=
mmain
# execute -m/-c/file/interactive
run
(
mmain
)
import
traceback
try
:
run
(
mmain
)
except
:
# print exception becore going to interactive inspect
if
inspect
:
traceback
.
print_exc
()
else
:
raise
finally
:
# interactive inspect
if
inspect
:
_interact
(
mmain
,
banner
=
''
)
# _interact runs interactive console in mmain namespace.
def
_interact
(
mmain
):
def
_interact
(
mmain
,
banner
=
None
):
import
code
,
sys
from
six.moves
import
input
as
raw_input
# like code.interact() but with overridden console.raw_input _and_
...
...
@@ -287,7 +305,7 @@ def _interact(mmain):
return
raw_input
(
''
)
console
.
raw_input
=
_
console
.
interact
()
console
.
interact
(
banner
=
banner
)
# execfile was removed in py3
...
...
gpython/gpython_test.py
View file @
e205dbf6
...
...
@@ -34,9 +34,10 @@ testprog = join(here, 'testprog')
is_pypy
=
(
platform
.
python_implementation
()
==
'PyPy'
)
is_cpython
=
(
platform
.
python_implementation
()
==
'CPython'
)
is_gpython
=
(
'GPython'
in
sys
.
version
)
# @gpython_only is marker to run a test only under gpython
gpython_only
=
pytest
.
mark
.
skipif
(
'GPython'
not
in
sys
.
versi
on
,
reason
=
"gpython-only test"
)
gpython_only
=
pytest
.
mark
.
skipif
(
not
is_gpyth
on
,
reason
=
"gpython-only test"
)
# runtime is pytest fixture that yields all variants of should be supported gpython runtimes:
# '' - not specified (gpython should autoselect)
...
...
@@ -188,6 +189,31 @@ def test_pymain():
_
=
pyout
([
'testdata/hello.py'
,
'abc'
,
'def'
],
cwd
=
here
)
assert
_
==
b"hello
\
n
world
\
n
['testdata/hello.py', 'abc', 'def']
\
n
"
# -i after stdin (also tests interactive mode as -i forces interactive even on non-tty)
d
=
{
b'hellopy'
:
b
(
hellopy
),
b'ps1'
:
b''
# cpython emits prompt to stderr
}
if
is_pypy
and
not
is_gpython
:
d
[
'ps1'
]
=
b'>>>> '
# native pypy emits prompt to stdout and >>>> instead of >>>
_
=
pyout
([
'-i'
],
stdin
=
b'import hello
\
n
'
,
cwd
=
testdata
)
assert
_
==
b"%(ps1)shello
\
n
world
\
n
['']
\
n
%(ps1)s"
%
d
_
=
pyout
([
'-i'
,
'-'
],
stdin
=
b'import hello
\
n
'
,
cwd
=
testdata
)
assert
_
==
b"%(ps1)shello
\
n
world
\
n
['-']
\
n
%(ps1)s"
%
d
_
=
pyout
([
'-i'
,
'-'
,
'zzz'
],
stdin
=
b'import hello
\
n
'
,
cwd
=
testdata
)
assert
_
==
b"%(ps1)shello
\
n
world
\
n
['-', 'zzz']
\
n
%(ps1)s"
%
d
# -i after -c
_
=
pyout
([
'-i'
,
'-c'
,
'import hello'
],
stdin
=
b'hello.tag'
,
cwd
=
testdata
)
assert
_
==
b"hello
\
n
world
\
n
['-c']
\
n
%(ps1)s'~~HELLO~~'
\
n
%(ps1)s"
%
d
# -i after -m
_
=
pyout
([
'-i'
,
'-m'
,
'hello'
],
stdin
=
b'world.tag'
,
cwd
=
testdata
)
assert
_
==
b"hello
\
n
world
\
n
['%(hellopy)s']
\
n
%(ps1)s'~~WORLD~~'
\
n
%(ps1)s"
%
d
# -i after file
_
=
pyout
([
'-i'
,
'testdata/hello.py'
],
stdin
=
b'tag'
,
cwd
=
here
)
assert
_
==
b"hello
\
n
world
\
n
['testdata/hello.py']
\
n
%(ps1)s'~~HELLO~~'
\
n
%(ps1)s"
%
d
# -W <opt>
_
=
pyout
([
'-Werror'
,
'-Whello'
,
'-W'
,
'ignore::DeprecationWarning'
,
'testprog/print_warnings_setup.py'
],
cwd
=
here
)
...
...
gpython/testdata/hello.py
View file @
e205dbf6
...
...
@@ -6,3 +6,6 @@ import world
# sys.argv
import
sys
print
(
sys
.
argv
)
# variable in module namespace
tag
=
'~~HELLO~~'
gpython/testdata/world.py
View file @
e205dbf6
print
(
'world'
)
# variable in module namespace
tag
=
'~~WORLD~~'
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