+
Skip to content

pyscf parses sys.argv when setting max_memory on a Mole object #470

@eljost

Description

@eljost

Dear developers,
while writing & executing some unittests for my own code that uses pyscf I encountered the following issue:

Setting max_memory on a mol object leads to a call of cmd_args() in [...]/pyscf/gto/mole.py. When I execute such code with pytest -m [some mark] then pyscf will parse sys.argv[1:] in cmd_args() and the mark supplied to pytest with -m [some mark] will be interpreted as memory argument py pyscf.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../anaconda3/lib/python3.6/site-packages/pyscf/gto/mole.py:2033: in build
    _update_from_cmdargs_(self)
../../../anaconda3/lib/python3.6/site-packages/pyscf/gto/mole.py:3103: in _update_from_cmdargs_
    opts = cmd_args.cmd_args()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def cmd_args():
        '''
        get input from cmdline
        '''
        parser = argparse.ArgumentParser()
        parser.add_argument('-v', '--verbose',
                            action='store_false', dest='verbose', default=0,
                            help='make lots of noise')
        parser.add_argument('-q', '--quiet',
                            action='store_false', dest='quite', default=False,
                            help='be very quiet')
        parser.add_argument('-o', '--output',
                            dest='output', metavar='FILE', help='write output to FILE')
        parser.add_argument('-m', '--max-memory',
                            action='store', dest='max_memory', metavar='NUM',
                            help='maximum memory to use (in MB)')
    
        (opts, args_left) = parser.parse_known_args()
    
        if opts.quite:
            opts.verbose = pyscf.lib.logger.QUIET
    
        if opts.verbose:
            opts.verbose = pyscf.lib.logger.DEBUG
    
        if opts.max_memory:
>           opts.max_memory = float(opts.max_memory)
E           ValueError: could not convert string to float: 'pyscf'

Here is code that reproduces the issue.

from pyscf import gto
import pytest


# Call with:
#   pytest -m pyscf bug.py


@pytest.mark.pyscf
def test_mark():
    mol = gto.Mole()
    atoms = "H H".split()
    coords = [(0., 0., 0.), (0., 0., 1.4)]
    mol.atom = [(atom, c) for atom, c
                in zip(atoms, coords)]
    mol.basis = "321g"
    mol.unit = "Bohr"
    mol.verbose = 4
    mol.max_memory = 2000
    mol.build()

    return mol

Executing this code with pytest -m pyscf bug.py will lead to the crash. I tested it with pyscf 1.6.5.

That pyscf parses sys.argv[1:] even though it is solely used as a python module is rather suprising.

With best regards & thanks for your work,
Johannes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载