@@ -128,13 +128,16 @@ class of the options message. The name of the class is required in case
128128 """
129129 self ._features = None
130130 self .file = file
131- self ._options = options
132- self ._loaded_options = None
131+ self ._original_options = options
132+ # These two fields are duplicated as a compatibility shim for old gencode
133+ # that resets them. In 26.x (cl/580304039) we renamed _options to,
134+ # _loaded_options breaking backwards compatibility.
135+ self ._options = self ._loaded_options = None
133136 self ._options_class_name = options_class_name
134137 self ._serialized_options = serialized_options
135138
136139 # Does this descriptor have non-default options?
137- self .has_options = (self ._options is not None ) or (
140+ self .has_options = (self ._original_options is not None ) or (
138141 self ._serialized_options is not None
139142 )
140143
@@ -186,7 +189,8 @@ def _ResolveFeatures(self, edition, raw_options):
186189
187190 def _LazyLoadOptions (self ):
188191 """Lazily initializes descriptor options towards the end of the build."""
189- if self ._loaded_options :
192+ if self ._options and self ._loaded_options == self ._options :
193+ # If neither has been reset by gencode, use the cache.
190194 return
191195
192196 # pylint: disable=g-import-not-at-top
@@ -206,12 +210,12 @@ def _LazyLoadOptions(self):
206210 descriptor_pb2 .Edition .Value (edition ), options_class ()
207211 )
208212 with _lock :
209- self ._loaded_options = options_class ()
213+ self ._options = self . _loaded_options = options_class ()
210214 if not self ._features :
211215 self ._features = features
212216 else :
213217 if not self ._serialized_options :
214- options = self ._options
218+ options = self ._original_options
215219 else :
216220 options = _ParseOptions (options_class (), self ._serialized_options )
217221
@@ -220,13 +224,13 @@ def _LazyLoadOptions(self):
220224 descriptor_pb2 .Edition .Value (edition ), options
221225 )
222226 with _lock :
223- self ._loaded_options = options
227+ self ._options = self . _loaded_options = options
224228 if not self ._features :
225229 self ._features = features
226230 if options .HasField ('features' ):
227231 options .ClearField ('features' )
228232 if not options .SerializeToString ():
229- self ._loaded_options = options_class ()
233+ self ._options = self . _loaded_options = options_class ()
230234 self .has_options = False
231235
232236 def GetOptions (self ):
@@ -235,9 +239,10 @@ def GetOptions(self):
235239 Returns:
236240 The options set on this descriptor.
237241 """
238- if not self ._loaded_options :
242+ # If either has been reset by gencode, reload options.
243+ if not self ._options or not self ._loaded_options :
239244 self ._LazyLoadOptions ()
240- return self ._loaded_options
245+ return self ._options
241246
242247
243248class _NestedDescriptorBase (DescriptorBase ):
0 commit comments