introduce py.builtin._tryimport to try importing modules in a row, fix a few places

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-09-04 19:08:10 +02:00
parent 32e2bf7d08
commit 1e51844519
11 changed files with 34 additions and 39 deletions

View File

@ -146,6 +146,7 @@ initpkg(__name__,
'builtin.GeneratorExit' : ('./builtin/builtin25.py', 'GeneratorExit'),
'builtin.print_' : ('./builtin/builtin31.py', 'print_'),
'builtin._reraise' : ('./builtin/builtin31.py', '_reraise'),
'builtin._tryimport' : ('./builtin/builtin31.py', '_tryimport'),
'builtin.exec_' : ('./builtin/builtin31.py', 'exec_'),
'builtin._basestring' : ('./builtin/builtin31.py', '_basestring'),
'builtin._totext' : ('./builtin/builtin31.py', '_totext'),

View File

@ -1,4 +1,4 @@
import py
import sys
if sys.version_info >= (3, 0):
@ -105,3 +105,13 @@ def _reraise(cls, val, tb):
def exec2(obj, globals, locals):
exec obj in globals, locals
""")
def _tryimport(*names):
""" return the first successfully imported module. """
assert names
for name in names:
try:
return __import__(name, None, None, '__doc__')
except ImportError:
excinfo = sys.exc_info()
py.builtin._reraise(*excinfo)

View File

@ -139,3 +139,10 @@ def test_exec():
d = {}
py.builtin.exec_("x=4", d)
assert d['x'] == 4
def test_tryimport():
py.test.raises(ImportError, py.builtin._tryimport, 'xqwe123')
x = py.builtin._tryimport('asldkajsdl', 'py')
assert x == py
x = py.builtin._tryimport('asldkajsdl', 'py.path')
assert x == py.path

View File

@ -2,11 +2,8 @@ import py
import sys
builtin_repr = repr
try:
import repr
except ImportError:
import reprlib as repr
repr = py.builtin._tryimport('repr', 'reprlib')
class Code(object):
""" wrapper around Python code objects """

View File

@ -1,10 +1,7 @@
try:
import Queue as queue
except ImportError:
import queue
import py
from py.__.code.code import FormattedExcinfo, ReprExceptionInfo
queue = py.builtin._tryimport('queue', 'Queue')
class TWMock:
def __init__(self):

View File

@ -1,12 +1,7 @@
import py, os, stat
try:
from hashlib import md5
except ImportError:
from md5 import md5
try:
from queue import Queue
except ImportError:
from Queue import Queue
md5 = py.builtin._tryimport('hashlib.md5', 'md5.md5')
queue = py.builtin._tryimport('queue.Queue', 'Queue.Queue')
class RSync(object):
""" This class allows to send a directory structure (recursively)

View File

@ -39,12 +39,8 @@ class TestLocalPath(common.CommonFSTests):
assert p == tmpdir
def test_gethash(self, tmpdir):
try:
from md5 import md5
from sha import sha
except ImportError:
from hashlib import md5 as md5
from hashlib import sha1 as sha
md5 = py.builtin._tryimport('md5.md5', 'hashlib.md5')
sha = py.builtin._tryimport('sha.sha', 'hashlib.sha1')
fn = tmpdir.join("testhashfile")
data = 'hello'.encode('ascii')
fn.write(data, mode="wb")

View File

@ -12,6 +12,7 @@ Current list:
import os, sys
import py
from subprocess import Popen, PIPE
#-----------------------------------------------------------
# posix external command execution
@ -24,15 +25,10 @@ def posix_exec_cmd(cmd):
the error-output from the command.
"""
#__tracebackhide__ = True
try:
from subprocess import Popen, PIPE
except ImportError:
from py.__.compat.subprocess import Popen, PIPE
import errno
child = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE,
close_fds=True)
child = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True)
stdin, stdout, stderr = child.stdin, child.stdout, child.stderr
# XXX sometimes we get a blocked r.read() call (see below)

View File

@ -8,10 +8,7 @@ import py
from py.__.test.session import Session
from py.__.test import outcome
from py.__.test.dist.nodemanage import NodeManager
try:
import queue
except ImportError:
import Queue as queue
queue = py.builtin._tryimport('queue', 'Queue')
debug_file = None # open('/tmp/loop.log', 'w')
def debug(*args):

View File

@ -1,11 +1,12 @@
import py
from py.__.test.dist.txnode import TXNode
Queue = py.builtin._tryimport("queue.Queue", "Queue.Queue")
class EventQueue:
def __init__(self, registry, queue=None):
if queue is None:
queue = py.std.Queue.Queue()
queue = Queue()
self.queue = queue
registry.register(self)
@ -43,7 +44,7 @@ class MySetup:
if config is None:
config = py.test.config._reparse([])
self.config = config
self.queue = py.std.Queue.Queue()
self.queue = Queue()
self.xspec = py.execnet.XSpec("popen")
self.gateway = py.execnet.makegateway(self.xspec)
self.id += 1

View File

@ -2,10 +2,8 @@ import threading
import time
import sys
import py
try:
import queue
except ImportError:
import Queue as queue
queue = py.builtin._tryimport('queue', 'Queue')
ERRORMARKER = object()