Skip to content

What would you like to be added to Construct? #824

Closed

Description

@arekbulski

@DanseMacabre
I am opening this ticket for a chat...

Activity

  1. arekbulski commented on Jan 17, 2020

    @arekbulski
    MemberAuthor

    @yannayl
    I also welcome your input. How is Lazy* stuff working for ya?

  2. changed the title [-]Chat with DanseMacabre[/-] [+]What would you like to be added to Construct?[/+] on Jan 17, 2020
  3. Evidlo commented on Jan 18, 2020

    @Evidlo

    I'm still looking for a way to differentiate exceptions raised by Checksum.

    I have multiple Checksums in my application which check different conditions (e.g. file validation, credentials authentication), but I have to print out a really generic error message when any of these fail because there's no way to tell the exceptions apart.

  4. arekbulski commented on Jan 18, 2020

    @arekbulski
    MemberAuthor

    I dont think this is really necessary to be available to everyone. But the solution is simple, just copy paste the source of Checksum into your script and change the constructor to take an exception type. Simple.

    class Checksum(Construct):
        def __init__(self, checksumfield, hashfunc, bytesfunc, exception):
            super(Checksum, self).__init__()
            self.checksumfield = checksumfield
            self.hashfunc = hashfunc
            self.bytesfunc = bytesfunc
            self.flagbuildnone = True
            self.exception = exception
    
        def _parse(self, stream, context, path):
            hash1 = self.checksumfield._parsereport(stream, context, path)
            hash2 = self.hashfunc(self.bytesfunc(context))
            if hash1 != hash2:
                raise self.exception("wrong checksum, read %r, computed %r" % (
                    hash1 if not isinstance(hash1,bytestringtype) else binascii.hexlify(hash1),
                    hash2 if not isinstance(hash2,bytestringtype) else binascii.hexlify(hash2), ))
            return hash1
    
        def _build(self, obj, stream, context, path):
            hash2 = self.hashfunc(self.bytesfunc(context))
            self.checksumfield._build(hash2, stream, context, path)
            return hash2
    
        def _sizeof(self, context, path):
            return self.checksumfield._sizeof(context, path)
  5. arekbulski commented on Jan 18, 2020

    @arekbulski
    MemberAuthor

    This approach was kinda already written in the docs, just not so directly. See for yourself:
    https://construct.readthedocs.io/en/latest/extending.html#constructs
    I added the green box just now.

  6. arekbulski commented on Jan 18, 2020

    @arekbulski
    MemberAuthor

    I just found your old Issue that you had for this: #731

  7. arekbulski commented on Jan 19, 2020

    @arekbulski
    MemberAuthor

    I guess we can also post here #826 which is showing path in exceptin messages. Already done.

  8. arekbulski commented on Jan 19, 2020

    @arekbulski
    MemberAuthor

    MESSAGE TO EVERYONE: I am about to remove Embedded and EmbeddedSwitch from the core library. I know its in use here and there but please understand, the semantics have changed so much that using this as is now is pointless.

  9. sbasharati commented on Jan 20, 2020

    @sbasharati

    @arekbulski Some code examples to migrate Embedded and EmbeddedSwitch to 2.10 would be useful.

  10. arekbulski commented on Jan 20, 2020

    @arekbulski
    MemberAuthor

    The alternative to embedding is simply nesting. Nesting is already covered in the docs through and through. Nesting is the recommended way of doing things anyway.

  11. arekbulski commented on Jan 20, 2020

    @arekbulski
    MemberAuthor
  12. sbasharati commented on Jan 21, 2020

    @sbasharati

    @arekbulski GreedyByte is no longer able to build from bytearray in python 3.
    the example below works in python 2.7 but not 3. prior to 2.10 this was working for both 2 and 3. I believe btyearray should be treated as bytes.

    d = bytearray(b'\x11\x01\x05')
    GreedyBytes.build(d)

  13. arekbulski commented on Jan 21, 2020

    @arekbulski
    MemberAuthor

    This must have been an issue at 2.9 or earlier, because 2.10 didnt touch these things.
    But yes, I will fix this.
    Fixed it.

  14. 58 remaining items

  15. arekbulski commented on Feb 8, 2020

    @arekbulski
    MemberAuthor

    What I think is that you are doing unsolicited advertisement of your project. It has nothing to do with Construct at all.

  16. BLooperZ commented on Feb 8, 2020

    @BLooperZ

    Sorry to hear that, just wanted to share this concept as it might benefit this project.
    (the linked repo was opened simply to show the example code to give the idea (regarding recent discussion about type annotations), so further work can be migrated here if the idea is good enough)

  17. arekbulski commented on Feb 8, 2020

    @arekbulski
    MemberAuthor

    Okay okay. The example you provide is even less comprehensible than examples that were provided in this thread. Unreadable.

  18. arekbulski commented on Feb 11, 2020

    @arekbulski
    MemberAuthor

    Hmm what happened to @DanseMacabre ...

  19. DanseMacabre commented on Feb 19, 2020

    @DanseMacabre

    @arekbulski Hi! GMail decided that it didn't want me to see any emails from github for a while.

    Should be fixed now, although I am looking in utter disgust at the increase in backlog!

  20. arekbulski commented on Feb 19, 2020

    @arekbulski
    MemberAuthor

    @DanseMacabre I seen your post on gitter and wanted to ask, what are your suggestions for further expansion of Construct?

  21. DanseMacabre commented on Feb 21, 2020

    @DanseMacabre

    My main desire at the moment is to see the currently monolithic nature of the project refactored properly into separately named files. This would have benefits to debugging by improving the quality of the stack trace (i.e. _sizeof in <subcon>.py, rather than _sizeof (which, there's many!) in core.py). It would also enable quicker zooming in on a specific subcon, or class for modification or understanding, and allow switching between different classes without scrolling loads in a file or using IDE bookmarks.

  22. arekbulski commented on Feb 21, 2020

    @arekbulski
    MemberAuthor

    This is not going to happen as long as I am the main developer of this library. When I took over the project, I found it jumping between modules to be a hassle and keeping everything (well almost everything) together to be much more manageable. You seem to be unaware of the fact that now all exceptions include path information (see ticket #826).

  23. DanseMacabre commented on Feb 21, 2020

    @DanseMacabre

    In the stack trace, I see the following (latest version) when running something which will cause exceptions (the sizeof thing being discussed in aother ticket, incidentally):

    File "C:\Users\me\ConstructVenv\lib\site-packages\construct\core.py", line 382, in sizeof
    return self._sizeof(context, "(sizeof)")
    File "C:\Users\me\ConstructVenv\lib\site-packages\construct\core.py", line 3642, in _sizeof
    return sc._sizeof(context, path)
    File "C:\Users\me\ConstructVenv\lib\site-packages\construct\core.py", line 2016, in _sizeof
    return sum(sc._sizeof(context, path) for sc in self.subcons)
    File "C:\Users\me\ConstructVenv\lib\site-packages\construct\core.py", line 2016, in
    return sum(sc._sizeof(context, path) for sc in self.subcons)
    File "C:\Users\me\ConstructVenv\lib\site-packages\construct\core.py", line 2447, in _sizeof
    return self.subcon._sizeof(context, path)
    File "C:\Users\me\ConstructVenv\lib\site-packages\construct\core.py", line 685, in _sizeof
    return self.subcon._sizeof(context, path)
    File "C:\Users\me\ConstructVenv\lib\site-packages\construct\core.py", line 4592, in _sizeof

    There are six different sizeof calls in six different locations of the core.py file, something specifying in which classes the issue occurred would be suitable, don't particularly care what method is used, though...

    Whilst python is known for it's good use of "flat" files and classes, a 5k line file is pushing it a bit, IMO, however I accept that the refactor work would be a lot of effort for not much gain.

    The new path exceptions are very nice though, thanks @Evidlo for those!

    As you know, I'd also like to see better checksumming/exceptions with ignorable checksum warnings etc

    Any other ideas are forgotten since that Gitter post, but I will be working again with Construct to parse another binary format imminently which will I'm sure throw up some ideas!

  24. arekbulski commented on Feb 22, 2020

    @arekbulski
    MemberAuthor

    Well, if you want to thank for the path exceptions info then half of that goes to me. I implemented half of it way way back. The difference that Evidlo made was that before, it was only visible on Travis as locals, now it displays for users on their consoles. Still, on everyone's behalf thank you @Evidlo.

    Checksum warnings are not gonna appear in the core library. I suggest that you open a new Issue and post there a modified version of Checksum. Just so other people can find this "recipe".
    EDIT: Already beat you to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions