Source

pytest / _pytest / main.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
""" core implementation of testing process: init, session, runtest loop. """

import py
import pytest, _pytest
import inspect
import os, sys, imp

from _pytest.monkeypatch import monkeypatch
from py._code.code import TerminalRepr

from _pytest.mark import MarkInfo

tracebackcutdir = py.path.local(_pytest.__file__).dirpath()

# exitcodes for the command line
EXIT_OK = 0
EXIT_TESTSFAILED = 1
EXIT_INTERRUPTED = 2
EXIT_INTERNALERROR = 3
EXIT_USAGEERROR = 4

name_re = py.std.re.compile("^[a-zA-Z_]\w*$")

def pytest_addoption(parser):
    parser.addini("norecursedirs", "directory patterns to avoid for recursion",
        type="args", default=('.*', 'CVS', '_darcs', '{arch}'))
    #parser.addini("dirpatterns",
    #    "patterns specifying possible locations of test files",
    #    type="linelist", default=["**/test_*.txt",
    #            "**/test_*.py", "**/*_test.py"]
    #)
    group = parser.getgroup("general", "running and selection options")
    group._addoption('-x', '--exitfirst', action="store_true", default=False,
               dest="exitfirst",
               help="exit instantly on first error or failed test."),
    group._addoption('--maxfail', metavar="num",
               action="store", type="int", dest="maxfail", default=0,
               help="exit after first num failures or errors.")
    group._addoption('--strict', action="store_true",
               help="run pytest in strict mode, warnings become errors.")

    group = parser.getgroup("collect", "collection")
    group.addoption('--collectonly',
        action="store_true", dest="collectonly",
        help="only collect tests, don't execute them."),
    group.addoption('--pyargs', action="store_true",
        help="try to interpret all arguments as python packages.")
    group.addoption("--ignore", action="append", metavar="path",
        help="ignore path during collection (multi-allowed).")
    group.addoption('--confcutdir', dest="confcutdir", default=None,
        metavar="dir",
        help="only load conftest.py's relative to specified dir.")

    group = parser.getgroup("debugconfig",
        "test session debugging and configuration")
    group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir",
               help="base temporary directory for this test run.")


def pytest_namespace():
    collect = dict(Item=Item, Collector=Collector, File=File, Session=Session)
    return dict(collect=collect)

def pytest_configure(config):
    py.test.config = config # compatibiltiy
    if config.option.exitfirst:
        config.option.maxfail = 1

def wrap_session(config, doit):
    """Skeleton command line program"""
    session = Session(config)
    session.exitstatus = EXIT_OK
    initstate = 0
    try:
        try:
            config.pluginmanager.do_configure(config)
            initstate = 1
            config.hook.pytest_sessionstart(session=session)
            initstate = 2
            doit(config, session)
        except pytest.UsageError:
            msg = sys.exc_info()[1].args[0]
            sys.stderr.write("ERROR: %s\n" %(msg,))
            session.exitstatus = EXIT_USAGEERROR
        except KeyboardInterrupt:
            excinfo = py.code.ExceptionInfo()
            config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
            session.exitstatus = EXIT_INTERRUPTED
        except:
            excinfo = py.code.ExceptionInfo()
            config.pluginmanager.notify_exception(excinfo, config.option)
            session.exitstatus = EXIT_INTERNALERROR
            if excinfo.errisinstance(SystemExit):
                sys.stderr.write("mainloop: caught Spurious SystemExit!\n")
    finally:
        if initstate >= 2:
            config.hook.pytest_sessionfinish(session=session,
                exitstatus=session.exitstatus or (session._testsfailed and 1))
        if not session.exitstatus and session._testsfailed:
            session.exitstatus = EXIT_TESTSFAILED
        if initstate >= 1:
            config.pluginmanager.do_unconfigure(config)
    return session.exitstatus

def pytest_cmdline_main(config):
    return wrap_session(config, _main)

def _main(config, session):
    """ default command line protocol for initialization, session,
    running tests and reporting. """
    config.hook.pytest_collection(session=session)
    config.hook.pytest_runtestloop(session=session)

