07-23-2014, 01:49 PM | #16 |
creator of calibre
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Now do the following little experiment,
u_genre = eval(u_genre) |
07-23-2014, 02:09 PM | #17 |
Deviser
Posts: 2,265
Karma: 2090983
Join Date: Aug 2013
Location: Texas
Device: none
|
Okay. Here it is, both in IDLE and then in ui.py:
This was the ui.py code: if isinstance(u_genre, bytes): print("u_genre is bytes", u_genre) else: if isinstance(u_genre, unicode): #e.g. u'N\xe3o-fic\xe7\xe3o' print("u_genre is utf8", u_genre) else: print("u_genre is something else...", u_genre) print("u_genre before repr(u_genre) ", u_genre) u_test = u_genre u = (repr(u_test)) print("u_genre clone after repr(clone) ", u) print("u_genre before eval(u_genre)") u_test = u_genre u_test = eval(u_test) #<====== eval print("after eval: ", u_test) #<====== failed. Line 191 ------------------ print output ------------------------------------------------ calibre 1.45 [64bit] isfrozen: True is64bit: True Windows-7-6.1.7601-SP1 Windows ('64bit', 'WindowsPE') ('Windows', '7', '6.1.7601') Python 2.7.5 Windows: ('7', '6.1.7601', 'SP1', 'Multiprocessor Free') Traceback (most recent call last): File "site-packages\calibre\gui2\proceed.py", line 123, in do_ask_question File "calibre_plugins.derive_genres.ui", line 191, in _proceed_with_updating_genres IOError: [Errno 0] Error I did the same thing in IDLE, and got a failure, but only after it printed it correctly: >>> u_genre = eval(u_genre) Traceback (most recent call last): File "<interactive input>", line 1, in <module> File "<string>", line 1 Não-ficção <<<<<<<<============================= CORRECT ^ SyntaxError: invalid syntax ------------------------------------------------------------------------------------ |
Advert | |
|
07-23-2014, 02:17 PM | #18 |
Deviser
Posts: 2,265
Karma: 2090983
Join Date: Aug 2013
Location: Texas
Device: none
|
eval(xxx) Fixed The Problem
Kovid,
I changed the code to use the eval statement you specified, but did NOT print it to avoid a runtime error. It worked! Thank you for solving this. Great job. |
07-23-2014, 02:17 PM | #19 |
creator of calibre
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
I give up, your output is completely self conflicting.
In the next to last post you have print repr(u_genre) --> u"u'Fiction:General'" If that is the case there is no way that eval(u_genre) can fail. Code:
>>> a = u'N\\xe3o-fic\\xe7\\xe3o' >>> print (a) N\xe3o-fic\xe7\xe3o >>> b = repr(a) >>> print (b) u'N\\xe3o-fic\\xe7\\xe3o' >>> print (repr(b)) "u'N\\\\xe3o-fic\\\\xe7\\\\xe3o'" >>> eval(b) == a True >>> |
07-23-2014, 02:19 PM | #20 |
creator of calibre
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
This is not a fix, it is just proof that u_genre is not what you think it is. It is for some reason already the result of calling repr() on the original string. You need to figure out why that is and solve it at the source.
Using eval() in production code is a very bad idea, it is an arbitrary code execution loophole. |
Advert | |
|
07-23-2014, 02:20 PM | #21 |
Deviser
Posts: 2,265
Karma: 2090983
Join Date: Aug 2013
Location: Texas
Device: none
|
Good Job.
Kovid, thanks again for figuring out that eval() solves the problem.
I did not use repr() on it prior to using eval(). I used repr() on a clone of u_genre. I used eval() directly on u_genre. Last edited by DaltonST; 07-23-2014 at 02:23 PM. |
Tags |
custcol['#value#'], set_user_metadata, unicode |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
PRS-T1 PRS-T1 & Asian Fonts/Unicode | komugi | Sony Reader | 20 | 10-05-2013 11:49 PM |
RegEx & Unicode | capnm | Library Management | 14 | 12-01-2011 08:23 PM |