Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'resulting': 0.04; 'attribute': 0.07; 'suppose': 0.07; 'test,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'undefined': 0.09; 'python': 0.11; 'jan': 0.12; '(it': 0.16; 'insofar': 0.16; 'programmers.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'simulate': 0.16; 'violated': 0.16; 'wrote:': 0.18; 'users.': 0.18; 'module': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '(this': 0.29; 'clever': 0.31; 'mod': 0.31; "user's": 0.31; 'class': 0.32; 'this.': 0.32; 'something': 0.35; 'johnson': 0.35; 'no,': 0.35; 'test': 0.35; 'there': 0.35; 'idle': 0.36; 'done': 0.36; 'method': 0.36; 'should': 0.36; 'actions': 0.38; 'depends': 0.38; 'saves': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'is.': 0.60; 'received:173': 0.61; 'hang': 0.67; 'details,': 0.68; 'user,': 0.69; 'behaviors': 0.74; 'behavior': 0.77; 'eyes': 0.78; 'mock': 0.84; 'mod.': 0.84; 'received:fios.verizon.net': 0.84; 'behaviors.': 0.91; 'subject:Global': 0.91; 'rick': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: PyMyth: Global variables are evil... WRONG! Date: Tue, 12 Nov 2013 20:20:09 -0500 References: <7b97786a-2aaf-454c-8c3a-1c19d20d4345@googlegroups.com> <82561733-d131-45de-ab10-c847f1960dc4@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-59-117-133.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <82561733-d131-45de-ab10-c847f1960dc4@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384305627 news.xs4all.nl 15938 [2001:888:2000:d::a6]:58567 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59272 On 11/11/2013 11:46 PM, Rick Johnson wrote: > No, Python modules can be poked, prodded, and violated by > any pervert who can spell the word "import". Or by clever programmers. > Attribute values can be reassigned and state can be > externally manipulated Perhaps for good reasons. > resulting in all types of undefined behaviors Not necessarily. Manipulation can also eliminate undefined behaviors. Suppose I want to test a Idle function that uses a tkinter.messagebox class to communicate with users. Perhaps the module has from tkinter.messagebox import askretrycancel The behavior of askretrycancel(*args) is undefined insofar as it depends on the arbitrary actions of a human user (it may even hang forever). In an automated test, there is no user, and it is difficult to simulate the eyes and fingers of one while leaving askretrycancel as it is. Monkeypatching with a mock solves this. Simplifying a bit, and leaving out details, I have done something like this. from mock_tkinter import mbox # mocks all messageboxes mock_arc = mbox() import mod mod.askretrycancel = mock_arc This makes mod.askretrycancel deterministic in that I can preload a response into mock_arc before calling the function or method in mod. (This simulates a user's fingers.) The mock also saves the values sent to it to see if they are what they should be. (This simulates a user's eyes.) -- Terry Jan Reedy