Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit a02c016

Browse files
committed
[WIP] Hybrid sysmodule.go+sys.py
Needs working __getattr__/__setattr__. Please wait if before merge
1 parent 3f2f3ab commit a02c016

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

lib/sys.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,37 @@
1515
"""System-specific parameters and functions."""
1616

1717
from __go__.os import Args
18-
from __go__.grumpy import SysModules, MaxInt, Stdin as stdin, Stdout as stdout, Stderr as stderr # pylint: disable=g-multiple-import
18+
from __go__.grumpy import SysmoduleDict, SysModules, MaxInt, Stdin, Stdout, Stderr # pylint: disable=g-multiple-import
1919
from __go__.runtime import Version
2020
from __go__.unicode import MaxRune
2121

22+
23+
__all__ = ('stdin', 'stdout', 'stderr', 'argv', '_goversion',
24+
'maxint', 'maxsize', 'maxunicode', 'modules', 'py3kwarning',
25+
'warnoptions', 'byteorder', 'flags', 'exc_info', 'exit')
26+
27+
2228
argv = []
2329
for arg in Args:
2430
argv.append(arg)
2531

2632
goversion = Version()
33+
34+
stdin = SysmoduleDict['stdin']
35+
stdout = SysmoduleDict['stdout']
36+
stderr = SysmoduleDict['stderr']
37+
2738
maxint = MaxInt
2839
maxsize = maxint
2940
maxunicode = MaxRune
3041
modules = SysModules
42+
3143
py3kwarning = False
3244
warnoptions = []
3345
# TODO: Support actual byteorder
3446
byteorder = 'little'
3547

48+
3649
class _Flags(object):
3750
"""Container class for sys.flags."""
3851
debug = 0
@@ -67,3 +80,19 @@ def exc_info():
6780
def exit(code=None): # pylint: disable=redefined-builtin
6881
raise SystemExit(code)
6982

83+
84+
# TODO: Clear this HACK: Should be the last lines of the python part of hybrid stuff
85+
class _SysModule(object):
86+
def __init__(self):
87+
for k in ('__name__', '__file__') + __all__:
88+
SysmoduleDict[k] = globals()[k]
89+
def __setattr__(self, name, value):
90+
SysmoduleDict[name] = value
91+
def __getattr__(self, name):
92+
resp = SysmoduleDict.get(name)
93+
if res is None and name not in SysmoduleDict:
94+
return super(_SysModule, self).__getattr__(name)
95+
return resp
96+
97+
modules = SysModules
98+
modules['sys'] = _SysModule()

runtime/sysmodule.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package grumpy
16+
17+
var (
18+
// Sysmodule is the __dict__ of the native part of sys.py.
19+
SysmoduleDict = newStringDict(map[string]*Object{
20+
"__file__": NewStr("<native>").ToObject(),
21+
"__name__": NewStr("_sys").ToObject(),
22+
"stdin": Stdin.ToObject(),
23+
"stdout": Stdout.ToObject(),
24+
"stderr": Stderr.ToObject(),
25+
})
26+
)

testing/sysmodule_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from StringIO import StringIO
2+
import sys
3+
4+
print 'To sys.stdout'
5+
6+
old_stdout = sys.stdout
7+
sio = StringIO()
8+
sys.stdout = sio
9+
10+
print 'To replaced sys.stdout'
11+
12+
sys.stdout = old_stdout
13+
print 'To original sys.stdout'
14+
15+
assert sio.tell() == len('To replaced sys.stdout')+1, 'Should had printed to StringIO, not STDOUT'

0 commit comments

Comments
 (0)