[Python-Dev] Re: GDB not breaking at the right place

2021-05-23 Thread Skip Montanaro
> I strongly suggest to only build Python with -O0 when using gdb. -Og
> enables too many optimizations which makes gdb less usable.

Thanks, Victor. It never made sense to me that you would want any
optimizations enabled when truly debugging code (as opposed to wanting
debug symbols and a sane traceback in production code).

I'm getting more convinced that the problem I'm seeing is a GCC/GDB
thing, particularly because I can move the erroneous stopping point by
changing the GCC optimization level. I'll probably open a bugzilla
report just so it's on that team's radar screen. In the meantime, to
get going again I wrote a crude script which maps the
file:function:label form to file:linenumber form. That way I can
save/restore breakpoints across GDB sessions and still avoid problems
when the offsets to specific instructions change.

Skip

Skip
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/AOKKJHCRLEYU64V425AJHMM46CCYW55M/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: GDB not breaking at the right place

2021-05-23 Thread MRAB

On 2021-05-23 14:56, Skip Montanaro wrote:

I strongly suggest to only build Python with -O0 when using gdb. -Og
enables too many optimizations which makes gdb less usable.


Thanks, Victor. It never made sense to me that you would want any
optimizations enabled when truly debugging code (as opposed to wanting
debug symbols and a sane traceback in production code).

I'm getting more convinced that the problem I'm seeing is a GCC/GDB
thing, particularly because I can move the erroneous stopping point by
changing the GCC optimization level. I'll probably open a bugzilla
report just so it's on that team's radar screen. In the meantime, to
get going again I wrote a crude script which maps the
file:function:label form to file:linenumber form. That way I can
save/restore breakpoints across GDB sessions and still avoid problems
when the offsets to specific instructions change.

When I want to step through the regex module, I turn off optimisation, 
because any optimisation could move things around or combine things, 
making single-stepping difficult, and this is with Microsoft Visual Studio.


Just turn off optimisation when you want to single-step.
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/EWHXLCO4DKWGM3CZPZC2YFTNZPUSUSIY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: GDB not breaking at the right place

2021-05-23 Thread Skip Montanaro
> Just turn off optimisation when you want to single-step.

But I don't just want to single-step. I want to break at the target
label associated with a specific opcode. (I am - in fits and starts -
working on register-based virtual machine instructions). If I'm
working on, for example, the register version of POP_JUMP_IF_FALSE,
stepping through a bunch of instances of the working register version
of LOAD_FAST or EXTENDED_ARG isn't going to be helpful. Further, I
have a set of GDB commands I want to execute at each breakpoint. And I
want to do this across GDB sessions (so, I save breakpoints and
user-defined commands in a GDB command file).

Just to make things concrete, here's what I want to print every time I
hit my JUMP_IF_FALSE_REG statement's code:

define print_opargs_jump
  p/x oparg
  p (oparg >> 16) & 0xff | (oparg >> 8) & 0xff
  p oparg & 0xff
  p *fastlocals@4
end

This break command should do the trick:

break ceval_reg.h:_PyEval_EvalFrameDefault:TARGET_JUMP_IF_FALSE_REG
  commands
print_opargs_jump
  end

but it doesn't. GDB stops execution in some completely other one of
the 50+ instructions I've implemented so far. And not even at the
start of said other instruction. This problem is true whether I
compile with -g -Og or -g -O0. The only difference between the two is
that GDB stops execution at different incorrect locations. That, as
you might imagine, makes debugging difficult.

Setting breakpoints by line number works as expected. In all the years
I've been using GDB I've never had a problem with that. However,
that's fragile in the face of changing offsets for different
instructions in the C code (add a new instruction, add or delete C
code, reorder instructions for some reason, etc), it's difficult to
maintain those kinds of breakpoints. I wrote a crude little script
that converts the above break command into this:

break ceval_reg.h:440
  commands
