put some imports back to function-level and streamline py2/py3 compat in one place

This commit is contained in:
holger krekel 2014-08-01 10:12:53 +02:00
parent d6fc489b2b
commit 97b671057d
6 changed files with 20 additions and 39 deletions

View File

@ -17,6 +17,9 @@ NEXT
- fix issue544 by only removing "@NUM" at the end of "::" separated parts - fix issue544 by only removing "@NUM" at the end of "::" separated parts
and if the part has an ".py" extension and if the part has an ".py" extension
- don't use py.std import helper, rather import things directly.
Thanks Bruno Oliveira.
2.6 2.6
----------------------------------- -----------------------------------

View File

@ -1,2 +1,2 @@
# #
__version__ = '2.6.1.dev1' __version__ = '2.6.1.dev2'

View File

@ -1,9 +1,6 @@
""" generate a single-file self-contained version of pytest """ """ generate a single-file self-contained version of pytest """
import base64
import pickle
import py import py
import sys import sys
import zlib
def find_toplevel(name): def find_toplevel(name):
@ -33,6 +30,7 @@ def pkg_to_mapping(name):
return name2src return name2src
def compress_mapping(mapping): def compress_mapping(mapping):
import base64, pickle, zlib
data = pickle.dumps(mapping, 2) data = pickle.dumps(mapping, 2)
data = zlib.compress(data, 9) data = zlib.compress(data, 9)
data = base64.encodestring(data) data = base64.encodestring(data)

View File

@ -2,8 +2,6 @@
Based on initial code from Ross Lawley. Based on initial code from Ross Lawley.
""" """
import codecs
import py import py
import os import os
import re import re
@ -11,20 +9,13 @@ import sys
import time import time
# Python 2.X and 3.X compatibility # Python 2.X and 3.X compatibility
try: if sys.version_info[0] < 3:
unichr(65) from codecs import open
except NameError: else:
unichr = chr unichr = chr
try:
unicode('A')
except NameError:
unicode = str unicode = str
try:
long(1)
except NameError:
long = int long = int
class Junit(py.xml.Namespace): class Junit(py.xml.Namespace):
pass pass
@ -207,11 +198,7 @@ class LogXML(object):
self.suite_start_time = time.time() self.suite_start_time = time.time()
def pytest_sessionfinish(self): def pytest_sessionfinish(self):
if sys.version_info[0] < 3: logfile = open(self.logfile, 'w', encoding='utf-8')
logfile = codecs.open(self.logfile, 'w', encoding='utf-8')
else:
logfile = open(self.logfile, 'w', encoding='utf-8')
suite_stop_time = time.time() suite_stop_time = time.time()
suite_time_delta = suite_stop_time - self.suite_start_time suite_time_delta = suite_stop_time - self.suite_start_time
numtests = self.passed + self.failed numtests = self.passed + self.failed

View File

@ -1,7 +1,6 @@
""" discovery and running of std-library "unittest" style tests. """ """ discovery and running of std-library "unittest" style tests. """
from __future__ import absolute_import from __future__ import absolute_import
import traceback import traceback
import unittest
import sys import sys
import pytest import pytest
@ -12,22 +11,15 @@ import py
from _pytest.python import transfer_markers from _pytest.python import transfer_markers
def is_unittest(obj):
"""Is obj a subclass of unittest.TestCase?"""
unittest = sys.modules.get('unittest')
if unittest is None:
return # nobody can have derived unittest.TestCase
try:
return issubclass(obj, unittest.TestCase)
except KeyboardInterrupt:
raise
except:
return False
def pytest_pycollect_makeitem(collector, name, obj): def pytest_pycollect_makeitem(collector, name, obj):
if is_unittest(obj): # has unittest been imported and is obj a subclass of its TestCase?
return UnitTestCase(name, parent=collector) try:
if not issubclass(obj, sys.modules["unittest"].TestCase):
return
except Exception:
return
# yes, so let's collect it
return UnitTestCase(name, parent=collector)
class UnitTestCase(pytest.Class): class UnitTestCase(pytest.Class):
@ -47,11 +39,12 @@ class UnitTestCase(pytest.Class):
super(UnitTestCase, self).setup() super(UnitTestCase, self).setup()
def collect(self): def collect(self):
from unittest import TestLoader
cls = self.obj cls = self.obj
if not getattr(cls, "__test__", True): if not getattr(cls, "__test__", True):
return return
self.session._fixturemanager.parsefactories(self, unittest=True) self.session._fixturemanager.parsefactories(self, unittest=True)
loader = unittest.TestLoader() loader = TestLoader()
module = self.getparent(pytest.Module).obj module = self.getparent(pytest.Module).obj
foundsomething = False foundsomething = False
for name in loader.getTestCaseNames(self.obj): for name in loader.getTestCaseNames(self.obj):

View File

@ -27,7 +27,7 @@ def main():
name='pytest', name='pytest',
description='pytest: simple powerful testing with Python', description='pytest: simple powerful testing with Python',
long_description=long_description, long_description=long_description,
version='2.6.1.dev1', version='2.6.1.dev2',
url='http://pytest.org', url='http://pytest.org',
license='MIT license', license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],