@@ -1284,6 +1284,23 @@ def _get_slots(cls):
12841284 raise TypeError (f"Slots of '{ cls .__name__ } ' cannot be determined" )
12851285
12861286
1287+ def _unwrap (func ):
1288+ # A copy of `inspect.unwrap()`, to avoid importing `inspect` module.
1289+ # Keep this in sync with the original.
1290+ f = func # remember the original func for error reporting
1291+ # Memoise by id to tolerate non-hashable objects, but store objects to
1292+ # ensure they aren't destroyed, which would allow their IDs to be reused.
1293+ memo = {id (f ): f }
1294+ recursion_limit = sys .getrecursionlimit ()
1295+ while not isinstance (func , type ) and hasattr (func , '__wrapped__' ):
1296+ func = func .__wrapped__
1297+ id_func = id (func )
1298+ if (id_func in memo ) or (len (memo ) >= recursion_limit ):
1299+ raise ValueError (f'wrapper loop when unwrapping { f !r} ' )
1300+ memo [id_func ] = func
1301+ return func
1302+
1303+
12871304def _update_func_cell_for__class__ (f , oldcls , newcls ):
12881305 # Returns True if we update a cell, else False.
12891306 if f is None :
@@ -1392,8 +1409,7 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
13921409
13931410 # If this is a wrapped function, unwrap it.
13941411 if not isinstance (member , type ) and hasattr (member , '__wrapped__' ):
1395- import inspect
1396- member = inspect .unwrap (member )
1412+ member = _unwrap (member )
13971413
13981414 if isinstance (member , types .FunctionType ):
13991415 if _update_func_cell_for__class__ (member , cls , newcls ):
0 commit comments