# HG changeset patch # User Paul Boddie # Date 1625003413 -7200 # Node ID 799711337453b7880a2f359f3390ca50a4eba6aa # Parent 67f4a86646748cd11c924ca88955d4bf56f57c15 Renamed the string class to str, replacing the str function with the new_str function, this being invoked specially by the string instantiation function. As with the Unicode type renaming, a more general instantiation mechanism might permit the new_str function to be part of the functionality of the str or basestring classes. diff -r 67f4a8664674 -r 799711337453 common.py --- a/common.py Tue Jun 29 22:41:05 2021 +0200 +++ b/common.py Tue Jun 29 23:50:13 2021 +0200 @@ -1594,9 +1594,7 @@ "Return the module name containing the given type 'name'." - if name == "string": - modname = "str" - elif name == "NoneType": + if name == "NoneType": modname = "none" else: modname = name @@ -1607,10 +1605,7 @@ "Return the type name provided by the given Python value 'name'." - if name == "str": - return "string" - else: - return name + return name def get_builtin_class(name): diff -r 67f4a8664674 -r 799711337453 generator.py --- a/generator.py Tue Jun 29 22:41:05 2021 +0200 +++ b/generator.py Tue Jun 29 23:50:13 2021 +0200 @@ -46,7 +46,7 @@ int_type = "__builtins__.int.int" list_type = "__builtins__.list.list" none_type = "__builtins__.none.NoneType" - string_type = "__builtins__.str.string" + string_type = "__builtins__.str.str" tuple_type = "__builtins__.tuple.tuple" type_type = "__builtins__.core.type" unicode_type = "__builtins__.unicode.unicode" @@ -1215,6 +1215,21 @@ encode_instantiator_pointer(path), ) + # Special-case the string types. + + # Here, the __builtins__.str.new_str function is called with the + # initialiser's parameter. + + elif path == self.string_type: + print >>f_code, """\ +__attr %s(__attr __self, __attr obj) +{ + return __fn___builtins___str_new_str(__NULL, obj); +} +""" % ( + encode_instantiator_pointer(path), + ) + # Generic instantiation support. else: @@ -1285,6 +1300,6 @@ return 0; } -""" % encode_function_pointer("__builtins__.str.str") +""" % encode_instantiator_pointer("__builtins__.str.str") # vim: tabstop=4 expandtab shiftwidth=4 diff -r 67f4a8664674 -r 799711337453 inspector.py --- a/inspector.py Tue Jun 29 22:41:05 2021 +0200 +++ b/inspector.py Tue Jun 29 23:50:13 2021 +0200 @@ -3,8 +3,7 @@ """ Inspect and obtain module structure. -Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, - 2018, 2019 Paul Boddie +Copyright (C) 2007-2019, 2021 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 @@ -78,13 +77,13 @@ ref = self.get_builtin("module") self.set_name("__class__", ref) - self.set_name("__name__", self.get_constant("string", self.name).reference()) - self.set_name("__file__", self.get_constant("string", filename).reference()) + self.set_name("__name__", self.get_constant("str", self.name).reference()) + self.set_name("__file__", self.get_constant("str", filename).reference()) # Reserve a constant for the encoding. if self.encoding: - self.get_constant("string", self.encoding) + self.get_constant("str", self.encoding) # Get module-level attribute usage details. @@ -531,7 +530,7 @@ # Provide leafname, parent and context attributes. parent, leafname = class_name.rsplit(".", 1) - self.set_name("__name__", self.get_constant("string", leafname).reference()) + self.set_name("__name__", self.get_constant("str", leafname).reference()) if class_name != "__builtins__.core.function": self.set_name("__parent__") @@ -681,7 +680,7 @@ # Define a leafname attribute value for the function instance. - ref = self.get_builtin_class("string") + ref = self.get_builtin_class("str") self.reserve_constant(function_name, name, ref.get_origin()) # Track attribute usage within the namespace. diff -r 67f4a8664674 -r 799711337453 lib/__builtins__/__init__.py --- a/lib/__builtins__/__init__.py Tue Jun 29 22:41:05 2021 +0200 +++ b/lib/__builtins__/__init__.py Tue Jun 29 23:50:13 2021 +0200 @@ -68,7 +68,7 @@ from __builtins__.none import None, NoneType from __builtins__.notimplemented import NotImplemented, NotImplementedType from __builtins__.set import frozenset, set -from __builtins__.str import basestring, str, string +from __builtins__.str import basestring, str from __builtins__.tuple import tuple from __builtins__.unicode import unicode diff -r 67f4a8664674 -r 799711337453 lib/__builtins__/buffer.py --- a/lib/__builtins__/buffer.py Tue Jun 29 22:41:05 2021 +0200 +++ b/lib/__builtins__/buffer.py Tue Jun 29 23:50:13 2021 +0200 @@ -53,7 +53,7 @@ if isinstance(s, buffer): list_concat(self, s.__data__) - elif isinstance(s, string): + elif isinstance(s, str): list_append(self, s) else: list_append(self, str(s)) diff -r 67f4a8664674 -r 799711337453 lib/__builtins__/str.py --- a/lib/__builtins__/str.py Tue Jun 29 22:41:05 2021 +0200 +++ b/lib/__builtins__/str.py Tue Jun 29 23:50:13 2021 +0200 @@ -3,7 +3,7 @@ """ String objects. -Copyright (C) 2015, 2016, 2017 Paul Boddie +Copyright (C) 2015, 2016, 2017, 2021 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 @@ -576,7 +576,7 @@ def upper(self): pass -class string(basestring): +class str(basestring): "A plain string of bytes." @@ -607,7 +607,7 @@ l = get_using(basestring.__get_multiple_items__, self)(start, end, step) return "".join(l) -def str(obj): +def new_str(obj): "Return the string representation of 'obj'." diff -r 67f4a8664674 -r 799711337453 templates/native/common.c --- a/templates/native/common.c Tue Jun 29 22:41:05 2021 +0200 +++ b/templates/native/common.c Tue Jun 29 23:50:13 2021 +0200 @@ -1,6 +1,6 @@ /* Common operations for native functions. -Copyright (C) 2016, 2017, 2018 Paul Boddie +Copyright (C) 2016, 2017, 2018, 2021 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 @@ -29,7 +29,7 @@ __attr __new_str(char *s, int size) { /* Create a new string and mutate the __data__, __size__ and __key__ attributes. */ - __attr attr = __NEWINSTANCE(__builtins___str_string); + __attr attr = __NEWINSTANCE(__builtins___str_str); __store_via_object(__VALUE(attr), __data__, (__attr) {.strvalue=s}); __store_via_object(__VALUE(attr), __size__, __INTVALUE(size)); __store_via_object(__VALUE(attr), __key__, __NULL); diff -r 67f4a8664674 -r 799711337453 tests/identity.py --- a/tests/identity.py Tue Jun 29 22:41:05 2021 +0200 +++ b/tests/identity.py Tue Jun 29 23:50:13 2021 +0200 @@ -1,7 +1,7 @@ -print isinstance("string", string) # True +print isinstance("string", str) # True print isinstance("string", int) # False print isinstance(123, int) # True -print isinstance(123, string) # False +print isinstance(123, str) # False print class A: diff -r 67f4a8664674 -r 799711337453 tests/unicode.py --- a/tests/unicode.py Tue Jun 29 22:41:05 2021 +0200 +++ b/tests/unicode.py Tue Jun 29 23:50:13 2021 +0200 @@ -18,31 +18,31 @@ s2 = b"\xe6\xf8\xe5" print "ISO-8859-15 values:" print s2 # æøå -print s2.__class__ # __builtins__.str.string +print s2.__class__ # __builtins__.str.str print len(s2) # 3 s3 = "\xe6\xf8\xe5" print "ISO-8859-15 values:" print s3 # æøå -print s3.__class__ # __builtins__.str.string +print s3.__class__ # __builtins__.str.str print len(s3) # 3 s4 = b"\u00e6\u00f8\u00e5" print "Untranslated values:" print s4 # \u00e6\u00f8\u00e5 -print s4.__class__ # __builtins__.str.string +print s4.__class__ # __builtins__.str.str print len(s4) # 18 s5 = b"\346\370\345" print "ISO-8859-15 values:" print s5 # æøå -print s5.__class__ # __builtins__.str.string +print s5.__class__ # __builtins__.str.str print len(s5) # 3 s6 = "\346\370\345" print "ISO-8859-15 values:" print s6 # æøå -print s6.__class__ # __builtins__.str.string +print s6.__class__ # __builtins__.str.str print len(s6) # 3 s7 = r"\346\370\345" @@ -98,7 +98,7 @@ u5 = "\u00e6\u00f8\u00e5" print "Unicode values:" print u5 # æøå -print u5.__class__ # __builtins__.unicode.ut8string +print u5.__class__ # __builtins__.unicode.unicode print len(u5) # 3 # Test some untranslated values. @@ -106,7 +106,7 @@ u6 = "\\u00e6\\u00f8\\u00e5" print "Untranslated values:" print u6 # \u00e6\u00f8\u00e5 -print u6.__class__ # __builtins__.unicode.ut8string +print u6.__class__ # __builtins__.unicode.unicode print len(u6) # 18 # Test Unicode values. @@ -114,7 +114,7 @@ u7 = u"\346\370\345" print "Unicode values:" print u7 # æøå -print u7.__class__ # __builtins__.unicode.ut8string +print u7.__class__ # __builtins__.unicode.unicode print len(u7) # 3 # Test Unicode values. @@ -122,7 +122,7 @@ u8 = ur"\346\370\345" print "Untranslated values:" print u8 # \346\370\345 -print u8.__class__ # __builtins__.unicode.ut8string +print u8.__class__ # __builtins__.unicode.unicode print len(u8) # 12 # Test invalid sequences. @@ -137,7 +137,7 @@ u10 = "\u00e6\xf8\u00e5" print "ISO-8859-15 values:" print u10 # \u00e6ø\u00e5 -print u10.__class__ # __builtins__.str.string +print u10.__class__ # __builtins__.str.str print len(u10) # 13 # Combine bytes and text. @@ -146,7 +146,7 @@ su = s + u print "ISO-8859-15 values:" print su # ÆØÅæøå -print su.__class__ # __builtins__.str.string +print su.__class__ # __builtins__.str.str print len(su) # 6 # Combine text and bytes. @@ -155,7 +155,7 @@ us = u + s print "ISO-8859-15 values:" print us # æøåÆØÅ -print us.__class__ # __builtins__.str.string +print us.__class__ # __builtins__.str.str print len(us) # 6 # Combine text and text.