RPi.GPIO-0.7.0 - a few questions about how it adds the PWM class

No there is no call to PyType_GenericNew (or tp_new) here - this is just setting a variable (PWMType.tp_new) to the value PyType_GenericNew which happens to be a pointer to a function that will (if called) create a new instance. The important point here is that it is *NOT* being called, simply referred to and assigned to a variable.

Er no, the tp_new field is a function pointer which when called with appropriate parameters will instantiate a new object and return it, but here it is not being called. This code is to arrange that when it is called that functionality (required by the definition of tp_new) is provided by PyType_GenericNew. Without it the first attempt to instantiate a PWMType would result in a SEGV induced core dump.

In both C and C++ the syntax for 'call this_function' is this_function() possibly with arguments in the brackets but without the brackets there is no function being called. The code you are looking at is an assignment pure and simple with no more side effects than pi = 3;.

--
Steve O'Hara-Smith                          |   Directable Mirror Arrays 
C:\>WIN                                     | A better way to focus the sun 
 Click to see the full signature
Reply to
Ahem A Rivet's Shot
Loading thread data ...

Richard,

Yep, there's the repeat again :-(((

I'm sorry, but I do not understand you there. That also means that explanation below it fully meaningless to me (doesn't have any soldid ground).

Regards, Rudy Wieser

Reply to
R.Wieser

Steve,

Ah, THERE you have the pivoting point !

Yep, /now/ it makes sense. Thank you.

For the record, I was assuming that "PyType_GenericNew" was a class. Hence my confusion.

Regards, Rudy Wieser

Reply to
R.Wieser

On Mon, 16 Dec 2019 12:12:16 +0100, "R.Wieser" declaimed the following:

It does NOT create an instance -- it assigns the /function/ PyType_GenericNew to the .tp_new "slot" . LATER, somewhere, .tp_new will be called, which results in calling this function to create the instance.

What that line is doing is initializing the C-language representation of a TYPE with a function that handles creating instances. The TYPE definition is in lines 150-189. I'll admit I'm perplexed that they didn't just make the assignment in line 188.

PyType_Ready() appears to handle the processing of superclasses for a class -- possibly merging in data fields that were not initialized in the class itself.

formatting link
""" PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject

*kwds) Return value: New reference.

Generic handler for the tp_new slot of a type object. Create a new instance using the type?s tp_alloc slot. """ Since the PWM type doesn't override tp_alloc, it inherits the base PyType_GenericAlloc

.tp_new appears to be called by type_call() line 973 (

formatting link
lines

954-1004)

Given that name "type_call", I'm presuming it gets called when a Python class is called... eg myPWM = RPi.GPIO.PWM(stuff)

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
	wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
Reply to
Dennis Lee Bieber

On Mon, 16 Dec 2019 13:16:17 +0100, "R.Wieser" declaimed the following:

NONE...

You have four variables all having the address of a function that IF CALLED will create an instance.

instance = *var1(args)

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
	wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
Reply to
Dennis Lee Bieber

Dennis,

It /assigns/ a pointer to the function, not calls it ? Oh shucks, its all about the name /not/ being followed by curly brackets, isn't it ?

Yep, that is what my problem started with - I was thinking of doing exactly that and got a green light for it when mentioning it here, but later on doubted if it would do the same as the origional and posted that doubt.

I came to the same conclusion, but based on the structures purpose and the fields name, "tp_new"

Yep, I realized that after Ahems "its a pointer" response.

If only I had thought about that example way earlier. :-)

Regards, Rudy Wieser

Reply to
R.Wieser

The line isn?t going to change meaning just because you keep stating strange theories about what it does and refusing to engage with attempts to correct you.

Well, no, it doesn?t mean that.

--
https://www.greenend.org.uk/rjk/
Reply to
Richard Kettlewell

Richard,

I thought that I've been engaging all this time, including posting examples, trying to get my point across ...

... which you seem to refuse to even to try to understand/figure out what my problem was, but just kept repeating the same line over-and-over. Thats not helpfull at all.

And funny that, the person who refuses to engage me demands I should be engaging him. Hypocritical much ?

I'm going to use your own approach here:

