|
16 | 16 | # under the License. |
17 | 17 | """Runtime container structures.""" |
18 | 18 | import tvm._ffi |
19 | | -from tvm._ffi.base import string_types |
20 | | -from tvm.runtime import Object, ObjectTypes |
21 | | -from tvm.runtime import _ffi_api |
| 19 | +from .object import Object, PyNativeObject |
| 20 | +from .object_generic import ObjectTypes |
| 21 | +from . import _ffi_api |
| 22 | + |
22 | 23 |
|
23 | 24 | def getitem_helper(obj, elem_getter, length, idx): |
24 | 25 | """Helper function to implement a pythonic getitem function. |
@@ -112,64 +113,26 @@ def tuple_object(fields=None): |
112 | 113 |
|
113 | 114 |
|
114 | 115 | @tvm._ffi.register_object("runtime.String") |
115 | | -class String(Object): |
116 | | - """The string object. |
| 116 | +class String(str, PyNativeObject): |
| 117 | + """TVM runtime.String object, represented as a python str. |
117 | 118 |
|
118 | 119 | Parameters |
119 | 120 | ---------- |
120 | | - string : str |
121 | | - The string used to construct a runtime String object |
122 | | -
|
123 | | - Returns |
124 | | - ------- |
125 | | - ret : String |
126 | | - The created object. |
| 121 | + content : str |
| 122 | + The content string used to construct the object. |
127 | 123 | """ |
128 | | - def __init__(self, string): |
129 | | - self.__init_handle_by_constructor__(_ffi_api.String, string) |
130 | | - |
131 | | - def __str__(self): |
132 | | - return _ffi_api.GetStdString(self) |
133 | | - |
134 | | - def __len__(self): |
135 | | - return _ffi_api.GetStringSize(self) |
136 | | - |
137 | | - def __hash__(self): |
138 | | - return _ffi_api.StringHash(self) |
139 | | - |
140 | | - def __eq__(self, other): |
141 | | - if isinstance(other, string_types): |
142 | | - return self.__str__() == other |
143 | | - |
144 | | - if not isinstance(other, String): |
145 | | - return False |
146 | | - |
147 | | - return _ffi_api.CompareString(self, other) == 0 |
148 | | - |
149 | | - def __ne__(self, other): |
150 | | - return not self.__eq__(other) |
151 | | - |
152 | | - def __gt__(self, other): |
153 | | - return _ffi_api.CompareString(self, other) > 0 |
154 | | - |
155 | | - def __lt__(self, other): |
156 | | - return _ffi_api.CompareString(self, other) < 0 |
157 | | - |
158 | | - def __getitem__(self, key): |
159 | | - return self.__str__()[key] |
160 | | - |
161 | | - def startswith(self, string): |
162 | | - """Check if the runtime string starts with a given string |
163 | | -
|
164 | | - Parameters |
165 | | - ---------- |
166 | | - string : str |
167 | | - The provided string |
168 | | -
|
169 | | - Returns |
170 | | - ------- |
171 | | - ret : boolean |
172 | | - Return true if the runtime string starts with the given string, |
173 | | - otherwise, false. |
174 | | - """ |
175 | | - return self.__str__().startswith(string) |
| 124 | + __slots__ = ["__tvm_object__"] |
| 125 | + |
| 126 | + def __new__(cls, content): |
| 127 | + """Construct from string content.""" |
| 128 | + val = str.__new__(cls, content) |
| 129 | + val.__init_tvm_object_by_constructor__(_ffi_api.String, content) |
| 130 | + return val |
| 131 | + |
| 132 | + # pylint: disable=no-self-argument |
| 133 | + def __from_tvm_object__(cls, obj): |
| 134 | + """Construct from a given tvm object.""" |
| 135 | + content = _ffi_api.GetFFIString(obj) |
| 136 | + val = str.__new__(cls, content) |
| 137 | + val.__tvm_object__ = obj |
| 138 | + return val |
0 commit comments