# HG changeset patch # User Emmanuel Gil Peyrot # Date 1412090064 -7200 # Node ID e15672733c93736db5cc5a436099c440e98d492e # Parent 0768122da81717c96fd2e74bba0a427c5300941f Switch to Python 3.x instead of 2.7. diff --git a/pytouhou/formats/exe.py b/pytouhou/formats/exe.py --- a/pytouhou/formats/exe.py +++ b/pytouhou/formats/exe.py @@ -74,17 +74,17 @@ class SHT(object): format = Struct('<4f2I') data_section = [section for section in pe_file.sections - if section.Name.startswith('.data')][0] + if section.Name.startswith(b'.data')][0] text_section = [section for section in pe_file.sections - if section.Name.startswith('.text')][0] + if section.Name.startswith(b'.text')][0] data_va = pe_file.image_base + data_section.VirtualAddress data_size = data_section.SizeOfRawData text_va = pe_file.image_base + text_section.VirtualAddress text_size = text_section.SizeOfRawData # Search the whole data segment for 4 successive character definitions - for addr in xrange(data_va, data_va + data_size, 4): - for character_id in xrange(4): + for addr in range(data_va, data_va + data_size, 4): + for character_id in range(4): pe_file.seek_to_va(addr + character_id * 24) (speed1, speed2, speed3, speed4, ptr1, ptr2) = format.unpack(pe_file.file.read(format.size)) @@ -101,7 +101,7 @@ class SHT(object): # Now, make sure the shoot function wrappers pass valid addresses # Search for the “push” instruction - for i in xrange(20): + for i in range(20): # Find the “push” instruction pe_file.seek_to_va(ptr1 + i) instr1, shtptr1 = unpack(' 0: player.score += score if label is None: - label = self._game.new_label((self.x, self.y), str(score)) + label = self._game.new_label((self.x, self.y), str(score).encode()) if color != 'white': label.set_color(color) diff --git a/pytouhou/game/player.pyx b/pytouhou/game/player.pyx --- a/pytouhou/game/player.pyx +++ b/pytouhou/game/player.pyx @@ -83,7 +83,7 @@ cdef class Player(Element): self._game.new_effect((self.x, self.y), 17) self._game.modify_difficulty(-1600) self.play_sound('pldead00') - for i in xrange(16): + for i in range(16): self._game.new_particle((self.x, self.y), 11, 256) #TODO: find the real size and range. @@ -205,11 +205,11 @@ cdef class Player(Element): m = self.invulnerable_time % 8 if m == 7 or self.invulnerable_time == 0: - for i in xrange(3): + for i in range(3): self.sprite._color[i] = 255 self.sprite.changed = True elif m == 1: - for i in xrange(3): + for i in range(3): self.sprite._color[i] = 64 self.sprite.changed = True @@ -255,7 +255,7 @@ cdef class Player(Element): self.continues -= 1 self.continues_used += 1 - for i in xrange(5): + for i in range(5): self._game.drop_bonus(self.x, self.y, 4, player=self, end_pos=(self._game.prng.rand_double() * 288 + 48, self._game.prng.rand_double() * 192 - 64)) @@ -271,7 +271,7 @@ cdef class Player(Element): self._game.drop_bonus(self.x, self.y, 2, player=self, end_pos=(self._game.prng.rand_double() * 288 + 48, # 102h.exe@0x41f3dc self._game.prng.rand_double() * 192 - 64)) # @0x41f3 - for i in xrange(5): + for i in range(5): self._game.drop_bonus(self.x, self.y, 0, player=self, end_pos=(self._game.prng.rand_double() * 288 + 48, self._game.prng.rand_double() * 192 - 64)) diff --git a/pytouhou/game/sprite.pyx b/pytouhou/game/sprite.pyx --- a/pytouhou/game/sprite.pyx +++ b/pytouhou/game/sprite.pyx @@ -57,7 +57,7 @@ cdef class Sprite: # Cython treats unsigned char* variables as bytes, so we can’t use # slicing here. - for i in xrange(4): + for i in range(4): self._color[i] = 255 diff --git a/pytouhou/game/text.pxd b/pytouhou/game/text.pxd --- a/pytouhou/game/text.pxd +++ b/pytouhou/game/text.pxd @@ -35,7 +35,7 @@ cdef class Text(GlyphCollection): cdef Interpolator fade_interpolator cdef unsigned char alpha - cpdef set_text(self, bytes text) + cpdef set_text(self, text) #def timeout_update(self) #def move_timeout_update(self) #def fadeout_timeout_update(self) @@ -60,7 +60,7 @@ cdef class NativeText(Element): cdef public object update cdef unicode text - cdef bytes align #TODO: use a proper enum. + cdef str align #TODO: use a proper enum. cdef unsigned long frame, timeout, duration, start cdef double to[2] cdef double end[2] diff --git a/pytouhou/game/text.py b/pytouhou/game/text.py --- a/pytouhou/game/text.py +++ b/pytouhou/game/text.py @@ -98,7 +98,7 @@ class GlyphCollection(Widget): class Text(GlyphCollection): - def __init__(self, pos, ascii_anm, back_anm=None, text='', + def __init__(self, pos, ascii_anm, back_anm=None, text=b'', xspacing=14, shift=21, back_script=22, align='left'): GlyphCollection.__init__(self, pos, ascii_anm, back_anm, xspacing=xspacing, back_script=back_script) @@ -119,7 +119,10 @@ class Text(GlyphCollection): if text == self.text: return - self.set_sprites([ord(c) - self.shift for c in text]) + if isinstance(text, str): + text = text.encode() + + self.set_sprites([c - self.shift for c in text]) self.text = text self.changed = True diff --git a/pytouhou/interfaces/eosd.py b/pytouhou/interfaces/eosd.py --- a/pytouhou/interfaces/eosd.py +++ b/pytouhou/interfaces/eosd.py @@ -40,13 +40,13 @@ class EoSDInterface(object): self.level_start = [] self.labels = { - 'highscore': Text((500, 58), self.ascii_anm, front, text='0'), - 'score': Text((500, 82), self.ascii_anm, front, text='0'), + 'highscore': Text((500, 58), self.ascii_anm, front, text=b'0'), + 'score': Text((500, 82), self.ascii_anm, front, text=b'0'), 'player': Counter((500, 122), front, front, script=16, value=0), 'bombs': Counter((500, 146), front, front, script=17, value=0), - 'power': Text((500, 186), self.ascii_anm, front, text='0'), - 'graze': Text((500, 206), self.ascii_anm, front, text='0'), - 'points': Text((500, 226), self.ascii_anm, front, text='0'), + 'power': Text((500, 186), self.ascii_anm, front, text=b'0'), + 'graze': Text((500, 206), self.ascii_anm, front, text=b'0'), + 'points': Text((500, 226), self.ascii_anm, front, text=b'0'), 'framerate': Text((512, 464), self.ascii_anm, front), 'debug?': Text((0, 464), self.ascii_anm, front), @@ -68,13 +68,13 @@ class EoSDInterface(object): def start_stage(self, game, stage): self.game = game if stage < 6: - text = 'STAGE %d' % stage + text = ('STAGE %d' % stage).encode() elif stage == 6: - text = 'FINAL STAGE' + text = b'FINAL STAGE' elif stage == 7: - text = 'EXTRA STAGE' + text = b'EXTRA STAGE' - self.stage_name = NativeText((192, 200), unicode(game.std.name), shadow=True, align='center') + self.stage_name = NativeText((192, 200), game.std.name, shadow=True, align='center') self.stage_name.set_timeout(240, effect='fadeout', duration=60, start=120) self.set_song_name(game.std.bgms[0][0]) @@ -86,7 +86,7 @@ class EoSDInterface(object): def set_song_name(self, name): #TODO: use the correct animation. - self.song_name = NativeText((384, 432), u'♪ ' + name, shadow=True, align='right') + self.song_name = NativeText((384, 432), '♪ ' + name, shadow=True, align='right') self.song_name.set_timeout(240, effect='fadeout', duration=60, start=120) diff --git a/pytouhou/lib/sdl.pxd b/pytouhou/lib/sdl.pxd --- a/pytouhou/lib/sdl.pxd +++ b/pytouhou/lib/sdl.pxd @@ -125,7 +125,7 @@ cdef void mix_open_audio(int frequency, cdef void mix_allocate_channels(int numchans) except * cdef int mix_volume(int channel, float volume) nogil cdef int mix_volume_music(float volume) nogil -cdef Music load_music(const char *filename) +cdef Music load_music(str filename) cdef Chunk load_chunk(file_) cdef Uint32 get_ticks() nogil cdef void delay(Uint32 ms) nogil diff --git a/pytouhou/lib/sdl.pyx b/pytouhou/lib/sdl.pyx --- a/pytouhou/lib/sdl.pyx +++ b/pytouhou/lib/sdl.pyx @@ -90,8 +90,8 @@ class SDL(object): cdef class Window: - def __init__(self, const char *title, int x, int y, int w, int h, Uint32 flags): - self.window = SDL_CreateWindow(title, x, y, w, h, flags) + def __init__(self, str title, int x, int y, int w, int h, Uint32 flags): + self.window = SDL_CreateWindow(title.encode(), x, y, w, h, flags) if self.window == NULL: raise SDLError(SDL_GetError()) @@ -205,7 +205,7 @@ cdef class Surface: image = self.surface.pixels alpha = alpha_surface.surface.pixels - for i in xrange(nb_pixels): + for i in range(nb_pixels): # Only use the red value, assume the others are equal. image[3+4*i] = alpha[3*i] @@ -236,8 +236,8 @@ cdef class Chunk: cdef class Font: - def __init__(self, const char *filename, int ptsize): - self.font = TTF_OpenFont(filename, ptsize) + def __init__(self, str filename, int ptsize): + self.font = TTF_OpenFont(filename.encode(), ptsize) if self.font == NULL: raise SDLError(SDL_GetError()) @@ -335,9 +335,9 @@ cdef int mix_volume_music(float volume) return Mix_VolumeMusic(int(volume * 128)) -cdef Music load_music(const char *filename): +cdef Music load_music(str filename): music = Music() - music.music = Mix_LoadMUS(filename) + music.music = Mix_LoadMUS(filename.encode()) if music.music == NULL: raise SDLError(SDL_GetError()) return music diff --git a/pytouhou/options.py b/pytouhou/options.py --- a/pytouhou/options.py +++ b/pytouhou/options.py @@ -13,7 +13,7 @@ ## import os -from ConfigParser import RawConfigParser, NoOptionError +from configparser import RawConfigParser, NoOptionError from pytouhou.utils.xdg import load_config_paths, save_config_path diff --git a/pytouhou/resource/loader.py b/pytouhou/resource/loader.py --- a/pytouhou/resource/loader.py +++ b/pytouhou/resource/loader.py @@ -60,7 +60,7 @@ class Directory(object): class ArchiveDescription(object): - _formats = {'PBG3': PBG3} + _formats = {b'PBG3': PBG3} def __init__(self, path, format_class, file_list=None): self.path = path diff --git a/pytouhou/ui/music.pyx b/pytouhou/ui/music.pyx --- a/pytouhou/ui/music.pyx +++ b/pytouhou/ui/music.pyx @@ -16,7 +16,7 @@ from os.path import join from glob import glob from pytouhou.lib import sdl -from pytouhou.lib cimport sdl +from pytouhou.lib.sdl cimport load_music, Music, load_chunk, Chunk from pytouhou.utils.helpers import get_logger from pytouhou.game.music cimport MusicPlayer @@ -37,27 +37,27 @@ cdef class BGMPlayer(MusicPlayer): track = resource_loader.get_track(posname) except KeyError: track = None - logger.warn(u'Music description “%s” not found, continuing without looping data.', posname) - globname = join(resource_loader.game_dir, bgm[1].encode('ascii')).replace('.mid', '.*') + logger.warn('Music description “%s” not found, continuing without looping data.', posname) + globname = join(resource_loader.game_dir, bgm[1]).replace('.mid', '.*') filenames = glob(globname) for filename in reversed(filenames): try: - source = sdl.load_music(filename) + source = load_music(filename) except sdl.SDLError as error: - logger.debug(u'Music file “%s” unreadable: %s', filename, error) + logger.debug('Music file “%s” unreadable: %s', filename, error) continue else: if track is not None: source.set_loop_points(track.start / 44100., track.end / 44100.) #TODO: retrieve the sample rate from the actual track. self.bgms.append(source) - logger.debug(u'Music file “%s” opened.', filename) + logger.debug('Music file “%s” opened.', filename) break else: self.bgms.append(None) - logger.warn(u'No working music file for “%s”, disabling bgm.', globname) + logger.warn('No working music file for “%s”, disabling bgm.', globname) cpdef play(self, index): - cdef sdl.Music bgm + cdef Music bgm bgm = self.bgms[index] if bgm is not None: bgm.play(-1) @@ -82,12 +82,12 @@ cdef class SFXPlayer(MusicPlayer): self.next_channel += 1 return self.channels[name] - cdef sdl.Chunk get_sound(self, name): - cdef sdl.Chunk chunk + cdef Chunk get_sound(self, name): + cdef Chunk chunk if name not in self.sounds: wave_file = self.loader.get_file(name) try: - chunk = sdl.load_chunk(wave_file) + chunk = load_chunk(wave_file) except sdl.SDLError as error: logger.warn(u'Sound “%s” not found: %s', name, error) chunk = None diff --git a/pytouhou/ui/opengl/backend.pxd b/pytouhou/ui/opengl/backend.pxd --- a/pytouhou/ui/opengl/backend.pxd +++ b/pytouhou/ui/opengl/backend.pxd @@ -9,4 +9,4 @@ cdef bint use_debug_group cdef bint use_vao cdef bint use_framebuffer_blit cdef bint use_primitive_restart -cdef str shader_header +cdef bytes shader_header diff --git a/pytouhou/ui/opengl/backend.pyx b/pytouhou/ui/opengl/backend.pyx --- a/pytouhou/ui/opengl/backend.pyx +++ b/pytouhou/ui/opengl/backend.pyx @@ -66,13 +66,13 @@ def discover_features(): except KeyError: assert version >= 33 glsl_version = version * 10 - shader_header = '#version %d\n\n' % glsl_version + shader_header = ('#version %d\n\n' % glsl_version).encode() else: # The attribute keyword isn’t supported past GLSL ES 3.0. if version >= 30: version = 20 glsl_version = {20: '100', 30: '300 es'}[version] - shader_header = '#version %s\n\nprecision highp float;\n\n' % glsl_version + shader_header = ('#version %s\n\nprecision highp float;\n\n' % glsl_version).encode() def create_window(title, x, y, width, height, swap_interval): diff --git a/pytouhou/ui/opengl/gamerenderer.pyx b/pytouhou/ui/opengl/gamerenderer.pyx --- a/pytouhou/ui/opengl/gamerenderer.pyx +++ b/pytouhou/ui/opengl/gamerenderer.pyx @@ -229,7 +229,7 @@ cdef class GameRenderer(Renderer): black = Color(0, 0, 0, 255) - for label in texts.itervalues(): + for label in texts.values(): texture = (label.texture).texture rect = Rect(label.x, label.y, label.width, label.height) gradient = [Color(*color, a=label.alpha) for color in label.gradient] diff --git a/pytouhou/ui/opengl/renderer.pyx b/pytouhou/ui/opengl/renderer.pyx --- a/pytouhou/ui/opengl/renderer.pyx +++ b/pytouhou/ui/opengl/renderer.pyx @@ -44,7 +44,7 @@ cdef class Texture: self.texture = texture # Find an unused key in the textures array. - for key in xrange(MAX_TEXTURES): + for key in range(MAX_TEXTURES): if renderer.textures[key] == 0: break else: @@ -53,7 +53,7 @@ cdef class Texture: self.key = key self.pointer = &renderer.textures[key] self.pointer[0] = texture - for i in xrange(2): + for i in range(2): renderer.indices[key][i] = self.indices[i] #XXX: keep a reference so that when __dealloc__ is called self.pointer is still valid. @@ -141,7 +141,7 @@ cdef class Renderer: nb_vertices = 0 memset(self.last_indices, 0, sizeof(self.last_indices)) - for element_idx in xrange(nb_elements): + for element_idx in range(nb_elements): element = self.elements[element_idx] ox, oy = element.x, element.y data = get_sprite_rendering_data(element.sprite) @@ -201,7 +201,7 @@ cdef class Renderer: previous_blendfunc = -1 previous_texture = -1 - for key in xrange(2 * MAX_TEXTURES): + for key in range(2 * MAX_TEXTURES): nb_indices = self.last_indices[key] if not nb_indices: continue diff --git a/pytouhou/ui/opengl/shader.pyx b/pytouhou/ui/opengl/shader.pyx --- a/pytouhou/ui/opengl/shader.pyx +++ b/pytouhou/ui/opengl/shader.pyx @@ -42,9 +42,9 @@ cdef class Shader: self.location_cache = {} # create the vertex shader - self.create_shader(vert[0], GL_VERTEX_SHADER) + self.create_shader(vert[0].encode(), GL_VERTEX_SHADER) # create the fragment shader - self.create_shader(frag[0], GL_FRAGMENT_SHADER) + self.create_shader(frag[0].encode(), GL_FRAGMENT_SHADER) #TODO: put those elsewhere. glBindAttribLocation(self.handle, 0, 'in_position') @@ -116,6 +116,8 @@ cdef class Shader: self.linked = True cdef GLint get_uniform_location(self, name): + if isinstance(name, str): + name = name.encode() if name not in self.location_cache: loc = glGetUniformLocation(self.handle, name) if loc == -1: diff --git a/pytouhou/ui/opengl/sprite.pyx b/pytouhou/ui/opengl/sprite.pyx --- a/pytouhou/ui/opengl/sprite.pyx +++ b/pytouhou/ui/opengl/sprite.pyx @@ -79,7 +79,7 @@ cdef void render_sprite(Sprite sprite) n data.bottom = ty * y_1 + toy data.top = (ty + th) * y_1 + toy - for i in xrange(4): + for i in range(4): data.color[i] = sprite._color[i] data.key = ((sprite.anm.texture).key << 1) | sprite.blendfunc diff --git a/pytouhou/ui/sdl/gamerenderer.py b/pytouhou/ui/sdl/gamerenderer.py --- a/pytouhou/ui/sdl/gamerenderer.py +++ b/pytouhou/ui/sdl/gamerenderer.py @@ -142,7 +142,7 @@ class GameRenderer(object): self.font_manager.load(texts) - for label in texts.itervalues(): + for label in texts.values(): texture = label.texture source = Rect(0, 0, label.width, label.height) diff --git a/pytouhou/utils/interpolator.pyx b/pytouhou/utils/interpolator.pyx --- a/pytouhou/utils/interpolator.pyx +++ b/pytouhou/utils/interpolator.pyx @@ -22,11 +22,11 @@ cdef class Interpolator: self._values = malloc(self._length * sizeof(double)) self.start_values = malloc(self._length * sizeof(double)) self.end_values = malloc(self._length * sizeof(double)) - for i in xrange(self._length): + for i in range(self._length): self._values[i] = values[i] self.start_values[i] = self._values[i] if end_values is not None: - for i in xrange(self._length): + for i in range(self._length): self.end_values[i] = end_values[i] self.start_frame = start_frame self.end_frame = end_frame @@ -42,7 +42,7 @@ cdef class Interpolator: property values: def __get__(self): - return tuple([self._values[i] for i in xrange(self._length)]) + return tuple([self._values[i] for i in range(self._length)]) def __nonzero__(self): @@ -50,13 +50,13 @@ cdef class Interpolator: cpdef set_interpolation_start(self, unsigned long frame, tuple values): - for i in xrange(self._length): + for i in range(self._length): self.start_values[i] = values[i] self.start_frame = frame cpdef set_interpolation_end(self, unsigned long frame, tuple values): - for i in xrange(self._length): + for i in range(self._length): self.end_values[i] = values[i] self.end_frame = frame @@ -66,7 +66,7 @@ cdef class Interpolator: cpdef set_interpolation_end_values(self, tuple values): - for i in xrange(self._length): + for i in range(self._length): self.end_values[i] = values[i] @@ -76,7 +76,7 @@ cdef class Interpolator: self._frame = frame if frame + 1 >= self.end_frame: #XXX: skip the last interpolation step # This bug is replicated from the original game - for i in xrange(self._length): + for i in range(self._length): self._values[i] = self.end_values[i] self.start_values[i] = self.end_values[i] self.start_frame = frame @@ -84,7 +84,7 @@ cdef class Interpolator: coeff = float(frame - self.start_frame) / float(self.end_frame - self.start_frame) if self._formula is not None: coeff = self._formula(coeff) - for i in xrange(self._length): + for i in range(self._length): start_value = self.start_values[i] end_value = self.end_values[i] self._values[i] = start_value + coeff * (end_value - start_value) diff --git a/pytouhou/utils/matrix.pyx b/pytouhou/utils/matrix.pyx --- a/pytouhou/utils/matrix.pyx +++ b/pytouhou/utils/matrix.pyx @@ -41,10 +41,10 @@ cdef void mul(Matrix *mat1, Matrix *mat2 d1 = mat1 d2 = mat2 d3 = out - for i in xrange(4): - for j in xrange(4): + for i in range(4): + for j in range(4): d3[4*i+j] = 0 - for k in xrange(4): + for k in range(4): d3[4*i+j] += d1[4*i+k] * d2[4*k+j] memcpy(mat1, out, sizeof(Matrix)) free(out) @@ -52,7 +52,7 @@ cdef void mul(Matrix *mat1, Matrix *mat2 cdef void flip(Matrix *mat) nogil: data = mat - for i in xrange(4): + for i in range(4): data[i] = -data[i] @@ -64,14 +64,14 @@ cdef void scale(Matrix *mat, float x, fl coordinate[1] = y coordinate[2] = z - for i in xrange(3): - for j in xrange(4): + for i in range(3): + for j in range(4): data[4*i+j] *= coordinate[i] cdef void scale2d(Matrix *mat, float x, float y) nogil: data = mat - for i in xrange(4): + for i in range(4): data[ i] *= x data[4+i] *= y @@ -80,11 +80,11 @@ cdef void translate(Matrix *mat, float[3 cdef float item[3] data = mat - for i in xrange(3): + for i in range(3): item[i] = data[12+i] * offset[i] - for i in xrange(3): - for j in xrange(4): + for i in range(3): + for j in range(4): data[4*i+j] += item[i] @@ -105,9 +105,9 @@ cdef void rotate_x(Matrix *mat, float an data = mat cos_a = cos(angle) sin_a = sin(angle) - for i in xrange(8): + for i in range(8): lines[i] = data[i+4] - for i in xrange(4): + for i in range(4): data[4+i] = cos_a * lines[i] - sin_a * lines[4+i] data[8+i] = sin_a * lines[i] + cos_a * lines[4+i] @@ -119,10 +119,10 @@ cdef void rotate_y(Matrix *mat, float an data = mat cos_a = cos(angle) sin_a = sin(angle) - for i in xrange(4): + for i in range(4): lines[i] = data[i] lines[i+4] = data[i+8] - for i in xrange(4): + for i in range(4): data[ i] = cos_a * lines[i] + sin_a * lines[4+i] data[8+i] = -sin_a * lines[i] + cos_a * lines[4+i] @@ -134,8 +134,8 @@ cdef void rotate_z(Matrix *mat, float an data = mat cos_a = cos(angle) sin_a = sin(angle) - for i in xrange(8): + for i in range(8): lines[i] = data[i] - for i in xrange(4): + for i in range(4): data[ i] = cos_a * lines[i] - sin_a * lines[4+i] data[4+i] = sin_a * lines[i] + cos_a * lines[4+i] diff --git a/pytouhou/utils/pe.py b/pytouhou/utils/pe.py --- a/pytouhou/utils/pe.py +++ b/pytouhou/utils/pe.py @@ -65,7 +65,7 @@ class PEStructs: directory_format = Struct(' diff --git a/scripts/pytouhou b/scripts/pytouhou --- a/scripts/pytouhou +++ b/scripts/pytouhou @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- encoding: utf-8 -*- ## ## Copyright (C) 2011 Thibaut Girka diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -76,7 +76,7 @@ default_libs = { def get_arguments(arg, libraries): try: - return check_output([COMMAND, arg] + libraries).split() + return check_output([COMMAND, arg] + libraries).decode().split() except CalledProcessError: # The error has already been displayed, just exit. sys.exit(1) @@ -96,6 +96,8 @@ if use_opengl: for directory, _, files in os.walk('pytouhou'): + if directory.endswith('/__pycache__'): + continue package = directory.replace(os.path.sep, '.') if not use_opengl and package in ('pytouhou.ui.opengl', 'pytouhou.ui.opengl.shaders'): continue