# HG changeset patch # User Paul Boddie # Date 1474575350 -7200 # Node ID bf07fa820dd1b7d18ed589599e6df8d97d938cc0 # Parent be69c5dc27abec24d6549ec35dc26e6c36cac57b Forbid self usage outside methods. Tidied/fixed affected built-in operations. diff -r be69c5dc27ab -r bf07fa820dd1 inspector.py --- a/inspector.py Thu Sep 22 22:08:37 2016 +0200 +++ b/inspector.py Thu Sep 22 22:15:50 2016 +0200 @@ -776,6 +776,11 @@ self.set_special(n.name, value) return value + # Test for self usage, which is only allowed in methods. + + if n.name == "self" and not (self.in_function and self.in_class): + raise InspectError("Use of self is only allowed in methods.", path, n) + # Record usage of the name. self.record_name(n.name) diff -r be69c5dc27ab -r bf07fa820dd1 lib/__builtins__/int.py --- a/lib/__builtins__/int.py Thu Sep 22 22:08:37 2016 +0200 +++ b/lib/__builtins__/int.py Thu Sep 22 22:15:50 2016 +0200 @@ -19,6 +19,7 @@ this program. If not, see . """ +from __builtins__.operator import _binary_op, _negate import native class int(object): @@ -135,24 +136,4 @@ "Return whether this int is non-zero." return _negate(native._int_eq(self, 0)) -# Utility functions. - -def _binary_op(self, other, op): - - "Test the type of 'other' and perform 'op'." - - if self.__class__ is other.__class__: - return op(self, other) - else: - return NotImplemented - -def _negate(result): - - "Negate any valid logical value." - - if result is NotImplemented: - return result - else: - return not result - # vim: tabstop=4 expandtab shiftwidth=4 diff -r be69c5dc27ab -r bf07fa820dd1 lib/__builtins__/operator.py --- a/lib/__builtins__/operator.py Thu Sep 22 22:08:37 2016 +0200 +++ b/lib/__builtins__/operator.py Thu Sep 22 22:15:50 2016 +0200 @@ -3,7 +3,7 @@ """ Operator-related functions. -Copyright (C) 2015 Paul Boddie +Copyright (C) 2015, 2016 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,12 +19,12 @@ this program. If not, see . """ -def _binary_op(self, other, op): +def _binary_op(this, other, op): - "Test the type of 'other' and perform 'op'." + "Test the types of 'this' and 'other', performing 'op' if identical." - if self.__class__ is other.__class__: - return op(self, other) + if this.__class__ is other.__class__: + return op(this, other) else: return NotImplemented