Context().import_() raise gpgme.GpgmeError in Python3

Bug #1212214 reported by Tojaj
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
PyGPGME
New
Undecided
Unassigned

Bug Description

Hello,
when I try to import a public gpg key in Python3 via Context() method import_ it raises an error:
gpgme.GpgmeError: (7, 32816, 'Invalid argument')
In Python2 it works fine.

Reproducer:
--------------
import gpgme
context = gpgme.Context()
context.import_(open("id_rsa.pub"))

Expected result:
-------------------
No exception raised

Current result:
-----------------
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    context.import_(open("id_rsa.pub"))
gpgme.GpgmeError: (7, 32816, 'Invalid argument')

Note:
I got the same results when I use the StringIO in Python3:

import gpgme
import io
context = gpgme.Context()
stringio = io.StringIO(open("id_rsa.pub").read())
context.import_(stringio)

Am I doing something wrong?

Revision history for this message
Tojaj (xtojaj) wrote :

Use of open("id_rsa.pub", 'rb') fix the issue.

But it seems to be little confusing, since my id_rsa.pub is in plaintext and not in binary format.
Moreover this issue is only Python3 specific and Python2 works well even without 'rb' mode specified.

Revision history for this message
AndreiM (andrei-macavei89) wrote :

Hi,

It seems that Context.import_() doesn't accept unicode strings and , if I remember correctly, in Python 3 all strings are unicode and after you open the file you get a unicode string as argument for the io.StringIO().

If you want you can use this (it works in Python2):

from StringIO import StringIO

ctx = gpgme.Context()
strio = StringIO(open("id_rsa.pub").read())
ctx.import_(strio)

Revision history for this message
Nils Rokita (akasch) wrote :

from io import BytesIO

gpgme.Context()
strio = BytesIO(open("id_rsa.pub", 'rb').read())
ctx.import_(strio)

Works for me. if the key is in a variable one have to encode it:

ctx = gpgme.Context()
result = ctx.import_(BytesIO(keytext.encode('utf-8')))

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.