It is currently Thu Apr 25, 2024 5:29 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 20 posts ] 
Author Message
 Post subject: The GIMP, Python, and the Truth (Resolved)
PostPosted: Tue Feb 02, 2016 10:03 pm  (#1) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
For most of the last 24 hours I have been struggling to find two bugs, which occurred in each of 3 scripts. I finally tumbled to something I found quite unusual. The GIMPs version of Python changes it's mind about what is true or false, depending on how a plugin is called!

If, for example, You have a plugin which takes a PF_BOOL input, and You activate by clicking on it's menu button, then the parameters are passed to it as actual boolean values. But if it is called from within another script, the parameters arrive as integers (1 and 0).

The same is true for PF_TOGGLE. PF_INT, and it's kin, can only pass numeric data, but if You use an int as a parameter for a call to a plugin requiring a bool, it is silently accepted.

Now most languages use integers for booleans in some way or another, but the problem here is the values don't always work! It took me many hours to realize that my code wasn't flawed, my parameters were not being interperated in a consistent manner.

My communication skills suck unless You want a boring three hour paper on this, so here is a sample instead. The attached plugin contains 7 very simple scripts. 1 and 2 use booleans only, A and B use values only, and the final three are proof of concepts for a work around I found that solves the problem.
The scripts install to a new menu on ypour main menu bar, called Demo. For cases 1,2,A, and B the first of the two options works as expected, but the second fails to work right. For the final three, the working one is the bottom option, the other two work because of a "hack" in the code.
Dang! Just remembered, the buttons for the color switch are all reversed! (Expected after almost 24 hours at this though I guess.)

So to all of the True Gimp Gods, and Python Deities around here I ask this. I assume something this major is a known issue. But I cannot seem to find a bug report for it anywhere. Perhaps one of You can find one? And if not please let me know and I'll fire one off.


Attachments:
jaz_idiosyncracy_demo.zip [1.14 KiB]
Downloaded 128 times

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image


Last edited by jazzon on Wed Feb 03, 2016 11:48 pm, edited 1 time in total.
Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 2:51 am  (#2) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Jazzon,
you wrote: " For cases 1,2,A, and B the first of the two options works as expected, but the second fails to work right"
In effects, I tried your script #1 and something failed.
Then, because I'm simple-minded, I rewrote your #1 in my own simple python style.
Here it is:
#!/usr/bin/env python
from gimpfu import *

def idiosync(inImage, inDrawable, inSwitch):

    pdb.gimp_context_set_foreground((0,0,0))
    pdb.gimp_context_set_background((255, 255, 255))

    if inSwitch == True:
        pdb.gimp_drawable_fill(inDrawable, BACKGROUND_FILL)
    else:
        pdb.gimp_drawable_fill(inDrawable, FOREGROUND_FILL)
     
    return
   
def idiosync_call(inImage, inDrawable, inSwitch):
    idiosync(inImage, inDrawable, inSwitch)
    return

register(
    "idiosync_call",
    "Alter an images background color based on a switch.",
    "Alter an images background color based on a switch.",
    "Jazzon",
    "C. Jason B.",
    "January 2016",
    "idiosync_call",
    "",
    [
      (PF_IMAGE, "image", "Input Image", None),
      (PF_DRAWABLE, "drawable", "Input Drawable", None),
      (PF_TOGGLE, "switch", "No is black\nYes is White", False),
    ],
    [],
    idiosync_call,
    menu="<Image>/DiegoTest/",
    )

main()



it seems to work both with Yes and No.
Did I miss something from your comments?

_________________
"Where am I ?"


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 3:42 am  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
also:
created a version 2 which passes 1 or 0 instead of True and False.
It works the same.
#!/usr/bin/env python
from gimpfu import *

def idiosync(inImage, inDrawable, inSwitch):

    pdb.gimp_context_set_foreground((0,0,0))
    pdb.gimp_context_set_background((255, 255, 255))

    if inSwitch == True:
        pdb.gimp_drawable_fill(inDrawable, BACKGROUND_FILL)
    else:
        pdb.gimp_drawable_fill(inDrawable, FOREGROUND_FILL)
     
    return
   
def idiosync_call2(inImage, inDrawable, inSwitch):
    if inSwitch == True:
        idiosync(inImage, inDrawable, 1)
    else:
        idiosync(inImage, inDrawable, 0)
    return

register(
    "idiosync_call2",
    "Alter an images background color based on a switch.",
    "Alter an images background color based on a switch.",
    "Jazzon",
    "C. Jason B.",
    "January 2016",
    "idiosync_call2",
    "",
    [
      (PF_IMAGE, "image", "Input Image", None),
      (PF_DRAWABLE, "drawable", "Input Drawable", None),
      (PF_TOGGLE, "switch", "No is black\nYes is White", False),
    ],
    [],
    idiosync_call2,
    menu="<Image>/DiegoTest/",
    )

main()

_________________
"Where am I ?"


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 4:53 am  (#4) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
jazzon wrote:
So to all of the True Gimp Gods, and Python Deities around here I ask this. I assume something this major is a known issue. But I cannot seem to find a bug report for it anywhere. Perhaps one of You can find one? And if not please let me know and I'll fire one off.

It is not a GIMP bug. It is a "feature" of the Python programming language.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 6:56 am  (#5) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
saulgoode wrote:
It is not a GIMP bug. It is a "feature" of the Python programming language.

That part I understand. In most Python code I have written True or 1 and False, 0, or -1 work as expected. But unless I am way off base here, all 7 of the scripts I originally attached should get the same results. The calls are identical, but the results are not. The only difference is one is called interactively, and one non-interactivly. (I only coded one example variant for each, not all of the possibilities.)

So why then does this fail?
#!/usr/bin/env python
from gimpfu import *


def truth_test(truth):
   
    if truth is True:
        msg = "Received true"
    else:
        msg = "Received False"
       
    pdb.gimp_message(msg)
    return
   
def truth_test2(truth):
       
    pdb.python_fu_jaz_truth_test_interactive(truth)
    return
   
register(
    "jaz_truth_test_interactive",
    "Simple truth test",
    "Simple truth test called interactively.",
    "Jazzon",
    "Jazzon",
    "January 2016",
    "Succeeds",
    "",
    [
     
      (PF_TOGGLE, "truth", "True or False?", True),
    ],
    [],
    truth_test,
    menu="<Image>/Demo")


register(
    "jaz_truth_test_noninteractive",
    "Simple truth test",
    "Simple truth test called noninteractively.",
    "Jazzon",
    "Jazzon",
    "January 2016",
    "Fails",
    "",
    [
     
      (PF_TOGGLE, "truth", "True or False?", True),
    ],
    [],
    truth_test2,
    menu="<Image>/Demo")

main()
That is what started me digging into this.

dinasset wrote:
<snip>
In effects, I tried your script #1 and something failed.
Then, because I'm simple-minded, I rewrote your #1 in my own simple python style.
Here it is:<snip>
it seems to work both with Yes and No.
Did I miss something from your comments?

dinasset Simple Minded? HA! I've seen Your work! I'll never believe that.

No You are hitting the nail on the head actually. The point I am trying to illustrate is the fact that unless I am seriously off in my understanding of something, all 7 of my scripts SHOULD WORK SYSTEM AGNOSTICALLY, but on my machine 1, A, and Work all work flawlessly. But on Yours 1 failed. Why? To my knowledge I have nothing installed which alters Python's or The GIMP's handling of it's parameters, or considering of Truth.

Python is from standard repositories for Linux Mint (granted that means a binary build and I do not know of modifications with out a lot of research on the issue), and ditto for The GIMP. No scripts or plugins that alter anything as far as I know. So why do they work on my end and not yours? That very question is the heart of what I was getting at ... There is no consistant handling of Truth and parameters AT RUN TIME.

Unless I am missing something, all 7 of these should succeed, but when I run it on my machine I get the following results:

Script 1: Changes to black or white;
Script 2: Changes from white to black, but will not under any circumstances change it to white.
Script A: Changes to black or white;
Script B: Again can change to black, but not to white.
Script Works: Changes to black or top white
Script int: Changes to black or to white
Script bool: Changes to black or to white.

But all 7 scripts are valid and should achieve the same successful results. Bu they do not. Something different between our machines altered Your results as compared to mine (or mine compared to Yours, same thing really).

Slightly off topic:
As for the difference in code (Yours to mine), I simply used PyGimps class interface to the drawable, while You are using PyGimp's pdb interface. They both do the same thing, just different means of accessing them (great thing about Python). Some time ago (well over a year since I found the article was I think for The GIMP 2.4 or maybe 2.6) I read that the class interface methods ran quicker. The same article also posted some stats on the run times. The differences exist, but if I recall, are not huge. Anyway, I try to use the class interface because of what I read. They are both the same thing.

I am not trying to criticise here, or in any of my other posts, Your code is great! Just style differences between You and I, and most people don't like my style anyway. ( I tend to try to super simplify things, and in the end result I often over complicate matters. If that makes any sense.)

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 8:21 am  (#6) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
I'm still having problems in reading your source (because of the interrelated definitions/registrations), hence my simple question:
- is your problem that when "called" the function doesn't work properly neither with True/False nor with 1/0? (while used directly work)
If so: did you try my simpler derived versions? They both "call" the function (even if the function is defined internally as an ancillary function, and not as a registerd one), one is using True/False the other one 1/0. Do they work properly in your system?

_________________
"Where am I ?"


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 10:05 am  (#7) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
dinasset: Look at the sample code I posted as a response to saulgoode. All it does it create a pdb method. The second simply calls the first one, passing the values in as they were received. The second one fails, and it shouldn't.

Sorry about the crappy code. I had been up for almost 40 hours when I wrote it, and it was after hours of testing that concept various ways. The code I posted to saulgoode should have been my first example.

The function accepts one paramater True or false, and uses gimp_message to display the result. That function works. A second function takes one parameter, True or False. It then calls the first function using the same unmodified parameter. It fails. It interprets True as False. ( at least on my machine ... I need others to test it so I can find out if I have a problem, gimp does, or python does, or Linux Mint maintainers do ...)

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 11:34 am  (#8) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Since (like Dinasset) I am unable to replicate your successes, here is the short version of the solution:
if bool(inSwitch) == True:
      inDrawable.fill(BACKGROUND_FILL)
    else:
      inDrawable.fill(FOREGROUND_FILL)

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 12:05 pm  (#9) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
Wow, I must have really been unclear. The problem isn't how to code this. The problem is it's broken.

Take this example:
A plugin takes only one parameter, a boolean value.
It uses pdb.gimp_message to display "recienved XXX" (Where xxx is a string true or false)
call the plugin from the menu.
supply true
plugin prints out "Received True"
supply False
plugin prints out "Received False"

Second plugin is written
It takes one parameter, a boolean true or false
this plugin calls the first one and hands over the unmodified parameter
first plugin prints result

run second plugin from it's menu
Supply False
Plugin prints "Recieved False"
Supply True
Plugin prints out "Received False" Even when both plugins are using the python keywords "True" and "False"

This reproducible at a 100% frequency on my machine, using the code I posted as a reply to saulgoode. The parameter passed by plugin 2, to plug in 1 is never even looked at by plugin 2. How was it changed? The code (uses the python keywords "True" and "False", nothing else.

A plugin ()exactly as I just described) is attached. The code minus registration bits, is this:
# First plugin
def truth_test(truth):   
    if truth is True:
        msg = "Received true"
    else:
        msg = "Received false"       
    pdb.gimp_message(msg)
    return

# Second plugin
def truth_test2(truth):
    # This calls plugin one       
    pdb.python_fu_jaz_truth_test_interactive(truth)
    return


Attachments:
jaz_truth_test.zip [480 Bytes]
Downloaded 108 times

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image
Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 1:04 pm  (#10) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Jazzon,
to check whether the problem has to do with your system, please unzip the attached folder and set it visible to your gimp plug-in (preferences).
These 2 filters have been written following your indication:
- idiosync is the "doer", id est: it prints and executes the received order, both if called directly from the menu as well as if called by the second one below
- idiosync_call simply calls the above idiosync passing the parameters as received

They work on my PC.

Attachment:
python forJazz.7z [744 Bytes]
Downloaded 121 times

_________________
"Where am I ?"


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 3:31 pm  (#11) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
jazzon wrote:
Wow, I must have really been unclear. The problem isn't how to code this. The problem is it's broken.

Take this example:
A plugin takes only one parameter, a boolean value.
It uses pdb.gimp_message to display "recienved XXX" (Where xxx is a string true or false)
call the plugin from the menu.
supply true
plugin prints out "Received True"

The plugin prints out "Received True" because that is what your print statement tells it to print. I submit that the value that is actually received is an integer "1".

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 5:53 pm  (#12) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
dinasset
You hit the nail on the head, but I'm not sure if You caught it.

Aside from syntactic sugar, the only difference between Your code and the Code I posted is the equality test.

Python.org documentation of bool() (empahasis mine) wrote:
Return a Boolean value, i.e. one of True or False. x is converted using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed further. Its only instances are False and True.

Python documentation section 5.9 wrote:
The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

In Python "True" and "False" are identities. The identity comparison operators are "is" and "is not", not "==".

This is a guarantee of the python programming language, and all implementations of Python are (as far as I can recall) required to utilize it. This is also reiterated in the Python PEP documents.

The test which I uploaded in post 9 is identical to Yours in 10 except for the "is" vs "==".

This is where the problem comes in. Coders coming into gimp who are fluent in Python know to use "is" in their boolean comparisons, and the ending result is code will fail. In fact, since the coding skill level of each author of a plugin cannot be known by the end user of the plugin, many will assume the plugins are just broke, or don't work as advertised, or do I as I did last night and spend many hours trying to figure out why my calling routine was failing. My syntax was correct, the result was not. Now that I know The GIMPS internal Python is not up to the standards of Python.org, I can code around it, but has this bug been reported? Is it known?

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 6:28 pm  (#13) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
saulgoode
You posted as I was writing that last one. The code I uploaded printed out "Recieved True" on Your machine? If that is the case something is wonky with My python or Yours, or my understanding of identity tests ...

I concur, it received an int type, but it was sent a bool type. Therein lies the issue. SOmehow the type is being changed between caller and receiver, thus standard truth tests (like: x is True) can fail.

The code I uploaded in post 9 fails 100% of every input of "True" to the second plugin on my machine.

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image


Last edited by jazzon on Wed Feb 03, 2016 7:49 pm, edited 1 time in total.

Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 6:42 pm  (#14) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
I modified the code slightly and ran gimp in command line as
gimp --verbose -c

the following is a copy paste of my terminal window:
Terminal window copy paste wrote:
loading menu '/usr/share/gimp/2.0/menus/image-menu.xml' for /image-menubar
Loading module '/usr/lib/gimp/2.0/modules/libdisplay-filter-lcms.so'
idiosync-Warning: Received True

idiosync-Warning: Received False

idiosync-Warning: Received True

Should Succeed-Warning: Received false

Should Succeed-Warning: Received false

Should Succeed-Warning: Received True

Sending: True
Should Succeed-Warning: Received false

Sending: False
Should Succeed-Warning: Received false

Sending: True
Should Succeed-Warning: Received false

Sending: True
Should Succeed-Warning: Received false

Sending: True
Should Succeed-Warning: Received false


first are three runs of dinassets code
second are three runs of my first plugin
then five from my second plugin

I used three in case the issue was actually the script being called by error with a "use last values" setting.
Only a few things can be happening here:
1. PyGimp isn't working right
2. gimpfu is altering something on the fly
3. My Python is fouled up
4. My GIMP is fouled up.
5. (In which case it is not a bug) Gimp is using a different instance of python for each script for some reason.

Further modifying the code (attached and posted below), Another run with the same command line parameters:
Command line output wrote:
loading menu '/usr/share/gimp/2.0/menus/image-menu.xml' for /image-menubar
Loading module '/usr/lib/gimp/2.0/modules/libdisplay-filter-lcms.so'
I added this blank to the paste for clarity

Plugin one Received: False Type(): <type 'bool'> repr(): False
Plugin one Received: True Type(): <type 'bool'> repr(): True
I added this blank for clarity

Plug in 2 Sending: False <type 'bool'> False
Plugin one Received: 0 Type(): <type 'int'> repr(): 0
Plug in 2 received 0 as a return from 1
type() and repr() for return: <type 'int'> 0

Plug in 2 Sending: True <type 'bool'> True
Plugin one Received: 1 Type(): <type 'int'> repr(): 1
Plug in 2 received 1 as a return from 1
type() and repr() for return: <type 'int'> 1


As You can see, the sent values are true boolean values, but the values received are not, when the plugin is called from another plugin.
This indicates that either gimpfu or pygimp has an issue. (Or GIMP is using separate instances of pygimp for each script)

Post of the source code minus the registration part:
def truth_test(truth):   
    print "Plugin one Received:" , truth, "Type(): ",type(truth), "repr(): ", repr(truth)
    return truth
   
def truth_test2(truth):
    print "Plug in 2 Sending: ", truth, type(truth), repr(truth)       
    truth = pdb.python_fu_jaz_truth_test_interactive(truth)
    print "Plug in 2 received ", truth, "as a return from 1"
    print "type() and repr() for return: ",type(truth), repr(truth),"\n"     
    return


Since I have included both sources in one file, I (not posted here) also tried calling the code directly as a function, it works as it shoult that way.

Once more modifying the code (final runs) I got this in the terminal for two tests of plugin one, and two tests of plugin two.(notice final entry) (I added some spacing for clarity):
Command line copy paste wrote:
loading menu '/usr/share/gimp/2.0/menus/image-menu.xml' for /image-menubar
Loading module '/usr/lib/gimp/2.0/modules/libdisplay-filter-lcms.so'

Plugin one Received: True Type(): <type 'bool'> repr(): True
test of 'truth is True': True
test of 'truth == True': True

Plugin one Received: False Type(): <type 'bool'> repr(): False
test of 'truth is True': False
test of 'truth == True': False

Plug in 2 Sending: False <type 'bool'> False
Plugin one Received: 0 Type(): <type 'int'> repr(): 0
test of 'truth is True': False
test of 'truth == True': False
Plug in 2 received 0 as a return from 1
type() and repr() for return: <type 'int'> 0

Plug in 2 Sending: True <type 'bool'> True
Plugin one Received: 1 Type(): <type 'int'> repr(): 1
test of 'truth is True': False
test of 'truth == True': True
Plug in 2 received 1 as a return from 1
type() and repr() for return: <type 'int'> 1


Attachments:
jaz_truth_test3.py.zip [601 Bytes]
Downloaded 66 times
jaz_truth_test2.py.zip [577 Bytes]
Downloaded 66 times

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image
Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 9:16 pm  (#15) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
jazzon wrote:
I concur, it received an int type, but it was sent a bool type. Therein lies the issue. SOmehow the type is being changed between caller and receiver, thus standard truth tests (like: x is True) can fail.

This is by design. There is no <type bool> in the PDB interface -- all Python True/False identities need to be supplied as values that the PDB understands (namely INT32). This mapping from True to "1" and False to "0" should be performed automatically by Pygimp.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: The GIMP, Python, and the Truth (or Gimp doesn't know True From Fa
PostPosted: Wed Feb 03, 2016 11:48 pm  (#16) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
saulgoode :tyspin That makes perfect sense. It's an issue between the interface to GIMP and Python. C Doesn't have a natural boolean type.

To All of You Thank You all for bearing with my idiosyncrasies and poor communication skills, and helping me to understand this. I know I can be difficult to understand, and sometimes to dense to get what others feel is an obvious point. Your persistence truly is appreciated.
:tyspin :tyspin :tyspin

-- Jazzon


Top
 Post subject: Re: The GIMP, Python, and the Truth (Resolved)
PostPosted: Thu Feb 04, 2016 12:50 pm  (#17) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
True/False are Python concepts. The PDB has to deal with every possible implementation of your callee: script-fu, perl-fu, C plugins... When you call a Python script through the PDB, your booleans become integers.

Despite all the Python code I have written for Gimp, this has never bothered me:) To dodge the problem I would use either:
inDrawable.fill([FOREGROUND_FILL,BACKGROUND_FILL][bool(inSwitch)])

(if inSwitch is anything but false or 0, bool(inSwitch) will be true, which as an array index is 1. Otherwise it is False, which is index 0).

Or the even more pythonic way is to use the ternary operator:

inDrawable.fill(BACKGROUND_FILL if inSwitch else FOREGROUND_FILL)

_________________
Image


Top
 Post subject: Re: The GIMP, Python, and the Truth (Resolved)
PostPosted: Thu Feb 04, 2016 1:33 pm  (#18) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
ofnuts
I know that now. I think the only reason I even stumbled on this is because the ide I use for python coding has the ability to auto adjust things in the code as You write it. I have it set up to "notice" == True and ==False, and auto replace them with is True and is False. When I was working on the wiremap scripts, I copy pasted pieces of dinasset's code which included these lines (I looked at His originals and He used the == form, but now they show the is form) and it corrected the code when I pasted it. Thus when I wrote the auto-collapsing variants, certain features got "broke". Which got reported to me as bugs. I simply have to shut that feature off, and use the less exact comparator of ==.

On a side note, check Your PMs. I've been trying to email You for days now, and keep getting the same error.

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image


Top
 Post subject: Re: The GIMP, Python, and the Truth (Resolved)
PostPosted: Thu Feb 04, 2016 6:08 pm  (#19) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
The right thing to do is to use neither "==" nor "is". Just use the boolean:
if someFlag:

and to check the opposite:
if not someFlag:

remember that in Python, many things are equivalent (but not equal) to True/False in tests: empty lists, empty strings, None...

_________________
Image


Top
 Post subject: Re: The GIMP, Python, and the Truth (Resolved)
PostPosted: Fri Feb 05, 2016 2:47 am  (#20) 
Offline
GimpChat Member
User avatar

Joined: Aug 13, 2015
Posts: 312
Location: Somewhere between lost and found.
ofnuts: I guess I would have to agree with the "no comparator" solution. Im just gonna have to unlearn some old habits now that I understand this issue.

_________________
The answer was 42. The question is long forgotten. The computer that solved it is now destroyed.
The MK-2 has been built. Should this be the next question?
(Solve if you can ... ;) )
Image


Top
Post new topic Reply to topic  [ 20 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Convert GIMP plugin from Python 2 to Python 3

4

No new posts Difference between "Python-Fu" and "Python" plugin

6

No new posts Exporting as PSD from Python?

2

No new posts Python 3 With GIMP 2

0

No new posts Attachment(s) How to add Python-FU to gimp

17



* Login  



Powered by phpBB3 © phpBB Group