def pytest_collection(session):
    return session.perform_collect()

def pytest_runtestloop(session):
    if session.config.option.collectonly:
        return True
    for i, item in enumerate(session.items):
        try:
            nextitem = session.items[i+1]
        except IndexError:
            nextitem = None
        item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
        if session.shouldstop:
            raise session.Interrupted(session.shouldstop)
    return True

def pytest_ignore_collect(path, config):
    p = path.dirpath()
    ignore_paths = config._getconftest_pathlist("collect_ignore", path=p)
    ignore_paths = ignore_paths or []
    excludeopt = config.getvalue("ignore")
    if excludeopt:
        ignore_paths.extend([py.path.local(x) for x in excludeopt])
    return path in ignore_paths

class HookProxy:
    def __init__(self, fspath, config):
        self.fspath = fspath
        self.config = config

    def __getattr__(self, name):
        hookmethod = getattr(self.config.hook, name)

        def call_matching_hooks(**kwargs):
            plugins = self.config._getmatchingplugins(self.fspath)
            return hookmethod.pcall(plugins, **kwargs)
        return call_matching_hooks

def compatproperty(name):
    def fget(self):
        # deprecated - use pytest.name
        return getattr(pytest, name)

    return property(fget)


class Node(object):
    """ base class for Collector and Item the test collection tree.
    Collector subclasses have children, Items are terminal nodes."""

    def __init__(self, name, parent=None, config=None, session=None):
        #: a unique name within the scope of the parent node
        self.name = name

        #: the parent collector node.
        self.parent = parent

        #: the pytest config object
        self.config = config or parent.config

        #: the session this node is part of
        self.session = session or parent.session

        #: filesystem path where this node was collected from (can be None)
        self.fspath = getattr(parent, 'fspath', None)

        #: keywords on this node (node name is always contained)
        self.keywords = {self.name: True}

        #: fspath sensitive hook proxy used to call pytest hooks
        self.ihook = self.session.gethookproxy(self.fspath)

        #self.extrainit()

    #def extrainit(self):
    #    """"extra initialization after Node is initialized.  Implemented
    #    by some subclasses. """

    Module = compatproperty("Module")
    Class = compatproperty("Class")
    Instance = compatproperty("Instance")
    Function = compatproperty("Function")
    File = compatproperty("File")
    Item = compatproperty("Item")

    def _getcustomclass(self, name):
        cls = getattr(self, name)
        if cls != getattr(pytest, name):
            py.log._apiwarn("2.0", "use of node.%s is deprecated, "
                "use pytest_pycollect_makeitem(...) to create custom "
                "collection nodes" % name)
        return cls

    def __repr__(self):
        return "<%s %r>" %(self.__class__.__name__,
                           getattr(self, 'name', None))

    # methods for ordering nodes
    @property
    def nodeid(self):
        """ a ::-separated string denoting its collection tree address. """
        try:
            return self._nodeid
        except AttributeError:
            self._nodeid = x = self._makeid()
            return x


    def _makeid(self):
        return self.parent.nodeid + "::" + self.name

    def __eq__(self, other):
        if not isinstance(other, Node):
            return False
        return (self.__class__ == other.__class__ and
                self.name == other.name and self.parent == other.parent)

    def __ne__(self, other):
        return not self == other

    def __hash__(self):
        return hash((self.name, self.parent))

    def setup(self):
        pass

    def teardown(self):
        pass

    def _memoizedcall(self, attrname, function):
        exattrname = "_ex_" + attrname
        failure = getattr(self, exattrname, None)
        if failure is not None:
            py.builtin._reraise(failure[0], failure[1], failure[2])
        if hasattr(self, attrname):
            return getattr(self, attrname)
        try:
            res = function()
        except py.builtin._sysex:
            raise
        except:
            failure = py.std.sys.exc_info()
            setattr(self, exattrname, failure)
            raise
        setattr(self, attrname, res)
        return res

    def listchain(self):
        """ return list of all parent collectors up to self,
            starting from root of collection tree. """
        chain = []
        item = self
        while item is not None:
            chain.append(item)
            item = item.parent
        chain.reverse()
        return chain

    def listnames(self):
        return [x.name for x in self.listchain()]

    def getplugins(self):
        return self.config._getmatchingplugins(self.fspath)

    def getparent(self, cls):
        current = self
        while current and not isinstance(current, cls):
            current = current.parent
        return current

    def _prunetraceback(self, excinfo):
        pass

    def _repr_failure_py(self, excinfo, style=None):
        LE = self.session.funcargmanager.FuncargLookupError
        if excinfo.errisinstance(LE):
            function = excinfo.value.function
            if function is not None:
                fspath, lineno = getfslineno(function)
                lines, _ = inspect.getsourcelines(function)
                for i, line in enumerate(lines):
                    if line.strip().startswith('def'):
                        return FuncargLookupErrorRepr(fspath,
                                    lineno, lines[:i+1],
                                    str(excinfo.value.msg))
        if self.config.option.fulltrace:
            style="long"
        else:
            self._prunetraceback(excinfo)
        # XXX should excinfo.getrepr record all data and toterminal()
        # process it?
        if style is None:
            if self.config.option.tbstyle == "short":
                style = "short"
            else:
                style = "long"
        return excinfo.getrepr(funcargs=True,
                               showlocals=self.config.option.showlocals,
                               style=style)

    repr_failure = _repr_failure_py

