From 3f2f3ab08c551a21d3c9013b316d14e44ace0380 Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Thu, 27 Apr 2017 19:15:41 -0300 Subject: [PATCH 01/10] PEP-8 Nazi (should I follow the Google 2-space or PEP 4-space?) --- lib/sys.py | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/sys.py b/lib/sys.py index 7d98c488..ecd4f7fb 100644 --- a/lib/sys.py +++ b/lib/sys.py @@ -21,7 +21,7 @@ argv = [] for arg in Args: - argv.append(arg) + argv.append(arg) goversion = Version() maxint = MaxInt @@ -34,35 +34,36 @@ byteorder = 'little' class _Flags(object): - """Container class for sys.flags.""" - debug = 0 - py3k_warning = 0 - division_warning = 0 - division_new = 0 - inspect = 0 - interactive = 0 - optimize = 0 - dont_write_bytecode = 0 - no_user_site = 0 - no_site = 0 - ignore_environment = 0 - tabcheck = 0 - verbose = 0 - unicode = 0 - bytes_warning = 0 - hash_randomization = 0 + """Container class for sys.flags.""" + debug = 0 + py3k_warning = 0 + division_warning = 0 + division_new = 0 + inspect = 0 + interactive = 0 + optimize = 0 + dont_write_bytecode = 0 + no_user_site = 0 + no_site = 0 + ignore_environment = 0 + tabcheck = 0 + verbose = 0 + unicode = 0 + bytes_warning = 0 + hash_randomization = 0 flags = _Flags() def exc_info(): - e, tb = __frame__().__exc_info__() # pylint: disable=undefined-variable - t = None - if e: - t = type(e) - return t, e, tb + e, tb = __frame__().__exc_info__() # pylint: disable=undefined-variable + t = None + if e: + t = type(e) + return t, e, tb def exit(code=None): # pylint: disable=redefined-builtin - raise SystemExit(code) + raise SystemExit(code) + From a02c0169bef0032345ae160a6481d576124fcd8b Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Fri, 28 Apr 2017 12:30:21 -0300 Subject: [PATCH 02/10] [WIP] Hybrid sysmodule.go+sys.py Needs working __getattr__/__setattr__. Please wait if before merge --- lib/sys.py | 31 ++++++++++++++++++++++++++++++- runtime/sysmodule.go | 26 ++++++++++++++++++++++++++ testing/sysmodule_test.py | 15 +++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 runtime/sysmodule.go create mode 100644 testing/sysmodule_test.py diff --git a/lib/sys.py b/lib/sys.py index ecd4f7fb..0d93e7b4 100644 --- a/lib/sys.py +++ b/lib/sys.py @@ -15,24 +15,37 @@ """System-specific parameters and functions.""" from __go__.os import Args -from __go__.grumpy import SysModules, MaxInt, Stdin as stdin, Stdout as stdout, Stderr as stderr # pylint: disable=g-multiple-import +from __go__.grumpy import SysmoduleDict, SysModules, MaxInt, Stdin, Stdout, Stderr # pylint: disable=g-multiple-import from __go__.runtime import Version from __go__.unicode import MaxRune + +__all__ = ('stdin', 'stdout', 'stderr', 'argv', '_goversion', + 'maxint', 'maxsize', 'maxunicode', 'modules', 'py3kwarning', + 'warnoptions', 'byteorder', 'flags', 'exc_info', 'exit') + + argv = [] for arg in Args: argv.append(arg) goversion = Version() + +stdin = SysmoduleDict['stdin'] +stdout = SysmoduleDict['stdout'] +stderr = SysmoduleDict['stderr'] + maxint = MaxInt maxsize = maxint maxunicode = MaxRune modules = SysModules + py3kwarning = False warnoptions = [] # TODO: Support actual byteorder byteorder = 'little' + class _Flags(object): """Container class for sys.flags.""" debug = 0 @@ -67,3 +80,19 @@ def exc_info(): def exit(code=None): # pylint: disable=redefined-builtin raise SystemExit(code) + +# TODO: Clear this HACK: Should be the last lines of the python part of hybrid stuff +class _SysModule(object): + def __init__(self): + for k in ('__name__', '__file__') + __all__: + SysmoduleDict[k] = globals()[k] + def __setattr__(self, name, value): + SysmoduleDict[name] = value + def __getattr__(self, name): + resp = SysmoduleDict.get(name) + if res is None and name not in SysmoduleDict: + return super(_SysModule, self).__getattr__(name) + return resp + +modules = SysModules +modules['sys'] = _SysModule() diff --git a/runtime/sysmodule.go b/runtime/sysmodule.go new file mode 100644 index 00000000..c0b15777 --- /dev/null +++ b/runtime/sysmodule.go @@ -0,0 +1,26 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package grumpy + +var ( + // Sysmodule is the __dict__ of the native part of sys.py. + SysmoduleDict = newStringDict(map[string]*Object{ + "__file__": NewStr("").ToObject(), + "__name__": NewStr("_sys").ToObject(), + "stdin": Stdin.ToObject(), + "stdout": Stdout.ToObject(), + "stderr": Stderr.ToObject(), + }) +) diff --git a/testing/sysmodule_test.py b/testing/sysmodule_test.py new file mode 100644 index 00000000..6d4bc3a1 --- /dev/null +++ b/testing/sysmodule_test.py @@ -0,0 +1,15 @@ +from StringIO import StringIO +import sys + +print 'To sys.stdout' + +old_stdout = sys.stdout +sio = StringIO() +sys.stdout = sio + +print 'To replaced sys.stdout' + +sys.stdout = old_stdout +print 'To original sys.stdout' + +assert sio.tell() == len('To replaced sys.stdout')+1, 'Should had printed to StringIO, not STDOUT' \ No newline at end of file From 89546c2172481d49e9aa236846cad895ff060743 Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Fri, 28 Apr 2017 12:40:07 -0300 Subject: [PATCH 03/10] docs. --- runtime/sysmodule.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/sysmodule.go b/runtime/sysmodule.go index c0b15777..9dc2eaf9 100644 --- a/runtime/sysmodule.go +++ b/runtime/sysmodule.go @@ -15,7 +15,7 @@ package grumpy var ( - // Sysmodule is the __dict__ of the native part of sys.py. + // SysmoduleDict is the __dict__ of the native part of sys.py. SysmoduleDict = newStringDict(map[string]*Object{ "__file__": NewStr("").ToObject(), "__name__": NewStr("_sys").ToObject(), From f8ea5e5a4521fe6a1c2eccbf8d37e2262be60dcc Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Sat, 29 Apr 2017 17:49:37 -0300 Subject: [PATCH 04/10] Use __getattribute__ instead of __getattr__ --- lib/sys.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sys.py b/lib/sys.py index 0d93e7b4..9e09c1e9 100644 --- a/lib/sys.py +++ b/lib/sys.py @@ -88,10 +88,10 @@ def __init__(self): SysmoduleDict[k] = globals()[k] def __setattr__(self, name, value): SysmoduleDict[name] = value - def __getattr__(self, name): + def __getattribute__(self, name): # TODO: replace w/ __getattr__ when implemented resp = SysmoduleDict.get(name) if res is None and name not in SysmoduleDict: - return super(_SysModule, self).__getattr__(name) + return super(_SysModule, self).__getattribute__(name) return resp modules = SysModules From c09c642ed2fc2430e01b036dc24274d2b0640bde Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Sun, 30 Apr 2017 23:31:24 -0300 Subject: [PATCH 05/10] Typos --- lib/sys.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sys.py b/lib/sys.py index 9e09c1e9..c0cee4df 100644 --- a/lib/sys.py +++ b/lib/sys.py @@ -20,7 +20,7 @@ from __go__.unicode import MaxRune -__all__ = ('stdin', 'stdout', 'stderr', 'argv', '_goversion', +__all__ = ('stdin', 'stdout', 'stderr', 'argv', 'goversion', 'maxint', 'maxsize', 'maxunicode', 'modules', 'py3kwarning', 'warnoptions', 'byteorder', 'flags', 'exc_info', 'exit') @@ -90,7 +90,7 @@ def __setattr__(self, name, value): SysmoduleDict[name] = value def __getattribute__(self, name): # TODO: replace w/ __getattr__ when implemented resp = SysmoduleDict.get(name) - if res is None and name not in SysmoduleDict: + if resp is None and name not in SysmoduleDict: return super(_SysModule, self).__getattribute__(name) return resp From 66f52c00eff83868e0e7632e36fa2b0e1d56c329 Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Sat, 10 Jun 2017 15:29:09 -0300 Subject: [PATCH 06/10] sys.platform is back Conflicts: lib/sys.py --- lib/sys.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sys.py b/lib/sys.py index c0cee4df..5fd7e50a 100644 --- a/lib/sys.py +++ b/lib/sys.py @@ -15,13 +15,13 @@ """System-specific parameters and functions.""" from __go__.os import Args -from __go__.grumpy import SysmoduleDict, SysModules, MaxInt, Stdin, Stdout, Stderr # pylint: disable=g-multiple-import +from __go__.grumpy import SysmoduleDict, SysModules, MaxInt # pylint: disable=g-multiple-import from __go__.runtime import Version from __go__.unicode import MaxRune __all__ = ('stdin', 'stdout', 'stderr', 'argv', 'goversion', - 'maxint', 'maxsize', 'maxunicode', 'modules', 'py3kwarning', + 'maxint', 'maxsize', 'maxunicode', 'modules', 'platform', 'py3kwarning', 'warnoptions', 'byteorder', 'flags', 'exc_info', 'exit') From 96c75688652a0c54aa8d74cf45d605f221b2c03e Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Sat, 10 Jun 2017 15:29:30 -0300 Subject: [PATCH 07/10] Segregate HybridModule hack to its own module --- lib/_gomodulehacks.py | 31 +++++++++++++++++++++++++++++++ lib/sys.py | 18 +++--------------- 2 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 lib/_gomodulehacks.py diff --git a/lib/_gomodulehacks.py b/lib/_gomodulehacks.py new file mode 100644 index 00000000..d213527a --- /dev/null +++ b/lib/_gomodulehacks.py @@ -0,0 +1,31 @@ +# coding: utf-8 + +from __go__.grumpy import SysModules + + +def hybrid_module(modulename, modulefile, moduledict, all_attrs, globals_): + """ + Augment native 'moduledict' with Python-sourced parts + + Allows 'modulename' to use 'moduledict' from outside, + for example a Grumpy dict from native module. + + And does include the resulting module on sys.modules at the end. + """ + class HybridModule(object): + def __init__(self): + moduledict['__name__'] = modulename + moduledict['__file__'] = modulefile + for k in all_attrs: + moduledict[k] = globals_[k] + def __setattr__(self, name, value): + moduledict[name] = value + def __getattribute__(self, name): # TODO: replace w/ __getattr__ when implemented + resp = moduledict.get(name) + if resp is None and name not in moduledict: + return super(HybridModule, self).__getattribute__(name) + return resp + + finalmodule = HybridModule() + SysModules[modulename] = finalmodule + return finalmodule diff --git a/lib/sys.py b/lib/sys.py index 5fd7e50a..0da419c0 100644 --- a/lib/sys.py +++ b/lib/sys.py @@ -18,6 +18,7 @@ from __go__.grumpy import SysmoduleDict, SysModules, MaxInt # pylint: disable=g-multiple-import from __go__.runtime import Version from __go__.unicode import MaxRune +import _gomodulehacks __all__ = ('stdin', 'stdout', 'stderr', 'argv', 'goversion', @@ -81,18 +82,5 @@ def exit(code=None): # pylint: disable=redefined-builtin raise SystemExit(code) -# TODO: Clear this HACK: Should be the last lines of the python part of hybrid stuff -class _SysModule(object): - def __init__(self): - for k in ('__name__', '__file__') + __all__: - SysmoduleDict[k] = globals()[k] - def __setattr__(self, name, value): - SysmoduleDict[name] = value - def __getattribute__(self, name): # TODO: replace w/ __getattr__ when implemented - resp = SysmoduleDict.get(name) - if resp is None and name not in SysmoduleDict: - return super(_SysModule, self).__getattribute__(name) - return resp - -modules = SysModules -modules['sys'] = _SysModule() +# Should be the last line of the python part of hybrid stuff +_gomodulehacks.hybrid_module(__name__, __file__, SysmoduleDict, __all__, globals()) From f745cdb8bcda8c97be38eb699bea36e6993dd46d Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Sat, 10 Jun 2017 16:16:02 -0300 Subject: [PATCH 08/10] HybridModule is a Module subclass --- lib/_gomodulehacks.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/_gomodulehacks.py b/lib/_gomodulehacks.py index d213527a..84b7a071 100644 --- a/lib/_gomodulehacks.py +++ b/lib/_gomodulehacks.py @@ -1,6 +1,10 @@ # coding: utf-8 from __go__.grumpy import SysModules +import errno + +# For some reason, importing and extending grumpy.Module does not work. +Module = type(errno) def hybrid_module(modulename, modulefile, moduledict, all_attrs, globals_): @@ -12,7 +16,7 @@ def hybrid_module(modulename, modulefile, moduledict, all_attrs, globals_): And does include the resulting module on sys.modules at the end. """ - class HybridModule(object): + class HybridModule(Module): def __init__(self): moduledict['__name__'] = modulename moduledict['__file__'] = modulefile From 7c1cfdee7e1f80b23355fa7d6332eec273c4ed15 Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Sat, 10 Jun 2017 16:16:19 -0300 Subject: [PATCH 09/10] Docs on intended call of hybrid_module --- lib/_gomodulehacks.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/_gomodulehacks.py b/lib/_gomodulehacks.py index 84b7a071..3412e485 100644 --- a/lib/_gomodulehacks.py +++ b/lib/_gomodulehacks.py @@ -15,6 +15,10 @@ def hybrid_module(modulename, modulefile, moduledict, all_attrs, globals_): for example a Grumpy dict from native module. And does include the resulting module on sys.modules at the end. + + Should be called as: + hybrid_module(__name__, __file__, YourmoduleDict, __all__, globals()) + On the last line of the Python-part of the module """ class HybridModule(Module): def __init__(self): From b632fedfe4bb51d0d67a51c05bb9173e15ae4557 Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Sat, 10 Jun 2017 16:37:23 -0300 Subject: [PATCH 10/10] gofmt --- runtime/sysmodule.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/sysmodule.go b/runtime/sysmodule.go index 9dc2eaf9..a3622336 100644 --- a/runtime/sysmodule.go +++ b/runtime/sysmodule.go @@ -19,8 +19,8 @@ var ( SysmoduleDict = newStringDict(map[string]*Object{ "__file__": NewStr("").ToObject(), "__name__": NewStr("_sys").ToObject(), - "stdin": Stdin.ToObject(), - "stdout": Stdout.ToObject(), - "stderr": Stderr.ToObject(), + "stdin": Stdin.ToObject(), + "stdout": Stdout.ToObject(), + "stderr": Stderr.ToObject(), }) )