introduce py.builtin._tryimport to try importing modules in a row, fix a few places
--HG-- branch : trunk
This commit is contained in:
parent
32e2bf7d08
commit
1e51844519
|
@ -146,6 +146,7 @@ initpkg(__name__,
|
||||||
'builtin.GeneratorExit' : ('./builtin/builtin25.py', 'GeneratorExit'),
|
'builtin.GeneratorExit' : ('./builtin/builtin25.py', 'GeneratorExit'),
|
||||||
'builtin.print_' : ('./builtin/builtin31.py', 'print_'),
|
'builtin.print_' : ('./builtin/builtin31.py', 'print_'),
|
||||||
'builtin._reraise' : ('./builtin/builtin31.py', '_reraise'),
|
'builtin._reraise' : ('./builtin/builtin31.py', '_reraise'),
|
||||||
|
'builtin._tryimport' : ('./builtin/builtin31.py', '_tryimport'),
|
||||||
'builtin.exec_' : ('./builtin/builtin31.py', 'exec_'),
|
'builtin.exec_' : ('./builtin/builtin31.py', 'exec_'),
|
||||||
'builtin._basestring' : ('./builtin/builtin31.py', '_basestring'),
|
'builtin._basestring' : ('./builtin/builtin31.py', '_basestring'),
|
||||||
'builtin._totext' : ('./builtin/builtin31.py', '_totext'),
|
'builtin._totext' : ('./builtin/builtin31.py', '_totext'),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
|
import py
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if sys.version_info >= (3, 0):
|
if sys.version_info >= (3, 0):
|
||||||
|
@ -105,3 +105,13 @@ def _reraise(cls, val, tb):
|
||||||
def exec2(obj, globals, locals):
|
def exec2(obj, globals, locals):
|
||||||
exec obj in 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)
|
||||||
|
|
|
@ -139,3 +139,10 @@ def test_exec():
|
||||||
d = {}
|
d = {}
|
||||||
py.builtin.exec_("x=4", d)
|
py.builtin.exec_("x=4", d)
|
||||||
assert d['x'] == 4
|
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
|
||||||
|
|
|
@ -2,11 +2,8 @@ import py
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
builtin_repr = repr
|
builtin_repr = repr
|
||||||
try:
|
|
||||||
import repr
|
|
||||||
except ImportError:
|
|
||||||
import reprlib as repr
|
|
||||||
|
|
||||||
|
repr = py.builtin._tryimport('repr', 'reprlib')
|
||||||
|
|
||||||
class Code(object):
|
class Code(object):
|
||||||
""" wrapper around Python code objects """
|
""" wrapper around Python code objects """
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
try:
|
|
||||||
import Queue as queue
|
|
||||||
except ImportError:
|
|
||||||
import queue
|
|
||||||
|
|
||||||
import py
|
import py
|
||||||
from py.__.code.code import FormattedExcinfo, ReprExceptionInfo
|
from py.__.code.code import FormattedExcinfo, ReprExceptionInfo
|
||||||
|
queue = py.builtin._tryimport('queue', 'Queue')
|
||||||
|
|
||||||
class TWMock:
|
class TWMock:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
|
@ -1,12 +1,7 @@
|
||||||
import py, os, stat
|
import py, os, stat
|
||||||
try:
|
|
||||||
from hashlib import md5
|
md5 = py.builtin._tryimport('hashlib.md5', 'md5.md5')
|
||||||
except ImportError:
|
queue = py.builtin._tryimport('queue.Queue', 'Queue.Queue')
|
||||||
from md5 import md5
|
|
||||||
try:
|
|
||||||
from queue import Queue
|
|
||||||
except ImportError:
|
|
||||||
from Queue import Queue
|
|
||||||
|
|
||||||
class RSync(object):
|
class RSync(object):
|
||||||
""" This class allows to send a directory structure (recursively)
|
""" This class allows to send a directory structure (recursively)
|
||||||
|
|
|
@ -39,12 +39,8 @@ class TestLocalPath(common.CommonFSTests):
|
||||||
assert p == tmpdir
|
assert p == tmpdir
|
||||||
|
|
||||||
def test_gethash(self, tmpdir):
|
def test_gethash(self, tmpdir):
|
||||||
try:
|
md5 = py.builtin._tryimport('md5.md5', 'hashlib.md5')
|
||||||
from md5 import md5
|
sha = py.builtin._tryimport('sha.sha', 'hashlib.sha1')
|
||||||
from sha import sha
|
|
||||||
except ImportError:
|
|
||||||
from hashlib import md5 as md5
|
|
||||||
from hashlib import sha1 as sha
|
|
||||||
fn = tmpdir.join("testhashfile")
|
fn = tmpdir.join("testhashfile")
|
||||||
data = 'hello'.encode('ascii')
|
data = 'hello'.encode('ascii')
|
||||||
fn.write(data, mode="wb")
|
fn.write(data, mode="wb")
|
||||||
|
|
|
@ -12,6 +12,7 @@ Current list:
|
||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
import py
|
import py
|
||||||
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
#-----------------------------------------------------------
|
#-----------------------------------------------------------
|
||||||
# posix external command execution
|
# posix external command execution
|
||||||
|
@ -24,15 +25,10 @@ def posix_exec_cmd(cmd):
|
||||||
the error-output from the command.
|
the error-output from the command.
|
||||||
"""
|
"""
|
||||||
#__tracebackhide__ = True
|
#__tracebackhide__ = True
|
||||||
try:
|
|
||||||
from subprocess import Popen, PIPE
|
|
||||||
except ImportError:
|
|
||||||
from py.__.compat.subprocess import Popen, PIPE
|
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
|
|
||||||
child = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE,
|
child = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True)
|
||||||
close_fds=True)
|
|
||||||
stdin, stdout, stderr = child.stdin, child.stdout, child.stderr
|
stdin, stdout, stderr = child.stdin, child.stdout, child.stderr
|
||||||
|
|
||||||
# XXX sometimes we get a blocked r.read() call (see below)
|
# XXX sometimes we get a blocked r.read() call (see below)
|
||||||
|
|
|
@ -8,10 +8,7 @@ import py
|
||||||
from py.__.test.session import Session
|
from py.__.test.session import Session
|
||||||
from py.__.test import outcome
|
from py.__.test import outcome
|
||||||
from py.__.test.dist.nodemanage import NodeManager
|
from py.__.test.dist.nodemanage import NodeManager
|
||||||
try:
|
queue = py.builtin._tryimport('queue', 'Queue')
|
||||||
import queue
|
|
||||||
except ImportError:
|
|
||||||
import Queue as queue
|
|
||||||
|
|
||||||
debug_file = None # open('/tmp/loop.log', 'w')
|
debug_file = None # open('/tmp/loop.log', 'w')
|
||||||
def debug(*args):
|
def debug(*args):
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
|
|
||||||
import py
|
import py
|
||||||
from py.__.test.dist.txnode import TXNode
|
from py.__.test.dist.txnode import TXNode
|
||||||
|
Queue = py.builtin._tryimport("queue.Queue", "Queue.Queue")
|
||||||
|
|
||||||
class EventQueue:
|
class EventQueue:
|
||||||
def __init__(self, registry, queue=None):
|
def __init__(self, registry, queue=None):
|
||||||
if queue is None:
|
if queue is None:
|
||||||
queue = py.std.Queue.Queue()
|
queue = Queue()
|
||||||
self.queue = queue
|
self.queue = queue
|
||||||
registry.register(self)
|
registry.register(self)
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ class MySetup:
|
||||||
if config is None:
|
if config is None:
|
||||||
config = py.test.config._reparse([])
|
config = py.test.config._reparse([])
|
||||||
self.config = config
|
self.config = config
|
||||||
self.queue = py.std.Queue.Queue()
|
self.queue = Queue()
|
||||||
self.xspec = py.execnet.XSpec("popen")
|
self.xspec = py.execnet.XSpec("popen")
|
||||||
self.gateway = py.execnet.makegateway(self.xspec)
|
self.gateway = py.execnet.makegateway(self.xspec)
|
||||||
self.id += 1
|
self.id += 1
|
||||||
|
|
|
@ -2,10 +2,8 @@ import threading
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
import py
|
import py
|
||||||
try:
|
|
||||||
import queue
|
queue = py.builtin._tryimport('queue', 'Queue')
|
||||||
except ImportError:
|
|
||||||
import Queue as queue
|
|
||||||
|
|
||||||
ERRORMARKER = object()
|
ERRORMARKER = object()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue