kronenthaler / mod-pbxproj

A python module to manipulate XCode projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] - Project fails on save, _get_comments() on a Unicode string

thebarndog opened this issue · comments

I'm writing a Swift script that imports pbxproj via PythonKit. Everything works fine and dandy until I call project.save(). I get this error:

Fatal error: 'try!' expression unexpectedly raised an error: Python exception: 'unicode' object has no attribute '_get_comment'
Traceback:
  File "/Library/Python/2.7/site-packages/pbxproj/XcodeProject.py", line 35, in save
    f.write(self.__repr__() + "\n")
  File "/Library/Python/2.7/site-packages/pbxproj/XcodeProject.py", line 47, in __repr__
    return '// !$*UTF8*$!\n' + super(XcodeProject, self).__repr__()
  File "/Library/Python/2.7/site-packages/pbxproj/PBXGenericObject.py", line 75, in __repr__
    return self._print_object()
  File "/Library/Python/2.7/site-packages/pbxproj/PBXGenericObject.py", line 83, in _print_object
    indentation_increment)
  File "/Library/Python/2.7/site-packages/pbxproj/PBXGenericObject.py", line 107, in _format
    indentation_increment)
  File "/Library/Python/2.7/site-packages/pbxproj/PBXObjects.py", line 42, in _print_object
    result += indentation_depth + '\t{0} = {1};\n'.format(value.get_id().__repr__(), obj)
  File "/Library/Python/2.7/site-packages/pbxproj/PBXKey.py", line 8, in __repr__
    comment = self._get_comment()
  File "/Library/Python/2.7/site-packages/pbxproj/PBXKey.py", line 20, in _get_comment
    return self.get_parent()._resolve_comment(self)
  File "/Library/Python/2.7/site-packages/pbxproj/PBXGenericObject.py", line 154, in _resolve_comment
    return self[key]._get_comment()
  File "/Library/Python/2.7/site-packages/pbxproj/pbxsections/PBXBuildFile.py", line 42, in _get_comment
    comment = self.fileRef._get_comment()

The issue is this line:

comment = self.fileRef._get_comment() because fileRef is a unicode string.

@kronenthaler the issue is in

def _get_comment(self):
        comment = '(null)'
        if hasattr(self, 'fileRef'):
            comment = self.fileRef._get_comment()
        return '{0} in {1}'.format(comment, self._get_section())

I'm not sure why this works for everyone else; maybe they don't use Xcode betas as heavily as I do and it's possible something changed in the betas. The fix should be easy though:

if hasattr(self, 'fileRef') and not isinstanceof(self.fileRef, str)

Forgive my incorrect python, not my strong suit.

Thanks for your report. The most recent version is fully python 3 and hence only unicode strings are used. I have recently added a unit test for this and it seems to be working fine.

Just to be thorough, could you provide the version you are using and the sample name of file that is causing the failure?

Sure the pbxproj version is 2.9.0.

And what do you mean exactly, the sample name of the file? The file in my code that’s causing the breakage? For me it’s just a script so it’s a main.swift. The trace back points to PBXBuildFile.py as the failure point

@kronenthaler It looks like the issue was I was pointing to the incorrect version of python, like 2.7 vs the newest 3.x. I'll close the issue, thanks for taking a look either way!