[py-dev] Re: [py-svn] r24998 - py/dist/py/test
On 25/03/06, cfbolz@codespeak.net <cfbolz@codespeak.net> wrote:
Author: cfbolz Date: Sat Mar 25 16:34:53 2006 New Revision: 24998
Modified: py/dist/py/test/cmdline.py Log: make driving py.test programmatically easier, by allowing to not use sys.argv. User feature request.
I like this idea, but I wonder if it a bit less magic to expect a list of args rather than splitting a string in main()? That way it would be easy to modify an existing copy of sys.argv or use the args from another option parsing library (like optparse) as well. Take care, -Brian
Modified: py/dist/py/test/cmdline.py ============================================================================== --- py/dist/py/test/cmdline.py (original) +++ py/dist/py/test/cmdline.py Sat Mar 25 16:34:53 2006 @@ -4,9 +4,13 @@ # main entry point #
-def main(): +def main(args=None): warn_about_missing_assertion() - config, args = py.test.Config.parse(py.std.sys.argv[1:]) + if args is None: + args = py.std.sys.argv[1:] + elif isinstance(args, basestring): + args = args.split(" ") + config, args = py.test.Config.parse(args) sessionclass = config.getsessionclass() session = sessionclass(config) try:
Brian Dorsey wrote:
On 25/03/06, cfbolz@codespeak.net <cfbolz@codespeak.net> wrote:
Author: cfbolz Date: Sat Mar 25 16:34:53 2006 New Revision: 24998
Modified: py/dist/py/test/cmdline.py Log: make driving py.test programmatically easier, by allowing to not use sys.argv. User feature request.
I like this idea, but I wonder if it a bit less magic to expect a list of args rather than splitting a string in main()? That way it would be easy to modify an existing copy of sys.argv or use the args from another option parsing library (like optparse) as well.
A list would be better, how should main() know that 'filename with spaces.py' is one name? The shell would interpret 'filename\ with\ spaces.py' or '"filename with spaces.py"' as one name with spaces (but we do not want to reimplement this). We should let the shell/user(who uses main directly) do the escaping stuff and let main work with a list. cheers, jan
Take care, -Brian
Modified: py/dist/py/test/cmdline.py ============================================================================== --- py/dist/py/test/cmdline.py (original) +++ py/dist/py/test/cmdline.py Sat Mar 25 16:34:53 2006 @@ -4,9 +4,13 @@ # main entry point #
-def main(): +def main(args=None): warn_about_missing_assertion() - config, args = py.test.Config.parse(py.std.sys.argv[1:]) + if args is None: + args = py.std.sys.argv[1:] + elif isinstance(args, basestring): + args = args.split(" ") + config, args = py.test.Config.parse(args) sessionclass = config.getsessionclass() session = sessionclass(config) try:
py-dev mailing list py-dev@codespeak.net http://codespeak.net/mailman/listinfo/py-dev
On Sat, Mar 25, 2006 at 20:13 +0100, Jan Balster wrote:
Brian Dorsey wrote:
On 25/03/06, cfbolz@codespeak.net <cfbolz@codespeak.net> wrote:
Author: cfbolz Date: Sat Mar 25 16:34:53 2006 New Revision: 24998
Modified: py/dist/py/test/cmdline.py Log: make driving py.test programmatically easier, by allowing to not use sys.argv. User feature request.
I like this idea, but I wonder if it a bit less magic to expect a list of args rather than splitting a string in main()? That way it would be easy to modify an existing copy of sys.argv or use the args from another option parsing library (like optparse) as well.
A list would be better, how should main() know that 'filename with spaces.py' is one name? The shell would interpret 'filename\ with\ spaces.py' or '"filename with spaces.py"' as one name with spaces (but we do not want to reimplement this). We should let the shell/user(who uses main directly) do the escaping stuff and let main work with a list.
We could probably decide on the input type we get, i guess. I don't know if optparse can do the splitting into arguments good enough, though. OTOH, for programmatically invoking the py.test cmdline it is not so bad to push the decision to the caller. holger
Hi Brian! Brian Dorsey wrote:
On 25/03/06, cfbolz@codespeak.net <cfbolz@codespeak.net> wrote:
Author: cfbolz Date: Sat Mar 25 16:34:53 2006 New Revision: 24998
Modified: py/dist/py/test/cmdline.py Log: make driving py.test programmatically easier, by allowing to not use sys.argv. User feature request.
I like this idea, but I wonder if it a bit less magic to expect a list of args rather than splitting a string in main()? That way it would be easy to modify an existing copy of sys.argv or use the args from another option parsing library (like optparse) as well.
Huh? But a list or args works too, no? def main(args=None): warn_about_missing_assertion() if args is None: args = py.std.sys.argv[1:] elif isinstance(args, basestring): args = args.split(" ") config, args = py.test.Config.parse(args) ... If args is a list it is passed unchanched to parse. I agree that splitting a string is maybe too magical. Cheers, Carl Friedrich
On 26/03/06, Carl Friedrich Bolz <cfbolz@gmx.de> wrote:
Huh? But a list or args works too, no?
def main(args=None): warn_about_missing_assertion() if args is None: args = py.std.sys.argv[1:] elif isinstance(args, basestring): args = args.split(" ") config, args = py.test.Config.parse(args) ...
If args is a list it is passed unchanched to parse. I agree that splitting a string is maybe too magical.
Thank you Carl. While reading it, I completely missed the fact that if args is a list, it passes through the if/elif unchanged. Apologies for missing that! Back to lurking mode for me... Take care, -Brian
[Brian Dorsey]
On 26/03/06, Carl Friedrich Bolz <cfbolz@gmx.de> wrote:
Huh? But a list or args works too, no?
def main(args=None): warn_about_missing_assertion() if args is None: args = py.std.sys.argv[1:] elif isinstance(args, basestring): args = args.split(" ") config, args = py.test.Config.parse(args) ...
If args is a list it is passed unchanched to parse. I agree that splitting a string is maybe too magical.
[...] if args is a list, it passes through the if/elif unchanged. Apologies for missing that!
I do not know if the following comments are relevant to this particular patch, or for pylib more generally. For one of my clients (an all-Python shop), and after good thought, we chose a useful convention to be followed by main programs. Programs look like: [...] def main(*arguments): [...] [...] if __name__ == '__main__': main(*sys.argv[1:]) So, a program may always execute another one by doing: import ANOTHER ANOTHER.main(ARG1, ARG2, ...) ARGs are strings in that context, there is no kind of concern about matters of shell splitting or escaping. What is really nice for this client is the site-wide uniformity/ubiquity of this convention. It also helps for interactive usage or debugging, even if compared to the shell, a few extra commas and string delimiters are required. Maybe (or maybe not) pylib could give itself a similar approach, quite systematically whenever or wherever it makes sense. P.S. - I simplified the above convention a bit, as we often have, instead of "def main(*arguments)", a construct similar to this one: class Main: DEFAULT-OPTION-VALUES-AS-CLASS-VARIABLES [...] def main(self, *arguments): [...] [...] run = Main() main = run.main -- François Pinard http://pinard.progiciels-bpi.ca
participants (5)
-
Brian Dorsey -
Carl Friedrich Bolz -
François Pinard -
hpk@trillke.net -
Jan Balster