[pypy-dev] Using PyPy as a compiler

Hart's Antler bhartsho at yahoo.com
Tue Feb 8 02:57:38 CET 2005


I've got the pypy compiler working! see the function at the bottom.  But now i'm having some
problems when importing the module and testing some of its functions.

here's the commands i typed to make the module:
pypy/tool/pyrexc myModule.pyx
gcc -c -fPIC -I/usr/include/python2.3/ myModule.c
gcc -shared myModule.o -o myModule.so

when i import the module and try isFloatingPoint(None, '0.001') it returns the error below, looks
like it can not find 'contains'  where does contain come from, should i have linked to another
library when making the .so file?

>>> mod.isFloatingPoint(None, '0.001')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "b11a3ad330bf84c9dabf6b4e40ba4baa.pyx", line 806, in
    return isFloatingPoint__2dbc1614(self_3077, s_3078)
  File "b11a3ad330bf84c9dabf6b4e40ba4baa.pyx", line 833, in
    v3119 = contains('0123456789.', char_3145)
NameError: contains

def isFloatingPoint(self, s):
	r = True
	chars = '0123456789.'
	for char in s:
		if char not in chars:
			return False
	return r

def isFloatingPoint(self_3077, s_3078):
  return isFloatingPoint__2dbc1614(self_3077, s_3078)
cdef object isFloatingPoint__2dbc1614(object self_3077, object s_3078):
  # untyped variables: char_3109 char_3113 char_3115 char_3124 char_3127 char_3143 char_3145
char_3148 char_3150 char_3155 char_3157 char_3162 etype_3080 evalue_3081 s_3087 s_3108 s_3112
s_3123 s_3126 s_3139 s_3142 s_3147 s_3154 s_3161 s_3167 self_3086 self_3107 self_3111 self_3122
self_3125 self_3138 self_3141 self_3146 self_3153 self_3160 self_3166 v3079 v3082 v3088 v3110
v3114 v3119 v3120 v3140 v3144 v3149 v3151 v3152 v3156 v3158 v3159 v3163 v3164 v3165 v3168 v3169
    v3082 = iter(s_3078)
  except TypeError, last_exc_value:
    last_exception = last_exc_value.__class__
    self_3166, s_3167, v3168, v3169 = self_3077, s_3078, last_exception, last_exc_value
    etype_3080, evalue_3081 = TypeError, v3169
    raise etype_3080, evalue_3081
    self_3138, s_3139, v3140 = self_3077, s_3078, v3082
    self_3086, s_3087, v3088 = self_3138, s_3139, v3140
    self_3111, s_3112, v3114 = self_3086, s_3087, v3088
    cinline "Label5:"
      char_3115 = v3114.next()
    except StopIteration, last_exc_value:
      last_exception = last_exc_value.__class__
      self_3160, s_3161, char_3162, v3163, v3164, v3165 = self_3111, s_3112, char_3113, v3114,
last_exception, last_exc_value
      self_3122, s_3123, char_3124 = self_3160, s_3161, char_3162
      self_3125, s_3126, char_3127 = self_3122, s_3123, char_3124
      v3079 = 1
      cinline "Label9:"
      return v3079
      self_3141, s_3142, char_3143, v3144, char_3145 = self_3111, s_3112, char_3113, v3114,
      v3119 = contains('0123456789.', char_3145)
      v3120 = not not v3119
      if v3120 == 1:
        self_3153, s_3154, char_3155, v3156, char_3157, v3158, v3159 = self_3141, s_3142,
char_3143, v3144, char_3145, v3119, v3120
        self_3107, s_3108, char_3109, v3110 = self_3153, s_3154, char_3157, v3156
        self_3111, s_3112, char_3113, v3114 = self_3107, s_3108, char_3109, v3110
        cinline "goto Label5;"
      else: # v3120 == 0
        self_3146, s_3147, char_3148, v3149, char_3150, v3151, v3152 = self_3141, s_3142,
char_3143, v3144, char_3145, v3119, v3120
        v3079 = 0
        cinline "goto Label9;"

def pypyCompile(txt):
	import md5
	from pypy.translator.translator import Translator

	txt = txt.replace('print ', '#print ')
	hashed = md5.new( txt ).hexdigest()

	# before compiling, check if there is a cached .so file
	if os.path.exists('%s.so'%hashed):
		cmd = 'import %s as module' %hashed
		exec cmd
		return module

	mod = {}
	code = compile(txt, 'pypy translator', 'exec')
	exec code in mod

	pyx = ""
	for a in mod:
		func = mod[a]
		#if func and func != '__builtins__':
		if type(func) == types.FunctionType:
			print 'converting python to pyrex for function', func
			t = Translator(func)
			d = t.pyrex()
			if 0:	#for debugging
				name = 'tmp/%s.pyx' %a
				f = open(name, 'wb')
				os.system('pypy/tool/pyrexc %s' %name)
				if os.path.exists(name):
					os.system('gcc -c -fPIC -I/usr/include/python2.3/ %s.c' %name)
				else: raise 'stop'

			pyx += d + '\n'

	url = hashed + '.pyx'
	f = open(url, 'wb')
	print 'saved .pyx file', url
	os.system('pypy/tool/pyrexc %s' %url)
	if os.path.exists(url):
		print 'pyrex translated to c'
		os.system('gcc -c -fPIC -I/usr/include/python2.3/ %s.c' %hashed)
		print 'compiled c file to o'
		os.system('gcc -shared %s.o -o %s.so' %(hashed,hashed))
		print 'compiled o file to so file'
	else: raise 'stop'

	cmd = 'import %s as module' %hashed
	exec cmd
	return module

Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Pypy-dev mailing list