call of non-function (type string) error 
Author Message
 call of non-function (type string) error

Greetings,

Any suggestions on how to get around this problem?  I'm trying to call
a function using variables, but it obviously doesn't work that way.

-----------------------------------------------

CIPHER = "DES3"
exec "from Crypto.Cipher import %s" % CIPHER

# cipherobj = DES3.new(KEY, DES3.CBC, IV)
cipherobj = CIPHER + 'new'(KEY,CIPHER + '.CBC',IV)

-----------------------------------------------

Traceback (most recent call last):
  File "<stdin>", line 10, in ?
TypeError: call of non-function (type string)

_____________________________________________________________________________
http://www.*-*-*.com/ - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Sun, 28 Sep 2003 08:23:03 GMT  
 call of non-function (type string) error

Quote:

> Greetings,

> Any suggestions on how to get around this problem?  I'm trying to call
> a function using variables, but it obviously doesn't work that way.

> -----------------------------------------------

> CIPHER = "DES3"
> exec "from Crypto.Cipher import %s" % CIPHER

> # cipherobj = DES3.new(KEY, DES3.CBC, IV)
> cipherobj = CIPHER + 'new'(KEY,CIPHER + '.CBC',IV)

Strings and exec are hardly ever necessary. They should be buried deep
in a module somewhere. Use getattr instead:

What you want is

CIPHER = "DES"
import Crypto.Cipher

cipher = getattr(Cyrpto.Cipher, CIPHER)
cipherobj = cipher.new(KEY, CIPHER+".CBC", IV)

--
Take a recipe. Leave a recipe.  
python Cookbook!  http://www.ActiveState.com/pythoncookbook



Sun, 28 Sep 2003 08:49:41 GMT  
 call of non-function (type string) error

Quote:
----- Original Message -----


Sent: Tuesday, April 10, 2001 5:23 PM
Subject: call of non-function (type string) error

> Greetings,

> Any suggestions on how to get around this problem?  I'm trying to call
> a function using variables, but it obviously doesn't work that way.

> -----------------------------------------------

> CIPHER = "DES3"
> exec "from Crypto.Cipher import %s" % CIPHER

> # cipherobj = DES3.new(KEY, DES3.CBC, IV)
> cipherobj = CIPHER + 'new'(KEY,CIPHER + '.CBC',IV)
> -----------------------------------------------

> Traceback (most recent call last):
>   File "<stdin>", line 10, in ?
> TypeError: call of non-function (type string)

I'm having almost as much trouble as python trying
to figure out your syntax :-)   I'm not familiar with the
Crypto module.

I'm sure someone will give you a correct anwer here
but you probably should practice using eval and exec
until you know -exactly- what they do.  Only practice
will help you understand how strings and the python
interpreter interact, and the difference between a string
literal and a named object or expression.  Start with
small examples where there are few unknowns.

Here's what doesn't work in your code:

... CIPHER + 'new'(...)

CIPHER is a string and 'new' is a string, so
CIPHER + 'new' is a string with value "DES3new".
You can't invoke a string as if it were a function,
(and anyway you are missing a ".").

Change it to ... eval(CIPHER+'.new')(...)

... (..., CIPHER + '.CBC', ...)

CIPHER+'.CBC' is a string equal to "DES3.CBC".
The function wants the value of the expression DES3.CBC.
A string is not the same as the value of it's expression.
That's why we have the eval() function.

Change it to ... (..., eval(CIPHER + '.CBC'), ...)

... ( ..., IV)

I don't know what IV is.

A better approach to this whole thing is this:

import Crypto.Cipher

CIPHER = "DES3"
ciphertype = getattr(Crypto.Cipher, CIPHER)
cipherobj = ciphertype.new(KEY, ciphertype.CBC, IV)

Have fun...

- Ken Seehof

www.neuralintegrator.com



Sun, 28 Sep 2003 09:09:35 GMT  
 call of non-function (type string) error

Quote:

> What you want is

> CIPHER = "DES"
> import Crypto.Cipher

> cipher = getattr(Crypto.Cipher, CIPHER)
> cipherobj = cipher.new(KEY, CIPHER+".CBC", IV)

This doesn't seem to work for me:

Quote:
>>> CIPHER = "DES3"
>>> import Crypto.Cipher
>>> cipher = getattr(Crypto.Cipher, CIPHER)

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: DES3

_____________________________________________________________________________
http://movies.yahoo.com.au - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Sun, 28 Sep 2003 23:25:24 GMT  
 call of non-function (type string) error

Quote:

> >>> CIPHER = "DES3"
> >>> import Crypto.Cipher
> >>> cipher = getattr(Crypto.Cipher, CIPHER)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: DES3

I don't think the Cipher package automatically imports all
subpackages, and this is a case where you really do need to use exec.
Obviously the Cipher package should provide a function to retrieve the
module for a given name...

--amk



Mon, 29 Sep 2003 00:37:02 GMT  
 call of non-function (type string) error

Quote:


> > >>> CIPHER = "DES3"
> > >>> import Crypto.Cipher
> > >>> cipher = getattr(Crypto.Cipher, CIPHER)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > AttributeError: DES3

> I don't think the Cipher package automatically imports all
> subpackages, and this is a case where you really do need to use exec.

Why wouldn't __import__("Crypto.Cipher.%s"%(CIPHER,),globals(),{},[1])
followed by cipher = getattr(Crypto.Cipher, CIPHER) work?

I don't have the Crypto package, but:

Quote:
>>> import distutils
>>> dir(distutils)

['__builtins__', '__doc__', '__file__', '__name__', '__path__',
 '__revision__', '__version__']
Quote:
>>> __import__("distutils.file_util",globals(),{},[1])

<module 'distutils.file_util' from '/usr/lib/python2.0/distutils/file_util.pyc'>
Quote:
>>> dir(distutils)

['__builtins__', '__doc__', '__file__', '__name__', '__path__',
 '__revision__', '__version__', 'errors', 'file_util']

Quote:
> Obviously the Cipher package should provide a function to retrieve the
> module for a given name...

This is probably easier!

Cheers,
M.

--
  ... when all the programmes on all the channels actually were made
  by actors with cleft pallettes speaking lines by dyslexic writers
  filmed by blind cameramen instead of merely seeming like that, it
  somehow made the whole thing more worthwhile.   -- HHGTG, Episode 11



Mon, 29 Sep 2003 01:16:48 GMT  
 call of non-function (type string) error
Graham Guttocks:

Quote:
>This doesn't seem to work for me:

>>>> CIPHER = "DES3"
>>>> import Crypto.Cipher
>>>> cipher = getattr(Crypto.Cipher, CIPHER)
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>AttributeError: DES3

How about

CIPHER = "DES3"
Crypto = __import__("Crypto.Cipher.%s" % CIPHER)
cipher = getattr(Crypto.Cipher, CIPHER)