class Collector(Node):
    """ Collector instances create children through collect()
        and thus iteratively build a tree.
    """
    class CollectError(Exception):
        """ an error during collection, contains a custom message. """

    def collect(self):
        """ returns a list of children (items and collectors)
            for this collection node.
        """
        raise NotImplementedError("abstract")

    def repr_failure(self, excinfo):
        """ represent a collection failure. """
        if excinfo.errisinstance(self.CollectError):
            exc = excinfo.value
            return str(exc.args[0])
        return self._repr_failure_py(excinfo, style="short")

    def _memocollect(self):
        """ internal helper method to cache results of calling collect(). """
        return self._memoizedcall('_collected', lambda: list(self.collect()))

    def _prunetraceback(self, excinfo):
        if hasattr(self, 'fspath'):
            path = self.fspath
            traceback = excinfo.traceback
            ntraceback = traceback.cut(path=self.fspath)
            if ntraceback == traceback:
                ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
            excinfo.traceback = ntraceback.filter()

class FSCollector(Collector):
    def __init__(self, fspath, parent=None, config=None, session=None):
        fspath = py.path.local(fspath) # xxx only for test_resultlog.py?
        name = fspath.basename
        if parent is not None:
            rel = fspath.relto(parent.fspath)
            if rel:
                name = rel
            name = name.replace(os.sep, "/")
        super(FSCollector, self).__init__(name, parent, config, session)
        self.fspath = fspath

    def _makeid(self):
        if self == self.session:
            return "."
        relpath = self.session.fspath.bestrelpath(self.fspath)
        if os.sep != "/":
            relpath = relpath.replace(os.sep, "/")
        return relpath

class File(FSCollector):
    """ base class for collecting tests from a file. """

class Item(Node):
    """ a basic test invocation item. Note that for a single function
    there might be multiple test invocation items.
    """
    nextitem = None

    def reportinfo(self):
        return self.fspath, None, ""

    def applymarker(self, marker):
        """ Apply a marker to this item.  This method is
        useful if you have several parametrized function
        and want to mark a single one of them.

        :arg marker: a :py:class:`_pytest.mark.MarkDecorator` object
            created by a call to ``py.test.mark.NAME(...)``.
        """
        if not isinstance(marker, pytest.mark.XYZ.__class__):
            raise ValueError("%r is not a py.test.mark.* object")
        self.keywords[marker.markname] = marker


    @property
    def location(self):
        try:
            return self._location
        except AttributeError:
            location = self.reportinfo()
            # bestrelpath is a quite slow function
            cache = self.config.__dict__.setdefault("_bestrelpathcache", {})
            try:
                fspath = cache[location[0]]
            except KeyError:
                fspath = self.session.fspath.bestrelpath(location[0])
                cache[location[0]] = fspath
            location = (fspath, location[1], str(location[2]))
            self._location = location
            return location

