Re: Re: Re: porting unittest.py to Prothon.
Serge Orlov <[email protected]> Wed, 23 Jun 2004 15:22:49 +0400
| Newsgroups | gmane.comp.lang.prothon.devel |
|---|---|
| Message-ID | <[email protected]> |
Mark Hahn wrote:
> I intentionally removed " from os import *" from Prothon. I considered it a
> bad feature in Python. Feel free to argue.
How do I do "from math import *"? I hope you don't suggest
writing
math.sin(x) + math.cos(y)
If the module is just a collection of functions like pyCompat
or math, the ability to say "import all functions" is needed.
>>>> from os import system
>>
>> Uncaught exception:
>> --- File: fromosimportsystem-src.txt, line: 2, char: 1
>> Program Error, No main module set for importing.
> This is a problem with the console. I will fix this.
>> I've come up with a hack:
>> import pyCompat
>> pyCompat.exportNames("*")
>>
>> I called it a hack because messing with stack is considered
>> a quick hack in Python. But after I used it for a while I
>> realized:
>> 1. I doesn't feel like like hack using caller :)
>> 2. It provides more functionality then "from X import *"
>> a. I don't want to override existing objects like range(),
>> "from X import *" doesn't let me control it.
>> b. I can implement .exportFewerNames() because I'm going
>> to play with the idea of incremental removing of
>> Python functionality from the ported module.
> Can you post the code?
the fix for bug 38 broke it, but here is the relevant code
name_ = "future"
# work around for lack of globals()
if isMain?():
myModule = Main
else:
myModule = sys.modules.attrs_.get(Symbol(name_))
allExportNames = """AttributeError ImportError abs enumerate isinstance type"""
allExportSymbols = [Symbol(name) for name in splitNames(allExportNames)]
def exportNames(namesStr):
"""export names into caller's namespace
namesStr is a whitespace separated list of functions to export,
a special name * means export all functions.
"""
namesList = splitNames(namesStr)
for name in namesList:
if name == "*":
symbolsToExport.extend!(allExportSymbols)
continue
symbol = Symbol(name)
if symbol not in allExportSymbols:
raise ImportError("module {name_} does not export symbol {name}".fmt())
symbolsToExport.append!(symbol)
for symbol in symbolsToExport:
caller.attrs_[symbol] = myModule.attrs_[symbol]
-- Serge