That also means that explanation below it fully meaningless to me (doesn't have any soldid ground).

There, I have said it twice. It should be clear to you now. Or do you need the same line a third time ? /s

Regards, Rudy Wieser

Reply to
R.Wieser

Be careful.

In the few years that I worked in the same company as Richard, I never knew him to be *wrong* openly.

In short, he didn't open his mouth until he was sure of his position.

--
     ?I know that most men, including those at ease with problems of the  
greatest complexity, can seldom accept even the simplest and most  
 Click to see the full signature
Reply to
The Natural Philosopher

Having also worked with Richard, I'll second that.

---druck

Reply to
druck

TNP,

And it turns out he wasn't. But thats not the problem.

For some reason he just assumed that his description of what actually happens should make sense to me, and when I indicated that it didn't he just posted the same again (instead of trying something different). Which is all sorts of things, but definitily /not/ helpfull.

Besides that, I think I was rather clear about what I thought would be happening (starting with "As far as I can (now) tell, the "PWMType.tp_new = PyType_GenericNew;" line creates a single instance" at 19/12/03), but somehow that was not picked up on.

Just a simple "no instance is created" reply by Ahem brought me, after more than a handfull of messages on the subject, back on the right track.

tl;dr: if richard may demand from me that I "just understand" his explanation, should I than not be allowed to demand he "just understands" my problem/confusion ? Or is there some unwritten law somewhere which forbids that ?

Regards, Rudy Wieser

Reply to
R.Wieser

I think Rudy was complaining Richard was unhelpful as opposed to wrong.

I have a little sympathy for that slant. The thread came across as Richard, the professional coder, unwilling to adapt his answer to suit the needs of an enthusiastic novice.

This is a Raspberry Pi group.

Reply to
Pancho

Less than ideal.

Also less than ideal.

No, but I tend to find that working to bridge the communication gap is more useful than bemoaning it.

--
Steve O'Hara-Smith                          |   Directable Mirror Arrays 
C:\>WIN                                     | A better way to focus the sun 
 Click to see the full signature
Reply to
Ahem A Rivet's Shot

Pancho,

Bingo !

Yep exactly that, and not at all.

No matter how correct an answer might be, if the person you're talking to doesn't understand it its worth exactly nothing to him.

So you don't change the answer itself, but you actually want to help you /do/ try to rephrase or even re-explain it so it gets across. At least, that is what I always did.

But for the record: richard clasified my attempts to explain what I thought was happening as "strange theories about what it does". Should /I/ now adapt my answer to suit the needs of him ? But not the other way around ? Really ?

And a straight-forward question to you: Should I simply /lie/ about me understanding what richard said (and drop the whole thing I was busy with as a result of not understanding this part) ? 'Cause thats the only option you seem to be giving me ... :-(

Regards, Rudy Wieser

Reply to
R.Wieser

Ahem,

Agreed.

Also agreed.

I tried several times only to, now, hear that I was just "stating strange theories" (attempts of mine to explain what I had problems with understanding). I tried to "bridge the communication gap" (the before mentioned attempts to explain), but I didn't feel anything of the same coming back. And I even mentioned that. Zero response (other than a repeat of something I still did not understand). :-(

Regards, Rudy Wieser

Reply to
R.Wieser

No, you should adapt your question. When asking "when does the dingbat generate a doodlefuck?"...

...Upon the answer 'it doesn't' should have led you to ask 'well what IS it doing then'?

Nope. Just try and understand it.

--
Labour - a bunch of rich people convincing poor people to vote for rich  
people by telling poor people that "other" rich people are the reason  
 Click to see the full signature
Reply to
The Natural Philosopher

TNP,

I hope you do realize that works, or /should/ work, both ways ?

Tried that. Didn't work. Obviously. Mentioned as much. Several times.

Regards, Rudy Wieser

Reply to
R.Wieser

I think you have misunderstood my comment, I was agreeing with you. I was commenting that Richard could have been more understanding. My English wasn't as plain as it should have been for a non native speaker.

I wasn't actually offering you any advice as to how you should behave. The Raspberry Pi is intended an educational tool and hence I think people asking questions on the group should not be expected to communicate at the same high level as would be expected in other software discussions, elsewhere.

For what it is worth I suspect some contributors to the group have many decades professional experience. In a professional context more would be expected of you. For instance I would have expected you to look up what PyType_GenericNew was and to know it was a function and hence that the field tp_new was function pointer. However I don't think that is a reasonable expectation in a group dedicated to an introductory learning device, which is what the RPi is designed to be, even if that isn't what a lot of us do with them.

Reply to
Pancho

Why?

What makes YOU so entitled?

Maybe you are too stupid to program a computer?

--
The theory of Communism may be summed up in one sentence: Abolish all  
private property. 
 Click to see the full signature
Reply to
The Natural Philosopher

On Tue, 17 Dec 2019 17:48:34 +0000, Pancho declaimed the following:

OTOH -- someone attempting to modify a /provided/ library that relies upon the Python embedded/extension API has gone far beyond "introductory learning" situation. Many people have used Python for decades and never had need to study the C-layer API. That layer almost demands one to have a good understanding of how Python works from the inside, and also crosses from Python to C.

I'd like to point out "C". NOT "C++". There are no "classes" native to the C layer; there are just structures being used to represent Python-level objects (classes, aka "types").

As an aside -- I'm pondering if the reason the .tp_new field of the PWM structure wasn't pre-populated with PyType_GenericNew may have been to support future subclassing -- with a variant that might need additional set-up (current PWM is a software output, possibly a future R-Pi may incorporate hardware PWM on some pins, and creating a PWM object would differ based upon which pin was specified, and hence need a different "new" operation).

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
	wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
Reply to
Dennis Lee Bieber

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.