class FuncargLookupError(LookupError):
    """ could not find a factory. """
    def __init__(self, function, msg):
        self.function = function
        self.msg = msg

class FuncargManager:
    _argprefix = "pytest_funcarg__"
    FuncargLookupError = FuncargLookupError

    def __init__(self, session):
        self.session = session
        self.config = session.config
        self.arg2facspec = {}
        session.config.pluginmanager.register(self, "funcmanage")
        self._holderobjseen = set()
        self.setuplist = []

    ### XXX this hook should be called for historic events like pytest_configure
    ### so that we don't have to do the below pytest_collection hook
    def pytest_plugin_registered(self, plugin):
        #print "plugin_registered", plugin
        nodeid = ""
        try:
            p = py.path.local(plugin.__file__)
        except AttributeError:
            pass
        else:
            if p.basename.startswith("conftest.py"):
                nodeid = p.dirpath().relto(self.session.fspath)
        self._parsefactories(plugin, nodeid)

    @pytest.mark.tryfirst
    def pytest_collection(self, session):
        plugins = session.config.pluginmanager.getplugins()
        for plugin in plugins:
            self.pytest_plugin_registered(plugin)

    def pytest_generate_tests(self, metafunc):
        funcargnames = list(metafunc.funcargnames)
        _, allargnames = self.getsetuplist(metafunc.parentid)
        #print "setuplist, allargnames", setuplist, allargnames
        funcargnames.extend(allargnames)
        seen = set()
        while funcargnames:
            argname = funcargnames.pop(0)
            if argname in seen or argname == "request":
                continue
            seen.add(argname)
            faclist = self.getfactorylist(argname, metafunc.parentid,
                                          metafunc.function, raising=False)
            if faclist is None:
                continue # will raise FuncargLookupError at setup time
            for facdef in faclist:
                if facdef.params is not None:
                    metafunc.parametrize(argname, facdef.params, indirect=True,
                                             scope=facdef.scope)
                funcargnames.extend(facdef.funcargnames)

    def pytest_collection_modifyitems(self, items):
        # separate parametrized setups
        def sortparam(item1, item2):
            try:
                cs1 = item1.callspec
                cs2 = item2.callspec
                common = set(cs1.params).intersection(cs2.params)
            except AttributeError:
                pass
            else:
                if common:
                    common = list(common)
                    common.sort(key=lambda x: cs1._arg2scopenum[x])
                    for x in common:
                        res = cmp(cs1.params[x], cs2.params[x])
                        if res != 0:
                            return res
            return 0  # leave previous order
        items.sort(cmp=sortparam)

    def pytest_runtest_teardown(self, item, nextitem):
        try:
            cs1 = item.callspec
        except AttributeError:
            return
        for name in cs1.params:
            try:
                if name in nextitem.callspec.params and \
                    cs1.params[name] == nextitem.callspec.params[name]:
                    continue
            except AttributeError:
                pass
            key = (name, cs1.params[name])
            item.session._setupstate._callfinalizers(key)

    def _parsefactories(self, holderobj, nodeid):
        if holderobj in self._holderobjseen:
            return
        #print "parsefactories", holderobj
        self._holderobjseen.add(holderobj)
        for name in dir(holderobj):
            #print "check", holderobj, name
            obj = getattr(holderobj, name)
            if not callable(obj):
                continue
            # funcarg factories either have a pytest_funcarg__ prefix
            # or are "funcarg" marked
            if not callable(obj):
                continue
            marker = getattr(obj, "funcarg", None)
            if marker is not None and isinstance(marker, MarkInfo):
                assert not name.startswith(self._argprefix)
                argname = name
                scope = marker.kwargs.get("scope")
                params = marker.kwargs.get("params")
            elif name.startswith(self._argprefix):
                argname = name[len(self._argprefix):]
                scope = None
                params = None
            else:
                # no funcargs. check if we have a setup function.
                setup = getattr(obj, "setup", None)
                if setup is not None and isinstance(setup, MarkInfo):
                    scope = setup.kwargs.get("scope")
                    sf = SetupCall(self, nodeid, obj, scope)
                    self.setuplist.append(sf)
                continue
            faclist = self.arg2facspec.setdefault(argname, [])
            factorydef = FactoryDef(self, nodeid, argname, obj, scope, params)
            faclist.append(factorydef)
            ### check scope/params mismatch?

    def getsetuplist(self, nodeid):
        l = []
        allargnames = set()
        for setupcall in self.setuplist:
            if nodeid.startswith(setupcall.baseid):
                l.append(setupcall)
                allargnames.update(setupcall.funcargnames)
        return l, allargnames


    def getfactorylist(self, argname, nodeid, function, raising=True):
        try:
            factorydeflist = self.arg2facspec[argname]
        except KeyError:
            if raising:
                self._raiselookupfailed(argname, function, nodeid)
        else:
            return self._matchfactories(factorydeflist, nodeid)

    def _matchfactories(self, factorydeflist, nodeid):
        l = []
        for factorydef in factorydeflist:
            #print "check", basepath, nodeid
            if nodeid.startswith(factorydef.baseid):
                l.append(factorydef)
        return l

    def _raiselookupfailed(self, argname, function, nodeid):
        available = []
        for name, facdef in self.arg2facspec.items():
            faclist = self._matchfactories(facdef, nodeid)
            if faclist:
                available.append(name)
        msg = "LookupError: no factory found for argument %r" % (argname,)
        msg += "\n available funcargs: %s" %(", ".join(available),)
        msg += "\n use 'py.test --funcargs [testpath]' for help on them."
        raise FuncargLookupError(function, msg)


