Re: How to test?
On 6/17/2020 12:34 PM, Tony Flury via Python-list wrote: In a recent application that I wrote (where output to the console was important), I tested it using the 'unittest' framework, and by patching sys.stderr to be a StringIO - that way my test case could inspect what was being output. Tony's code with hard returns added so that code lines remain separated instead of wrapping. with patch('sys.stderr', StringIO()) as stderr: application.do_stuff() self.assertTrue(stderr.getvalue(), 'Woops - that didn\'t work') This doc, worth reading more than once, is https://docs.python.org/3/library/unittest.mock.html#the-patchers -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Property for dataclass field with default value
Ivan Ivanyuk wrote: > On Thu, 18 Jun 2020 at 11:26, Peter Otten <__pete...@web.de> wrote: >> >> Ivan Ivanyuk wrote: >> >> > Hello All, >> > >> > I have some trouble using @dataclass together with @property decorator >> > or property() function. >> > >> > From the documentation and PEP is seems that the intended behaviour of >> > @dataclass is to be the same as normal __init__() that sets instance >> > variables. >> > >> > But it seems that when using @property decorator some parts work >> > differently when relying on default values. I'm using Pyhton 3.8.3 for >> > this. >> > >> > Using the code: >> > >> > from dataclasses import dataclass >> > >> > @dataclass >> > class Container: >> > x: int = 30 >> > >> > @property >> > def x(self) -> int: >> > return self._x >> > >> > @x.setter >> > def x(self, z: int): >> > if z > 1: >> > self._x = z >> > else: >> > raise ValueError [...] >> Your class definition is basically >> >> class Container: >> x = default_value >> x = property_x >> >> i. e. you use the same name twice. A possible workaround might be to >> define the property in a subclass. That way you get distinct namespaces >> for the default value and the property: [...] > Didn't think about it in such a way! But now that it was pointed, I > see no obvious way to resolve this in dataclass decorator given the > way it's implemented now. And while adding another class looks easy it > somewhat detracts from the dataclasses' purpose of removing > boilerplate. > > Does it seems like a good idea to ask for documenting that behaviour > in dataclasses documentation or it's not popular enough use case? I don't know. From what I've seen runtime value checking has not even been considered even though it is part of attrs which seems to be a source of inspiration for dataclasses. https://www.attrs.org/en/stable/examples.html#validators If you make a feature request on python-ideas or the bugtracker you may at least find out why this part was not copied. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to test?
On 24/04/2020 19:40, Manfred Lotz wrote: I have a command like application which checks a directory tree for certain things. If there are errors then messages will be written to stdout. How to test this in the best way? One idea was for the error situations to write messages to files and then later when running the tests to compare the error messages output to the previously saved output. Is there anything better? In a recent application that I wrote (where output to the console was important), I tested it using the 'unittest' framework, and by patching sys.stderr to be a StringIO - that way my test case could inspect what was being output. with patch('sys.stderr', StringIO()) as stderr:application.do_stuff()self.assertTrue(stderr.getvalue(), 'Woops - that didn\'t work') I am not sure of the structure of your application, and whether you have a callable API that you can invoke. -- https://mail.python.org/mailman/listinfo/python-list
Re: why same ctypes code runs on win7, but not on win10?
On 6/19/20, oyster wrote: > The attachment is a very simple code that uses the DLL from > https://github.com/ying32/govcl to create a GUI application. The code > runs on my python 3.6.10 64 bits with win7 64 bits, but failed on my > python 3.6.10 64 bits and python 3.7.5 with win10 64 bits, by saying > following message. What is the problem? Thanks. > ``` > vcl.Application_Initialize(Application) > OSError: exception: access violation reading 0x9BCEE490 > ``` The default return type is a 32-bit signed integer, and apparently Application_Instance returns a memory address (i.e. a pointer). You can't rely on a 64-bit process loading DLLs and allocating memory only in the lower 32-bit address space. The following should fix the problem: import ctypes # In Python 3.8+, ctypes no longer searches PATH or the # current working directory when loading DLLs. liblcl_path = 'path/to/liblcl.dll' vcl = ctypes.CDLL(liblcl_path) # Use a c_void_p subclass, which, unlike c_void_p itself, # will not be automatically converted to a Python integer. class vclapp_p(ctypes.c_void_p): """VCL Application Instance""" # Override the default c_int result type. vcl.Application_Instance.restype = vclapp_p def main(): app = vcl.Application_Instance() vcl.Application_Initialize(app) form = vcl.Application_CreateForm(app, False) vcl.Application_Run(app) -- https://mail.python.org/mailman/listinfo/python-list
why same ctypes code runs on win7, but not on win10?
The attachment is a very simple code that uses the DLL from https://github.com/ying32/govcl to create a GUI application. The code runs on my python 3.6.10 64 bits with win7 64 bits, but failed on my python 3.6.10 64 bits and python 3.7.5 with win10 64 bits, by saying following message. What is the problem? Thanks. ``` vcl.Application_Initialize(Application) OSError: exception: access violation reading 0x9BCEE490 ``` ```python import ctypes def main(): vcl = ctypes.CDLL('liblcl.dll') Application = vcl.Application_Instance() vcl.Application_Initialize(Application) form = vcl.Application_CreateForm(Application, False); vcl.Application_Run(Application); main() ``` -- https://mail.python.org/mailman/listinfo/python-list