(Sorry, don't have Crypto installed to test it out.)

                    Andrew



Mon, 29 Sep 2003 01:51:05 GMT  
 call of non-function (type string) error

Quote:

> ...
> >>> CIPHER = "DES3"
> >>> import Crypto.Cipher
> >>> cipher = getattr(Crypto.Cipher, CIPHER)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: DES3

As Andrew pointed out this is an annoying corner case where you probably
do have to use exec. I'm not really thrilled with all of these corner
cases in Python's package handling.

On the other hand, once you have the module object you can use getattr
to get the "new" function out of the module.

--
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.ActiveState.com/pythoncookbook



Mon, 29 Sep 2003 01:49:41 GMT  
 call of non-function (type string) error
Sez Graham Guttocks:

Quote:
> >>> CIPHER = "DES3"
> >>> Crypto = __import__("Crypto.Cipher.%s" % CIPHER)
> >>> cipher = getattr(Crypto.Cipher, CIPHER)
> >>> cipherobj = cipher.new(KEY, CIPHER+".CBC", IV)

> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.0/site-packages/Crypto/Ciphers.py", line 84, in
> __init__
>     self.instance = self.C_new(key,mode,IV)
> TypeError: an integer is required

Perhaps try

Quote:
>>> cipherobj = cipher.new(KEY, cipher.CBC, IV)
or
>>> cipherobj = cipher.new(KEY, Crypto.CBC, IV)

I have no idea what module you're using, haven't read the previous messages
in the thread and so on.  This is just a guess...

Peace,
  Kalle
--

Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
 [ Not signed due to lossage.  Blame Microsoft Outlook Express. ]



Mon, 29 Sep 2003 03:38:37 GMT  
 call of non-function (type string) error

Quote:

> How about

> CIPHER = "DES3"
> Crypto = __import__("Crypto.Cipher.%s" % CIPHER)
> cipher = getattr(Crypto.Cipher, CIPHER)

This get me a bit further thanks, but now there is a problem with
returning the ciphering object.

Quote:
>>> CIPHER = "DES3"
>>> Crypto = __import__("Crypto.Cipher.%s" % CIPHER)
>>> cipher = getattr(Crypto.Cipher, CIPHER)
>>> cipherobj = cipher.new(KEY, CIPHER+".CBC", IV)

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.0/site-packages/Crypto/Ciphers.py", line 84, in
__init__
    self.instance = self.C_new(key,mode,IV)
TypeError: an integer is required

_____________________________________________________________________________
http://movies.yahoo.com.au - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Mon, 29 Sep 2003 03:13:59 GMT  
 call of non-function (type string) error

Quote:

> Perhaps try
> >>> cipherobj = cipher.new(KEY, cipher.CBC, IV)

That did it, thanks mate!

For the record, here is the whole working sequence:  
(Whew, what a mess):

---------------------------------------------------
CIPHER = "DES3"
exec "from Crypto.Cipher import %s" % CIPHER

Crypto = __import__("Crypto.Cipher.%s" % CIPHER)
cipher = getattr(Crypto.Cipher, CIPHER)
cipherobj = cipher.new(KEY, cipher.CBC, IV)
---------------------------------------------------

Cheers,
Graham

_____________________________________________________________________________
http://movies.yahoo.com.au - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Mon, 29 Sep 2003 04:05:55 GMT  
 call of non-function (type string) error

Quote:

> Why wouldn't __import__("Crypto.Cipher.%s"%(CIPHER,),globals(),{},[1])
> followed by cipher = getattr(Crypto.Cipher, CIPHER) work?

To get the module for CIPHER imported properly, you just need to pass
its name in the fourth parameter to __import__:

Quote:
>>> CIPHER = 'DES3'
>>> pkg = __import__('Crypto.Cipher', globals(), locals(), [CIPHER])
>>> module = getattr(pkg, CIPHER)
>>> print module

<module 'Crypto.Cipher.DES3' from '/home/carey/lib/python/Crypto/Cipher/DES3.pyc'>

No need to use exec, eval or %s anywhere.

keeping-the-world-free-of-exec-ly y'rs   - Carey

--
            "Quiet, you'll miss the humorous conclusion."



Mon, 29 Sep 2003 19:03:50 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Example of using non-string types in Tcl?

2. VC++ calling fortran function and fortran function calling a c++ function

3. non-function members of string-module.

4. Calling an non-DLL external function

5. Calling non-Clarion library functions

6. Calling functions from a non-Clarion DLL, what embed for the prototypes

7. Threaded Tcl calls non-thread-safe library functions

8. ISAM-Error on NON-ANSI-IO-Function

9. How to call non-existing .dll without getting error messages

10. Wanted: Ada Package for hot I/O and c type string functions

11. How to find out name of calling function from called function

12. Call a defined dll-function with various kinds of data-types

 

 
Powered by phpBB® Forum Software