3

In this Python code

import gc
gc.disable()
<some code ...>
MyClass()
<more code...>

I am hoping that the anonymous object created by MyClass constructor would not be garbage-collected. MyClass actually links to a shared object library of C++ code, and there through raw memory pointers I am able to inspect the contents of the anonymous object.

I can then see that the object is immediately corrupted (garbage collected).

How to prevent Python garbage collection for everything?

I have to keep this call anonymous. I cannot change the part of the code MyClass() - it has to be kept as is.

MyClass() has to be kept as is, because it is an exact translation from C++ (by way of SWIG) and the two should be identical for the benefit of people who translate.

I have to prevent the garbage collection by some "initialization code", that is only called once at the beginning of the program. I cannot touch anything after that.

10
  • 5
    Why don't you just assign it to something? That seems like a simpler solution than fiddling with gc details. Commented Nov 17, 2013 at 20:16
  • @BrenBarn I know. I specifically do not want to assign, but keep it anonymous. Commented Nov 17, 2013 at 20:17
  • 1
    @MarkGaleck: Why can't you assign it? Will something bad happen if you do, or are you searching for only the most convoluted solution to your self-imposed problem? Commented Nov 17, 2013 at 20:20
  • 1
    This feels like an XY problem to me, and I'm not sure what the real problem being solved is. This means that it's not clear what workarounds would be acceptable. Commented Nov 17, 2013 at 20:41
  • 3
    Okay, so add `_anonymous_object =\` to the line before. Your need to have the object be anonymous makes little sense in Python, and I don't follow the SWIG-related constraints. Commented Nov 17, 2013 at 20:47

1 Answer 1

4

The "garbage collector" referred to in gc is only used for resolving circular references. In Python (at least in the main C implementation, CPython) the main method of memory management is reference counting. In your code, the result of MyClass() has no references, so will always be disposed immediately. There's no way of preventing that.

What is not clear, even with your edit, is why you can't simply assign it to something? If the target audience is "people who translate", those people can presumably read, so write a comment explaining why you're doing the assignment.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not translating myself, they are translating. And it has to be made as easy as possible for them. Your answer is good because it explains to me, that there is no way to accomplish that. So "they" will have to translate it with assignment.
@MarkGaleck, you could change your MyClass() constructor to save a reference to self. Where? Anywhere. Say, append self to some module-global list hidden in the module. Then it would be invisible to users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.