title: bad argument to internal function error in python
body: <p>I have a python script that uses ReportLab to create a report, the script has been tested and works as expected however on occasions I get the following error message </p>
  
  <pre><code>TraceBack info:
  
  File "C:\tools\LegionellaTool.py", line 605, in &lt;module&gt;
  
  createReport(selectedCoolingTowers, Report_outputs_folder, dbo_COLGIS, openPDF)
  
  Error Info:
  
  ..\Objects\moduleobject.c:50: bad argument to internal function
  </code></pre>
  
  <p>Line 605 is <code>createReport(selectedCoolingTowers, Report_outputs_folder, dbo_COLGIS, openPDF)</code></p>
  
  <p>createReport is the function that ...creates the report. 
  I have debugged through the code and I havent got any errors, what might make the error to appear, the error only happens on occasions whereas on other occasions the report is created fine.</p>
  
  <p><strong>EDIT=====</strong> createReport code</p>
  
  <pre><code>def createReport(myReportTable, myReportFolder, M3Table, OpenPDF):
      global Author
      global TimeStamp
  
  ##    myReportTable = gp.GetParameterAsText(0)
  ##    myReportFolder = gp.GetParameterAsText(1)
  ##    M3Table = gp.GetParameterAsText(2)
  ##    OpenPDF = gp.GetParameterAsText(3)
  
      Author = getpass.getuser() #gets OS user name
      TimeStamp = str(datetime.datetime.now().hour) + ':' + str(datetime.datetime.now().minute) + ' on the ' + str(datetime.date.today().day) + '/' + str(datetime.date.today().month) + '/' + str(datetime.date.today().year) 
  
      #add time stamp to file name
      myFile = myReportFolder + os.sep + 'CoolingTowersForInspection_' + str(datetime.datetime.now().hour) + '_' + str(datetime.datetime.now().minute) + '_' + str(datetime.date.today().day) + '_' + str(datetime.date.today().month) + '_' + str(datetime.date.today().year) + '.pdf'
      c = reportlab.pdfgen.canvas.Canvas(myFile)
  
      #creates sectors array
      sectors = []
      sectors.append('NW')
      sectors.append('NE')
      sectors.append('SW')
      sectors.append('SE')
  
      for sector in sectors:
          #sector header
          #framePage(c, 'Cooling Towers for Inspection - ' + sector + ' sector')
          title = 'Cooling Towers for Inspection - ' + sector + ' sector'
          c.setFont('Helvetica',20) #title font
          c.drawString(reportlab.lib.units.inch, 10.5 * reportlab.lib.units.inch, title) #creates title
  
          c.setFont('Helvetica',10) #header and footer font
  
          #creates header
          c.drawCentredString(4.135 * reportlab.lib.units.inch, 0.75 * reportlab.lib.units.inch,
                                   'Report generated by ' + Author + ' at ' + TimeStamp + ' - Page %d' % c.getPageNumber())
  
          #creates footer
          c.drawCentredString(4.135 * reportlab.lib.units.inch, 11.00 * reportlab.lib.units.inch,
                                  'Environmental Services')
  
          #draw a border
          c.setStrokeColorRGB(1,0,0)
          c.setLineWidth(5)
          c.line(0.8 * reportlab.lib.units.inch, reportlab.lib.units.inch, 0.8 * reportlab.lib.units.inch, 10.75 * reportlab.lib.units.inch)
  
          #reset carefully afterwards
          c.setLineWidth(1)
          c.setStrokeColorRGB(0,0,0)
  
          c.setFont('Helvetica', 10)
  
          #gets towers in that sector
          myTowers = arcpy.SearchCursor(myReportTable,"\"SECTOR\" = '" + sector + "'","","")
          selTower = myTowers.next()
  
          y = 730
  
          if selTower is not None:
  
              while selTower:
                  #insert page break when close to the end of the page
                  if y &lt; 110:
                      c.showPage()
                      y = 730
                      framePage(c, 'Cooling Towers for Inspection - ' + sector + ' sector')
  
                  if selTower.TOWER_NAME &lt;&gt; None:
                      c.drawString(100, y, string.strip(selTower.TOWER_NAME))
                  else:
                      c.drawString(100, y, "na")
                  y = y - 12
  
                  if selTower.TOWER_ADDRESS &lt;&gt; None:
                      c.drawString(100, y, string.strip(selTower.TOWER_ADDRESS))
                  else:
                      c.drawString(100, y, "na")
                  y = y - 12
  
                  c.drawString(100, y, "Number of towers: " + str(int(selTower.NUMBER_OF_TOWERS)) + " ;   M3 Code: " + selTower.UKEY + " ;   Distance band: " + str(int(selTower.distance)) + " meters")
                  y = y - 12
  
                  inspectString = ""
                  if selTower.TOWER_RATING &lt;&gt; None:
                      inspectString = "Inspection rating: " + selTower.TOWER_RATING
                  else:
                      inspectString = "Inspection rating: na;"
  
                  if selTower.TOWER_LAST_INSPECTION &lt;&gt; None:
                      t = selTower.TOWER_LAST_INSPECTION
                      strLastInspection = t.strftime("%A, %d %b %Y")
  
                      inspectString = inspectString + "   Last inspection: " + strLastInspection
                      #inspectString = inspectString + "   Last inspection: " + selTower.TOWER_LAST_INSPECTION
                  else:
                      inspectString = inspectString + "   Last inspection: na"
  
                  c.drawString(100, y, inspectString)
                  y = y - 12
  
                  c.drawString(100, y, "Contacts:")
                  y = y - 12
  
                  myTowerUKEY = selTower.UKEY
  
                  #gets contacts for that cooling tower            
                  myTowerContacts = arcpy.SearchCursor(M3Table,"\"UKEY\" = '" + myTowerUKEY + "'","","")
  
                  selContact = myTowerContacts.next()
  
                  while selContact:
                      if y &lt; 110:
                          c.showPage()
                          y = 730
                          framePage(c, 'Cooling Towers for Inspection - ' + sector + ' sector')
  
                      contact = ""
                      if selContact.TITLE &lt;&gt; None:
                          if string.strip(selContact.TITLE) &lt;&gt; "":
                              contact = string.strip(selContact.TITLE) + " "
                      if selContact.FIRSTNAME &lt;&gt; None:
                          contact = contact + selContact.FIRSTNAME + " "
                      if selContact.FAMILYNAME &lt;&gt; None:
                          contact = contact + selContact.FAMILYNAME + " "
                      if selContact.JOBTITLE &lt;&gt; None:
                          contact = contact + "(" + selContact.JOBTITLE + ") "
                      if selContact.TELW &lt;&gt; None:
                          contact = contact + selContact.TELW + "(work) "
                      if selContact.MOBILE &lt;&gt; None:
                          if string.strip(selContact.MOBILE) &lt;&gt; "":
                              contact = contact + string.strip(selContact.MOBILE) + "(mobile) "
                      if selContact.TELH &lt;&gt; None:
                          if string.strip(selContact.TELH) &lt;&gt; "":
                              contact = contact + string.strip(selContact.TELH) + "(home)"
  
                      contact = string.strip(contact)
  
                      c.drawString(100, y, contact)
                      y = y - 12
                      selContact = myTowerContacts.next()                
  
                  y = y - 12
                  del myTowerContacts
                  selTower = myTowers.next()
          else:
              c.drawString(100, y, "no cooling towers for inspection in this sector")
  
          c.showPage() #insert page break after each sector
  
      del myTowers
  
      c.save()
  
      if OpenPDF == "true":
          os.startfile(myFile)
  
  #function that creates each page
  def framePage(canvas, title):
      canvas.setFont('Helvetica',20) #title font
      canvas.drawString(reportlab.lib.units.inch, 10.5 * reportlab.lib.units.inch, title) #creates title
  
      canvas.setFont('Helvetica',10) #header and footer font
  
      #creates header
      canvas.drawCentredString(4.135 * reportlab.lib.units.inch, 0.75 * reportlab.lib.units.inch,
                               'Report generated by ' + Author + ' at ' + TimeStamp + ' - Page %d' % canvas.getPageNumber())
  
      #creates footer
      canvas.drawCentredString(4.135 * reportlab.lib.units.inch, 11.00 * reportlab.lib.units.inch,
                              'Environmental Services')
  
      #draw a border
      canvas.setStrokeColorRGB(1,0,0)
      canvas.setLineWidth(5)
      canvas.line(0.8 * reportlab.lib.units.inch, reportlab.lib.units.inch, 0.8 * reportlab.lib.units.inch, 10.75 * reportlab.lib.units.inch)
  
      #reset carefully afterwards
      canvas.setLineWidth(1)
      canvas.setStrokeColorRGB(0,0,0)
  </code></pre>
  
  <p>EDIT Number 2 <strong>*</strong></p>
  
  <p>Full traceback is here</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\tools\LegionellaTool.py", line 613, in &lt;module&gt;
      createReport(selectedCoolingTowers, Report_outputs_folder, dbo_COLGIS, openPDF)
  
    File "C:\tools\LegionellaTool.py", line 120, in createReport
  
      c.drawString(reportlab.lib.units.inch, 10.5 * reportlab.lib.units.inch, title) #creates title
  
    File "c:\python26\arcgis10.0\lib\site-packages\reportlab\pdfgen\canvas.py", line 1481, in drawString
  
      t.textLine(text)
  
    File "c:\python26\arcgis10.0\lib\site-packages\reportlab\pdfgen\textobject.py", line 426, in textLine
  
      self._code.append('%s T*' % self._formatText(text))
    File "c:\python26\arcgis10.0\lib\site-packages\reportlab\pdfgen\textobject.py", line 393, in _formatText
  
      for f, t in pdfmetrics.unicode2T1(text,[font]+font.substitutionFonts):
  SystemError: ..\Objects\moduleobject.c:50: bad argument to internal function
  
  &lt;type 'exceptions.SystemExit'&gt;: 1
  </code></pre>
  
  <p>Thanks </p>