Skip to content

Commit 2750a3a

Browse files
committed
Improve registration robustness for instances of registered classes
1 parent 704dada commit 2750a3a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Pyro5/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ def register(self, obj_or_class, objectId=None, force=False, weak=False):
660660
obj_or_class._pyroInstancing = ("session", None)
661661
if not force:
662662
if hasattr(obj_or_class, "_pyroId") and obj_or_class._pyroId != "": # check for empty string is needed for Cython
663-
raise errors.DaemonError("object or class already has a Pyro id")
663+
pyro_id = obj_or_class._pyroId
664+
if pyro_id and self.objectsById.get(pyro_id) is obj_or_class:
665+
raise errors.DaemonError("object or class already has a Pyro id")
664666
if objectId in self.objectsById:
665667
raise errors.DaemonError("an object or class is already registered with that id")
666668
# set some pyro attributes
@@ -674,7 +676,7 @@ def register(self, obj_or_class, objectId=None, force=False, weak=False):
674676
else:
675677
ser.register_type_replacement(type(obj_or_class), _pyro_obj_to_auto_proxy)
676678
# register the object/class in the mapping
677-
self.objectsById[obj_or_class._pyroId] = (obj_or_class if not weak else weakref.ref(obj_or_class))
679+
self.objectsById[obj_or_class._pyroId] = obj_or_class if not weak else weakref.ref(obj_or_class)
678680
if weak: weakref.finalize(obj_or_class,self.unregister,objectId)
679681
return self.uriFor(objectId)
680682

tests/test_daemon.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,24 @@ def testRegisterTwice(self):
182182
assert not(hasattr(o1, "_pyroId"))
183183
assert not(hasattr(o1, "_pyroDaemon"))
184184
o1._pyroId = "FOOBAR"
185-
with pytest.raises(DaemonError) as x:
186-
d.register(o1)
187-
assert str(x.value) == "object or class already has a Pyro id"
185+
d.register(o1)
188186
o1._pyroId = ""
189187
d.register(o1) # with empty-string _pyroId register should work
190188

189+
def testRegisterInstanceAndClass(self):
190+
with Pyro5.server.Daemon(port=0) as d:
191+
o1 = MyObj("object1")
192+
o2 = MyObj("object2")
193+
d.register(MyObj)
194+
d.register(o1)
195+
d.register(o2)
196+
with pytest.raises(DaemonError) as x:
197+
d.register(o2)
198+
assert str(x.value) == "object or class already has a Pyro id"
199+
with pytest.raises(DaemonError) as x:
200+
d.register(MyObj)
201+
assert str(x.value) == "object or class already has a Pyro id"
202+
191203
def testRegisterTwiceForced(self):
192204
with Pyro5.server.Daemon(port=0) as d:
193205
o1 = MyObj("object1")

0 commit comments

Comments
 (0)