print_opargs_jump
  end

This is just a workaround until someone (unlikely to be me) solves the
problem with breaking at labels.

If someone could refute or verify my contention that breaking via
labels is broken, I'd much appreciate it. I've not yet checked setting
labeled breakpoints directly in ceval.c. To minimize merge conflicts,
I'm implementing my register instructions in a new header file,
Python/ceval_reg.h, which is #included in ceval.c at the desired spot.
Maybe that factors into the issue.

Skip
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/DE33TZBPIRV5DEOFFZBFDXPWRLZE47IB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: The repr of a sentinel

2021-05-23 Thread Luciano Ramalho
On Sun, May 23, 2021 at 3:37 AM Tal Einat  wrote:
> I put up an early draft of a PEP on a branch in the PEPs repo:
> https://github.com/python/peps/blob/sentinels/pep-.rst

Thanks for that PEP, Tal. Good ideas and recap there.

I think repr= should have a default: the name of the class within <>:
.

Sentinels don't have state or any other data besides a name, so I
would prefer not to force users to create a class just so they can
instantiate it.

Why not just this?

NotGiven = sentinel('')

In this case it's harder to provide a good default repr.

On the other hand, if the user must create a class, the class itself
should be the sentinel. Class objects are already singletons, so that
makes sense.

Here is a possible class-based API:

class NotGiven(Sentinel):
pass

That's it. Now I can use NotGiven as the sentinel, and its default
repr is .

Behind the scenes we can have a SentinelMeta metaclass with all the
magic that could be required--including the default __repr__ method.

What do you think?

Cheers,

Luciano


>
> (Note: This link will break once the temporary branch is deleted.)
>
> I wrote it to summarize the discussions, organize my thoughts and
> explore the options. I stopped working on it late at night and sent a
> link to a few people to get some opinions. I didn’t intend to make it
> public yet, but it was noticed and replied to on the
> discuss.python.org thread where I put up the poll [1], so the cat is
> out of the proverbial bag now…
>
> Luciano, your wish is granted! ;)
> - Tal Einat
>
> [1] https://discuss.python.org/t/sentinel-values-in-the-stdlib/8810/



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
| http://shop.oreilly.com/product/0636920032519.do
|  Technical Principal at ThoughtWorks
|  Twitter: @ramalhoorg
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/32FKEQ4GBXHGVNSX5NYPW5AKCOFNBI5C/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: The repr of a sentinel

2021-05-23 Thread Luciano Ramalho
Sorry about my detour into the rejected idea of a factory function.

But how about this class-based API?

class NotGiven(Sentinel):
pass


Now I can use NotGiven as the sentinel, and its default repr is .

Behind the scenes we can have a SentinelMeta metaclass with all the
magic that could be required--including the default __repr__ method.

Cheers,

Luciano



