[pypy-dev] Testing if a function exists?

Dan Stromberg drsalists at gmail.com
Sat Dec 4 06:42:33 CET 2010


This sounds promising, though sadly, it doesn't appear to work in Python 3.x:

$ /usr/local/cpython-3.1/bin/python3 -c 'import __builtin__'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named __builtin__
benchbox-dstromberg:~/src/home-svn/backshift/trunk i686-pc-linux-gnu
22275 - above cmd done 2010 Fri Dec 03 09:40 PM

$ /usr/local/cpython-3.0/bin/python3.0 -c 'import __builtin__'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named __builtin__
benchbox-dstromberg:~/src/home-svn/backshift/trunk i686-pc-linux-gnu
22275 - above cmd done 2010 Fri Dec 03 09:40 PM

$ /usr/local/cpython-2.7/bin/python -c 'import __builtin__'
benchbox-dstromberg:~/src/home-svn/backshift/trunk i686-pc-linux-gnu
22275 - above cmd done 2010 Fri Dec 03 09:41 PM

$ /usr/local/cpython-2.6/bin/python -c 'import __builtin__'
benchbox-dstromberg:~/src/home-svn/backshift/trunk i686-pc-linux-gnu
22275 - above cmd done 2010 Fri Dec 03 09:41 PM

$ /usr/local/cpython-2.5/bin/python -c 'import __builtin__'
benchbox-dstromberg:~/src/home-svn/backshift/trunk i686-pc-linux-gnu
22275 - above cmd done 2010 Fri Dec 03 09:41 PM

$


On Fri, Dec 3, 2010 at 11:37 AM, Maciej Fijalkowski <fijall at gmail.com> wrote:
> Use:
>
> import __builtin__
>
> and treat it as module. __builtins__ is an ugly hack that is sometimes
> a dict and sometimes a module and pypy has different corner cases.
> __builtin__ will always work
>
> On Fri, Dec 3, 2010 at 9:13 PM, Leonardo Santagada <santagada at gmail.com> wrote:
>> (I'm guessing but) In python 2.5 modules are not iterable, you can use
>> getattr for the same effect.
>>
>> On Fri, Dec 3, 2010 at 5:04 PM, Dan Stromberg <drsalists at gmail.com> wrote:
>>> How does one test if a function exists in pypy?
>>>
>>> In CPython 2.x and 3.x, it appears to be sufficient to use:
>>>
>>>   'funcname' in __bultins__
>>>
>>> ...but that doesn't appear to work in pypy 1.4:
>>>
>>>>>>> print 'platform_version' in platform
>>> Traceback (most recent call last):
>>>  File "<console>", line 1, in <module>
>>> TypeError: 'module' object is not iterable
>>>>>>> print 'bytes' in __builtins__
>>> Traceback (most recent call last):
>>>  File "<console>", line 1, in <module>
>>> TypeError: 'module' object is not iterable
>>>>>>>
>>
>>
>>
>> --
>> Leonardo Santagada
>> _______________________________________________
>> pypy-dev at codespeak.net
>> http://codespeak.net/mailman/listinfo/pypy-dev
>



More information about the Pypy-dev mailing list