user2456373
user2456373

Reputation: 53

Error in Python2.7 takes exactly 2 arguments (1 given)

I am a beginner in Python. I wonder why it is throwing an error. I am getting an error saying TypeError: client_session() takes exactly 2 arguments (1 given) The client_session method returns the SecureCookie object.

I have this code here

from werkzeug.utils import cached_property
from werkzeug.contrib.securecookie import SecureCookie
from werkzeug.wrappers import BaseRequest, AcceptMixin, ETagRequestMixin,

class Request(BaseRequest):

 def client_session(self,SECRET_KEY1):
  data = self.cookies.get('session_data')
  print " SECRET_KEY " ,  SECRET_KEY1 
  if not data: 
   print "inside if data" 
   cookie = SecureCookie({"SECRET_KEY":    SECRET_KEY1},secret_key=SECRET_KEY1)
   cookie.serialize() 
   return cookie 
  print 'self.form[login.name] ', self.form['login.name'] 
  print 'data new' , data
  return SecureCookie.unserialize(data, SECRET_KEY1)


#and another 
class Application(object):
 def __init__(self):
  self.SECRET_KEY = os.urandom(20)

 def dispatch_request(self, request):
  return self.application(request)

 def application(self,request): 
  return request.client_session(self.SECRET_KEY).serialize() 


 # This is our externally-callable WSGI entry point
 def __call__(self, environ, start_response):
  """Invoke our WSGI application callable object""" 
  return self.wsgi_app(environ, start_response)

Upvotes: 4

Views: 2312

Answers (1)

zmo
zmo

Reputation: 24802

Usually, that means you're calling the client_session as an unbound method, giving it only one argument. You should introspect a bit, and look at what is exactly the request you're using in the application() method, maybe it is not what you're expecting it to be.

To be sure what it is, you can always add a debug printout point:

print "type: ", type(request)
print "methods: ", dir(request)

and I expect you'll see that request is the original Request class that werkzeug gives you...

Here, you're extending the BaseRequest of werkzeug, and in application() you expect that werkzeug knows about your own implementation of the BaseRequest class magically. But if you read the zen of python, you will know that "explicit is better than implicit", so python never does stuff magically, you have to tell your library that you made a change somehow.

So after reading werkzeug's documentation, you can find out that this is actually the case:

The request object is created with the WSGI environment as first argument and will add itself to the WSGI environment as 'werkzeug.request' unless it’s created with populate_request set to False.

This may not be totally clear for people who don't know what werkzeug is, and what is the design logic behind.

But a simple google lookup, showed usage examples of BaseRequest:

I only googled for from werkzeug.wrappers import BaseRequest`

So now, you should be able to guess what's to be changed in your application. As you only gave a few parts of the application, I can't advise you exactly where/what to change.

Upvotes: 2

Related Questions