> >
> > (Note: This link will break once the temporary branch is deleted.)
> >
> > I wrote it to summarize the discussions, organize my thoughts and
> > explore the options. I stopped working on it late at night and sent a
> > link to a few people to get some opinions. I didn’t intend to make it
> > public yet, but it was noticed and replied to on the
> > discuss.python.org thread where I put up the poll [1], so the cat is
> > out of the proverbial bag now…
> >
> > Luciano, your wish is granted! ;)
> > - Tal Einat
> >
> > [1] https://discuss.python.org/t/sentinel-values-in-the-stdlib/8810/
>
>
>
> --
> Luciano Ramalho
> |  Author of Fluent Python (O'Reilly, 2015)
> | http://shop.oreilly.com/product/0636920032519.do
> |  Technical Principal at ThoughtWorks
> |  Twitter: @ramalhoorg



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
| http://shop.oreilly.com/product/0636920032519.do
|  Technical Principal at ThoughtWorks
|  Twitter: @ramalhoorg
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/2B6VXRYS7UHJRX762DI2LSOGGGRJHXIV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: The repr of a sentinel

2021-05-23 Thread MRAB

On 2021-05-24 01:37, Luciano Ramalho wrote:

Sorry about my detour into the rejected idea of a factory function.

But how about this class-based API?

class NotGiven(Sentinel):
 pass


Now I can use NotGiven as the sentinel, and its default repr is .

The repr of other singletons are the names of those singletons, eg. 
"None", so why "" instead of "NotGiven"?



Behind the scenes we can have a SentinelMeta metaclass with all the
magic that could be required--including the default __repr__ method.


___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/C672ZR7E6AD6KD5UVLUWD3GDQT66VACA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: The repr of a sentinel

2021-05-23 Thread Luciano Ramalho
Here is a simple implementation of that Sentinel class:

https://github.com/fluentpython/example-code-2e/blob/master/25-class-metaprog/sentinel/sentinel.py

Tests in the same directory. That's not a real package yet, just a
couple of super simple examples I may use in Fluent Python 2e.

Cheers,

Luciano

On Sun, May 23, 2021 at 9:37 PM Luciano Ramalho  wrote:
>
> Sorry about my detour into the rejected idea of a factory function.
>
> But how about this class-based API?
>
> class NotGiven(Sentinel):
> pass
>
>
> Now I can use NotGiven as the sentinel, and its default repr is .
>
> Behind the scenes we can have a SentinelMeta metaclass with all the
> magic that could be required--including the default __repr__ method.
>
> Cheers,
>
> Luciano
>
>
>
> > >
> > > (Note: This link will break once the temporary branch is deleted.)
> > >
> > > I wrote it to summarize the discussions, organize my thoughts and
> > > explore the options. I stopped working on it late at night and sent a
> > > link to a few people to get some opinions. I didn’t intend to make it
> > > public yet, but it was noticed and replied to on the
> > > discuss.python.org thread where I put up the poll [1], so the cat is
> > > out of the proverbial bag now…
> > >
> > > Luciano, your wish is granted! ;)
> > > - Tal Einat
> > >
> > > [1] https://discuss.python.org/t/sentinel-values-in-the-stdlib/8810/
> >
> >
> >
> > --
> > Luciano Ramalho
> > |  Author of Fluent Python (O'Reilly, 2015)
> > | http://shop.oreilly.com/product/0636920032519.do
> > |  Technical Principal at ThoughtWorks
> > |  Twitter: @ramalhoorg
>
>
>
> --
> Luciano Ramalho
> |  Author of Fluent Python (O'Reilly, 2015)
> | http://shop.oreilly.com/product/0636920032519.do
> |  Technical Principal at ThoughtWorks
> |  Twitter: @ramalhoorg



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
| http://shop.oreilly.com/product/0636920032519.do
|  Technical Principal at ThoughtWorks
|  Twitter: @ramalhoorg
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/DAUQOKAJG7FZYY24JBG7ZIGA3TFZA4OY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: The repr of a sentinel

2021-05-23 Thread Christopher Barker
Thanks Tal for writing this up.

A couple comments:

1) “Add a single new sentinel value, e.g. MISSING or Sentinel” (under
rejected)

I was one of the proponent of that -- but not as an alternative to having a
stadardars way to create unique sentinels, but as an addition. That's kind
of orthogonal to the PEP, but it would still be nice to have it at the same
time.

Why? There was a fair bit of discussion as to why you would not want to
require everyone to use  MISSING (dataclasses, in particular), but I still
think better for the readability of everyone's code for the common case(s?)
to use the same singleton, if they can.

2) couldn't a factory function simply return a sentinel subclass? Maybe I'm
missing something, but I can't see why that would require stack frame
inspection.

But frankly Luciano's idea of a base class that can be subclassed seems the
most startightford to me.

-CHB
___
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/7VUT5PLMXBGXHJAWCPQSHX774QLTNSG4/
Code of Conduct: http://python.org/psf/codeofconduct/