Enterprise LAMP

Casey Duncan: Decorate Thy Keyboard Controls

As part of a game engine called Grease I’m working on (more on that sometime soon) I was thinking about ways to create a clean and efficient api for handling keyboard events. Too often this ubiquitous section of game code consists of a tangle of if/elif statements, a construct that is a personal pet peeve of mine. So to avoid that mess, a typical strategy I’ve employed is to map keys to methods using a dictionary for easy dispatch. Doing this for one-off applications is simple enough, if not particularly clean and tidy.

Anyway taking a step back for a sec, let’s examine some goals. Basically what I’m after is a way to define some methods (or even functions) that get executed in response to key events. Specifically there are three types of key events that I’m interested in: key press, key release and key hold. The first two get dispatched once per key “stroke” as you’d expect. The key hold event fires every game “tick” that a key remains down; useful for continuous functions like thrust, etc.

So I need a clean way to map methods to specific keys and key event types. Sounds like a job for: decorators! Truth be told I haven’t felt the need to write many decorators, but after prototyping a non-decorator version that came out less than clean even though it only supported one type of key event, I thought I’d give decorators a go. Below is an example of how to implement key controls using what I’ve cooked up:

class PlayerControls(KeyControls):

    @KeyControls.key_press(key.LEFT)    def start_turn_left(self):        ship.rotation = -ship.turn

    @KeyControls.key_release(key.LEFT)    def stop_turn_left(self):        if ship.rotation < 0:            ship.rotation = 0

    @KeyControls.key_press(key.RIGHT)    def start_turn_right(self):        ship.rotation = ship.turn

    @KeyControls.key_release(key.RIGHT)    def stop_turn_right(self):        if ship.rotation > 0:            ship.rotation = 0

    @KeyControls.key_hold(key.UP)    def thrust(self, dt):        ship.body.apply_local_force(            ship.player.thrust * dt)

    @KeyControls.key_press(key.P)    def pause(self, dt):        global paused        paused = not paused

Here’s the code needed to wire this into pyglet:

window = pyglet.window.Window()controls = KeyControls(window)pyglet.clock.schedule_interval(controls.run, 1.0/60.0)pyglet.app.run()

Though using the decorators binds the keys to specific methods of the class at compile-time, KeyControls also contains additional methods for changing the key bindings at run-time. I may also add support to load and store key bindings from a configuration file if that feature is needed.

This implementation is designed to work with pyglet, though I think the same general approach could be used with pygame. The module for this, although part of a larger project I am working on, doesn’t depend on anything else. Feel free to give it a try and see what you think.

[Edit a new version of this module is now available]

Get the module here.

Casey Duncan: Grease 0.1 Released

I released the inaugural 0.1 release of my game engine Grease today. This is basically a throw-it-over-the-wall release so that I can use the code and push it further during the upcoming pyweek compo at the end of March. That said it is complete enough to implement an entire game, an example of which is included in the distribution. Obligatory screenshots below:

The game uses Grease features such as its awesomely-retro vector polygon renderer, collision detection, decorator-based key binding and mode management for hot-seat multiplayer. The entire game is a single script that weighs in about 550 lines, which includes whitespace and docstrings. Even so no real effort is made to make the code short, it was written with clarity in mind, not brevity. Most of the code savings comes from the abstractions available in Grease.

At the moment, Grease is implemented entirely in Python and sits on top of Pyglet. My upcoming efforts on this project will be to make the blasteroids example into a full tutorial and get everything fully documented. After that I will be creating more complex example games and adding native-code parts to Grease where needed. The intention is to always have pure-Python versions of everything available though, and the Python versions will be developed first.

Of course you are welcome and encouraged to try it for yourself. You can download it from pypi here:

http://pypi.python.org/pypi/grease

The code of the example blasteroids game above can be scrutinized here:

blasteroids.py

Enjoy.

Kay Schluehr: reverb – a revival

Sometimes software is given up by people and you realize it only a few years later.  Large packages or libraries will inevitably be flagged as legacy and die but tiny modules might have a chance to survive and find a maintainer. I have done the latter now for reverb.py.

Xdebug 2.1.0beta3 released – Derick Rethans

Patch breaks Suhosin Security Feature in Debian Unstable/Testing – Stefan Esser

Two days ago I installed a mail client on my reinstalled desktop system that was not doing anything for 2 month and checked mails of the hardened-php account that were not checked for 2 months. Usually noone uses this email account to contact me, but the Suhosin bug reports sometimes end up there. While killing thousands of SPAM messages I also found a message from the Debian PHP maintainers, dating back to the 10th February 2010, telling me about a crash problem inside the Suhosin patch. The email also contained their solution to the problem: a patch for the suhosin patch. You can view this patch here. However you should not commit this patch to your PHP because it does not solve the problem correctly.

I previously blogged about one of the new features in Suhosin Patch for PHP 5.3.x. It is now possible to adjust several internal features by setting certain environment variables on startup. This includes the memory manager canary protection, the sanitization of free memory blocks, the protection of linked lists and hashtables. When a Suhosin patched PHP starts the environment variables are evaluated and the suhosin config is written into a variable called suhosin_config.

It should be obvious that this kind of feature comes with a little problem. Certain bytes in memory now control if Suhosin’s internal memory protections are activated or not. This means that a memory corruption vulnerability in PHP could be used by an attacker to overwrite the config variable and disable the security. Because of this Suhosin Patch tries to align the suhosin_config variable to a page boundary and then set it to read only.

/* hack that needs to be fixed */
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
 
#ifdef ZEND_WIN32
__declspec(align(PAGE_SIZE))
#endif
char suhosin_config[PAGE_SIZE]
#if defined(__GNUC__)
__attribute__ ((aligned(PAGE_SIZE)))
#endif
;
 
static void suhosin_write_protect_configuration()
{
#if defined(__GNUC__)
   mprotect(suhosin_config, PAGE_SIZE, PROT_READ);
#endif
}

The implementation has some problems. First of all it only works in case of a GNU C compiler. The second and more serious problem is that it assumes that the PAGE_SIZE is smaller than or equal to 4096. Otherwise mprotect() will not correctly work. On systems where the PAGE_SIZE is bigger than 4096 the mprotect() will either fail or set too many bytes to read only. In case of a write access after the suhosin_config variable this can lead to a crash.

The Debian people saw this crash on some architectures and reacted with a patch. However they did misunderstand the security idea behind it and therefore their patch looks like this.

char *suhosin_config = NULL;
 
