473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Bug in Getsource?

I've noticed inspect.getsource acts strangely for lambda expressions.
For instance:

from inspect import getsource
somefunc = lambda(a):abs(a)
print 'somefunc source:',getsource(somefunc)

results in:

somefunc source: from inspect import getsource

I'd understand if inspect can't handle lambda expressions, but claiming
something is an object's source when it is obviously not is a bug IMO.
Is this correct? Is there any way to fix this?
Jul 18 '05 #1
2 1475
"Chris S." <ch*****@NOSPAMudel.edu> wrote in message news:<cb**********@scrotar.nss.udel.edu>...
I've noticed inspect.getsource acts strangely for lambda expressions.
For instance:

from inspect import getsource
somefunc = lambda(a):abs(a)
print 'somefunc source:',getsource(somefunc)

results in:

somefunc source: from inspect import getsource

Yep, same here.
I'd understand if inspect can't handle lambda expressions, but claiming
something is an object's source when it is obviously not is a bug IMO.
Is this correct? Is there any way to fix this?


Looks like the problem is in inspect.findsource(), line 433
pat = re.compile(r'^(\s*def\s)|(.*\slambda(:|\s))')

As you can see, this pattern doesn't match (perfectly legal) "lambda(a):"
(note the parentheses).

If I change pattern to r'^(\s*def\s)|(.*\slambda(:|\s|\())',
getsource returns "somefunc = lambda(a): abs(a)",
which is better, but still not satisfactory.

Alas, I'm too lazy and/or stupid and/or busy to conceive a good patch ;)

- kv
Jul 18 '05 #2
Konstantin Veretennicov wrote:
"Chris S." <ch*****@NOSPAMudel.edu> wrote in message news:<cb**********@scrotar.nss.udel.edu>...
I've noticed inspect.getsource acts strangely for lambda expressions.
For instance:

from inspect import getsource
somefunc = lambda(a):abs(a)
print 'somefunc source:',getsource(somefunc)

results in:

somefunc source: from inspect import getsource

Yep, same here.

I'd understand if inspect can't handle lambda expressions, but claiming
something is an object's source when it is obviously not is a bug IMO.
Is this correct? Is there any way to fix this?

Looks like the problem is in inspect.findsource(), line 433
pat = re.compile(r'^(\s*def\s)|(.*\slambda(:|\s))')

As you can see, this pattern doesn't match (perfectly legal) "lambda(a):"
(note the parentheses).

If I change pattern to r'^(\s*def\s)|(.*\slambda(:|\s|\())',
getsource returns "somefunc = lambda(a): abs(a)",
which is better, but still not satisfactory.

Alas, I'm too lazy and/or stupid and/or busy to conceive a good patch ;)

- kv


How is your current fix not satisfactory? Is that not the correct code?
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ron Adam | last post by:
While playing around with the inspect module I found that the Blockfinder doesn't recognize single line function definitions. Adding the following two lines to it fixes it, but I'm not sure if it...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
0
by: Richie01 | last post by:
Ok, here i have the code for the form...but whenever i click update, delete or search all it does is close the form and nothing is done to the database. Could someone be kind enough to help me out...
2
by: carlos123 | last post by:
im pretty new to java. this might sound like a weird question but! how do i add this code class FileWrite { public static void main(String args) { try{ // Create file ...
2
by: carlos123 | last post by:
Ok i have a few different arrays for names, last names, address, and city , and i have a textfield for each one. Ultimatly i want my project to be able to enter the names in and hit "save" and it...
3
by: carlos123 | last post by:
Ok, im having EXTREME problems, with arraylists, i simply dont understand them, yes i have read your article. Would someone just take a look at this code, i know it sucks. Look at the areas that say...
2
by: deezle | last post by:
Hello, I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help...
10
by: aboolamoola | last post by:
hi, I recently started programing about a month ago. my teacher ask me to make a game. A connect 4 game. I try to make, but failed. I need help. here is my code: import java.awt.*; import...
2
by: JinFTW | last post by:
Hey guys I hate to bother you with this, but I'm having a problem with creating an arrayList of JButtons that will end up using an actionlistener. My original program was this: import...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.