Re: Is exec() also not used in python 2.7.1 anymore?

2011-10-05 Thread Gabriel Genellina
En Tue, 04 Oct 2011 07:32:41 -0300, Wong Wah Meng-R32813  
r32...@freescale.com escribió:


Haha... yeah I reviewed the code, it is supposed to exposed some remote  
methods locally (RMI proxy usage). However, I am not sure why what it  
does is merely a pass.


I commented out this code and haven't seen any negative implication. I  
will look into this again if I am convinced the next error I see is due  
to I commented out this code.



  exec('def %s(self, *args, **kw): pass'%methodStrName)


In case you convince yourself that defining this dynamic but empty  
function is really needed, you can avoid exec this way:


  def some_function(...)

...
# instead of exec('def ...' % methodStrName)
def empty_function(self, *args, **kw): pass
empty_function.func_name = methodStrName
...
# presumably methodStrName is referred somehow in
# the remaining code block, or perhaps locals();
# modify accordingly


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Is exec() also not used in python 2.7.1 anymore?

2011-10-04 Thread Wong Wah Meng-R32813
In migrating my application from python 1.5.2 to 2.7.1, one of my modules 
breaks when I import it. Here is the line where it breaks. Can I have a quick 
check if this built-in function still supported in python 2.7.1 and if so, what 
ought to be changed here? Thanks in advance for replying.

  exec('def %s(self, *args, **kw): pass'%methodStrName)
SyntaxError: unqualified exec is not allowed in function '_exposeMethods' it 
contains a nested function with free variables

Regards,
Wah Meng

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is exec() also not used in python 2.7.1 anymore?

2011-10-04 Thread Chris Rebert
On Tue, Oct 4, 2011 at 12:51 AM, Wong Wah Meng-R32813
r32...@freescale.com wrote:
 In migrating my application from python 1.5.2 to 2.7.1, one of my modules
 breaks when I import it. Here is the line where it breaks. Can I have a
 quick check if this built-in function still supported in python 2.7.1

Er, `exec` is a primitive statement, not a built-in function (so, the
parens around it are superfluous), but yes, it's still present in 2.7.
(Ironically, exec was changed to a function in Python 3.0.)

 and if
 so, what ought to be changed here? Thanks in advance for replying.

   exec('def %s(self, *args, **kw): pass'%methodStrName)

Defining a do-nothing, dynamically-named function seems kinda strange
in the first place; why are you doing this?

Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is exec() also not used in python 2.7.1 anymore?

2011-10-04 Thread Peter Otten
Wong Wah Meng-R32813 wrote:

 In migrating my application from python 1.5.2 to 2.7.1, one of my modules
 breaks when I import it. Here is the line where it breaks. Can I have a
 quick check if this built-in function still supported in python 2.7.1 and
 if so, what ought to be changed here? Thanks in advance for replying.
 
   exec('def %s(self, *args, **kw): pass'%methodStrName)
 SyntaxError: unqualified exec is not allowed in function '_exposeMethods'
 it contains a nested function with free variables

I think the message is quite explicit about the problem. You have a nested 
function along the lines:

 def f():
... exec 
... def g(): return x
...
  File stdin, line 2
SyntaxError: unqualified exec is not allowed in function 'f' it contains a 
nested function with free variables

With the current scoping rules x in the inner function may refer either to a 
local variable in f() or a global variable -- and because of the exec 
statement the compiler cannot determine which. Depending on your usecase a 
workaround may be to declare the free variable as global:

 def f():
... exec 
... def g():
... global x
... return x
... return g
...
 x = 42
 f()()
42


-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Is exec() also not used in python 2.7.1 anymore?

2011-10-04 Thread Wong Wah Meng-R32813
Haha... yeah I reviewed the code, it is supposed to exposed some remote methods 
locally (RMI proxy usage). However, I am not sure why what it does is merely a 
pass. 

I commented out this code and haven't seen any negative implication. I will 
look into this again if I am convinced the next error I see is due to I 
commented out this code. 

Thanks!


Regards,
Wah Meng

-Original Message-
From: ch...@rebertia.com [mailto:ch...@rebertia.com] On Behalf Of Chris Rebert
Sent: Tuesday, October 04, 2011 4:26 PM
To: Wong Wah Meng-R32813
Cc: python-list@python.org
Subject: Re: Is exec() also not used in python 2.7.1 anymore?

On Tue, Oct 4, 2011 at 12:51 AM, Wong Wah Meng-R32813
r32...@freescale.com wrote:
 In migrating my application from python 1.5.2 to 2.7.1, one of my modules
 breaks when I import it. Here is the line where it breaks. Can I have a
 quick check if this built-in function still supported in python 2.7.1

Er, `exec` is a primitive statement, not a built-in function (so, the
parens around it are superfluous), but yes, it's still present in 2.7.
(Ironically, exec was changed to a function in Python 3.0.)

 and if
 so, what ought to be changed here? Thanks in advance for replying.

   exec('def %s(self, *args, **kw): pass'%methodStrName)

Defining a do-nothing, dynamically-named function seems kinda strange
in the first place; why are you doing this?

Cheers,
Chris
--
http://rebertia.com


-- 
http://mail.python.org/mailman/listinfo/python-list