3

Sounds quite simple, but I couldn’t found a solution anywhere. I need to copy a built‑in function object (I’m not talking about the function code but the object that represent it) such as :

id(func)≠id(func_copy)

Where func and func_copy share the same object type.
However if (for example) I do :

import copy
new_type=copy.copy(oct)

then id(new_type) and id(oct) are still equal.

I also couldn’t found how to modify oct attributes or append attribute to it (it would have resulted in changing the idea).

Update

This question isn’t a duplicate because copy python function and built‑in function isn’t the same problem : you can’t copy built‑in function objects with the same methods for python function objects and vice versa.

So none of the solutions on How can I make a deepcopy of a function in Python? can work here.

Please vote to re‑open

Community
  • 1
  • 1
user2284570
  • 2,891
  • 3
  • 26
  • 74
  • copy.deepcopy might work? – reptilicus Sep 06 '16 at 15:54
  • @reptilicus : nope I tried it and it doesn’t. – user2284570 Sep 06 '16 at 15:56
  • Why? The simplest solution may to just make a new function `wrapper(*args, **kwargs)` that calls the original function. – Jared Goguen Sep 06 '16 at 15:57
  • 2
    http://stackoverflow.com/questions/6527633/how-can-i-make-a-deepcopy-of-a-function-in-python – reptilicus Sep 06 '16 at 15:58
  • 1
    but yes, "why" is a good question... – reptilicus Sep 06 '16 at 15:58
  • @JaredGoguen : Well internally, `PyObject` representing built‑in functions are loaded into read‑only memory after python initialized. I want a `PyObject` of type `PyCFunctionObject` stored into a place of ʀᴀᴍ with write permissions *(I’m still talking about the .rdata variable, not the .text segment containing the code)*. – user2284570 Sep 06 '16 at 15:59
  • @reptilicus : The solutions don’t work for built‑in functions. – user2284570 Sep 06 '16 at 16:00
  • What about: `func_copy = lambda: func` or `func_copy = lambda x: func(x)`? I don't really understand your use case, but this was just something that popped into my head. – elethan Sep 06 '16 at 16:01
  • @elethan : then `func_copy` is wrapper of `func`, not a copy of the `func` object. `func_copy` is python function, not a built‑in function. – user2284570 Sep 06 '16 at 16:07
  • Are you trying to write some sort of exploit again? You really need to make it clearer from the start that you're trying to defeat Python's protections, or you'll just get people telling you that Python doesn't let you do that. – user2357112 Sep 06 '16 at 16:23
  • @user2357112 : If python doesn’t let me do that, I have still the golden hammer way to it. But normally I think python let you to copy all kind of objects or class. – user2284570 Sep 06 '16 at 16:26
  • "But normally I think python let you to copy all kind of objects." - nope! That's completely wrong. – user2357112 Sep 06 '16 at 16:27
  • @user2357112 : do you mean the `__dict__` attribute is a requirement ? But as I said, the gold hammer way would be to read and parse memory for changing C structures *(would require several tens line of code)*. – user2284570 Sep 06 '16 at 16:29
  • Whether an object has a `__dict__` has nothing to do with whether Python will let you copy it. – user2357112 Sep 06 '16 at 16:31
  • If you're talking about the underlying C representation of things then by very definition _you're not talking about Python!_ You're talking about implementation details, which makes this question similar to "Why is `x = 3; y = 3; x is y == True` in Python?" Because it's only applicable to CPython. If you want to know how copy works _read the source_. – Wayne Werner Sep 07 '16 at 14:02
  • @WayneWerner : Actually, in cpython, a C *PyObject and a python object are the same thing. And yes, my question is CPython specific, hence the tag. – user2284570 Sep 07 '16 at 14:34

0 Answers0