class FactoryDef:
    """ A container for a factory definition. """
    def __init__(self, funcargmanager, baseid, argname, func, scope, params):
        self.funcargmanager = funcargmanager
        self.baseid = baseid
        self.func = func
        self.argname = argname
        self.scope = scope
        self.params = params
        self.funcargnames = getfuncargnames(func)

class SetupCall:
    """ a container/helper for managing calls to setup functions. """
    def __init__(self, funcargmanager, baseid, func, scope):
        self.funcargmanager = funcargmanager
        self.baseid = baseid
        self.func = func
        self.funcargnames = getfuncargnames(func)
        self.scope = scope
        self.active = False
        self._finalizer = []

    def execute(self, kwargs):
        #assert not self.active
        self.active = True
        mp = monkeypatch()
        #if "request" in kwargs:
        #    request = kwargs["request"]
        #    def addfinalizer(func):
        #        #scopeitem = request._getscopeitem(scope)
        #        self._finalizer.append(func)
        #    mp.setattr(request, "addfinalizer", addfinalizer)
        try:
            self.func(**kwargs)
        finally:
            mp.undo()


class NoMatch(Exception):
    """ raised if matching cannot locate a matching names. """

class Session(FSCollector):
    class Interrupted(KeyboardInterrupt):
        """ signals an interrupted test run. """
        __module__ = 'builtins' # for py3

    def __init__(self, config):
        FSCollector.__init__(self, py.path.local(), parent=None,
                             config=config, session=self)
        self.config.pluginmanager.register(self, name="session", prepend=True)
        self._testsfailed = 0
        self.shouldstop = False
        self.trace = config.trace.root.get("collection")
        self._norecursepatterns = config.getini("norecursedirs")
        self.funcargmanager = FuncargManager(self)

    def pytest_collectstart(self):
        if self.shouldstop:
            raise self.Interrupted(self.shouldstop)

    def pytest_runtest_logreport(self, report):
        if report.failed and not hasattr(report, 'wasxfail'):
            self._testsfailed += 1
            maxfail = self.config.getvalue("maxfail")
            if maxfail and self._testsfailed >= maxfail:
                self.shouldstop = "stopping after %d failures" % (
                    self._testsfailed)
    pytest_collectreport = pytest_runtest_logreport

    def isinitpath(self, path):
        return path in self._initialpaths

    def gethookproxy(self, fspath):
        return HookProxy(fspath, self.config)

    def perform_collect(self, args=None, genitems=True):
        hook = self.config.hook
        try:
            items = self._perform_collect(args, genitems)
            hook.pytest_collection_modifyitems(session=self,
                config=self.config, items=items)
        finally:
            hook.pytest_collection_finish(session=self)
        return items

    def _perform_collect(self, args, genitems):
        if args is None:
            args = self.config.args
        self.trace("perform_collect", self, args)
        self.trace.root.indent += 1
        self._notfound = []
        self._initialpaths = set()
        self._initialparts = []
        self.items = items = []
        for arg in args:
            parts = self._parsearg(arg)
            self._initialparts.append(parts)
            self._initialpaths.add(parts[0])
        self.ihook.pytest_collectstart(collector=self)
        rep = self.ihook.pytest_make_collect_report(collector=self)
        self.ihook.pytest_collectreport(report=rep)
        self.trace.root.indent -= 1
        if self._notfound:
            for arg, exc in self._notfound:
                line = "(no name %r in any of %r)" % (arg, exc.args[0])
                raise pytest.UsageError("not found: %s\n%s" %(arg, line))
        if not genitems:
            return rep.result
        else:
            if rep.passed:
                for node in rep.result:
                    self.items.extend(self.genitems(node))
            return items

    def collect(self):
        for parts in self._initialparts:
            arg = "::".join(map(str, parts))
            self.trace("processing argument", arg)
            self.trace.root.indent += 1
            try:
                for x in self._collect(arg):
                    yield x
            except NoMatch:
                # we are inside a make_report hook so
                # we cannot directly pass through the exception
                self._notfound.append((arg, sys.exc_info()[1]))
                self.trace.root.indent -= 1
                break
            self.trace.root.indent -= 1

    def _collect(self, arg):
        names = self._parsearg(arg)
        path = names.pop(0)
        if path.check(dir=1):
            assert not names, "invalid arg %r" %(arg,)
            for path in path.visit(fil=lambda x: x.check(file=1),
                                   rec=self._recurse, bf=True, sort=True):
                for x in self._collectfile(path):
                    yield x
        else:
            assert path.check(file=1)
            for x in self.matchnodes(self._collectfile(path), names):
                yield x

    def _collectfile(self, path):
        ihook = self.gethookproxy(path)
        if not self.isinitpath(path):
            if ihook.pytest_ignore_collect(path=path, config=self.config):
                return ()
        return ihook.pytest_collect_file(path=path, parent=self)

    def _recurse(self, path):
        ihook = self.gethookproxy(path.dirpath())
        if ihook.pytest_ignore_collect(path=path, config=self.config):
            return
        for pat in self._norecursepatterns:
            if path.check(fnmatch=pat):
                return False
        ihook = self.gethookproxy(path)
        ihook.pytest_collect_directory(path=path, parent=self)
        return True

    def _tryconvertpyarg(self, x):
        mod = None
        path = [os.path.abspath('.')] + sys.path
        for name in x.split('.'):
            # ignore anything that's not a proper name here
            # else something like --pyargs will mess up '.'
            # since imp.find_module will actually sometimes work for it
            # but it's supposed to be considered a filesystem path
            # not a package
            if name_re.match(name) is None:
                return x
            try:
                fd, mod, type_ = imp.find_module(name, path)
            except ImportError:
                return x
            else:
                if fd is not None:
                    fd.close()

            if type_[2] != imp.PKG_DIRECTORY:
                path = [os.path.dirname(mod)]
            else:
                path = [mod]
        return mod

    def _parsearg(self, arg):
        """ return (fspath, names) tuple after checking the file exists. """
        arg = str(arg)
        if self.config.option.pyargs:
            arg = self._tryconvertpyarg(arg)
        parts = str(arg).split("::")
        relpath = parts[0].replace("/", os.sep)
        path = self.fspath.join(relpath, abs=True)
        if not path.check():
            if self.config.option.pyargs:
                msg = "file or package not found: "
            else:
                msg = "file not found: "
            raise pytest.UsageError(msg + arg)
        parts[0] = path
        return parts

    def matchnodes(self, matching, names):
        self.trace("matchnodes", matching, names)
        self.trace.root.indent += 1
        nodes = self._matchnodes(matching, names)
        num = len(nodes)
        self.trace("matchnodes finished -> ", num, "nodes")
        self.trace.root.indent -= 1
        if num == 0:
            raise NoMatch(matching, names[:1])
        return nodes

    def _matchnodes(self, matching, names):
        if not matching or not names:
            return matching
        name = names[0]
        assert name
        nextnames = names[1:]
        resultnodes = []
        for node in matching:
            if isinstance(node, pytest.Item):
                if not names:
                    resultnodes.append(node)
                continue
            assert isinstance(node, pytest.Collector)
            node.ihook.pytest_collectstart(collector=node)
            rep = node.ihook.pytest_make_collect_report(collector=node)
            if rep.passed:
                has_matched = False
                for x in rep.result:
                    if x.name == name:
                        resultnodes.extend(self.matchnodes([x], nextnames))
                        has_matched = True
                # XXX accept IDs that don't have "()" for class instances
                if not has_matched and len(rep.result) == 1 and x.name == "()":
                    nextnames.insert(0, name)
                    resultnodes.extend(self.matchnodes([x], nextnames))
            node.ihook.pytest_collectreport(report=rep)
        return resultnodes

    def genitems(self, node):
        self.trace("genitems", node)
        if isinstance(node, pytest.Item):
            node.ihook.pytest_itemcollected(item=node)
            yield node
        else:
            assert isinstance(node, pytest.Collector)
            node.ihook.pytest_collectstart(collector=node)
            rep = node.ihook.pytest_make_collect_report(collector=node)
            if rep.passed:
                for subnode in rep.result:
                    for x in self.genitems(subnode):
                        yield x
            node.ihook.pytest_collectreport(report=rep)

    # XXX not used yet
    def register_resource_factory(self, name, factoryfunc,
                                  matchscope=None,
                                  cachescope=None):
        """ register a factory function for the given name.

        :param name: the name which can be used to retrieve a value constructed
                     by the factory function later.
        :param factoryfunc: a function accepting (name, reqnode) parameters
                     and returning a value.
        :param matchscope: denotes visibility of the factory func.
                     Pass a particular Node instance if you want to
                     restrict factory function visilbility to its descendants.
                     Pass None if you want the factory func to be globally
                     availabile.
        :param cachescope: denotes caching scope. If you pass a node instance
                     the value returned by getresource() will be reused
                     for all descendants of that node.  Pass None (the default)
                     if you want no caching. Pass "session" if you want to
                     to cache on a per-session level.
        """

