Don't waste any time on this.
Instead, if you want to play with MemoryError
exceptions create a design that isolates object construction so you can test it.
Instead of this
for i in range(1000000000000000000000):
try:
y = AnotherClass()
except MemoryError:
# the thing we wanted to test
Consider this.
for i in range(1000000000000000000000):
try:
y = makeAnotherClass()
except MemoryError:
# the thing we wanted to test
This requires one tiny addition to your design.
class AnotherClass( object ):
def __init__( self, *args, **kw ):
blah blah blah
def makeAnotherClass( *args, **kw ):
return AnotherClass( *args, **kw )
The extra function -- in the long run -- proves to be a good design pattern. It's a Factory, and you often need something like it.
You can then replace this makeAnotherClass
with something like this.
class Maker( object ):
def __init__( self, count= 12 ):
self.count= count
def __call__( self, *args, **kw ):
if self.count == 0:
raise MemoryError
self.count -= 1
return AnotherClass( *args, **kw )
makeAnotherClass= Maker( count=12 )
This version will raise an exception without you having to limit memory in any obscure, unsupportable, complex or magical way.