static void suhosin_write_protect_configuration()
{
#if defined(__GNUC__)
   mprotect(suhosin_config, sysconf(_SC_PAGESIZE), PROT_READ)

Truncated by Planet PHP, read more at the original (another 3081 bytes)

Debian breaks Suhosin Security Feature – Stefan Esser

Everyone who cares about security will remember the Debian Openssl disaster in 2008. The debian developers had patched their version of openssl to fix compiler warnings. This resulted in a broken random number generator that made all keys generated by Debian systems predictable. One would think that Debian developers are more careful with patching “bugs” in security tools since that day.

However two days ago I finally installed a mail client on my reinstalled desktop system and checked mails of the hardened-php account that were not checked for 2 months, because usually noone uses this account to email me. While killing thousands of SPAM messages I also found a message from the Debian PHP maintainers, dating back to the 10th February 2010, telling me about a crash problem inside the Suhosin patch. The email also contained their solution to the problem: a patch for the suhosin patch. You can view this patch here. However you should not commit this patch to your PHP because it breaks Suhosin’s security.

I previously blogged about one of the new features in Suhosin Patch for PHP 5.3.x. It is now possible to adjust several internal features by setting certain environment variables on startup. This includes the memory manager canary protection, the sanitization of free memory blocks, the protection of linked lists and hashtables. When a Suhosin patched PHP starts the environment variables are evaluated and the suhosin config is written into a variable called suhosin_config.

It should be obvious that this kind of feature comes with a little problem. Certain bytes in memory now control if Suhosin’s internal memory protections are activated or not. This means that a memory corruption vulnerability in PHP could be used by an attacker to overwrite the config variable and disable the security. Because of this Suhosin Patch tries to align the suhosin_config variable to a page boundary and then set it to read only.

/* hack that needs to be fixed */
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
 
#ifdef ZEND_WIN32
__declspec(align(PAGE_SIZE))
#endif
char suhosin_config[PAGE_SIZE]
#if defined(__GNUC__)
__attribute__ ((aligned(PAGE_SIZE)))
#endif
;
 
static void suhosin_write_protect_configuration()
{
#if defined(__GNUC__)
   mprotect(suhosin_config, PAGE_SIZE, PROT_READ);
#endif
}

The implementation has some problems. First of all it only works in case of a GNU C compiler. The second and more serious problem is that it assumes that the PAGE_SIZE is smaller than or equal to 4096. Otherwise mprotect() will not correctly work. On systems where the PAGE_SIZE is bigger than 4096 the mprotect() will either fail or set too many bytes to read only. In case of a write access after the suhosin_config variable this can lead to a crash.

The Debian people saw this crash on some architectures and reacted with a patch. However they do not understand the security feature and therefore their patch looks like this.

char *suhosin_config = NULL;
 
static void suhosin_write_protect_configuration&

Truncated by Planet PHP, read more at the original (another 2834 bytes)

Montreal Python User Group: Upcoming Sprints

ConFoo brings several experts in town and we take this opportunity to launch two sprints: one on TurboGears with Chris Perkins on 2010-03-13 starting at 10h30 am and one on Distribute with Tarek Ziadé on 2010-03-15 starting at 6 pm. Both sprints will be at Brasseurs Numériques’ office, 1124 Marie-Anne, suite 11.

The TurboGears sprint will focus on polishing the code base and the documentation for the upcoming 2.1 release. Some work on the Sprox from generator and on the C5T CMS is also very likely. Anyone somewhat familiar with Pylons should be able to contribute.

The Distribute sprint will focus on implementing the newly accepted PEPs: 345, Metadata; and 386, version schemas. Anyone who’ve used Setuptools to some exetent should be able to contribute.

Bring your laptop or make a pairing agreement on with someone else. As usual, capacity is limited to 12 sprinters so please sign up on the mailing list.

PyCharm: PyCharm build 94.429 is available

We’ve just released a new EAP build of PyCharm, with a number of major new features:

  • virtualenv is now supported (to configure, go to Settings | Python Interpreter, click Add, and select the python script inside the Scripts subdirectory of your virtualenv directory);
  • Inline Local refactoring;
  • SASS syntax highlighing and code insight;
  • Inspection to highlight unused local variables;
  • Many improvements in the Django support, such as parser fixes, tag folding, Goto Definition for variables defined inside templates, navigation from template to view, navigation from ‘include’ in urls.py to referenced urlpatterns, and more;
  • Initial support for Python version migration: an inspection to highlight usages of language features removed in Python 3 (with quick fixes to replace with new syntax when possible), and an inspection to highlight imports of deprecated standard library modules.

You can download PyCharm from the EAP page, or find the complete list of changes for the new build in the Release Notes page.

PHP UK 2010: RDBMS in the social networks age – Lorenzo Alberton

Wrapup of the PHP UK Conference 2010. Uploaded slides of my talk “RDBMS in the social networks age” about graphs in the database.

Max Ischenko: See you, PyCon

It was my first PyCon ever and the first trip to the United States. Things I’ve done at PyCon:

  • drove a segway
  • went to the world’s largest aquarium
  • sipped a margarita on a skyscraper’s under-the-roof bar
  • got into a car accident
  • got a few job inquiries
  • tasted 64 flavors of coca soda (not really 64)
  • bought a nexus one phone
As you see, the conference was quite an event, I look forward to attending more of this kind.

More seriously, I am very glad and happy finally meeting a lot of people I knew through online and making some new acquaintances. As Grig wrote (more or less): "don’t bother attending talks, go networking". I definitely should have stayed for sprint days, it’s a great opportunity to try and work along with bright people on interesting problems.

Thanks to PSF financial aid to cover registration fee, that’s really appreciated. Personal thanks to Joseph Tate and Yarko Tymciurak, I owe you guys.

Permalink

| Leave a comment  »

« go backkeep looking »

Warning: include(/home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/r_sidebar.php) [function.include]: failed to open stream: No such file or directory in /home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/archive.php on line 23

Warning: include() [function.include]: Failed opening '/home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/r_sidebar.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/archive.php on line 23