def getfslineno(obj):
    # xxx let decorators etc specify a sane ordering
    if hasattr(obj, 'place_as'):
        obj = obj.place_as
    fslineno = py.code.getfslineno(obj)
    assert isinstance(fslineno[1], int), obj
    return fslineno


class FuncargLookupErrorRepr(TerminalRepr):
    def __init__(self, filename, firstlineno, deflines, errorstring):
        self.deflines = deflines
        self.errorstring = errorstring
        self.filename = filename
        self.firstlineno = firstlineno

    def toterminal(self, tw):
        tw.line()
        for line in self.deflines:
            tw.line("    " + line.strip())
        for line in self.errorstring.split("\n"):
            tw.line("        " + line.strip(), red=True)
        tw.line()
        tw.line("%s:%d" % (self.filename, self.firstlineno+1))

def getfuncargnames(function, startindex=None):
    # XXX merge with main.py's varnames
    argnames = py.std.inspect.getargs(py.code.getrawcode(function))[0]
    if startindex is None:
        startindex = py.std.inspect.ismethod(function) and 1 or 0
    defaults = getattr(function, 'func_defaults',
                       getattr(function, '__defaults__', None)) or ()
    numdefaults = len(defaults)
    if numdefaults:
        return argnames[startindex:-numdefaults]
    return argnames[startindex:]

def readscope(func, markattr):
    marker = getattr(func, markattr, None)
    if marker is not None:
        return marker.kwargs.get("scope")