Patching a class method
davidkrisch opened this issue · 2 comments
I'm having trouble getting a "Hello Patchy" example working - patching a class method is specifically what I would like to do. I am working towards using patchy to apply a single line patch to django.contrib.sites.SiteManager.get_current
in Django 1.8.
Thank you in advance for any guidance...
hello.py
class Hello(object):
def say(self):
return "Hey"
hello2.py
def say(self):
return "Hello"
To get the diff I did
$ diff -u hello.py hello2.py
--- hello.py 2016-09-14 12:44:38.000000000 -1000
+++ hello2.py 2016-09-14 13:11:13.000000000 -1000
@@ -1,3 +1,2 @@
-class Hello(object):
- def say(self):
- return "Hey"
+def say(self):
+ return "Hello"
Then I apply the patch in main.py
import patchy
from hello import Hello
patchy.patch(Hello.say, '''\
--- hello.py 2016-09-14 12:44:38.000000000 -1000
+++ hello2.py 2016-09-14 13:11:13.000000000 -1000
@@ -1,3 +1,2 @@
-class Hello(object):
- def say(self):
- return "Hey"
+def say(self):
+ return "Hello"
''')
h = Hello()
print h.say()
I get the following error
ValueError: Could not apply the patch to 'say'. The message from `patch` was:
patching file /var/folders/wb/ylzhww1j12zcq32hbr7q7mt80000gp/T/patchym8Kcyv/say.py
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file /var/folders/wb/ylzhww1j12zcq32hbr7q7mt80000gp/T/patchym8Kcyv/say.py.rej
The code to patch was:
def say(self):
return "Hey"
The patch was:
--- hello.py 2016-09-14 12:44:38.000000000 -1000
+++ hello2.py 2016-09-14 13:11:13.000000000 -1000
@@ -1,3 +1,2 @@
-class Hello(object):
- def say(self):
- return "Hey"
+def say(self):
+ return "Hello"
What am I doing wrong? (thank you)
Got it! I read the README a little closer and figured it out. The diff
shouldn't include the original file.
hello.py
class Hello(object):
def say(self):
return "Hey"
hello1.py
def say(self):
return "Hey"
hello2.py
def say(self):
return "Hello"
To get the diff...
$ diff -u hello1.py hello2.py
--- hello1.py 2016-09-14 13:32:53.000000000 -1000
+++ hello2.py 2016-09-14 13:11:13.000000000 -1000
@@ -1,2 +1,2 @@
def say(self):
- return "Hey"
+ return "Hello"
main.py
import patchy
from hello import Hello
patchy.patch(Hello.say, '''\
@@ -1,2 +1,2 @@
def say(self):
- return "Hey"
+ return "Hello"
''')
h = Hello()
print h.say()
Running main.py
works!
$ python main.py
Hello
That's it! Yup patchy operates only on a function basis so including the class definition screws it up.