|
26 | 26 | from cryptography.hazmat.primitives.asymmetric import rsa as _RSA |
27 | 27 |
|
28 | 28 | from . import exceptions as _EX |
29 | | -from .utils import ensure_bytestring, ensure_string |
| 29 | +from .utils import ensure_bytestring, ensure_string, nullsafe_getattr |
30 | 30 | from .utils import md5_fingerprint as _FP_MD5 |
31 | 31 | from .utils import sha256_fingerprint as _FP_SHA256 |
32 | 32 | from .utils import sha512_fingerprint as _FP_SHA512 |
@@ -129,12 +129,15 @@ def __init__( |
129 | 129 | _SERIALIZATION.Encoding.OpenSSH, |
130 | 130 | _SERIALIZATION.PublicFormat.OpenSSH, |
131 | 131 | ] |
| 132 | + |
| 133 | + # Ensure comment is not None |
| 134 | + self.comment = nullsafe_getattr(self, "comment", "") |
132 | 135 |
|
133 | 136 | @classmethod |
134 | 137 | def from_class( |
135 | 138 | cls, |
136 | 139 | key_class: PubkeyClasses, |
137 | | - comment: Union[str, bytes] = None, |
| 140 | + comment: Union[str, bytes] = "", |
138 | 141 | key_type: Union[str, bytes] = None, |
139 | 142 | ) -> "PublicKey": |
140 | 143 | """ |
@@ -266,7 +269,7 @@ def to_string(self, encoding: str = "utf-8") -> str: |
266 | 269 | return " ".join( |
267 | 270 | [ |
268 | 271 | ensure_string(self.serialize(), encoding), |
269 | | - ensure_string(getattr(self, "comment", ""), encoding), |
| 272 | + ensure_string(nullsafe_getattr(self, "comment", ""), encoding), |
270 | 273 | ] |
271 | 274 | ) |
272 | 275 |
|
|
0 commit comments