Conversation
Edited 1 hour ago
Edited: Wrong diagnosis, sry!
4
1
1

@buherator IDA and Ghidra specifically, or in a script (shell) also? They might handle state differently

0
1
0

@buherator shouldn't happen, can you provide a reproducible example?

if i do

def write_to(path, s):
with open(path, "w") as fd:
fd.write(s)

write_to("/tmp/a", "hello ")
write_to("/tmp/a", "world")

And run it, i do see "world" only in the output file. Is that different from what you are doing?

1
0
0
@tmr232 @nieldk @cs @tshirtman Thanks for the responses, but my diagnosis was wrong - although I still don't quite get it: it turns out that the object I'm writing out (json.dump) contains a list that gets extended by the script (this sort of explains why I'm writing out more and more data), BUT the list is part of an object that I reinstantiate on every run, which should empty the list (I call super() with an empty list). Problem is the (super)class is generated code and I suspect the list is not in fact an object member but a static class variable that may cause this??
1
0
0

@tshirtman @buherator

Hi my friend
plz can you quote and donate to help my family plz help me 🙏🙏🙏🙏

0
0
0

@buherator @tmr232 @nieldk @tshirtman possibly so; it sounds like the root cause is reuse of the same list over multiple object instantiations, one way or another.

The classic python error in this form is functions like:

def f(items=[]):

providing an empty list as the default for the items parameter. But that list is made at function definition time, and gets reused on every call where the default is used.

I'd be happy to help debug your real code if you'd like. Mastadon a terrible place for such a conversionation though. Email, slack, discord?

1
0
1

@cs @buherator @tmr232 @nieldk i concur, very common bug in Python code, everyone has to be bitten once to learn this lesson.

Using a mutable default value is generally a mistake in python.

It makes sense once you understand that the python signature is a line of code evaluated when the module is imported, and that the value stored as default is an object created at that time, not every time the function is called.

1
0
1
@tshirtman @cs @tmr232 @nieldk You are right and it even seems to be my code dammit! Thanks for your help!
1
0
3

@buherator @cs @nieldk @tshirtman might be worth giving Ruff and mypy a go. They catch _a lot_ of those gotchas. Here specifically - https://docs.astral.sh/ruff/rules/mutable-argument-default/

0
0
1